inputmask.phone.extensions.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Input Mask plugin extensions
  3. http://github.com/RobinHerbots/jquery.inputmask
  4. Copyright (c) 2010 - Robin Herbots
  5. Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
  6. Version: 0.0.0-dev
  7. Phone extension.
  8. */
  9. (function (factory) {
  10. if (typeof define === "function" && define.amd) {
  11. define(["inputmask.dependencyLib", "inputmask"], factory);
  12. } else if (typeof exports === "object") {
  13. module.exports = factory(require("./inputmask.dependencyLib"), require("./inputmask"));
  14. } else {
  15. factory(window.dependencyLib || jQuery, window.Inputmask);
  16. }
  17. }
  18. (function ($, Inputmask) {
  19. var analyseMaskBase = Inputmask.analyseMask;
  20. function MaskToken(isGroup, isOptional, isQuantifier, isAlternator) {
  21. this.matches = [];
  22. this.isGroup = isGroup || false;
  23. this.isOptional = isOptional || false;
  24. this.isQuantifier = isQuantifier || false;
  25. this.isAlternator = isAlternator || false;
  26. this.quantifier = {
  27. min: 1,
  28. max: 1
  29. };
  30. }
  31. Inputmask.analyseMask = function (mask, opts) {
  32. function consolidateAlternateTokens(matches) {
  33. var consolidatedTokens = [];
  34. $.each(matches, function (ndx, maskToken) {
  35. if (maskToken.isAlternator) {
  36. var cleanupNdx = [];
  37. $.each(maskToken.matches, function (ndx, childToken) {
  38. if (childToken && childToken.matches) {
  39. if (childToken.matches.length > 0) {
  40. if (consolidatedTokens[childToken.matches[0].nativeDef] === undefined && childToken.matches && !childToken.isGroup && !childToken.isOptional && !childToken.isQuantifier && !childToken.isAlternator) {
  41. consolidatedTokens[childToken.matches[0].nativeDef] = new MaskToken(false, false, false, true);
  42. var alternateToken = consolidatedTokens[childToken.matches[0].nativeDef];
  43. var token = new MaskToken();
  44. token.matches = childToken.matches.slice(1);
  45. alternateToken.matches.push(token);
  46. childToken.matches.splice(1, token.matches.length, alternateToken);
  47. }
  48. else {
  49. cleanupNdx.push(ndx);
  50. var alternateToken = consolidatedTokens[childToken.matches[0].nativeDef];
  51. var token = new MaskToken();
  52. token.matches = childToken.matches.slice(1);
  53. alternateToken.matches.push(token);
  54. }
  55. } else {
  56. maskToken.matches.splice(ndx, 1);
  57. }
  58. }
  59. });
  60. maskToken.matches = $.map(maskToken.matches, function (match, ndx) {
  61. if (cleanupNdx.indexOf(ndx) === -1) return match;
  62. });
  63. }
  64. if (maskToken.matches) {
  65. if (maskToken.matches.length === 1) //remove unnecessary nesting
  66. matches[ndx] = maskToken.matches[0];
  67. else if (maskToken.matches.length > 1) {
  68. var prevMatch;
  69. $.each(maskToken.matches, function (ndx, match) {
  70. if (prevMatch) {
  71. } else prevMatch = match;
  72. });
  73. }
  74. }
  75. if (matches[ndx].matches) {
  76. consolidateAlternateTokens(matches[ndx].matches);
  77. }
  78. });
  79. }
  80. var mt = analyseMaskBase.call(this, mask, opts);
  81. if (false && opts.phoneCodes) {
  82. console.time("Optimizing...");
  83. consolidateAlternateTokens(mt[0].matches);
  84. if (mt[0].matches && mt[0].matches.length === 1) //remove unnecessary nesting
  85. mt[0] = mt[0].matches[0];
  86. console.timeEnd("Optimizing...");
  87. console.log(JSON.stringify(mt));
  88. }
  89. return mt;
  90. };
  91. Inputmask.extendAliases({
  92. "abstractphone": {
  93. countrycode: "",
  94. phoneCodes: [],
  95. mask: function (opts) {
  96. opts.definitions = {"#": opts.definitions["9"]};
  97. //do some sorting
  98. var masks = opts.phoneCodes.sort(function (a, b) {
  99. var maska = (a.mask || a).replace(/#/g, "9").replace(/[\)]/, "9").replace(/[\+\(\)#-]/g, ""),
  100. maskb = (b.mask || b).replace(/#/g, "9").replace(/[\)]/, "9").replace(/[\+\(\)#-]/g, ""),
  101. maskas = (a.mask || a).split("#")[0],
  102. maskbs = (b.mask || b).split("#")[0];
  103. return maskbs.indexOf(maskas) === 0 ? -1 : (maskas.indexOf(maskbs) === 0 ? 1 : maska.localeCompare(maskb));
  104. });
  105. return masks;
  106. },
  107. keepStatic: true,
  108. onBeforeMask: function (value, opts) {
  109. var processedValue = value.replace(/^0{1,2}/, "").replace(/[\s]/g, "");
  110. if (processedValue.indexOf(opts.countrycode) > 1 || processedValue.indexOf(opts.countrycode) === -1) {
  111. processedValue = "+" + opts.countrycode + processedValue;
  112. }
  113. return processedValue;
  114. },
  115. onUnMask: function (maskedValue, unmaskedValue, opts) {
  116. //implement me
  117. return unmaskedValue;
  118. },
  119. inputmode: "tel",
  120. }
  121. });
  122. return Inputmask;
  123. }));