inputmask.phone.extensions.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(["./dependencyLibs/inputmask.dependencyLib", "./inputmask"], factory);
  12. } else if (typeof exports === "object") {
  13. module.exports = factory(require("./dependencyLibs/inputmask.dependencyLib"), require("./inputmask"));
  14. } else {
  15. factory(window.dependencyLib || jQuery, window.Inputmask);
  16. }
  17. }
  18. (function ($, Inputmask) {
  19. function maskSort(a, b) {
  20. var maska = (a.mask || a).replace(/#/g, "9").replace(/\)/, "9").replace(/[+()#-]/g, ""),
  21. maskb = (b.mask || b).replace(/#/g, "9").replace(/\)/, "9").replace(/[+()#-]/g, ""),
  22. maskas = (a.mask || a).split("#")[0],
  23. maskbs = (b.mask || b).split("#")[0];
  24. return maskbs.indexOf(maskas) === 0 ? -1 : (maskas.indexOf(maskbs) === 0 ? 1 : maska.localeCompare(maskb));
  25. }
  26. var analyseMaskBase = Inputmask.prototype.analyseMask;
  27. Inputmask.prototype.analyseMask = function (mask, regexMask, opts) {
  28. var maskGroups = {};
  29. function reduceVariations(masks, previousVariation, previousmaskGroup) {
  30. previousVariation = previousVariation || "";
  31. previousmaskGroup = previousmaskGroup || maskGroups;
  32. if (previousVariation !== "")
  33. previousmaskGroup[previousVariation] = {};
  34. var variation = "", maskGroup = previousmaskGroup[previousVariation] || previousmaskGroup;
  35. for (var i = masks.length - 1; i >= 0; i--) {
  36. mask = masks[i].mask || masks[i];
  37. variation = mask.substr(0, 1);
  38. maskGroup[variation] = maskGroup[variation] || [];
  39. maskGroup[variation].unshift(mask.substr(1));
  40. masks.splice(i, 1);
  41. }
  42. for (var ndx in maskGroup) {
  43. if (maskGroup[ndx].length > 500) {
  44. reduceVariations(maskGroup[ndx].slice(), ndx, maskGroup);
  45. }
  46. }
  47. }
  48. function rebuild(maskGroup) {
  49. var mask = "", submasks = [];
  50. for (var ndx in maskGroup) {
  51. if ($.isArray(maskGroup[ndx])) {
  52. if (maskGroup[ndx].length === 1)
  53. submasks.push(ndx + maskGroup[ndx]);
  54. else
  55. submasks.push(ndx + opts.groupmarker.start + maskGroup[ndx].join(opts.groupmarker.end + opts.alternatormarker + opts.groupmarker.start) + opts.groupmarker.end);
  56. } else {
  57. submasks.push(ndx + rebuild(maskGroup[ndx]));
  58. }
  59. }
  60. if (submasks.length === 1) {
  61. mask += submasks[0];
  62. } else {
  63. mask += opts.groupmarker.start + submasks.join(opts.groupmarker.end + opts.alternatormarker + opts.groupmarker.start) + opts.groupmarker.end;
  64. }
  65. return mask;
  66. }
  67. if (opts.phoneCodes) {
  68. if (opts.phoneCodes && opts.phoneCodes.length > 1000) {
  69. mask = mask.substr(1, mask.length - 2);
  70. reduceVariations(mask.split(opts.groupmarker.end + opts.alternatormarker + opts.groupmarker.start));
  71. mask = rebuild(maskGroups);
  72. }
  73. //escape 9 definition
  74. mask = mask.replace(/9/g, "\\9");
  75. }
  76. // console.log(mask);
  77. var mt = analyseMaskBase.call(this, mask, regexMask,opts);
  78. return mt;
  79. };
  80. Inputmask.extendAliases({
  81. "abstractphone": {
  82. groupmarker: {
  83. start: "<",
  84. end: ">"
  85. },
  86. countrycode: "",
  87. phoneCodes: [],
  88. mask: function (opts) {
  89. opts.definitions = {"#": Inputmask.prototype.definitions["9"]};
  90. return opts.phoneCodes.sort(maskSort);
  91. },
  92. keepStatic: true,
  93. onBeforeMask: function (value, opts) {
  94. var processedValue = value.replace(/^0{1,2}/, "").replace(/[\s]/g, "");
  95. if (processedValue.indexOf(opts.countrycode) > 1 || processedValue.indexOf(opts.countrycode) === -1) {
  96. processedValue = "+" + opts.countrycode + processedValue;
  97. }
  98. return processedValue;
  99. },
  100. onUnMask: function (maskedValue, unmaskedValue, opts) {
  101. var unmasked = maskedValue.replace(/[()#-]/g, "");
  102. return unmasked;
  103. },
  104. inputmode: "tel",
  105. }
  106. });
  107. return Inputmask;
  108. }));