PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cosm-entitlement/src/main/webapp/resources/js/JS full versions/input-mask.js

https://gitlab.com/ayemi/entitlement
JavaScript | 281 lines | 208 code | 36 blank | 37 comment | 43 complexity | 382aa71c0e07ad41e8c412d62fc6640f MD5 | raw file
  1. /**
  2. * jquery.mask.js
  3. * @version: v1.5.3
  4. * @author: Igor Escobar
  5. *
  6. * Created by Igor Escobar on 2012-03-10. Please report any bug at http://blog.igorescobar.com
  7. *
  8. * Copyright (c) 2012 Igor Escobar http://blog.igorescobar.com
  9. *
  10. * The MIT License (http://www.opensource.org/licenses/mit-license.php)
  11. *
  12. * Permission is hereby granted, free of charge, to any person
  13. * obtaining a copy of this software and associated documentation
  14. * files (the "Software"), to deal in the Software without
  15. * restriction, including without limitation the rights to use,
  16. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the
  18. * Software is furnished to do so, subject to the following
  19. * conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be
  22. * included in all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  26. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  28. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  29. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  31. * OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. (function ($) {
  34. "use strict";
  35. var Mask = function (el, mask, options) {
  36. var jMask = this, old_value;
  37. el = $(el);
  38. mask = typeof mask === "function" ? mask(el.val(), undefined, el, options) : mask;
  39. jMask.init = function() {
  40. options = options || {};
  41. jMask.byPassKeys = [9, 16, 17, 18, 36, 37, 38, 39, 40, 91];
  42. jMask.translation = {
  43. '0': {pattern: /\d/},
  44. '9': {pattern: /\d/, optional: true},
  45. '#': {pattern: /\d/, recursive: true},
  46. 'A': {pattern: /[a-zA-Z0-9]/},
  47. 'S': {pattern: /[a-zA-Z]/}
  48. };
  49. jMask.translation = $.extend({}, jMask.translation, options.translation);
  50. jMask = $.extend(true, {}, jMask, options);
  51. el.each(function() {
  52. if (options.maxlength !== false) {
  53. el.attr('maxlength', mask.length);
  54. }
  55. el.attr('autocomplete', 'off');
  56. p.destroyEvents();
  57. p.events();
  58. p.val(p.getMasked());
  59. });
  60. };
  61. var p = {
  62. getCaret: function () {
  63. var sel,
  64. pos = 0,
  65. ctrl = el.get(0),
  66. dSel = document.selection,
  67. cSelStart = ctrl.selectionStart;
  68. // IE Support
  69. if (dSel && navigator.appVersion.indexOf("MSIE 10") === -1) {
  70. ctrl.focus();
  71. sel = dSel.createRange ();
  72. sel.moveStart ('character', -ctrl.value.length);
  73. pos = sel.text.length;
  74. }
  75. // Firefox support
  76. else if (cSelStart || cSelStart === '0') {
  77. pos = cSelStart;
  78. }
  79. return pos;
  80. },
  81. setCaret: function(pos) {
  82. var range, ctrl = el.get(0);
  83. if (ctrl.setSelectionRange) {
  84. ctrl.focus();
  85. ctrl.setSelectionRange(pos,pos);
  86. } else if (ctrl.createTextRange) {
  87. range = ctrl.createTextRange();
  88. range.collapse(true);
  89. range.moveEnd('character', pos);
  90. range.moveStart('character', pos);
  91. range.select();
  92. }
  93. },
  94. events: function() {
  95. el.on('keydown.mask', function() {
  96. old_value = p.val();
  97. });
  98. el.on('keyup.mask', p.behaviour);
  99. el.on("paste.mask", function() {
  100. setTimeout(function() {
  101. el.keydown().keyup();
  102. }, 100);
  103. });
  104. },
  105. destroyEvents: function() {
  106. el.off('keydown.mask keyup.mask paste.mask');
  107. },
  108. val: function(v) {
  109. var isInput = el.get(0).tagName.toLowerCase() === "input";
  110. return arguments.length > 0
  111. ? (isInput ? el.val(v) : el.text(v))
  112. : (isInput ? el.val() : el.text());
  113. },
  114. behaviour: function(e) {
  115. e = e || window.event;
  116. if ($.inArray(e.keyCode || e.which, jMask.byPassKeys) === -1) {
  117. var changeCaret, caretPos = p.getCaret();
  118. if (caretPos < p.val().length) {
  119. changeCaret = true;
  120. }
  121. p.val(p.getMasked());
  122. if (changeCaret) {
  123. p.setCaret(caretPos);
  124. }
  125. return p.callbacks(e);
  126. }
  127. },
  128. getMasked: function (skipMaskChars) {
  129. var buf = [],
  130. value = p.val(),
  131. m = 0, maskLen = mask.length,
  132. v = 0, valLen = value.length,
  133. offset = 1, addMethod = "push",
  134. resetPos = -1,
  135. lastMaskChar,
  136. check;
  137. if (options.reverse) {
  138. addMethod = "unshift";
  139. offset = -1;
  140. lastMaskChar = 0;
  141. m = maskLen - 1;
  142. v = valLen - 1;
  143. check = function () {
  144. return m > -1 && v > -1;
  145. };
  146. } else {
  147. lastMaskChar = maskLen - 1;
  148. check = function () {
  149. return m < maskLen && v < valLen;
  150. };
  151. }
  152. while (check()) {
  153. var maskDigit = mask.charAt(m),
  154. valDigit = value.charAt(v),
  155. translation = jMask.translation[maskDigit];
  156. if (translation) {
  157. if (valDigit.match(translation.pattern)) {
  158. buf[addMethod](valDigit);
  159. if (translation.recursive) {
  160. if (resetPos === -1) {
  161. resetPos = m;
  162. } else if (m === lastMaskChar) {
  163. m = resetPos - offset;
  164. }
  165. if (lastMaskChar === resetPos) {
  166. m -= offset;
  167. }
  168. }
  169. m += offset;
  170. } else if (translation.optional) {
  171. m += offset;
  172. v -= offset;
  173. }
  174. v += offset;
  175. } else {
  176. if (!skipMaskChars) {
  177. buf[addMethod](maskDigit);
  178. }
  179. if (valDigit === maskDigit) {
  180. v += offset;
  181. }
  182. m += offset;
  183. }
  184. }
  185. var lastMaskCharDigit = mask.charAt(lastMaskChar);
  186. if (maskLen === valLen + 1 && !jMask.translation[lastMaskCharDigit]) {
  187. buf.push(lastMaskCharDigit);
  188. }
  189. return buf.join("");
  190. },
  191. callbacks: function (e) {
  192. var val = p.val(),
  193. changed = p.val() !== old_value;
  194. if (changed === true) {
  195. if (typeof options.onChange === "function") {
  196. options.onChange(val, e, el, options);
  197. }
  198. }
  199. if (changed === true && typeof options.onKeyPress === "function") {
  200. options.onKeyPress(val, e, el, options);
  201. }
  202. if (typeof options.onComplete === "function" && val.length === mask.length) {
  203. options.onComplete(val, e, el, options);
  204. }
  205. }
  206. };
  207. // public methods
  208. jMask.remove = function() {
  209. p.destroyEvents();
  210. p.val(jMask.getCleanVal()).removeAttr('maxlength');
  211. };
  212. // get value without mask
  213. jMask.getCleanVal = function() {
  214. return p.getMasked(true);
  215. };
  216. jMask.init();
  217. };
  218. $.fn.mask = function(mask, options) {
  219. return this.each(function() {
  220. $(this).data('mask', new Mask(this, mask, options));
  221. });
  222. };
  223. $.fn.unmask = function() {
  224. return this.each(function() {
  225. try {
  226. $(this).data('mask').remove();
  227. } catch (e) {}
  228. });
  229. };
  230. $.fn.cleanVal = function() {
  231. return $(this).data('mask').getCleanVal();
  232. };
  233. // looking for inputs with data-mask attribute
  234. $('*[data-mask]').each(function() {
  235. var input = $(this),
  236. options = {};
  237. if (input.attr('data-mask-reverse') === 'true') {
  238. options.reverse = true;
  239. }
  240. if (input.attr('data-mask-maxlength') === 'false') {
  241. options.maxlength = false;
  242. }
  243. input.mask(input.attr('data-mask'), options);
  244. });
  245. })(window.jQuery || window.Zepto);