/**
* yvan.yvnumber.js
* @author luoyifan
* 2019-07-14 11:14:00
*/
'use strict';
(function ($) {
function init(target) {
var state = $.data(target, 'yvnumber');
var option = state.options;
var $dom = $(target);
var jqxOpt = {
width: option.width,
height: option.height,
};
$dom.jqxInput(jqxOpt);
$dom.attr('type', "number");
$dom.addClass('yvgrid-numberbox');
var re;
if (!option.precision) {
//不需要小数
re = new RegExp("^(\\-)*(\\d+).*$", "g");
$dom.on('input propertychange', function (event) {
this.value = this.value.replace(re, '$1$2');
if ($.type(option.onChange) === 'function') {
option.onChange.call(this, this.value, event);
}
});
} else {
//需要指定位数的小数
re = new RegExp("^(\\-)*(\\d+)\\.(\\d{0," + option.precision + "}).*$", "g");
$dom.on('input propertychange', function (event) {
this.value = this.value.replace(re, '$1$2.$3');
if ($.type(option.onChange) === 'function') {
option.onChange.call(this, this.value, event);
}
});
}
}
$.fn.yvnumber = function (options, param) {
if (typeof options == 'string') {
var method = $.fn.yvnumber.methods[options];
if (method) {
var args = [this];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
return method.apply(this, args);
} else {
console.error('not found method:' + options);
}
}
options = options || {};
return this.each(function () {
var state = $.data(this, 'yvnumber');
if (state) {
$.extend(state.options, options);
} else {
$.data(this, 'yvnumber', {
options: $.extend({}, $.fn.yvnumber.defaults, options)
});
}
init(this);
});
};
function setValueInner(target, newValue) {
var state = $.data(target, "yvnumber");
var option = state.options;
//option.value = parseFloat(value);
var value = option.parser.call(target, newValue);
//console.log('value:' + newValue + ', parser:' + value + ', precision:' + option.precision);
//var text = option.formatter.call(target, value);
option.value = value;
$(target).jqxInput('val', value);
//$(target).textbox("setText", text).textbox("setValue", value);
//text = option.formatter.call(target, $(target).textbox("getValue"));
//$(target).textbox("setText", text);
}
$.fn.yvnumber.methods = {
getValue: function ($dom) {
return $(this).jqxInput('val');
},
setValue: function (jq, value) {
return jq.each(function () {
setValueInner(this, value);
//return $(this).jqxInput('val', value);
});
},
focus: function ($dom) {
setTimeout(function () {
$dom[0].focus();
}, 200);
},
};
$.fn.power.defaults.xtype.yvnumber = function ($targetDOM, option) {
if (option.name) {
var $dom = $("").appendTo($targetDOM);
} else {
$dom = $("").appendTo($targetDOM);
}
return $dom.yvnumber(option);
};
$.fn.yvnumber.defaults = {
precision: 0,
min: 0,
parser: function (value) {
value = value + "";
var option = $.data(this, 'yvnumber').options;
if (option.prefix) {
value = $.trim(value.replace(new RegExp("\\" + $.trim(option.prefix), "g"), ""));
}
if (option.suffix) {
value = $.trim(value.replace(new RegExp("\\" + $.trim(option.suffix), "g"), ""));
}
if (parseFloat(value) !== option.value) {
if (option.groupSeparator) {
value = $.trim(value.replace(new RegExp("\\" + option.groupSeparator, "g"), ""));
}
if (option.decimalSeparator) {
value = $.trim(value.replace(new RegExp("\\" + option.decimalSeparator, "g"), "."));
}
value = value.replace(/\s/g, "");
}
var val = parseFloat(value).toFixed(option.precision);
if (isNaN(val)) {
val = "";
} else {
if (typeof (option.min) == "number" && val < option.min) {
val = option.min.toFixed(option.precision);
} else {
if (typeof (option.max) == "number" && val > option.max) {
val = option.max.toFixed(option.precision);
}
}
}
return val;
},
formatter: function (value) {
if (!value) {
return value;
}
value = value + "";
var state = $.data(this, 'yvnumber').options;
var s1 = value, s2 = "";
var _1c = value.indexOf(".");
if (_1c >= 0) {
s1 = value.substring(0, _1c);
s2 = value.substring(_1c + 1, value.length);
}
if (state.groupSeparator) {
var p = /(\d+)(\d{3})/;
while (p.test(s1)) {
s1 = s1.replace(p, "$1" + state.groupSeparator + "$2");
}
}
if (s2) {
return state.prefix + s1 + state.decimalSeparator + s2 + state.suffix;
} else {
return state.prefix + s1 + state.suffix;
}
}
};
})(jQuery);