Created
May 11, 2015 13:49
-
-
Save victorpavlenko/e080d4294333aff5cb2a to your computer and use it in GitHub Desktop.
class.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Вычисляем общую сумму **/ | |
var setSumm = { | |
sum : function(dataObject){ | |
var summary = [], | |
sum = 0; | |
for( var i = 0; i < Object.keys(dataObject).length - 1; i++){ | |
summary.push(dataObject[i].summ) | |
} | |
summary.map(function(name) { | |
return sum += name; | |
}); | |
dataObject.summary = sum; | |
$('.b-order-sharing-select-total-price').text( dataObject.summary + ' руб.') | |
} | |
}; | |
var calculateDocument = function(template, itemOnJson, settings, dataObject){ | |
var self = this, | |
dom | |
/** Инициализируем **/ | |
this.init = function() { | |
$('.b-order-sharing-select-document').show(); | |
dom = $('.b-order-sharing-select-document').find('.b-order-sharing-select-list').append(template); | |
this.initListeners(); | |
}; | |
/** Отслеживаем изменения **/ | |
this.initListeners = function() { | |
var doubleSidePrinting = dom.find('input[name=doublesidePrinting]'), | |
stiching = dom.find('input[name=stiching]'), | |
colorPrint = dom.find('input[name=colorPrint]'), | |
number = dom.find('.b-order-sharing-select-number'), | |
price = dom.find('.b-order-sharing-select-price'), | |
priceNow = 0, | |
stichingSum = 0; | |
doubleSidePrinting.on('change', function(){ | |
this.checked ? dataObject[itemOnJson].doublesidePrinting = true : dataObject[itemOnJson].doublesidePrinting = false; | |
number.trigger('change'); | |
}); | |
stiching.on('change', function(){ | |
this.checked ? dataObject[itemOnJson].stiching = true : dataObject[itemOnJson].stiching = false; | |
number.trigger('change'); | |
}); | |
colorPrint.on('change', function(){ | |
if(this.checked){ | |
price.text( number.val() * 5 + ' руб.' ); | |
dataObject[itemOnJson].colorPrint = true; | |
dataObject[itemOnJson].summ = number.val() *5; | |
}else{ | |
price.text( number.val() * 4 + ' руб.' ); | |
dataObject[itemOnJson].colorPrint = false; | |
dataObject[itemOnJson].summ = number.val() * 4; | |
} | |
number.trigger('change'); | |
}); | |
number.on('keyup keydown change', function(){ | |
if(colorPrint.is(':checked')){ | |
priceNow = this.value * 5; | |
price.text( this.value * 5 + ' руб.' ); | |
dataObject[itemOnJson].summ = this.value * 5; | |
}else{ | |
priceNow = this.value * 4; | |
price.text( priceNow + ' руб.' ); | |
dataObject[itemOnJson].summ = this.value * 4; | |
} | |
if(number.val() < 15){ | |
stichingSum = 0 | |
}else if(number.val() > 15 && number.val() < 55){ | |
stichingSum = 65 | |
}else if(number.val() > 55 && number.val() < 86){ | |
stichingSum = 65 | |
}else if(number.val() > 86 && number.val() < 97){ | |
stichingSum = 70 | |
}else if(number.val() > 97 && number.val() < 112){ | |
stichingSum = 80 | |
}else if(number.val() > 112 && number.val() < 168){ | |
stichingSum = 100 | |
}else{ | |
} | |
if(stiching.is(':checked')){ | |
price.text( priceNow + stichingSum + ' руб.'); | |
dataObject[itemOnJson].stiching = true; | |
dataObject[itemOnJson].summ = +priceNow + stichingSum ; | |
}else{ | |
price.text( priceNow + ' руб.'); | |
dataObject[itemOnJson].stiching = false; | |
dataObject[itemOnJson].summ = priceNow - stichingSum ; | |
} | |
dataObject[itemOnJson].count = number.val(); | |
self.sum(dataObject); | |
}); | |
number.trigger('change'); | |
}; | |
}; | |
var calculateImage = function(template, itemOnJson, settings, dataObject){ | |
var self = this, | |
dom; | |
this.init = function() { | |
$('.b-order-sharing-select-image').show(); | |
dom = $('.b-order-sharing-select-image').find('.b-order-sharing-select-list').append(template); | |
this.initListeners(); | |
}; | |
this.initListeners = function() { | |
var format = $(dom).find('select:eq( 0 )'), | |
paperType = $(dom).find('select:eq( 1 )'), | |
number = $(dom).find('.b-order-sharing-select-number'), | |
price = $(dom).find('.b-order-sharing-select-price') | |
format.on('change', function(){ | |
price.text(this.value * number.val() + ' руб.') | |
dataObject[itemOnJson].format = $(this).find('option:selected').text(); | |
dataObject[itemOnJson].summ = this.value * number.val(); | |
number.trigger('change'); | |
}); | |
paperType.on('change', function(){ | |
dataObject[itemOnJson].paperType = $(dom).find('select.type option:selected').text(); | |
}); | |
number.on('keyup change', function(){ | |
var number = $(dom).find('select').val() | |
price.text( | |
number * this.value + ' руб.' | |
); | |
dataObject[itemOnJson].summ = number * this.value; | |
dataObject[itemOnJson].count = this.value; | |
self.sum(dataObject); | |
}); | |
number.trigger('change'); | |
}; | |
}; | |
var calculateCard = function(template, itemOnJson, settings, dataObject){ | |
var self = this, | |
dom; | |
this.init = function() { | |
$('.b-order-sharing-select-card').show(); | |
dom = $('.b-order-sharing-select-card').find('.b-order-sharing-select-list').append(template); | |
this.initListeners(); | |
}; | |
this.initListeners = function() { | |
var count = $(dom).find('select.count'); | |
var paperType = $(dom).find('select.type'); | |
var doublePrint = $(dom).find('.cardDoubleSidePrinting'); | |
var number = $(dom).find('.b-order-sharing-select-number'); | |
var price = $(dom).find('.b-order-sharing-select-price'); | |
var cardPricing = settings.cardPricing; | |
count.on('change', function(){ | |
if(dataObject[itemOnJson].doublePrint){ | |
var pricing = cardPricing[paperType.val()][count.val()][1]; | |
price.text( pricing + ' руб.' ); | |
dataObject[itemOnJson].summ = pricing; | |
dataObject[itemOnJson].count = count.val(); | |
}else{ | |
var pricing = cardPricing[paperType.val()][count.val()][0]; | |
price.text( pricing + ' руб.' ); | |
dataObject[itemOnJson].summ = pricing; | |
dataObject[itemOnJson].count = count.val(); | |
} | |
self.sum(dataObject) | |
}); | |
count.trigger('change'); | |
doublePrint.on('change', function(){ | |
this.checked ? dataObject[itemOnJson].doublePrint = true : dataObject[itemOnJson].doublePrint = false; | |
count.trigger('change') | |
}); | |
paperType.on('change', function(){ | |
dataObject[itemOnJson].papyrType = paperType.val(); | |
count.trigger('change') | |
}); | |
}; | |
}; | |
/** Расиширяем классы, добавляя общий метод **/ | |
$.extend(calculateImage.prototype, setSumm ); | |
$.extend(calculateCard.prototype, setSumm ); | |
$.extend(calculateDocument.prototype, setSumm ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment