fd-gulp-convert-encoding
Version:
convert file to assigned charset
231 lines (188 loc) • 6.64 kB
JavaScript
/**
* purchasingList view交互逻辑
*/
define('detail.lib.amountControl.AmountControl',
['jQuery', 'Class', 'detail.core.Event'],
function($, Class, Event) {
return Class({
init: function(root, config, validator) {
this.onEvent = $.os.supportsTouch ? 'tap' : 'click';
this.div = $('div.unit-d-amount-control', root);
this.config = config ? config : {};
this.amountValue = 0;
this.amountNode = $('input.amount-input', this.div);
this.amountUpNode = $('a.amount-up', this.div);
this.amountDownNode = $('a.amount-down', this.div);
this.validator = validator;
this.callback = function(){};
this.beforeValidator = function(){return true;};
this.reg = /^[0-9]+\d*$/;
this._amountInputHandle();
this._amountOffset();
},
_amountInputHandle: function(){
var temp = 0,
that = this;
this.amountNode.on('input', function(){
var elm = $(this),
val = elm.val() * 1,
state = that.activateValidator(val);
if (!that.beforeValidator.call(that)) {
return;
}
elm.val(val);
if (val) {
if (that.reg.test(val)) {
if (state !== 14 && state !== 11){
if (state === 10){
// Event.trigger('updateAmount', val);
that.callback.call(that, val, true);
} else {
that.callback.call(that, val, false);
}
temp = val;
} else {
that.callback.call(that, val, false);
}
} else {
elm.val(temp);
}
} else {
temp = 0;
that.callback.call(that, val, true);
}
});
this.amountNode.on('focus', function(){
// Event.trigger('hidePurchasingFullInfo');
});
this.amountNode.on('blur', function(){
var elm = $(this),
ev, state, upState, downState;
// Event.trigger('showPurchasingFullInfo');
ev = $.trim(elm.val()) ? elm.val() * 1 : 0;
elm.val(ev);
state = that.activateValidator(ev);
if (state === 10){
// Event.trigger('updateAmount', val);
that.callback.call(that, ev, true);
} else {
that.callback.call(that, ev, false);
}
});
},
_amountOffset: function(){
var $amountUpId = this.amountUpNode,
$amountDownId = this.amountDownNode,
$amountId = this.amountNode,
upDownNum = this.config.scale ? this.config.scale * 1 : 1,
that = this;
$amountUpId.on(this.onEvent, function(e){
e.preventDefault();
if (!that.beforeValidator.call(that)) {
return;
}
var val = $amountId.val(),
ev = val * 1,
state;
if (!$.trim(val) || !that.reg.test(ev)){
return;
}
if (that.config.scale && ev % that.config.scale !== 0) {
ev = Math.floor(ev / that.config.scale) * that.config.scale;
}
ev += upDownNum;
state = that.activateValidator(ev);
if (state === 10){
$amountId.val(ev);
// Event.trigger('updateAmount', val);
that.callback.call(that, ev, true);
} else {
//如果是低于最小的问题,减少调节的时候设置成最小值
if (state === 16){
ev = that.validator.min * 1;
$amountId.val(ev);
// Event.trigger('updateAmount', val);
that.callback.call(that, ev, true);
} else {
that.callback.call(that, ev, false);
}
}
});
$amountDownId.on(this.onEvent, function(e){
e.preventDefault();
if (!that.beforeValidator.call(that)) {
return;
}
var val = $amountId.val(),
ev = val * 1,
state;
if (!$.trim(val) || !that.reg.test(ev)){
return;
}
if (that.config.scale && ev % that.config.scale !== 0) {
ev = (Math.floor(ev / that.config.scale) + 1) * that.config.scale;
}
ev -= upDownNum;
state = that.activateValidator(ev);
if (state === 10){
$amountId.val(ev);
// Event.trigger('updateAmount', val);
that.callback.call(that, ev, true);
} else {
//如果是超过最大值的问题,减少调节的时候设置成最大值
if (state === 14){
ev = that.validator.max * 1;
$amountId.val(ev);
// Event.trigger('updateAmount', val);
that.callback.call(that, ev, true);
} else {
that.callback.call(that, ev, false);
}
}
});
},
activateValidator: function(ev, price){
var state = 10;
if (ev === null){
ev = this.getAmountValue();
}
if (price === null){
price = this.getPriceValue();
}
if (this.validator){
state = this.validator.valid(ev, price, 'blur');
}
return state;
},
attachValue: function(){
this.amountNode.trigger('blur');
},
resetValue: function(){
this.amountValue = 0;
this.amountNode.val(0);
},
setAmountValue: function(value){
this.amountValue = value;
this.amountNode.val(value);
},
getAmountValue: function(){
return this.amountValue;
},
setPriceValue: function(value){
this.priceValue = value;
},
getPriceValue: function(){
return this.priceValue;
},
setBeforeValidator: function(func){
if (typeof(func) === 'function'){
this.beforeValidator = func;
}
},
setCallback: function(func){
if (typeof(func) === 'function'){
this.callback = func;
}
}
});
});