Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

maskable.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Maskable
  3. *
  4. * @mixin
  5. *
  6. * Creates an input mask that is
  7. * generated from a masked str
  8. *
  9. * Example: mask="#### #### #### ####"
  10. */
  11. import { isMaskDelimiter, maskText as _maskText, unmaskText as _unmaskText } from '../util/mask';
  12. /* @vue/component */
  13. export default {
  14. name: 'maskable',
  15. props: {
  16. dontFillMaskBlanks: Boolean,
  17. mask: {
  18. type: [Object, String],
  19. default: null
  20. },
  21. returnMaskedValue: Boolean,
  22. value: { required: false }
  23. },
  24. data: function data(vm) {
  25. return {
  26. selection: 0,
  27. lazySelection: 0,
  28. lazyValue: vm.value,
  29. preDefined: {
  30. 'credit-card': '#### - #### - #### - ####',
  31. 'date': '##/##/####',
  32. 'date-with-time': '##/##/#### ##:##',
  33. 'phone': '(###) ### - ####',
  34. 'social': '###-##-####',
  35. 'time': '##:##',
  36. 'time-with-seconds': '##:##:##'
  37. }
  38. };
  39. },
  40. computed: {
  41. masked: function masked() {
  42. var preDefined = this.preDefined[this.mask];
  43. var mask = preDefined || this.mask || '';
  44. return mask.split('');
  45. }
  46. },
  47. watch: {
  48. /**
  49. * Make sure the cursor is in the correct
  50. * location when the mask changes
  51. */
  52. mask: function mask() {
  53. var _this = this;
  54. if (!this.$refs.input) return;
  55. var oldValue = this.$refs.input.value;
  56. var newValue = this.maskText(_unmaskText(this.lazyValue));
  57. var position = 0;
  58. var selection = this.selection;
  59. for (var index = 0; index < selection; index++) {
  60. isMaskDelimiter(oldValue[index]) || position++;
  61. }
  62. selection = 0;
  63. if (newValue) {
  64. for (var _index = 0; _index < newValue.length; _index++) {
  65. isMaskDelimiter(newValue[_index]) || position--;
  66. selection++;
  67. if (position <= 0) break;
  68. }
  69. }
  70. this.$nextTick(function () {
  71. _this.$refs.input.value = newValue;
  72. _this.setCaretPosition(selection);
  73. });
  74. }
  75. },
  76. beforeMount: function beforeMount() {
  77. if (!this.mask || this.value == null || !this.returnMaskedValue) return;
  78. var value = this.maskText(this.value);
  79. // See if masked value does not
  80. // match the user given value
  81. if (value === this.value) return;
  82. this.$emit('input', value);
  83. },
  84. methods: {
  85. setCaretPosition: function setCaretPosition(selection) {
  86. var _this2 = this;
  87. this.selection = selection;
  88. window.setTimeout(function () {
  89. _this2.$refs.input && _this2.$refs.input.setSelectionRange(_this2.selection, _this2.selection);
  90. }, 0);
  91. },
  92. updateRange: function updateRange() {
  93. /* istanbul ignore next */
  94. if (!this.$refs.input) return;
  95. var newValue = this.maskText(this.lazyValue);
  96. var selection = 0;
  97. this.$refs.input.value = newValue;
  98. if (newValue) {
  99. for (var index = 0; index < newValue.length; index++) {
  100. if (this.lazySelection <= 0) break;
  101. isMaskDelimiter(newValue[index]) || this.lazySelection--;
  102. selection++;
  103. }
  104. }
  105. this.setCaretPosition(selection);
  106. // this.$emit() must occur only when all internal values are correct
  107. this.$emit('input', this.returnMaskedValue ? this.$refs.input.value : this.lazyValue);
  108. },
  109. maskText: function maskText(text) {
  110. return this.mask ? _maskText(text, this.masked, this.dontFillMaskBlanks) : text;
  111. },
  112. unmaskText: function unmaskText(text) {
  113. return this.mask && !this.returnMaskedValue ? _unmaskText(text) : text;
  114. },
  115. // When the input changes and is
  116. // re-created, ensure that the
  117. // caret location is correct
  118. setSelectionRange: function setSelectionRange() {
  119. this.$nextTick(this.updateRange);
  120. },
  121. resetSelections: function resetSelections(input) {
  122. if (!input.selectionEnd) return;
  123. this.selection = input.selectionEnd;
  124. this.lazySelection = 0;
  125. for (var index = 0; index < this.selection; index++) {
  126. isMaskDelimiter(input.value[index]) || this.lazySelection++;
  127. }
  128. }
  129. }
  130. };
  131. //# sourceMappingURL=maskable.js.map