jquery.inputmask.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Input Mask plugin for jquery
  3. * http://github.com/RobinHerbots/jquery.inputmask
  4. * Copyright (c) Robin Herbots
  5. * Licensed under the MIT license
  6. */
  7. import $ from "jquery";
  8. import Inputmask from "./inputmask";
  9. if ($.fn.inputmask === undefined) {
  10. // jquery plugin
  11. $.fn.inputmask = function (fn, options) {
  12. let nptmask,
  13. input = this[0];
  14. if (options === undefined) options = {};
  15. if (typeof fn === "string") {
  16. switch (fn) {
  17. case "unmaskedvalue":
  18. return input && input.inputmask
  19. ? input.inputmask.unmaskedvalue()
  20. : $(input).val();
  21. case "remove":
  22. return this.each(function () {
  23. if (this.inputmask) this.inputmask.remove();
  24. });
  25. case "getemptymask":
  26. return input && input.inputmask ? input.inputmask.getemptymask() : "";
  27. case "hasMaskedValue": // check whether the returned value is masked or not; currently only works reliable when using jquery.val fn to retrieve the value
  28. return input && input.inputmask
  29. ? input.inputmask.hasMaskedValue()
  30. : false;
  31. case "isComplete":
  32. return input && input.inputmask ? input.inputmask.isComplete() : true;
  33. case "getmetadata": // return mask metadata if exists
  34. return input && input.inputmask
  35. ? input.inputmask.getmetadata()
  36. : undefined;
  37. case "setvalue":
  38. Inputmask.setValue(input, options);
  39. break;
  40. case "option":
  41. if (typeof options === "string") {
  42. if (input && input.inputmask !== undefined) {
  43. return input.inputmask.option(options);
  44. }
  45. } else {
  46. return this.each(function () {
  47. if (this.inputmask !== undefined) {
  48. return this.inputmask.option(options);
  49. }
  50. });
  51. }
  52. break;
  53. default:
  54. options.alias = fn;
  55. nptmask = new Inputmask(options);
  56. return this.each(function () {
  57. nptmask.mask(this);
  58. });
  59. }
  60. } else if (Array.isArray(fn)) {
  61. options.alias = fn;
  62. nptmask = new Inputmask(options);
  63. return this.each(function () {
  64. nptmask.mask(this);
  65. });
  66. } else if (typeof fn === "object") {
  67. nptmask = new Inputmask(fn);
  68. if (fn.mask === undefined && fn.alias === undefined) {
  69. return this.each(function () {
  70. if (this.inputmask !== undefined) {
  71. return this.inputmask.option(fn);
  72. } else nptmask.mask(this);
  73. });
  74. } else {
  75. return this.each(function () {
  76. nptmask.mask(this);
  77. });
  78. }
  79. } else if (fn === undefined) {
  80. // look for data-inputmask atributes
  81. return this.each(function () {
  82. nptmask = new Inputmask(options);
  83. nptmask.mask(this);
  84. });
  85. }
  86. };
  87. }