Commit 9fe4fa3c authored by niksmr's avatar niksmr Committed by GitHub

Merge branch 'master' into feature/mask-as-object

parents 1056f6d3 61211263
node_modules/**/*
dist/**/*
src/ff-polyfill.js
\ No newline at end of file
{
"extends": "airbnb-base",
"env": {
"browser": true
}
}
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
// Copy paste from https://gist.github.com/nuxodin/9250e56a3ce6c0446efa
export default function () {
var w = window,
d = w.document;
if (w.onfocusin === undefined) {
d.addEventListener('focus', addPolyfill, true);
d.addEventListener('blur', addPolyfill, true);
d.addEventListener('focusin', removePolyfill, true);
d.addEventListener('focusout', removePolyfill, true);
}
function addPolyfill(e) {
var type = e.type === 'focus' ? 'focusin' : 'focusout';
var event = new CustomEvent(type, {
bubbles: true,
cancelable: false
});
event.c1Generated = true;
e.target.dispatchEvent(event);
}
function removePolyfill(e) {
if (!e.c1Generated) {
// focus after focusin, so chrome will the first time trigger tow times focusin
d.removeEventListener('focus', addPolyfill, true);
d.removeEventListener('blur', addPolyfill, true);
d.removeEventListener('focusin', removePolyfill, true);
d.removeEventListener('focusout', removePolyfill, true);
}
setTimeout(function () {
d.removeEventListener('focusin', removePolyfill, true);
d.removeEventListener('focusout', removePolyfill, true);
});
}
};
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
import InputMask from 'inputmask-core';
import ffpoly from './ff-polyfill'; // Firefox Polyfill for focus events
ffpoly();
export default {
name: 'MaskedInput',
render: function render(h) {
return h('input', {
ref: 'input',
attrs: {
disabled: this.maskCore === null || this.disabled
},
domProps: {
value: this.value
},
on: {
keydown: this.keyDown,
keypress: this.keyPress,
keyup: this.keyUp,
textInput: this.textInput,
mouseup: this.mouseUp,
focusout: this.focusOut,
cut: this.cut,
copy: this.copy,
paste: this.paste
}
});
},
data: function data() {
return {
marginLeft: 0,
maskCore: null,
updateAfterAll: false
};
},
props: {
value: {
type: String
},
mask: {
type: String,
required: true,
validator: function validator(value) {
return !!(value && value.length >= 1);
}
},
placeholderChar: {
type: String,
default: '_',
validator: function validator(value) {
return !!(value && value.length === 1);
}
},
disabled: {
type: Boolean,
default: false
}
},
watch: {
mask: function mask() {
this.initMask();
},
value: function value(newValue) {
if (this.maskCore) this.maskCore.setValue(newValue); // For multiple inputs support
}
},
mounted: function mounted() {
this.initMask();
},
methods: {
initMask: function initMask() {
var _this = this;
try {
this.maskCore = new InputMask({
pattern: this.mask,
value: '',
placeholderChar: this.placeholderChar,
/* eslint-disable quote-props */
formatCharacters: {
'a': {
validate: function validate(char) {
return (/^[A-Za-zА-Яа-я]$/.test(char)
);
}
},
'A': {
validate: function validate(char) {
return (/^[A-Za-zА-Яа-я]$/.test(char)
);
},
transform: function transform(char) {
return char.toUpperCase();
}
},
'*': {
validate: function validate(char) {
return (/^[\dA-Za-zА-Яа-я]$/.test(char)
);
}
},
'#': {
validate: function validate(char) {
return (/^[\dA-Za-zА-Яа-я]$/.test(char)
);
},
transform: function transform(char) {
return char.toUpperCase();
}
},
'+': {
validate: function validate() {
return true;
}
}
}
});
[].concat(_toConsumableArray(this.$refs.input.value)).reduce(function (memo, item) {
return _this.maskCore.input(item);
}, null);
this.maskCore.setSelection({
start: 0,
end: 0
});
if (this.$refs.input.value === '') {
this.$emit('input', '', '');
} else {
this.updateToCoreState();
}
} catch (e) {
this.maskCore = null;
this.$refs.input.value = 'Error';
this.$emit('input', this.$refs.input.value, '');
}
},
getValue: function getValue() {
return this.maskCore ? this.maskCore.getValue() : '';
},
keyDown: function keyDown(e) {
// Always
if (this.maskCore === null) {
e.preventDefault();
return;
}
this.setNativeSelection();
switch (e.keyCode) {
// backspace
case 8:
e.preventDefault();
if (this.maskCore.selection.start > this.marginLeft || this.maskCore.selection.start !== this.maskCore.selection.end) {
this.maskCore.backspace();
this.updateToCoreState();
}
break;
// left arrow
case 37:
e.preventDefault();
if (this.$refs.input.selectionStart === this.$refs.input.selectionEnd) {
// this.$refs.input.selectionEnd = this.$refs.input.selectionStart - 1; @TODO
this.$refs.input.selectionStart -= 1;
}
this.maskCore.selection = {
start: this.$refs.input.selectionStart,
end: this.$refs.input.selectionStart
};
this.updateToCoreState();
break;
// right arrow
case 39:
e.preventDefault();
if (this.$refs.input.selectionStart === this.$refs.input.selectionEnd) {
this.$refs.input.selectionEnd += 1;
}
this.maskCore.selection = {
start: this.$refs.input.selectionEnd,
end: this.$refs.input.selectionEnd
};
this.updateToCoreState();
break;
// end
case 35:
e.preventDefault();
this.$refs.input.selectionStart = this.$refs.input.value.length;
this.$refs.input.selectionEnd = this.$refs.input.value.length;
this.maskCore.selection = {
start: this.$refs.input.selectionEnd,
end: this.$refs.input.selectionEnd
};
this.updateToCoreState();
break;
// home
case 36:
e.preventDefault();
this.$refs.input.selectionStart = 0;
this.$refs.input.selectionEnd = 0;
this.maskCore.selection = {
start: this.$refs.input.selectionStart,
end: this.$refs.input.selectionStart
};
this.updateToCoreState();
break;
// delete
case 46:
e.preventDefault();
if (this.$refs.input.selectionStart === this.$refs.input.selectionEnd) {
this.maskCore.setValue('');
this.maskCore.setSelection({
start: 0,
end: 0
});
this.$refs.input.selectionStart = this.maskCore.selection.start;
this.$refs.input.selectionEnd = this.maskCore.selection.start;
} else {
this.maskCore.backspace();
}
this.updateToCoreState();
break;
default:
break;
}
},
keyPress: function keyPress(e) {
// works only on Desktop
if (e.ctrlKey) return; // Fix FF copy/paste issue
// IE & FF are not trigger textInput event, so we have to force it
/* eslint-disable */
var isIE = /*@cc_on!@*/false || !!document.documentMode; //by http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
/* eslint-enable */
var isFirefox = typeof InstallTrigger !== 'undefined';
if (isIE || isFirefox) {
e.preventDefault();
e.data = e.key;
this.textInput(e);
}
},
textInput: function textInput(e) {
if (e.preventDefault) e.preventDefault();
if (this.maskCore.input(e.data)) {
this.updateAfterAll = true;
}
this.updateToCoreState();
},
keyUp: function keyUp(e) {
if (e.keyCode === 9) {
// Preven change selection for Tab in
return;
}
this.updateToCoreState();
this.updateAfterAll = false;
},
cut: function cut(e) {
e.preventDefault();
if (this.$refs.input.selectionStart !== this.$refs.input.selectionEnd) {
try {
document.execCommand('copy');
} catch (err) {} // eslint-disable-line no-empty
this.maskCore.backspace();
this.updateToCoreState();
}
},
copy: function copy() {},
paste: function paste(e) {
var _this2 = this;
e.preventDefault();
var text = e.clipboardData.getData('text');
[].concat(_toConsumableArray(text)).reduce(function (memo, item) {
return _this2.maskCore.input(item);
}, null);
this.updateToCoreState();
},
updateToCoreState: function updateToCoreState() {
if (this.maskCore === null) {
return;
}
if (this.$refs.input.value !== this.maskCore.getValue()) {
this.$refs.input.value = this.maskCore.getValue();
this.$emit('input', this.$refs.input.value, this.maskCore.getRawValue());
}
this.$refs.input.selectionStart = this.maskCore.selection.start;
this.$refs.input.selectionEnd = this.maskCore.selection.end;
},
isEmpty: function isEmpty() {
if (this.maskCore === null) return true;
return this.maskCore.getValue() === this.maskCore.emptyValue;
},
focusOut: function focusOut() {
if (this.isEmpty()) {
this.$refs.input.value = '';
this.maskCore.setSelection({
start: 0,
end: 0
});
this.$emit('input', '', '');
}
},
setNativeSelection: function setNativeSelection() {
this.maskCore.selection = {
start: this.$refs.input.selectionStart,
end: this.$refs.input.selectionEnd
};
},
mouseUp: function mouseUp() {
if (this.isEmpty() && this.$refs.input.selectionStart === this.$refs.input.selectionEnd) {
this.maskCore.setSelection({
start: 0,
end: 0
});
this.$refs.input.selectionStart = this.maskCore.selection.start;
this.$refs.input.selectionEnd = this.maskCore.selection.start;
this.marginLeft = this.maskCore.selection.start;
this.updateToCoreState();
} else {
this.setNativeSelection();
}
}
}
};
{
"name": "vue-masked-input",
"description": "Masked input component for Vue.js 2.X",
"version": "0.4.1",
"version": "0.5.1",
"author": "niksmr",
"license": "MIT",
"homepage": "https://github.com/niksmr/vue-masked-input",
"main": "src/MaskedInput.js",
"main": "dist/maskedInput.js",
"scripts": {
"dev": "cross-env NODE_ENV=development webpack-dev-server --open --inline --hot",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules"
"babel": "npm run babel-component & npm run babel-poly",
"babel-component": "babel src/ff-polyfill.js --out-file dist/ff-polyfill.js",
"babel-poly": "babel src/MaskedInput.js --out-file dist/maskedInput.js",
"build": "cross-env NODE_ENV=production webpack --progress --hide-modules & npm run babel"
},
"repository": {
"type": "git",
......@@ -22,17 +25,21 @@
],
"dependencies": {
"inputmask-core": "^2.2.0",
"vue": "^2.1.0"
"vue": "^2.2.6"
},
"devDependencies": {
"babel-cli": "^6.24.1",
"babel-core": "^6.0.0",
"babel-loader": "^6.0.0",
"babel-loader": "^7.0.0",
"babel-polyfill": "^6.22.0",
"babel-preset-es2015": "^6.0.0",
"cross-env": "^3.0.0",
"css-loader": "^0.25.0",
"file-loader": "^0.9.0",
"vue-loader": "^10.0.0",
"cross-env": "^4.0.0",
"css-loader": "^0.28.0",
"eslint": "^3.19.0",
"eslint-config-airbnb-base": "^11.1.3",
"eslint-plugin-import": "^2.2.0",
"file-loader": "^0.11.1",
"vue-loader": "^12.0.0",
"vue-template-compiler": "^2.1.0",
"webpack": "^2.2.0",
"webpack-dev-server": "^2.2.0"
......
This diff is collapsed.
//https://gist.github.com/nuxodin/9250e56a3ce6c0446efa
// Copy paste from https://gist.github.com/nuxodin/9250e56a3ce6c0446efa
export default function() {
var w = window,
var w = window,
d = w.document;
if (w.onfocusin === undefined) {
......@@ -32,5 +32,4 @@ export default function() {
d.removeEventListener('focusout', removePolyfill, true);
});
}
};
import Vue from 'vue'
import App from './App.vue'
import Vue from 'vue';
import App from './App.vue';
// eslint-disable-next-line no-new
new Vue({
el: '#app',
render: h => h(App)
})
render: h => h(App),
});
......@@ -3,8 +3,7 @@ var webpack = require('webpack')
module.exports = {
entry: {
demo: './src/main.js',
component: './src/MaskedInput.js'
demo: './src/main.js'
},
output: {
path: path.resolve(__dirname, './dist'),
......
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