2
0
mirror of https://github.com/tenrok/maska.git synced 2026-06-20 20:00:34 +03:00
Files
maska/dist/es6/maska.js
T
2021-02-06 20:10:51 +03:00

7 lines
4.1 KiB
JavaScript

/*!
* maska v1.4.0
* (c) 2019-2021 Alexander Shabunevich
* Released under the MIT License.
*/
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Maska={})}(this,(function(t){"use strict";function e(t,e,s,r=!0){return n(e).length>1?function(t){const e=n(t).sort(((t,e)=>t.length-e.length));return function(t,n,s,r=!0){let o=0;for(;o<e.length;){const n=e[o];o++;const i=e[o];if(!(i&&a(t,i,s,!0).length>n.length))return a(t,n,s,r)}return""}}(e)(t,e,s,r):a(t,e,s,r)}function n(t){try{return JSON.parse(t)}catch{return[t]}}function a(t,e,n,a=!0){let r=0,o=0,i="",u="";for(;r<e.length&&o<t.length;){let u=e[r];const l=t[o],c=n[u];if(c&&c.pattern)c.pattern.test(l)&&(i+=s(l,c),r++,a&&e[r]&&!n[e[r]]&&(i+=e[r],r++)),o++;else if(c&&c.repeat){const t=n[e[r-1]];t&&!t.pattern.test(l)?r++:r--}else c&&c.escape&&(r++,u=e[r]),a&&(i+=u),l===u&&o++,r++}for(;a&&r<e.length;){const t=e[r];if(n[t]){u="";break}u+=t,r++}return i+u}function s(t,e){return e.transform&&(t=e.transform(t)),e.uppercase?t.toLocaleUpperCase():e.lowercase?t.toLocaleLowerCase():t}var r={"#":{pattern:/[0-9]/},X:{pattern:/[0-9a-zA-Z]/},S:{pattern:/[a-zA-Z]/},A:{pattern:/[a-zA-Z]/,uppercase:!0},a:{pattern:/[a-zA-Z]/,lowercase:!0},"!":{escape:!0},"*":{repeat:!0}};function o(t){return t instanceof HTMLInputElement?t:t.querySelector("input")||t}function i(t){return"[object String]"===Object.prototype.toString.call(t)}class u{constructor(t,e={}){if(!t)throw new Error("Maska: no element for mask");if(e.tokens)for(const t in e.tokens)e.tokens[t]={...e.tokens[t]},e.tokens[t].pattern&&i(e.tokens[t].pattern)&&(e.tokens[t].pattern=new RegExp(e.tokens[t].pattern));this._opts={mask:e.mask,tokens:{...r,...e.tokens}},this._el=i(t)?document.querySelectorAll(t):t.length?t:[t],this.init()}init(){for(let t=0;t<this._el.length;t++){const e=o(this._el[t]);!this._opts.mask||e.dataset.mask&&e.dataset.mask===this._opts.mask||(e.dataset.mask=this._opts.mask),this.updateValue(e),e.dataset.maskInited||(e.dataset.maskInited=!0,e.addEventListener("input",(t=>this.updateValue(t.target,t))),e.addEventListener("beforeinput",(t=>this.beforeInput(t))))}}destroy(){for(let t=0;t<this._el.length;t++){const e=o(this._el[t]);e.removeEventListener("input",(t=>this.updateValue(t.target,t))),e.removeEventListener("beforeinput",(t=>this.beforeInput(t))),delete e.dataset.mask,delete e.dataset.maskInited}}updateValue(t,n){const a=t.type.match(/^number$/i)&&t.validity.badInput;if(!t.value&&!a||!t.dataset.mask)return t.dataset.maskRawValue="",void this.dispatch("maska",t,n);let s=t.selectionEnd;const r=t.value,o=r[s-1];t.dataset.maskRawValue=e(t.value,t.dataset.mask,this._opts.tokens,!1),t.value=e(t.value,t.dataset.mask,this._opts.tokens),n&&"insertText"===n.inputType&&s===r.length&&(s=t.value.length),function(t,e,n){for(;e&&e<t.value.length&&t.value.charAt(e-1)!==n;)e++;(t.type?t.type.match(/^(text|search|password|tel|url)$/i):!t.type)&&t===document.activeElement&&(t.setSelectionRange(e,e),setTimeout((function(){t.setSelectionRange(e,e)}),0))}(t,s,o),this.dispatch("maska",t,n),t.value!==r&&this.dispatch("input",t,n)}beforeInput(t){t.target.type.match(/^number$/i)&&t.data&&isNaN(t.target.value+t.data)&&t.preventDefault()}dispatch(t,e,n){e.dispatchEvent(function(t,e=null){const n=document.createEvent("Event");return n.initEvent(t,!0,!0),e&&(n.inputType=e),n}(t,n&&n.inputType||null))}}function l(t,e){if(e.value)return e.value&&function(t){return!(i(t.value)&&t.value===t.oldValue||Array.isArray(t.value)&&JSON.stringify(t.value)===JSON.stringify(t.oldValue)||t.value&&t.value.mask&&t.oldValue&&t.oldValue.mask&&t.value.mask===t.oldValue.mask)}(e)?new u(t,function(t){const e={};return t.mask?(e.mask=Array.isArray(t.mask)?JSON.stringify(t.mask):t.mask,e.tokens=t.tokens?{...t.tokens}:{}):e.mask=Array.isArray(t)?JSON.stringify(t):t,e}(e.value)):void 0}function c(t){t.directive("maska",l)}"undefined"!=typeof window&&window.Vue&&window.Vue.use&&window.Vue.use(c),t.create=function(t,e){return new u(t,e)},t.default=c,t.install=c,t.mask=e,t.maska=l,t.tokens=r,Object.defineProperty(t,"__esModule",{value:!0})}));