This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
70 lines (54 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _vanillaMasker = require('vanilla-masker');
var _vanillaMasker2 = _interopRequireDefault(_vanillaMasker);
var _vue = require('vue');
var _vue2 = _interopRequireDefault(_vue);
var _eventListener = require('./event-listener');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var applyMaskToDefault = function applyMaskToDefault(el, mask, isMoney) {
var inputText = getInputText(el);
var isMoneyType = isMoney && inputText.value.length > 0;
if (isMoneyType) {
inputText.value = _vanillaMasker2.default.toMoney(inputText.value, { showSignal: true });
} else {
inputText.value = mask && mask.length > 0 ? _vanillaMasker2.default.toPattern(inputText.value, mask) : inputText.value;
}
};
var setMaxLength = function setMaxLength(inputText) {
var maskSize = inputText.getAttribute('data-mask').length;
inputText.setAttribute('maxlength', maskSize);
};
var getInputText = function getInputText(el) {
return !el instanceof HTMLInputElement ? el.querySelector('input') : el;
};
exports.default = {
bind: function bind(el, binding) {
if (binding.value.length < 1) return;
var inputText = getInputText(el);
var isMoney = binding.value === 'money';
inputText.setAttribute('data-mask', binding.value);
inputText.addEventListener('keyup', _eventListener.inputHandler);
!isMoney && setMaxLength(inputText);
applyMaskToDefault(inputText, binding.value, isMoney);
},
update: function update(el, binding) {
// this is only for v-model
if (binding.value.length < 1) return;
var inputText = getInputText(el);
var isMoney = binding.value === 'money';
if (!isMoney) {
inputText.setAttribute('data-mask', binding.value);
inputText.setAttribute('maxlength', inputText.getAttribute('data-mask').length);
}
applyMaskToDefault(inputText, binding.value, isMoney);
},
unbind: function unbind(el, binding) {
if (binding.value.length < 1) return;
var inputText = getInputText(el);
inputText.removeAttribute('maxlength');
inputText.removeEventListener('keyup', _eventListener.inputHandler);
}
};