jquerymask.min.js 7.1 KB

1
  1. (function(e,t,n){typeof define=="function"&&define.amd?define(["jquery"],e):typeof exports=="object"&&typeof Meteor=="undefined"?module.exports=e(require("jquery")):e(t||n)})(function(e){"use strict";var t=function(t,n,r){var i={invalid:[],getCaret:function(){try{var e,n=0,r=t.get(0),s=document.selection,o=r.selectionStart;if(s&&navigator.appVersion.indexOf("MSIE 10")===-1)e=s.createRange(),e.moveStart("character",-i.val().length),n=e.text.length;else if(o||o==="0")n=o;return n}catch(u){}},setCaret:function(e){try{if(t.is(":focus")){var n,r=t.get(0);r.setSelectionRange?r.setSelectionRange(e,e):(n=r.createTextRange(),n.collapse(!0),n.moveEnd("character",e),n.moveStart("character",e),n.select())}}catch(i){}},events:function(){t.on("keydown.mask",function(e){t.data("mask-keycode",e.keyCode||e.which),t.data("mask-previus-value",t.val()),t.data("mask-previus-caret-pos",i.getCaret()),i.maskDigitPosMapOld=i.maskDigitPosMap}).on(e.jMaskGlobals.useInput?"input.mask":"keyup.mask",i.behaviour).on("paste.mask drop.mask",function(){setTimeout(function(){t.keydown().keyup()},100)}).on("change.mask",function(){t.data("changed",!0)}).on("blur.mask",function(){o!==i.val()&&!t.data("changed")&&t.trigger("change"),t.data("changed",!1)}).on("blur.mask",function(){o=i.val()}).on("focus.mask",function(t){r.selectOnFocus===!0&&e(t.target).select()}).on("focusout.mask",function(){r.clearIfNotMatch&&!u.test(i.val())&&i.val("")})},getRegexMask:function(){var e=[],t,r,i,o,u,a;for(var f=0;f<n.length;f++)t=s.translation[n.charAt(f)],t?(r=t.pattern.toString().replace(/.{1}$|^.{1}/g,""),i=t.optional,o=t.recursive,o?(e.push(n.charAt(f)),u={digit:n.charAt(f),pattern:r}):e.push(!i&&!o?r:r+"?")):e.push(n.charAt(f).replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&"));return a=e.join(""),u&&(a=a.replace(new RegExp("("+u.digit+"(.*"+u.digit+")?)"),"($1)?").replace(new RegExp(u.digit,"g"),u.pattern)),new RegExp(a)},destroyEvents:function(){t.off(["input","keydown","keyup","paste","drop","blur","focusout",""].join(".mask "))},val:function(e){var n=t.is("input"),r=n?"val":"text",i;return arguments.length>0?(t[r]()!==e&&t[r](e),i=t):i=t[r](),i},calculateCaretPosition:function(e){var n=i.getMasked(),r=i.getCaret();if(e!==n){var s=t.data("mask-previus-caret-pos")||0,o=n.length,u=e.length,a=0,f=0,l=0,c=0,h=0;for(h=r;h<o;h++){if(!i.maskDigitPosMap[h])break;f++}for(h=r-1;h>=0;h--){if(!i.maskDigitPosMap[h])break;a++}for(h=r-1;h>=0;h--)i.maskDigitPosMap[h]&&l++;for(h=s-1;h>=0;h--)i.maskDigitPosMapOld[h]&&c++;if(r>u)r=o*10;else if(s>=r&&s!==u){if(!i.maskDigitPosMapOld[r]){var d=r;r-=c-l,r-=a,i.maskDigitPosMap[r]&&(r=d)}}else r>s&&(r+=l-c,r+=f)}return r},behaviour:function(n){n=n||window.event,i.invalid=[];var r=t.data("mask-keycode");if(e.inArray(r,s.byPassKeys)===-1){var o=i.getMasked(),u=i.getCaret(),a=t.data("mask-previus-value")||"";return setTimeout(function(){i.setCaret(i.calculateCaretPosition(a))},e.jMaskGlobals.keyStrokeCompensation),i.val(o),i.setCaret(u),i.callbacks(n)}},getMasked:function(e,t){var o=[],u=t===undefined?i.val():t+"",a=0,f=n.length,l=0,c=u.length,h=1,d="push",v=-1,m=0,g=[],y,b;r.reverse?(d="unshift",h=-1,y=0,a=f-1,l=c-1,b=function(){return a>-1&&l>-1}):(y=f-1,b=function(){return a<f&&l<c});var w;while(b()){var E=n.charAt(a),S=u.charAt(l),x=s.translation[E];x?(S.match(x.pattern)?(o[d](S),x.recursive&&(v===-1?v=a:a===y&&a!==v&&(a=v-h),y===v&&(a-=h)),a+=h):S===w?(m--,w=undefined):x.optional?(a+=h,l-=h):x.fallback?(o[d](x.fallback),a+=h,l-=h):i.invalid.push({p:l,v:S,e:x.pattern}),l+=h):(e||o[d](E),S===E?(g.push(l),l+=h):(w=E,g.push(l+m),m++),a+=h)}var T=n.charAt(y);f===c+1&&!s.translation[T]&&o.push(T);var N=o.join("");return i.mapMaskdigitPositions(N,g,c),N},mapMaskdigitPositions:function(e,t,n){var s=r.reverse?e.length-n:0;i.maskDigitPosMap={};for(var o=0;o<t.length;o++)i.maskDigitPosMap[t[o]+s]=1},callbacks:function(e){var s=i.val(),u=s!==o,a=[s,e,t,r],f=function(e,t,n){typeof r[e]=="function"&&t&&r[e].apply(this,n)};f("onChange",u===!0,a),f("onKeyPress",u===!0,a),f("onComplete",s.length===n.length,a),f("onInvalid",i.invalid.length>0,[s,e,t,i.invalid,r])}};t=e(t);var s=this,o=i.val(),u;n=typeof n=="function"?n(i.val(),undefined,t,r):n,s.mask=n,s.options=r,s.remove=function(){var e=i.getCaret();return s.options.placeholder&&t.removeAttr("placeholder"),t.data("mask-maxlength")&&t.removeAttr("maxlength"),i.destroyEvents(),i.val(s.getCleanVal()),i.setCaret(e),t},s.getCleanVal=function(){return i.getMasked(!0)},s.getMaskedVal=function(e){return i.getMasked(!1,e)},s.init=function(o){o=o||!1,r=r||{},s.clearIfNotMatch=e.jMaskGlobals.clearIfNotMatch,s.byPassKeys=e.jMaskGlobals.byPassKeys,s.translation=e.extend({},e.jMaskGlobals.translation,r.translation),s=e.extend(!0,{},s,r),u=i.getRegexMask();if(o)i.events(),i.val(i.getMasked());else{r.placeholder&&t.attr("placeholder",r.placeholder),t.data("mask")&&t.attr("autocomplete","off");for(var a=0,f=!0;a<n.length;a++){var l=s.translation[n.charAt(a)];if(l&&l.recursive){f=!1;break}}f&&t.attr("maxlength",n.length).data("mask-maxlength",!0),i.destroyEvents(),i.events();var c=i.getCaret();i.val(i.getMasked()),i.setCaret(c)}},s.init(!t.is("input"))};e.maskWatchers={};var n=function(){var n=e(this),i={},s="data-mask-",o=n.attr("data-mask");n.attr(s+"reverse")&&(i.reverse=!0),n.attr(s+"clearifnotmatch")&&(i.clearIfNotMatch=!0),n.attr(s+"selectonfocus")==="true"&&(i.selectOnFocus=!0);if(r(n,o,i))return n.data("mask",new t(this,o,i))},r=function(t,n,r){r=r||{};var i=e(t).data("mask"),s=JSON.stringify,o=e(t).val()||e(t).text();try{return typeof n=="function"&&(n=n(o)),typeof i!="object"||s(i.options)!==s(r)||i.mask!==n}catch(u){}},i=function(e){var t=document.createElement("div"),n;return e="on"+e,n=e in t,n||(t.setAttribute(e,"return;"),n=typeof t[e]=="function"),t=null,n};e.fn.mask=function(n,i){i=i||{};var s=this.selector,o=e.jMaskGlobals,u=o.watchInterval,a=i.watchInputs||o.watchInputs,f=function(){if(r(this,n,i))return e(this).data("mask",new t(this,n,i))};return e(this).each(f),s&&s!==""&&a&&(clearInterval(e.maskWatchers[s]),e.maskWatchers[s]=setInterval(function(){e(document).find(s).each(f)},u)),this},e.fn.masked=function(e){return this.data("mask").getMaskedVal(e)},e.fn.unmask=function(){return clearInterval(e.maskWatchers[this.selector]),delete e.maskWatchers[this.selector],this.each(function(){var t=e(this).data("mask");t&&t.remove().removeData("mask")})},e.fn.cleanVal=function(){return this.data("mask").getCleanVal()},e.applyDataMask=function(t){t=t||e.jMaskGlobals.maskElements;var r=t instanceof e?t:e(t);r.filter(e.jMaskGlobals.dataMaskAttr).each(n)};var s={maskElements:"input,td,span,div",dataMaskAttr:"*[data-mask]",dataMask:!0,watchInterval:300,watchInputs:!0,keyStrokeCompensation:10,useInput:!/Chrome\/[2-4][0-9]|SamsungBrowser/.test(window.navigator.userAgent)&&i("input"),watchDataMask:!1,byPassKeys:[9,16,17,18,36,37,38,39,40,91],translation:{0:{pattern:/\d/},9:{pattern:/\d/,optional:!0},"#":{pattern:/\d/,recursive:!0},A:{pattern:/[a-zA-Z0-9]/},S:{pattern:/[a-zA-Z]/}}};e.jMaskGlobals=e.jMaskGlobals||{},s=e.jMaskGlobals=e.extend(!0,{},s,e.jMaskGlobals),s.dataMask&&e.applyDataMask(),setInterval(function(){e.jMaskGlobals.watchDataMask&&e.applyDataMask()},s.watchInterval)},window.jQuery,window.Zepto)