PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/jquery.inputmask/3.1.27/inputmask/jquery.inputmask.numeric.extensions.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 246 lines | 239 code | 0 blank | 7 comment | 108 complexity | f2963c03ff917d9514b8e6826dfdcd0a MD5 | raw file
  1. /*
  2. * jquery.inputmask.numeric.extensions.js
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) 2010 - 2014 Robin Herbots
  5. * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. * Version: 3.1.27
  7. */
  8. !function(factory) {
  9. "function" == typeof define && define.amd ? define([ "jquery", "./jquery.inputmask" ], factory) : factory(jQuery);
  10. }(function($) {
  11. return $.extend($.inputmask.defaults.aliases, {
  12. numeric: {
  13. mask: function(opts) {
  14. if (0 !== opts.repeat && isNaN(opts.integerDigits) && (opts.integerDigits = opts.repeat),
  15. opts.repeat = 0, opts.groupSeparator == opts.radixPoint && (opts.groupSeparator = "." == opts.radixPoint ? "," : "," == opts.radixPoint ? "." : ""),
  16. " " === opts.groupSeparator && (opts.skipOptionalPartCharacter = void 0), opts.autoGroup = opts.autoGroup && "" != opts.groupSeparator,
  17. opts.autoGroup && isFinite(opts.integerDigits)) {
  18. var seps = Math.floor(opts.integerDigits / opts.groupSize), mod = opts.integerDigits % opts.groupSize;
  19. opts.integerDigits += 0 == mod ? seps - 1 : seps;
  20. }
  21. opts.definitions[";"] = opts.definitions["~"];
  22. var mask = opts.prefix;
  23. return mask += "[+]", mask += "~{1," + opts.integerDigits + "}", void 0 != opts.digits && (isNaN(opts.digits) || parseInt(opts.digits) > 0) && (mask += opts.digitsOptional ? "[" + (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}]" : (opts.decimalProtect ? ":" : opts.radixPoint) + ";{" + opts.digits + "}"),
  24. mask += opts.suffix;
  25. },
  26. placeholder: "",
  27. greedy: !1,
  28. digits: "*",
  29. digitsOptional: !0,
  30. groupSeparator: "",
  31. radixPoint: ".",
  32. radixFocus: !0,
  33. groupSize: 3,
  34. autoGroup: !1,
  35. allowPlus: !0,
  36. allowMinus: !0,
  37. integerDigits: "+",
  38. prefix: "",
  39. suffix: "",
  40. rightAlign: !0,
  41. decimalProtect: !0,
  42. postFormat: function(buffer, pos, reformatOnly, opts) {
  43. var needsRefresh = !1, charAtPos = buffer[pos];
  44. if ("" == opts.groupSeparator || -1 != $.inArray(opts.radixPoint, buffer) && pos >= $.inArray(opts.radixPoint, buffer) || new RegExp("[-+]").test(charAtPos)) return {
  45. pos: pos
  46. };
  47. var cbuf = buffer.slice();
  48. charAtPos == opts.groupSeparator && (cbuf.splice(pos--, 1), charAtPos = cbuf[pos]),
  49. reformatOnly ? cbuf[pos] = "?" : cbuf.splice(pos, 0, "?");
  50. var bufVal = cbuf.join("");
  51. if (opts.autoGroup || reformatOnly && -1 != bufVal.indexOf(opts.groupSeparator)) {
  52. var escapedGroupSeparator = $.inputmask.escapeRegex.call(this, opts.groupSeparator);
  53. needsRefresh = 0 == bufVal.indexOf(opts.groupSeparator), bufVal = bufVal.replace(new RegExp(escapedGroupSeparator, "g"), "");
  54. var radixSplit = bufVal.split(opts.radixPoint);
  55. if (bufVal = radixSplit[0], bufVal != opts.prefix + "?0" && bufVal.length >= opts.groupSize + opts.prefix.length) {
  56. needsRefresh = !0;
  57. for (var reg = new RegExp("([-+]?[\\d?]+)([\\d?]{" + opts.groupSize + "})"); reg.test(bufVal); ) bufVal = bufVal.replace(reg, "$1" + opts.groupSeparator + "$2"),
  58. bufVal = bufVal.replace(opts.groupSeparator + opts.groupSeparator, opts.groupSeparator);
  59. }
  60. radixSplit.length > 1 && (bufVal += opts.radixPoint + radixSplit[1]);
  61. }
  62. buffer.length = bufVal.length;
  63. for (var i = 0, l = bufVal.length; l > i; i++) buffer[i] = bufVal.charAt(i);
  64. var newPos = $.inArray("?", buffer);
  65. return reformatOnly ? buffer[newPos] = charAtPos : buffer.splice(newPos, 1), {
  66. pos: newPos,
  67. refreshFromBuffer: needsRefresh
  68. };
  69. },
  70. onKeyDown: function(e, buffer, caretPos, opts) {
  71. if (e.keyCode == $.inputmask.keyCode.TAB && "0" != opts.placeholder.charAt(0)) {
  72. var radixPosition = $.inArray(opts.radixPoint, buffer);
  73. if (-1 != radixPosition && isFinite(opts.digits)) {
  74. for (var i = 1; i <= opts.digits; i++) (void 0 == buffer[radixPosition + i] || buffer[radixPosition + i] == opts.placeholder.charAt(0)) && (buffer[radixPosition + i] = "0");
  75. return {
  76. refreshFromBuffer: {
  77. start: ++radixPosition,
  78. end: radixPosition + opts.digits
  79. }
  80. };
  81. }
  82. } else if (opts.autoGroup && (e.keyCode == $.inputmask.keyCode.DELETE || e.keyCode == $.inputmask.keyCode.BACKSPACE)) {
  83. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  84. return rslt.caret = rslt.pos + 1, rslt;
  85. }
  86. },
  87. onKeyPress: function(e, buffer, caretPos, opts) {
  88. if (opts.autoGroup) {
  89. var rslt = opts.postFormat(buffer, caretPos - 1, !0, opts);
  90. return rslt.caret = rslt.pos + 1, rslt;
  91. }
  92. },
  93. regex: {
  94. integerPart: function() {
  95. return new RegExp("[-+]?\\d+");
  96. },
  97. integerNPart: function() {
  98. return new RegExp("\\d+");
  99. }
  100. },
  101. signHandler: function(chrs, buffer, pos, strict, opts) {
  102. if (!strict && (opts.allowMinus && "-" === chrs || opts.allowPlus && "+" === chrs)) {
  103. var matchRslt = buffer.join("").match(opts.regex.integerPart(opts));
  104. if (matchRslt && matchRslt.length > 0 && "0" !== matchRslt[matchRslt.index]) return buffer[matchRslt.index] == ("-" === chrs ? "+" : "-") ? {
  105. pos: matchRslt.index,
  106. c: chrs,
  107. remove: matchRslt.index,
  108. caret: pos
  109. } : buffer[matchRslt.index] == ("-" === chrs ? "-" : "+") ? {
  110. remove: matchRslt.index,
  111. caret: pos - 1
  112. } : {
  113. pos: matchRslt.index,
  114. c: chrs,
  115. caret: pos + 1
  116. };
  117. }
  118. return !1;
  119. },
  120. radixHandler: function(chrs, maskset, pos, strict, opts) {
  121. if (!strict && chrs === opts.radixPoint) {
  122. var radixPos = $.inArray(opts.radixPoint, maskset.buffer), integerValue = maskset.buffer.join("").match(opts.regex.integerPart(opts));
  123. if (-1 != radixPos) return maskset.validPositions[radixPos - 1] ? {
  124. caret: radixPos + 1
  125. } : {
  126. pos: integerValue.index,
  127. c: integerValue[0],
  128. caret: radixPos + 1
  129. };
  130. }
  131. return !1;
  132. },
  133. leadingZeroHandler: function(chrs, maskset, pos, strict, opts) {
  134. var matchRslt = maskset.buffer.join("").match(opts.regex.integerNPart(opts)), radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  135. if (matchRslt && !strict && (-1 == radixPosition || matchRslt.index < radixPosition)) if (0 == matchRslt[0].indexOf("0") && pos >= opts.prefix.length) {
  136. if (-1 == radixPosition || radixPosition >= pos && void 0 == maskset.validPositions[radixPosition]) return maskset.buffer.splice(matchRslt.index, 1),
  137. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  138. pos: pos,
  139. remove: matchRslt.index
  140. };
  141. if (pos > matchRslt.index && radixPosition >= pos) return maskset.buffer.splice(matchRslt.index, 1),
  142. pos = pos > matchRslt.index ? pos - 1 : matchRslt.index, {
  143. pos: pos,
  144. remove: matchRslt.index
  145. };
  146. } else if ("0" == chrs && pos <= matchRslt.index) return !1;
  147. return !0;
  148. },
  149. definitions: {
  150. "~": {
  151. validator: function(chrs, maskset, pos, strict, opts) {
  152. var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
  153. if (!isValid && (isValid = opts.radixHandler(chrs, maskset, pos, strict, opts),
  154. !isValid && (isValid = strict ? new RegExp("[0-9" + $.inputmask.escapeRegex.call(this, opts.groupSeparator) + "]").test(chrs) : new RegExp("[0-9]").test(chrs),
  155. isValid === !0 && (isValid = opts.leadingZeroHandler(chrs, maskset, pos, strict, opts),
  156. isValid === !0)))) {
  157. var radixPosition = $.inArray(opts.radixPoint, maskset.buffer);
  158. return opts.digitsOptional === !1 && pos > radixPosition && !strict ? {
  159. pos: pos,
  160. remove: pos
  161. } : {
  162. pos: pos
  163. };
  164. }
  165. return isValid;
  166. },
  167. cardinality: 1,
  168. prevalidator: null
  169. },
  170. "+": {
  171. validator: function(chrs, maskset, pos, strict, opts) {
  172. var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
  173. if (!isValid) {
  174. var signed = "[";
  175. opts.allowMinus === !0 && (signed += "-"), opts.allowPlus === !0 && (signed += "+"),
  176. signed += "]", isValid = new RegExp(signed).test(chrs);
  177. }
  178. return isValid;
  179. },
  180. cardinality: 1,
  181. prevalidator: null,
  182. placeholder: ""
  183. },
  184. ":": {
  185. validator: function(chrs, maskset, pos, strict, opts) {
  186. var isValid = opts.signHandler(chrs, maskset.buffer, pos, strict, opts);
  187. if (!isValid) {
  188. var radix = "[" + $.inputmask.escapeRegex.call(this, opts.radixPoint) + "]";
  189. isValid = new RegExp(radix).test(chrs), isValid && maskset.validPositions[pos] && maskset.validPositions[pos].match.placeholder == opts.radixPoint && (isValid = {
  190. pos: pos,
  191. remove: pos
  192. });
  193. }
  194. return isValid;
  195. },
  196. cardinality: 1,
  197. prevalidator: null,
  198. placeholder: function(opts) {
  199. return opts.radixPoint;
  200. }
  201. }
  202. },
  203. insertMode: !0,
  204. autoUnmask: !1,
  205. onUnMask: function(maskedValue, unmaskedValue, opts) {
  206. var processValue = maskedValue.replace(opts.prefix, "");
  207. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), "");
  208. },
  209. isComplete: function(buffer, opts) {
  210. var maskedValue = buffer.join(""), bufClone = buffer.slice();
  211. if (opts.postFormat(bufClone, 0, !0, opts), bufClone.join("") != maskedValue) return !1;
  212. var processValue = maskedValue.replace(opts.prefix, "");
  213. return processValue = processValue.replace(opts.suffix, ""), processValue = processValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  214. processValue = processValue.replace($.inputmask.escapeRegex.call(this, opts.radixPoint), "."),
  215. isFinite(processValue);
  216. },
  217. onBeforeMask: function(initialValue, opts) {
  218. if (isFinite(initialValue)) return initialValue.toString().replace(".", opts.radixPoint);
  219. var kommaMatches = initialValue.match(/,/g), dotMatches = initialValue.match(/\./g);
  220. return dotMatches && kommaMatches ? dotMatches.length > kommaMatches.length ? (initialValue = initialValue.replace(/\./g, ""),
  221. initialValue = initialValue.replace(",", opts.radixPoint)) : kommaMatches.length > dotMatches.length && (initialValue = initialValue.replace(/,/g, ""),
  222. initialValue = initialValue.replace(".", opts.radixPoint)) : initialValue = initialValue.replace(new RegExp($.inputmask.escapeRegex.call(this, opts.groupSeparator), "g"), ""),
  223. initialValue;
  224. }
  225. },
  226. currency: {
  227. prefix: "$ ",
  228. groupSeparator: ",",
  229. radixPoint: ".",
  230. alias: "numeric",
  231. placeholder: "0",
  232. autoGroup: !0,
  233. digits: 2,
  234. digitsOptional: !1,
  235. clearMaskOnLostFocus: !1,
  236. decimalProtect: !0
  237. },
  238. decimal: {
  239. alias: "numeric"
  240. },
  241. integer: {
  242. alias: "numeric",
  243. digits: "0"
  244. }
  245. }), $.fn.inputmask;
  246. });