39 lines
6.0 KiB
JavaScript
39 lines
6.0 KiB
JavaScript
/*! jQuery number 2.1.0 (c) github.com/teamdf/jquery-number | opensource.teamdf.com/license */
|
|
(function (h) {
|
|
function r(d, a) { if (this.createTextRange) { var c = this.createTextRange(); c.collapse(true); c.moveStart("character", d); c.moveEnd("character", a - d); c.select() } else if (this.setSelectionRange) { this.focus(); this.setSelectionRange(d, a) } } function s(d) {
|
|
var a = this.value.length; d = d.toLowerCase() == "start" ? "Start" : "End"; if (document.selection) {
|
|
a = document.selection.createRange(); var c; c = a.duplicate(); c.expand("textedit"); c.setEndPoint("EndToEnd", a); c = c.text.length - a.text.length; a = c + a.text.length; return d ==
|
|
"Start" ? c : a
|
|
} else if (typeof this["selection" + d] != "undefined") a = this["selection" + d]; return a
|
|
} var q = { codes: { 188: 44, 109: 45, 190: 46, 191: 47, 192: 96, 220: 92, 222: 39, 221: 93, 219: 91, 173: 45, 187: 61, 186: 59, 189: 45, 110: 46 }, shifts: { 96: "~", 49: "!", 50: "@", 51: "#", 52: "$", 53: "%", 54: "^", 55: "&", 56: "*", 57: "(", 48: ")", 45: "_", 61: "+", 91: "{", 93: "}", 92: "|", 59: ":", 39: '"', 44: "<", 46: ">", 47: "?"} }; h.fn.number = function (d, a, c, l) {
|
|
l = typeof l === "undefined" ? "," : l; c = typeof c === "undefined" ? "." : c; a = typeof a === "undefined" ? 0 : a; var i = "\\u" + ("0000" +
|
|
c.charCodeAt(0).toString(16)).slice(-4), n = RegExp("[^" + i + "0-9]", "g"), o = RegExp(i, "g"); if (d === true) return this.is("input:text") ? this.on({ "keydown.format": function (b) {
|
|
var f = h(this), e = f.data("numFormat"), g = b.keyCode ? b.keyCode : b.which, m = "", j = s.apply(this, ["start"]), p = s.apply(this, ["end"]), k = ""; k = false; if (q.codes.hasOwnProperty(g)) g = q.codes[g]; if (!b.shiftKey && g >= 65 && g <= 90) g += 32; else if (!b.shiftKey && g >= 69 && g <= 105) g -= 48; else if (b.shiftKey && q.shifts.hasOwnProperty(g)) m = q.shifts[g]; if (m == "") m = String.fromCharCode(g);
|
|
if (g !== 8 && m != c && !m.match(/[0-9]/)) { f = b.keyCode ? b.keyCode : b.which; if (f == 46 || f == 8 || f == 9 || f == 27 || f == 13 || (f == 65 || f == 82) && (b.ctrlKey || b.metaKey) === true || f >= 35 && f <= 39) return; b.preventDefault(); return false } if ((j == 0 && p == this.value.length || f.val() == 0) && !b.metaKey && !b.ctrlKey && !b.altKey && m.length === 1 && m != 0) { j = p = 1; this.value = ""; e.init = a > 0 ? -1 : 0; e.c = a > 0 ? -(a + 1) : 0; r.apply(this, [0, 0]) } else e.c = p - this.value.length; if (a > 0 && m == c && j == this.value.length - a - 1) {
|
|
e.c++; e.init = Math.max(0, e.init); b.preventDefault(); k = this.value.length +
|
|
e.c
|
|
} else if (m == c) { e.init = Math.max(0, e.init); b.preventDefault() } else if (a > 0 && g == 8 && j == this.value.length - a) { b.preventDefault(); e.c--; k = this.value.length + e.c } else if (a > 0 && g == 8 && j > this.value.length - a) { if (this.value === "") return; if (this.value.slice(j - 1, j) != "0") { k = this.value.slice(0, j - 1) + "0" + this.value.slice(j); f.val(k.replace(n, "").replace(o, c)) } b.preventDefault(); e.c--; k = this.value.length + e.c } else if (g == 8 && this.value.slice(j - 1, j) == l) { b.preventDefault(); e.c--; k = this.value.length + e.c } else if (a > 0 && j == p &&
|
|
this.value.length > a + 1 && j > this.value.length - a - 1 && isFinite(+m) && !b.metaKey && !b.ctrlKey && !b.altKey && m.length === 1) { this.value = k = p === this.value.length ? this.value.slice(0, j - 1) : this.value.slice(0, j) + this.value.slice(j + 1); k = j } k !== false && r.apply(this, [k, k]); f.data("numFormat", e)
|
|
}, "keyup.format": function (b) {
|
|
var f = h(this), e = f.data("numFormat"); b = b.keyCode ? b.keyCode : b.which; var g = s.apply(this, ["start"]); if (!(this.value === "" || (b < 48 || b > 57) && (b < 96 || b > 105) && b !== 8)) {
|
|
f.val(f.val()); if (a > 0) if (e.init < 1) {
|
|
g = this.value.length -
|
|
a - (e.init < 0 ? 1 : 0); e.c = g - this.value.length; e.init = 1; f.data("numFormat", e)
|
|
} else if (g > this.value.length - a && b != 8) { e.c++; f.data("numFormat", e) } f = this.value.length + e.c; r.apply(this, [f, f])
|
|
}
|
|
}, "paste.format": function (b) { var f = h(this), e = b.originalEvent, g = null; if (window.clipboardData && window.clipboardData.getData) g = window.clipboardData.getData("Text"); else if (e.clipboardData && e.clipboardData.getData) g = e.clipboardData.getData("text/plain"); f.val(g); b.preventDefault(); return false }
|
|
}).each(function () {
|
|
var b = h(this).data("numFormat",
|
|
{ c: -(a + 1), decimals: a, thousands_sep: l, dec_point: c, regex_dec_num: n, regex_dec: o, init: false }); this.value !== "" && b.val(b.val())
|
|
}) : this.each(function () { var b = h(this), f = +b.text().replace(n, "").replace(o, "."); b.number(!isFinite(f) ? 0 : +f, a, c, l) }); return this.text(h.number.apply(window, arguments))
|
|
}; var t = null, u = null; if (h.valHooks.text) { t = h.valHooks.text.get; u = h.valHooks.text.set } else h.valHooks.text = {}; h.valHooks.text.get = function (d) {
|
|
var a = h(d).data("numFormat"); if (a) {
|
|
if (d.value === "") return ""; d = +d.value.replace(a.regex_dec_num,
|
|
"").replace(a.regex_dec, "."); return "" + (isFinite(d) ? d : 0)
|
|
} else if (h.isFunction(t)) return t(d)
|
|
}; h.valHooks.text.set = function (d, a) { var c = h(d).data("numFormat"); if (c) return d.value = h.number(a, c.decimals, c.dec_point, c.thousands_sep); else if (h.isFunction(u)) return u(d, a) }; h.number = function (d, a, c, l) {
|
|
l = typeof l === "undefined" ? "," : l; c = typeof c === "undefined" ? "." : c; a = !isFinite(+a) ? 0 : Math.abs(a); var i = "\\u" + ("0000" + c.charCodeAt(0).toString(16)).slice(-4); d = (d + "").replace(RegExp(i, "g"), ".").replace(RegExp("[^0-9+-Ee.]",
|
|
"g"), ""); d = !isFinite(+d) ? 0 : +d; i = ""; i = function (n, o) { var b = Math.pow(10, o); return "" + Math.round(n * b) / b }; i = (a ? i(d, a) : "" + Math.round(d)).split("."); if (i[0].length > 3) i[0] = i[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, l); if ((i[1] || "").length < a) { i[1] = i[1] || ""; i[1] += Array(a - i[1].length + 1).join("0") } return i.join(c)
|
|
}
|
|
})(jQuery);
|