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-11-07 15:46:51 +03:00

7 lines
4.4 KiB
JavaScript

/*!
* maska v1.4.7
* (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";var e={"#":{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 n(t,n,r=e,i=!0){return a(n).length>1?function(t){const e=a(t).sort(((t,e)=>t.length-e.length));return function(t,a,r,i=!0){const o=e.map((e=>s(t,e,r,!1))).pop();for(let a in e)if(n(o,e[a],r))return s(t,e[a],r,i);return""};function n(t,e,n){for(let t in n)n[t].escape&&(e=e.replace(new RegExp(t+".{1}","g"),""));return e.split("").filter((t=>n[t]&&n[t].pattern)).length>=t.length}}(n)(t,n,r,i):s(t,n,r,i)}function a(t){try{return JSON.parse(t)}catch{return[t]}}function s(t,e,n,a=!0){let s=0,i=0,o="",u="";for(;s<e.length&&i<t.length;){let u=e[s];const l=t[i],p=n[u];if(p&&p.pattern)p.pattern.test(l)&&(o+=r(l,p),s++,a&&e[s]&&(n[e[s]]?n[e[s]]&&n[e[s]].escape&&(o+=e[s+1],s+=2):(o+=e[s],s++))),i++;else if(p&&p.repeat){const t=n[e[s-1]];t&&!t.pattern.test(l)?s++:s--}else p&&p.escape&&(s++,u=e[s]),a&&(o+=u),l===u&&i++,s++}for(;a&&s<e.length;){const t=e[s];if(n[t]){u="";break}u+=t,s++}return o+u}function r(t,e){return e.transform&&(t=e.transform(t)),e.uppercase?t.toLocaleUpperCase():e.lowercase?t.toLocaleLowerCase():t}function i(t){return t instanceof HTMLInputElement?t:t.querySelector("input")||t}function o(t){return"[object String]"===Object.prototype.toString.call(t)}class u{constructor(t,n={}){if(!t)throw new Error("Maska: no element for mask");if(n.tokens)for(const t in n.tokens)n.tokens[t]={...n.tokens[t]},n.tokens[t].pattern&&o(n.tokens[t].pattern)&&(n.tokens[t].pattern=new RegExp(n.tokens[t].pattern));this._opts={mask:n.mask,tokens:{...e,...n.tokens}},this._el=o(t)?document.querySelectorAll(t):t.length?t:[t],this.inputEvent=t=>this.updateValue(t.target,t),this.init()}init(){for(let t=0;t<this._el.length;t++){const e=i(this._el[t]);!this._opts.mask||e.dataset.mask&&e.dataset.mask===this._opts.mask||(e.dataset.mask=this._opts.mask),setTimeout((()=>this.updateValue(e)),0),e.dataset.maskInited||(e.dataset.maskInited=!0,e.addEventListener("input",this.inputEvent),e.addEventListener("beforeinput",this.beforeInput))}}destroy(){for(let t=0;t<this._el.length;t++){const e=i(this._el[t]);e.removeEventListener("input",this.inputEvent),e.removeEventListener("beforeinput",this.beforeInput),delete e.dataset.mask,delete e.dataset.maskInited}}updateValue(t,e){if(!t||!t.type)return;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,e);let s=t.selectionEnd;const r=t.value,i=r[s-1];t.dataset.maskRawValue=n(t.value,t.dataset.mask,this._opts.tokens,!1),t.value=n(t.value,t.dataset.mask,this._opts.tokens),e&&"insertText"===e.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,i),this.dispatch("maska",t,e),t.value!==r&&this.dispatch("input",t,e)}beforeInput(t){t&&t.target&&t.target.type&&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))}}var l=(()=>{const t=new WeakMap;return(e,n)=>{n.value&&(t.has(e)&&!function(t){return!(o(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)}(n)||t.set(e,new u(e,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}(n.value))))}})();function p(t){t.directive("maska",l)}"undefined"!=typeof window&&window.Vue&&window.Vue.use&&window.Vue.use(p),t.create=function(t,e){return new u(t,e)},t.default=p,t.install=p,t.mask=n,t.maska=l,t.tokens=e,Object.defineProperty(t,"__esModule",{value:!0})}));