-
Notifications
You must be signed in to change notification settings - Fork 0
/
currency-mask.min.js
37 lines (37 loc) · 15.4 KB
/
currency-mask.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function _MaskAPI(){this.version="0.9";this.instances=0;this.objects={};this.GetMaskForInputWithId=function(a){for(var c=null,b=Object.keys(this.objects),e=0;e<b.length;e++){var f=this.objects[b[e]];if("undefined"!=typeof f&&null!=f.target&&f.target.id===a){c=f;break}}return c};this.RefreshAllInputs=function(){for(var a=Object.keys(this.objects),c=a.length,b=0;b<c;b++){var e=this.objects[a[b]];"undefined"!=typeof e&&null!=e.target&&(e.target.value=e.format(e.target.value))}return c};this.RefreshInputWithId=
function(a){a=this.GetMaskForInputWithId(a);null!=a&&null!=a.target&&(a.target.value=a.format(a.target.value))}}MaskAPI=new _MaskAPI;
function Mask(a,c,b){this.mask=a;this.type="string"==typeof c?c:"string";this.error=[];this.errorCodes=[];this.value="";this.logLevel="debug";this.strippedValue="";this.allowPartial=!1;this.numericMaskDisplayFlags={};this.target=null;this.id=MaskAPI.instances++;this.ref="MaskAPI.objects['"+this.id+"']";this.round="undefined"!=typeof b?b:!0;MaskAPI.objects[this.id]=this}
Mask.prototype.attach=function(a){var c=navigator.userAgent;this.msie=c.match(/MSIE/g)||c.match(/Trident/);this.msie9AndAbove=this.msie9=!1;if(this.msie){var b=1;null!=/MSIE ([0-9]{1,}[.0-9]{0,})/.exec(c)&&(b=parseFloat(RegExp.$1));this.msie9=9==b;this.msie9AndAbove=9<=b||c.match(/Trident/)}this.webkit=c.match(/WebKit/g);this.mozilla=c.match(/Mozilla/g);$addEvent(a,"onkeydown","return "+this.ref+".isAllowKeyPress(event, this);",!0);$addEvent(a,"onkeyup","return "+this.ref+".getKeyPress(event, this);",
!0);this.target=a;var e=this;this.msie&&!this.msie9||this.webkit?$(a).blur(function(){$(this).change();e.log("Mask "+e+" for input: #"+this.id+" triggered change event","info");return!0}):this.mozilla&&$addEvent(a,"onblur","this.value = "+this.ref+".format(this.value);"+this.ref+".target.value=this.value;$('#'+this.id).change();return this.value;",!0);c=this.format(a.value);a.value=c;this.log("Mask "+this.mask+" now attached to input: #"+a.id,"debug")};
Mask.prototype.isAllowKeyPress=function(a,c){if("string"!=this.type)return!0;var b=new qEvent(a);return 47<b.keyCode&&c.value.length>=this.mask.length&&!b.ctrlKey?!1:!0};
Mask.prototype.getKeyPress=function(a,c,b){this.allowPartial=!0;a=new qEvent(a);var e=this.getCaretPosition(c),f=c.value.length;if(47<a.keyCode||1==b||8==a.keyCode||46==a.keyCode||110==a.keyCode){var h=c.value,k;k=190==a.keyCode||110==a.keyCode?!0:!1;"number"==this.type?(this.log("Mask for "+c.id+" type:"+this.type+" _u:"+b+" key:"+a.keyCode+" decoded:"+String.fromCharCode(a.keyCode)+" setNumber("+h+","+k+")","debug"),this.value=this.setNumber(h,k)):this.value="date"==this.type?this.setDateKeyPress(h,
k):this.setGeneric(h,k);c.value=this.value;f=c.value.length>f?1<f?e+1:c.value.length:e;this.setCaretPosition(c,f);this.log("Mask for "+c.id+" type:"+this.type+" _u:"+b+" key:"+a.keyCode+" decoded:"+String.fromCharCode(a.keyCode)+" new value:"+c.value+" oc"+e+" nc:"+f,"debug")}else this.log("Mask for "+c.id+" _u:"+b+" key:"+a.keyCode+" decoded:"+String.fromCharCode(a.keyCode)+" ignore","debug");this.allowPartial=!1;return!0};
Mask.prototype.getCaretPosition=function(a){var c=0,b=null,e=null;if(this.msie){if("undefined"!=typeof document.selection)b=document.selection,e=b.createRange();else if(b=window.getSelection(),null!=b&&(e=0<b.rangeCount?b.getRangeAt(0):null,null==e))return 0;c=e.text.length;e.moveStart("character",-a.value.length);c=e.text.length-c;caretEnd=c+e.text.length}else if(a.selectionStart||"0"==a.selectionStart)c=a.selectionStart;return c};
Mask.prototype.setCaretPosition=function(a,c){var b=null,e=null;if(this.msie)"undefined"!=typeof document.selection?(b=document.selection,e=b.createRange(),e.moveStart("character",-a.value.length),e.collapse(!0),e.moveEnd("character",c),e.moveStart("character",c),e.select()):(b=window.getSelection(),null!=b&&(e=0<b.rangeCount?b.getRangeAt(0):null,null!=e&&(b.removeAllRanges(),b.addRange(c))));else if(a.selectionStart||"0"==a.selectionStart)a.selectionStart=c,a.selectionEnd=c};
Mask.prototype.format=function(a,c){return this.value="number"==this.type?this.setNumber(a):"date"==this.type?this.setDate(a):this.setGeneric(a)};Mask.prototype.throwError=function(a,c,b){this.error[this.error.length]=c;this.errorCodes[this.errorCodes.length]=a;return"string"==typeof b?b:!0};
Mask.prototype.setGeneric=function(a,c){var b,e=this.mask,f=[],h="",k,n,l=[],p=0,q={x:"A-Za-z","#":"0-9","*":"A-Za-z0-9"};b=a.replace(new RegExp("[^"+q["*"]+"]","gi"),"");1==c&&b.length==this.strippedValue.length&&(b=b.substring(0,b.length-1));this.strippedValue=b;for(var m=0;m<e.length;m++){n=e.charAt(m);k=-1<"x#*".indexOf(n);"!"==n&&(n=e.charAt(m++));if(k&&!this.allowPartial||k&&this.allowPartial&&f.length<b.length)f[f.length]="["+q[n]+"]";l[l.length]={chr:n,mask:k}}e=!1;if(!this.allowPartial&&
!(new RegExp(f.join(""))).test(b))return this.throwError(1,'The value "'+a+'" must be in the format '+this.mask+".",a);if(this.allowPartial&&0<b.length||!this.allowPartial)for(m=0;m<l.length;m++){if(l[m].mask){for(;0<b.length&&!(new RegExp(f[p])).test(b.charAt(p));)b=1==b.length?"":b.substring(1);0<b.length&&(h+=b.charAt(p),e=!0);p++}else h+=l[m].chr;if(this.allowPartial&&p>b.length)break}this.allowPartial&&!e&&(h="");this.allowPartial&&(this.nextValidChar=h.length<l.length?q[l[h.length].chr]:null);
return h};Mask.prototype.normalizeNumberDecimal=function(a){return a=a.replace(/\./,"d").replace(/\./g,"").replace(/d/,".")};Mask.prototype.normalizeNumberSign=function(a){a=String(a).replace(/[^\d.-]*/gi,"");if(("+"==a||"-"==a)&&-1==this.mask.indexOf(a))return"";0<this.value.length&&(-1!=a.indexOf("+")||-1!=a.indexOf("-"))&&(a=a.replace(/\+/g,""),a=a.replace(/\-/g,""));a=a.replace(/\+/,"d").replace(/\+/g,"").replace(/d/,"+");return a=a.replace(/\-/,"d").replace(/\-/g,"").replace(/d/,"-")};
Mask.prototype.normalizeNumberValue=function(a,c,b){if(!/^[\$]?((\$?[\+-]?([0#]{1,3},)?[0#]*(\.[0#]*)?)|([\+-]?\([\+-]?([0#]{1,3},)?[0#]*(\.[0#]*)?\)))$/.test(this.mask))return this.throwError(1,"An invalid mask was specified for the \nMask constructor.",a);1==c&&b.length==this.strippedValue.length&&(b=b.substring(0,b.length-1));if(this.allowPartial&&0==b.replace(/[^0-9]/,"").length)return b;this.strippedValue=b;c=0==b.length?NaN:Number(b);if(isNaN(c))return this.throwError(2,"The value entered was not a number.",
a);a=0!=c&&-1*Math.abs(c)==c;if(0==this.mask.length)return b;c=this.mask;this.numericMaskDisplayFlags.$=/^[\$]/.test(c);this.numericMaskDisplayFlags["("]=a&&-1<c.indexOf("(");this.numericMaskDisplayFlags["+"]=-1!=c.indexOf("+")&&!a;this.numericMaskDisplayFlags["-"]=a&&(!this.numericMaskDisplayFlags["("]||-1!=c.indexOf("-"));return b};
Mask.prototype.padNumericDecimalPart=function(a){var c=this.mask.replace(/[^#0.,]*/gi,""),b=-1<a.indexOf("."),e=b?a.split(".")[1]:"";a=e;c=b?c.split(".")[1]:"";if(0==c.length)e="";else{var f=c.lastIndexOf("0")+1;if(e.length>c.length)this.round?(e="1"+e.substring(0,c.length+1),e=Math.round(Number(e)/10),e=String(e).substring(1)):e=e.substring(0,c.length);else for(;e.length<f;)e+="0"}f="";f=0<e.length&&!this.allowPartial||0<c.length&&this.allowPartial&&b&&a.length>=e.length?e:0<c.length&&this.allowPartial&&
b&&a.length<e.length?a:"";this.log("vd="+e+" l="+e.length+" _vd="+a+" l="+a.length+" aP="+this.allowPartial+" hD="+b+" vd2="+f,"debug");return f};Mask.prototype.padNumericIntegerPart=function(a){var c=this.mask.replace(/[^#0.,]*/gi,""),b=-1<a.indexOf(".");a=b?a.split(".")[0]:a;a=Math.abs(a);b||(a=Math.round(a));a=String(a);c=-1<c.indexOf(".")?c.split(".")[0]:c;c=c.replace(/[^0#]+/gi,"");b=c.indexOf("0")+1;if(0<b)for(b=c.length-b+1;a.length<b;)a="0"+a;return a};
Mask.prototype.applyNumericDisplayFlags=function(a){this.numericMaskDisplayFlags.$&&(a=this.mask.replace(/(^[\$])(.+)/gi,"$")+a);this.numericMaskDisplayFlags["+"]&&(a="+"+a);this.numericMaskDisplayFlags["-"]&&(a="-"+a);this.numericMaskDisplayFlags["("]&&(a="("+a+")");return a};
Mask.prototype.applyNumericThousandsSeparators=function(a){var c=this.mask.replace(/[^#0.,]*/gi,"");if(!/[#0]+,[#0]{3}/.test(c))return a;try{for(var c=[],b=0,e=Number(a);999<e;)c[b]="00"+String(e%1E3),c[b]=c[b].substring(c[b].length-3),e=Math.floor(e/1E3),b++;c[b]=String(e%1E3);a=c.reverse().join(",")}catch(f){this.log("applyThousandsSeparators error"+f)}return a};
Mask.prototype.setNumber=function(a,c){var b=this.normalizeNumberSign(a);if(0==b.length)return b;b=this.normalizeNumberDecimal(b);b=this.normalizeNumberValue(a,c,b);if(0==b.length&&!c)return b;this.strippedValue=b;var e=this.padNumericIntegerPart(b),b=this.padNumericDecimalPart(b);this.strippedValue=e;0<b.length&&(this.strippedValue+="."+b);e=this.applyNumericThousandsSeparators(e);b=0<b.length||c?e+"."+b:e;return b=this.applyNumericDisplayFlags(b)};
Mask.prototype.setDate=function(a){var c=this.mask,b,e,f,h,k,n;b=c.split(/[^mdy]+/);n=c.split(/[mdy]+/);e=a.split(/[^0-9]/);0==n[0].length&&n.splice(0,1);for(var l=0;l<b.length;l++)c=b[l].charAt(0).toLowerCase(),"m"==c?f=parseInt(e[l],10)-1:"d"==c?h=parseInt(e[l],10):"y"==c&&(k=parseInt(e[l],10));3>String(k).length&&(k=2E3+k,(new Date).getFullYear()+5<k&&(k-=100));k=new Date(k,f,h);if(k.getDate()!=h)return this.throwError(1,"An invalid day was entered.",a);if(k.getMonth()!=f)return this.throwError(2,
"An invalid month was entered.",a);a="";for(l=0;l<b.length;l++)c=b[l].charAt(0).toLowerCase(),"m"==c?(f++,2==b[l].length&&(f="0"+f,f=f.substring(f.length-2)),a+=f):"d"==c?(2==b[l].length&&(h="0"+h,h=h.substring(h.length-2)),a+=h):"y"==c&&(a=2==b[l].length?a+k.getYear():a+k.getFullYear()),l<b.length-1&&(a+=n[l]);return a};
Mask.prototype.setDateKeyPress=function(a,c){var b=a,e=this.mask,f=b.charAt(b.length-1),h,k,n,l,p="",q="",m="",t,g,u;if(1==c){for(;/[^0-9]/gi.test(b.charAt(b.length-1));)b=b.substring(0,b.length-1);/[^0-9]/gi.test(this.strippedValue.charAt(this.strippedValue.length-1))&&(b=b.substring(0,b.length-1));if(0==b.length)return""}h=e.split(/[^mdy]/);s=e.split(/[mdy]+/);for(g=0;g<s.length;g++)0==s[g].length&&(s.splice(g,1),--g);b=b.split(/[^0-9]/);for(g=0;g<b.length;g++)0==b[g].length&&(b.splice(g,1),--g);
g=0<b.length?b.length-1:0;k=h[g].charAt(0);n=h[g].length;for(g=0;g<b.length;g++)t=h[g].charAt(0).toLowerCase(),"m"==t?p=parseInt(b[g],10)-1:"d"==t?q=parseInt(b[g],10):"y"==t&&(m=parseInt(b[g],10));var r="";for(g=0;g<b.length;g++){t=h[g].charAt(0).toLowerCase();if("m"==t){u=/[^0-9]/.test(f)&&"m"==k;p++;if(2==b[g].length&&10>p||2==h[g].length&&"m"!=k||1<p&&"m"==k||u&&2==h[g].length)p="0"+p,p=p.substring(p.length-2);l=String(p).length;n=2;r+=p}else if("d"==t){u=/[^0-9]/.test(f)&&"d"==k;if(2==b[g].length&&
10>q||2==h[g].length&&"d"!=k||3<q&&"d"==k||u&&2==h[g].length)q="0"+q,q=q.substring(q.length-2);l=String(q).length;n=2;r+=q}else if("y"==t){u=/[^0-9]/.test(f)&&"y"==k;m="y"==k?String(m):2==h[g].length?d.getYear():d.getFullYear();if(2==b[g].length&&10>m||2==h[g].length&&"y"!=k||u&&2==h[g].length)m="0"+m,m=m.substring(m.length-2);n=h[g].length;l=String(m).length;r+=m}if((n==l||u)&&t==k&&g<s.length||g<s.length&&t!=k)r+=s[g]}r.length>e.length&&(r=r.substring(0,e.length));return this.strippedValue="NaN"==
r?"":r};Mask.prototype.updateFormattedValue=function(a){a=this.format(a);return this.target.value=a};
Mask.prototype.log=function(a,c){if(this.webkit){switch(c){case "debug":if("debug"===this.logLevel)try{console.debug(a)}catch(b){}break;case "error":try{console.error(a)}catch(e){}break;case "info":if("debug"===this.logLevel||"info"===this.logLevel)try{console.info(a)}catch(f){}break;default:try{console.debug(a)}catch(h){}}return a}if(this.msie&&(c===this.logLevel||"debug"===this.logLevel||"info"===this.logLevel&&("info"===c||"error"===c)))try{console.log(a)}catch(k){}return a};
function qEvent(a){if(navigator.userAgent.match(/MSIE/g))a=window.event,this.keyCode=parseInt(a.keyCode,10),this.button=a.button,this.srcElement=a.srcElement,this.type=a.type,document.all?(this.x=a.clientX+document.body.scrollLeft,this.y=a.clientY+document.body.scrollTop):(this.x=a.clientX,this.y=a.clientY),this.screenX=a.screenX,this.screenY=a.screenY,this.altKey=a.altKey,this.ctrlKey=a.ctrlKey,this.shiftKey=a.shiftKey;else{var c="key"==a.type.substring(0,3);this.keyCode=c?parseInt(a.which,10):0;
this.button=c?0:parseInt(a.which,10);this.srcElement=a.target;this.type=a.type;this.x=a.pageX;this.y=a.pageY;this.screenX=a.screenX;this.screenY=a.screenY;document.layers?(this.altKey=0<(a.modifiers&Event.ALT_MASK),this.ctrlKey=0<(a.modifiers&Event.CONTROL_MASK),this.shiftKey=0<(a.modifiers&Event.SHIFT_MASK),this.keyCode=this.translateKeyCode(this.keyCode)):(this.altKey=a.altKey,this.ctrlKey=a.ctrlKey,this.shiftKey=a.shiftKey)}0==this.button&&(this.setKeyPressed(this.keyCode),this.keyChar=String.fromCharCode(this.keyCode))}
qEvent.prototype.translateKeyCode=function(a){var c={};if(document.layers){if(96<this.keyCode&&123>this.keyCode)return this.keyCode-32;c={96:192,126:192,33:49,64:50,35:51,36:52,37:53,94:54,38:55,42:56,40:57,41:48,92:220,124:220,125:221,93:221,91:219,123:219,39:222,34:222,47:191,63:191,46:190,62:190,44:188,60:188,45:189,95:189,43:187,61:187,59:186,58:186,"null":null}}return c[a]?c[a]:a};
qEvent.prototype.setKP=function(a,c){this.keyPressedCode=a;this.keyPressed=(this.keyNonChar="string"==typeof c)?c:String.fromCharCode(a);this.isNumeric=parseInt(this.keyPressed,10)==this.keyPressed;this.isAlpha=64<this.keyCode&&91>this.keyCode&&!this.altKey&&!this.ctrlKey;return!0};
qEvent.prototype.setKeyPressed=function(a){var c=this.shiftKey;if(!c&&64<a&&91>a)return this.setKP(a+32);if(95<a&&106>a)return this.setKP(a-48);switch(a){case 49:case 51:case 52:case 53:c&&(a-=16);break;case 50:c&&(a=64);break;case 54:c&&(a=94);break;case 55:c&&(a=38);break;case 56:c&&(a=42);break;case 57:c&&(a=40);break;case 48:c&&(a=41);break;case 192:a=c?126:96;break;case 189:a=c?95:45;break;case 187:a=c?43:61;break;case 220:a=c?124:92;break;case 221:a=c?125:93;break;case 219:a=c?123:91;break;
case 222:a=c?34:39;break;case 186:a=c?58:59;break;case 191:a=c?63:47;break;case 190:a=c?62:46;break;case 188:a=c?60:44;break;case 106:case 57379:a=42;break;case 107:case 57380:a=43;break;case 109:case 57381:a=45;break;case 110:a=46;break;case 111:case 57378:a=47;break;case 8:return this.setKP(a,"[backspace]");case 9:return this.setKP(a,"[tab]");case 13:return this.setKP(a,"[enter]");case 16:case 57389:return this.setKP(a,"[shift]");case 17:case 57390:return this.setKP(a,"[ctrl]");case 18:case 57388:return this.setKP(a,
"[alt]");case 19:case 57402:return this.setKP(a,"[break]");case 20:return this.setKP(a,"[capslock]");case 32:return this.setKP(a,"[space]");case 91:return this.setKP(a,"[windows]");case 93:return this.setKP(a,"[properties]");case 33:case 57371:return this.setKP(-1*a,"[pgup]");case 34:case 57372:return this.setKP(-1*a,"[pgdown]");case 35:case 57370:return this.setKP(-1*a,"[end]");case 36:case 57369:return this.setKP(-1*a,"[home]");case 37:case 57375:return this.setKP(-1*a,"[left]");case 38:case 57373:return this.setKP(-1*
a,"[up]");case 39:case 57376:return this.setKP(-1*a,"[right]");case 40:case 57374:return this.setKP(-1*a,"[down]");case 45:case 57382:return this.setKP(-1*a,"[insert]");case 46:case 57383:return this.setKP(-1*a,"[delete]");case 144:case 57400:return this.setKP(-1*a,"[numlock]")}return 111<a&&124>a?this.setKP(-1*a,"[f"+(a-111)+"]"):this.setKP(a)};
function $addEvent(a,c,b,e){c=c.toLowerCase();e="boolean"==typeof e?e:!0;var f=a[c]?a[c].toString():"",f=f.substring(f.indexOf("{")+1,f.lastIndexOf("}")),f=(e?f+b:b+f)+"\n";return a[c]=window.Event?new Function("event",f):new Function(f)}Object.keys||(Object.keys=function(a){if(a!==Object(a))throw new TypeError("Object.keys called on non-object");var c=[],b;for(b in a)Object.prototype.hasOwnProperty.call(a,b)&&c.push(b);return c});