primevue-mirror/components/inputmask/InputMask.vue

522 lines
16 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<input :class="inputClass" :readonly="readonly" @input="onInput" @focus="onFocus" @blur="onBlur" @keydown="onKeyDown" @keypress="onKeyPress" @paste="onPaste" />
</template>
<script>
2022-09-14 11:26:01 +00:00
import { DomHandler } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'InputMask',
emits: ['update:modelValue', 'focus', 'blur', 'keydown', 'complete', 'keypress', 'paste'],
props: {
modelValue: null,
slotChar: {
type: String,
default: '_'
},
mask: {
type: String,
default: null
},
autoClear: {
type: Boolean,
default: true
},
unmask: {
type: Boolean,
default: false
},
readonly: {
type: Boolean,
default: false
}
},
2022-09-14 11:26:01 +00:00
mounted() {
this.tests = [];
this.partialPosition = this.mask.length;
this.len = this.mask.length;
this.firstNonMaskPos = null;
this.defs = {
9: '[0-9]',
a: '[A-Za-z]',
'*': '[A-Za-z0-9]'
};
let ua = DomHandler.getUserAgent();
this.androidChrome = /chrome/i.test(ua) && /android/i.test(ua);
let maskTokens = this.mask.split('');
for (let i = 0; i < maskTokens.length; i++) {
let c = maskTokens[i];
if (c === '?') {
this.len--;
this.partialPosition = i;
} else if (this.defs[c]) {
this.tests.push(new RegExp(this.defs[c]));
if (this.firstNonMaskPos === null) {
this.firstNonMaskPos = this.tests.length - 1;
}
if (i < this.partialPosition) {
this.lastRequiredNonMaskPos = this.tests.length - 1;
}
} else {
this.tests.push(null);
}
}
this.buffer = [];
for (let i = 0; i < maskTokens.length; i++) {
let c = maskTokens[i];
if (c !== '?') {
if (this.defs[c]) this.buffer.push(this.getPlaceholder(i));
else this.buffer.push(c);
}
}
this.defaultBuffer = this.buffer.join('');
this.updateValue(false);
},
updated() {
if (this.isValueUpdated()) {
this.updateValue();
}
},
2022-09-06 12:03:37 +00:00
methods: {
onInput(event) {
2022-09-14 11:26:01 +00:00
if (this.androidChrome) this.handleAndroidInput(event);
else this.handleInputChange(event);
2022-09-06 12:03:37 +00:00
this.$emit('update:modelValue', event.target.value);
},
onFocus(event) {
if (this.readonly) {
return;
}
this.focus = true;
clearTimeout(this.caretTimeoutId);
let pos;
this.focusText = this.$el.value;
pos = this.checkVal();
this.caretTimeoutId = setTimeout(() => {
if (this.$el !== document.activeElement) {
return;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.writeBuffer();
2022-09-14 11:26:01 +00:00
if (pos === this.mask.replace('?', '').length) {
2022-09-06 12:03:37 +00:00
this.caret(0, pos);
} else {
this.caret(pos);
}
}, 10);
2022-09-14 11:26:01 +00:00
this.$emit('focus', event);
2022-09-06 12:03:37 +00:00
},
onBlur(event) {
this.focus = false;
this.checkVal();
this.updateModel(event);
if (this.$el.value !== this.focusText) {
let e = document.createEvent('HTMLEvents');
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
e.initEvent('change', true, false);
this.$el.dispatchEvent(e);
}
this.$emit('blur', event);
},
onKeyDown(event) {
if (this.readonly) {
return;
}
let k = event.which || event.keyCode,
pos,
begin,
end;
let iPhone = /iphone/i.test(DomHandler.getUserAgent());
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.oldVal = this.$el.value;
//backspace, delete, and escape get special treatment
if (k === 8 || k === 46 || (iPhone && k === 127)) {
pos = this.caret();
begin = pos.begin;
end = pos.end;
if (end - begin === 0) {
begin = k !== 46 ? this.seekPrev(begin) : (end = this.seekNext(begin - 1));
end = k === 46 ? this.seekNext(end) : end;
}
this.clearBuffer(begin, end);
this.shiftL(begin, end - 1);
this.updateModel(event);
event.preventDefault();
2022-09-14 11:26:01 +00:00
} else if (k === 13) {
// enter
2022-09-06 12:03:37 +00:00
this.$el.blur();
this.updateModel(event);
2022-09-14 11:26:01 +00:00
} else if (k === 27) {
// escape
2022-09-06 12:03:37 +00:00
this.$el.value = this.focusText;
this.caret(0, this.checkVal());
this.updateModel(event);
event.preventDefault();
}
this.$emit('keydown', event);
},
onKeyPress(event) {
if (this.readonly) {
return;
}
var k = event.which || event.keyCode,
pos = this.caret(),
p,
c,
next,
completed;
2022-09-14 11:26:01 +00:00
if (event.ctrlKey || event.altKey || event.metaKey || k < 32) {
//Ignore
2022-09-06 12:03:37 +00:00
return;
} else if (k && k !== 13) {
if (pos.end - pos.begin !== 0) {
this.clearBuffer(pos.begin, pos.end);
this.shiftL(pos.begin, pos.end - 1);
}
p = this.seekNext(pos.begin - 1);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (p < this.len) {
c = String.fromCharCode(k);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.tests[p].test(c)) {
this.shiftR(p);
this.buffer[p] = c;
this.writeBuffer();
next = this.seekNext(p);
if (/android/i.test(DomHandler.getUserAgent())) {
//Path for CSP Violation on FireFox OS 1.1
let proxy = () => {
this.caret(next);
};
setTimeout(proxy, 0);
} else {
this.caret(next);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (pos.begin <= this.lastRequiredNonMaskPos) {
completed = this.isCompleted();
}
}
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
event.preventDefault();
}
this.updateModel(event);
if (completed) {
this.$emit('complete', event);
}
this.$emit('keypress', event);
},
2022-09-14 11:26:01 +00:00
onPaste(event) {
2022-09-06 12:03:37 +00:00
this.handleInputChange(event);
this.$emit('paste', event);
},
caret(first, last) {
let range, begin, end;
if (!this.$el.offsetParent || this.$el !== document.activeElement) {
return;
}
if (typeof first === 'number') {
begin = first;
2022-09-14 11:26:01 +00:00
end = typeof last === 'number' ? last : begin;
2022-09-06 12:03:37 +00:00
if (this.$el.setSelectionRange) {
this.$el.setSelectionRange(begin, end);
2022-09-14 11:26:01 +00:00
} else if (this.$el['createTextRange']) {
2022-09-06 12:03:37 +00:00
range = this.$el['createTextRange']();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', begin);
range.select();
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
if (this.$el.setSelectionRange) {
begin = this.$el.selectionStart;
end = this.$el.selectionEnd;
2022-09-14 11:26:01 +00:00
} else if (document['selection'] && document['selection'].createRange) {
2022-09-06 12:03:37 +00:00
range = document['selection'].createRange();
begin = 0 - range.duplicate().moveStart('character', -100000);
end = begin + range.text.length;
}
return { begin: begin, end: end };
}
},
isCompleted() {
for (let i = this.firstNonMaskPos; i <= this.lastRequiredNonMaskPos; i++) {
if (this.tests[i] && this.buffer[i] === this.getPlaceholder(i)) {
return false;
}
}
return true;
},
getPlaceholder(i) {
if (i < this.slotChar.length) {
return this.slotChar.charAt(i);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return this.slotChar.charAt(0);
},
seekNext(pos) {
while (++pos < this.len && !this.tests[pos]);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return pos;
},
seekPrev(pos) {
while (--pos >= 0 && !this.tests[pos]);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
return pos;
},
shiftL(begin, end) {
let i, j;
if (begin < 0) {
return;
}
for (i = begin, j = this.seekNext(end); i < this.len; i++) {
if (this.tests[i]) {
if (j < this.len && this.tests[i].test(this.buffer[j])) {
this.buffer[i] = this.buffer[j];
this.buffer[j] = this.getPlaceholder(j);
} else {
break;
}
j = this.seekNext(j);
}
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.writeBuffer();
this.caret(Math.max(this.firstNonMaskPos, begin));
},
shiftR(pos) {
let i, c, j, t;
for (i = pos, c = this.getPlaceholder(pos); i < this.len; i++) {
if (this.tests[i]) {
j = this.seekNext(i);
t = this.buffer[i];
this.buffer[i] = c;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (j < this.len && this.tests[j].test(t)) {
c = t;
} else {
break;
}
}
}
},
handleAndroidInput(event) {
var curVal = this.$el.value;
var pos = this.caret();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.oldVal && this.oldVal.length && this.oldVal.length > curVal.length) {
// a deletion or backspace happened
this.checkVal(true);
2022-09-14 11:26:01 +00:00
while (pos.begin > 0 && !this.tests[pos.begin - 1]) pos.begin--;
2022-09-06 12:03:37 +00:00
if (pos.begin === 0) {
2022-09-14 11:26:01 +00:00
while (pos.begin < this.firstNonMaskPos && !this.tests[pos.begin]) pos.begin++;
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.caret(pos.begin, pos.begin);
} else {
this.checkVal(true);
2022-09-14 11:26:01 +00:00
while (pos.begin < this.len && !this.tests[pos.begin]) pos.begin++;
2022-09-06 12:03:37 +00:00
this.caret(pos.begin, pos.begin);
}
if (this.isCompleted()) {
this.$emit('complete', event);
}
},
clearBuffer(start, end) {
let i;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (i = start; i < end && i < this.len; i++) {
if (this.tests[i]) {
this.buffer[i] = this.getPlaceholder(i);
}
}
},
writeBuffer() {
this.$el.value = this.buffer.join('');
},
checkVal(allow) {
this.isValueChecked = true;
//try to place characters where they belong
let test = this.$el.value,
lastMatch = -1,
i,
c,
pos;
for (i = 0, pos = 0; i < this.len; i++) {
if (this.tests[i]) {
this.buffer[i] = this.getPlaceholder(i);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
while (pos++ < test.length) {
c = test.charAt(pos - 1);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.tests[i].test(c)) {
this.buffer[i] = c;
lastMatch = i;
break;
}
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (pos > test.length) {
this.clearBuffer(i + 1, this.len);
break;
}
} else {
if (this.buffer[i] === test.charAt(pos)) {
pos++;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (i < this.partialPosition) {
lastMatch = i;
}
}
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (allow) {
this.writeBuffer();
} else if (lastMatch + 1 < this.partialPosition) {
if (this.autoClear || this.buffer.join('') === this.defaultBuffer) {
// Invalid value. Remove it and replace it with the
// mask, which is the default behavior.
if (this.$el.value) this.$el.value = '';
this.clearBuffer(0, this.len);
} else {
// Invalid value, but we opt to show the value to the
// user and allow them to correct their mistake.
this.writeBuffer();
}
} else {
this.writeBuffer();
this.$el.value = this.$el.value.substring(0, lastMatch + 1);
}
2022-09-14 11:26:01 +00:00
return this.partialPosition ? i : this.firstNonMaskPos;
2022-09-06 12:03:37 +00:00
},
handleInputChange(event) {
if (this.readonly) {
return;
}
var pos = this.checkVal(true);
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.caret(pos);
this.updateModel(event);
if (this.isCompleted()) {
this.$emit('complete', event);
}
},
getUnmaskedValue() {
let unmaskedBuffer = [];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let i = 0; i < this.buffer.length; i++) {
let c = this.buffer[i];
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.tests[i] && c !== this.getPlaceholder(i)) {
unmaskedBuffer.push(c);
}
}
return unmaskedBuffer.join('');
},
updateModel(e) {
let val = this.unmask ? this.getUnmaskedValue() : e.target.value;
2022-09-14 11:26:01 +00:00
this.$emit('update:modelValue', this.defaultBuffer !== val ? val : '');
2022-09-06 12:03:37 +00:00
},
updateValue(updateModel = true) {
if (this.$el) {
if (this.modelValue == null) {
this.$el.value = '';
updateModel && this.$emit('update:modelValue', '');
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.$el.value = this.modelValue;
this.checkVal();
setTimeout(() => {
if (this.$el) {
this.writeBuffer();
this.checkVal();
if (updateModel) {
let val = this.unmask ? this.getUnmaskedValue() : this.$el.value;
2022-09-14 11:26:01 +00:00
this.$emit('update:modelValue', this.defaultBuffer !== val ? val : '');
2022-09-06 12:03:37 +00:00
}
}
}, 10);
}
this.focusText = this.$el.value;
}
},
isValueUpdated() {
2022-09-14 11:26:01 +00:00
return this.unmask ? this.modelValue != this.getUnmaskedValue() : this.defaultBuffer !== this.$el.value && this.$el.value !== this.modelValue;
2022-09-06 12:03:37 +00:00
}
},
computed: {
filled() {
2022-09-14 11:26:01 +00:00
return this.modelValue != null && this.modelValue.toString().length > 0;
2022-09-06 12:03:37 +00:00
},
inputClass() {
2022-09-14 11:26:01 +00:00
return [
'p-inputmask p-inputtext p-component',
{
'p-filled': this.filled
}
];
}
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>