Commit 11910f64 authored by NikulinR's avatar NikulinR

refact, mobile chrome supp added

parent 40a6b871
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
<!DOCTYPE html>
<html lang="en">
<head>
<head>
<meta charset="utf-8">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="theme-color" content="#4fc08d">
<title>vue-masked-input</title>
</head>
<body>
</head>
<body>
<div id="app"></div>
<script src="./dist/build.js"></script>
</body>
</body>
</html>
......@@ -40,6 +40,7 @@
</p>
<br />
<h4>Check <a href="https://github.com/niksmr/vue-masked-input">GitHub</a> for more</h4>
<br />
</div>
</template>
......@@ -76,6 +77,7 @@ body {
}
input {
margin: 12px 0;
font-family: inherit;
font-size: inherit;
padding: 8px 16px;
......@@ -107,7 +109,8 @@ h4 {
span {
opacity: 0.5;
margin-left: 32px;
margin-left: 16px;
white-space: nowrap;
}
.code {
......
<template>
<input ref="input" :value="value" @keypress.prevent="keyPress(arguments[0])" @keydown="keyDown(arguments[0])" @mouseup="mouseUp(arguments[0])" @focus.prevent="focusin(arguments[0])" @focusout="focusout(arguments[0])" @cut="cut(arguments[0])" @copy="copy(arguments[0])"
@paste="paste(arguments[0])" />
<input ref="input"
:value="value"
@keydown="keyDown(arguments[0])"
@keypress="keyPress(arguments[0])"
@keyup="keyUp(arguments[0])"
@textInput="textInput(arguments[0])"
@mouseup="mouseUp(arguments[0])"
@focus.prevent="focusin(arguments[0])"
@focusout="focusout(arguments[0])"
@cut="cut(arguments[0])"
@copy="copy(arguments[0])"
@paste="paste(arguments[0])"
:disabled="mask_core===null"
/>
</template>
<script>
import InputMask from 'inputmask-core';
import ffpoly from './ff-polyfill.js'; //Firefox Polyfill for focus events
ffpoly();
import InputMask from 'inputmask-core'
import ffpoly from './ff-polyfill.js' //Firefox Polyfill for focus events
ffpoly()
export default {
name: 'MaskedInput',
data: () => ({
firstFocus: true,
marginLeft: 0,
mask_core: null
mask_core: null,
updateAfterAll: false
}),
props: {
......@@ -33,44 +45,48 @@ export default {
},
watch: {
// whenever question changes, this function will run
mask: function(newMask) {
try {
this.mask_core = new InputMask({
pattern: newMask,
value: this.default
})
} catch (e) {
this.mask_core = new InputMask({
pattern: 'B\\ad1M\\ask',
value: this.default
})
}
this.update();
this.initMask()
}
},
mounted() {
this.initMask()
},
methods: {
initMask() {
try {
this.mask_core = new InputMask({
pattern: this.mask,
value: this.default
})
this.$refs.input.value = this.default
this.$refs.input.value = this.mask_core.setValue = this.default
this.mask_core.setSelection({
start: 0,
end: 0
})
this.$emit('input', this.default)
}
catch (e) {
this.mask_core = null
this.$refs.input.value = '0 editable chars in mask'
this.$emit('input', this.$refs.input.value)
}
},
methods: {
getValue() {
return this.$refs.input.value
if (this.mask_core === null) return '';
return this.mask_core.getValue()
},
keyPress(e) {
this.mask_core.input(e.key)
this.update()
},
keyDown(e) { //Always
if (this.mask_core === null) {
e.preventDefault()
return;
}
keyDown(e) {
this.setNativeSelection()
switch (e.keyCode) {
//backspace
......@@ -80,7 +96,8 @@ export default {
this.mask_core.selection.start > this.marginLeft ||
this.mask_core.selection.start != this.mask_core.selection.end
) {
this.mask_core.backspace();
this.mask_core.backspace()
this.updateToCoreState()
}
break;
......@@ -89,12 +106,13 @@ export default {
e.preventDefault()
if (this.$refs.input.selectionStart === this.$refs.input.selectionEnd)
this.$refs.input.selectionStart--
this.$refs.input.selectionEnd = this.$refs.input.selectionStart--
this.mask_core.selection = {
start: this.$refs.input.selectionStart,
end: this.$refs.input.selectionStart
}
this.updateToCoreState()
break;
//right arrow
......@@ -108,6 +126,7 @@ export default {
start: this.$refs.input.selectionEnd,
end: this.$refs.input.selectionEnd
}
this.updateToCoreState()
break;
//end
......@@ -119,6 +138,7 @@ export default {
start: this.$refs.input.selectionEnd,
end: this.$refs.input.selectionEnd
}
this.updateToCoreState()
break;
//home
......@@ -129,6 +149,7 @@ export default {
start: this.$refs.input.selectionStart,
end: this.$refs.input.selectionStart
}
this.updateToCoreState()
break;
//delete
......@@ -136,34 +157,42 @@ export default {
e.preventDefault()
if (this.$refs.input.selectionStart === this.$refs.input.selectionEnd) {
if (this.$refs.input.selectionEnd !== this.mask_core.length) {
this.mask_core.setSelection({
start: 0,
end: this.mask_core.getValue().length
})
this.mask_core.backspace()
this.mask_core.setValue('');
this.mask_core.setSelection({
start: 0,
end: 0
})
this.$refs.input.selectionStart = this.mask_core.selection.start;
this.$refs.input.selectionEnd = this.mask_core.selection.start;
}
} else {
this.mask_core.backspace()
}
this.updateToCoreState()
break;
}
},
keyPress(e) { //works only on Desktop
},
this.update()
textInput(e) {
e.preventDefault()
if (this.mask_core.input(e.data)) {
this.updateToCoreState()
this.updateAfterAll = true
}
},
keyUp(e) {
if (this.updateAfterAll) this.updateToCoreState()
this.updateAfterAll = false
},
cut(e) {
e.preventDefault();
if (this.$refs.input.selectionStart !== this.$refs.input.selectionEnd) {
let text = this.$refs.input.value.slice(
this.$refs.input.selectionStart,
this.$refs.input.selectionEnd
......@@ -172,20 +201,22 @@ export default {
document.execCommand('copy')
} catch (err) {}
this.mask_core.backspace()
this.update()
this.updateToCoreState()
}
},
copy(e) {},
paste(e) {
e.preventDefault();
e.preventDefault()
this.mask_core.paste(e.clipboardData.getData('text'))
this.update()
this.updateToCoreState()
},
update() {
updateToCoreState() {
if (this.mask_core === null) {
return;
}
this.$refs.input.value = this.mask_core.getValue()
this.$refs.input.selectionStart = this.mask_core.selection.start;
this.$refs.input.selectionEnd = this.mask_core.selection.end;
......@@ -193,12 +224,11 @@ export default {
},
focusin(e) {
this.mask_core.setValue(this.value)
this.update()
},
isEmpty() {
return this.mask_core.emptyValue === this.$refs.input.value
if (this.mask_core === null) return true;
return this.mask_core.getValue() === this.mask_core.emptyValue
},
focusout(e) {
......@@ -208,7 +238,7 @@ export default {
start: 0,
end: 0
})
this.$emit('input', '')
this.$emit('input', this.default)
}
},
......@@ -220,7 +250,7 @@ export default {
},
mouseUp(e) {
if (this.$refs.input.value === this.mask_core.emptyValue &&
if (this.isEmpty() &&
this.$refs.input.selectionStart === this.$refs.input.selectionEnd) {
this.mask_core.setSelection({
start: 0,
......@@ -229,8 +259,9 @@ export default {
this.$refs.input.selectionStart = this.mask_core.selection.start;
this.$refs.input.selectionEnd = this.mask_core.selection.start;
this.marginLeft = this.mask_core.selection.start;
} else {
this.updateToCoreState();
}
else {
this.setNativeSelection();
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment