gumga-mask-ng
Version:
Gumga Mask (Angular 1.x)
1 lines • 5.46 kB
JavaScript
!function(e){function n(r){if(t[r])return t[r].exports;var i=t[r]={i:r,l:!1,exports:{}};return e[r].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var t={};n.m=e,n.c=t,n.i=function(e){return e},n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{configurable:!1,enumerable:!0,get:r})},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,n){return Object.prototype.hasOwnProperty.call(e,n)},n.p="",n(n.s=0)}([function(e,n,t){"use strict";function r(e){function n(e){return e===document.activeElement&&(!document.hasFocus||document.hasFocus())&&!!(e.type||e.href||~e.tabIndex)}return{priority:100,require:"ngModel",restrict:"A",scope:!1,compile:function(){var t={maskDefinitions:{9:/\d/,A:/[a-zA-Z]/,"*":/[a-zA-Z0-9]/},clearOnBlur:!0,eventsToHandle:["input","keyup","click","focus"]};return function(r,i,u,a){function o(e){return angular.isDefined(e)?(k(e),K?(d(),g(),!0):f()):f()}function l(e){e&&(j=e,K&&i.val(v(m(i.val()))))}function c(e){return K?(q=m(e||""),z=p(q),a.$setValidity("mask",z),z&&q.length?v(q):void 0):e}function s(e){return K?(q=m(e||""),z=p(q),a.$viewValue=q.length?v(q):"",a.$setValidity("mask",z),""===q&&u.required&&a.$setValidity("required",!a.$error.required),z?q:void 0):e}function f(){return K=!1,h(),angular.isDefined(C)?i.attr("placeholder",C):i.removeAttr("placeholder"),angular.isDefined(G)?i.attr("maxlength",G):i.removeAttr("maxlength"),i.val(a.$modelValue),a.$viewValue=a.$modelValue,!1}function d(){q=Z=m(a.$modelValue||""),T=H=v(q),z=p(q);var e=z&&q.length?T:"";u.maxlength&&i.attr("maxlength",2*D[D.length-1]),C||i.attr("placeholder",j),i.val(e),a.$viewValue=e,a.$setValidity("mask",z)}function g(){W||(i.bind("blur",y),i.bind("mousedown mouseup",x),i.bind(J.eventsToHandle.join(" "),O),i.bind("paste",w),W=!0)}function h(){W&&(i.unbind("blur",y),i.unbind("mousedown",x),i.unbind("mouseup",x),i.unbind("input",O),i.unbind("keyup",O),i.unbind("click",O),i.unbind("focus",O),i.unbind("paste",w),W=!1)}function p(e){return!e.length||e.length>=R-I}function m(e){var n="",t=P.slice();return e=e.toString(),angular.forEach(E,function(n){e=e.replace(n,"")}),angular.forEach(e.split(""),function(e){t.length&&t[0].test(e)&&(n+=e,t.shift())}),n}function v(e){var n="",t=D.slice();return angular.forEach(j.split(""),function(r,i){e.length&&i===t[0]?(n+=e.charAt(0)||"_",e=e.substr(1),t.shift()):n+=r}),n}function b(e){var n=angular.isDefined(u.gumgaMaskPlaceholder)?u.gumgaMaskPlaceholder:u.placeholder;return void 0!==n&&n[e]?n[e]:"_"}function $(){return j.replace(/[_]+/g,"_").replace(/([^_]+)([a-zA-Z0-9])([^_])/g,"$1$2_$3").split("_")}function k(e){var n=0;if(D=[],P=[],j="","string"==typeof e){R=0,I=0;var t=e.split("");angular.forEach(t,function(e,t){J.maskDefinitions[e]?(D.push(n),j+=b(t-I),P.push(J.maskDefinitions[e]),n++,R++):"?"===e?I++:(j+=e,n++)})}D.push(D.slice().pop()+1),E=$(),K=D.length>1}function y(){J.clearOnBlur&&(B=0,F=0,z&&0!==q.length||(T="",i.val(""),r.$apply(function(){a.$setViewValue("")})))}function x(e){"mousedown"===e.type?i.bind("mouseout",V):i.unbind("mouseout",V)}function V(){F=A(this),i.unbind("mouseout",V)}function w(){S(this,i.val().length)}function O(e){e=e||{};var n=e.which,t=e.type;if(16!==n&&91!==n){var r,u=i.val(),o=H,l=m(u),c=Z,s=_(this)||0,f=B||0,d=s-f,g=D[0],h=D[l.length]||D.slice().shift(),p=F||0,b=A(this)>0,$=p>0,k=u.length>o.length||p&&u.length>o.length-p,y=u.length<o.length||p&&u.length===o.length-p,x=n>=37&&n<=40&&e.shiftKey,V=37===n,w=8===n||"keyup"!==t&&y&&-1===d,O=46===n||"keyup"!==t&&y&&0===d&&!$,P=(V||w||"click"===t)&&s>g;if(F=A(this),!x&&(!b||"click"!==t&&"keyup"!==t)){if("input"===t&&y&&!$&&l===c){for(;w&&s>g&&!M(s);)s--;for(;O&&s<h&&-1===D.indexOf(s);)s++;var j=D.indexOf(s);l=l.substring(0,j)+l.substring(j+1)}for(r=v(l),H=r,Z=l,i.val(r),a.$setViewValue(l),k&&s<=g&&(s=g+1),P&&s--,s=s>h?h:s<g?g:s;!M(s)&&s>g&&s<h;)s+=P?-1:1;(P&&s<h||k&&!M(f))&&s++,B=s,S(this,s)}}}function M(e){return D.indexOf(e)>-1}function _(e){if(!e)return 0;if(void 0!==e.selectionStart)return e.selectionStart;if(document.selection&&n(i[0])){e.focus();var t=document.selection.createRange();return t.moveStart("character",e.value?-e.value.length:0),t.text.length}return 0}function S(e,t){if(!e)return 0;if(0!==e.offsetWidth&&0!==e.offsetHeight)if(e.setSelectionRange)n(i[0])&&(e.focus(),e.setSelectionRange(t,t));else if(e.createTextRange){var r=e.createTextRange();r.collapse(!0),r.moveEnd("character",t),r.moveStart("character",t),r.select()}}function A(e){return e?void 0!==e.selectionStart?e.selectionEnd-e.selectionStart:document.selection?document.selection.createRange().text.length:0:0}var D,P,j,E,R,q,T,z,H,Z,B,F,I,K=!1,W=!1,C=u.placeholder,G=u.maxlength,J=t;u.gumgaMaskOptions?(J=r.$eval(u.gumgaMaskOptions),angular.isObject(J)&&(J=function(e,n){for(var t in e)Object.prototype.hasOwnProperty.call(e,t)&&(void 0===n[t]?n[t]=angular.copy(e[t]):angular.extend(n[t],e[t]));return n}(t,J))):J=t,u.$observe("gumgaMask",o),angular.isDefined(u.gumgaMaskPlaceholder)?u.$observe("gumgaMaskPlaceholder",l):u.$observe("placeholder",l);var L=!1;u.$observe("modelViewValue",function(e){"true"===e&&(L=!0)}),r.$watch(u.ngModel,function(n){if(L&&n){e(u.ngModel).assign(r,a.$viewValue)}}),a.$formatters.push(c),a.$parsers.push(s),i.bind("mousedown mouseup",x)}}}}Object.defineProperty(n,"__esModule",{value:!0}),r.$inject=["$parse"];var i=angular.module("gumga.mask",[]).directive("gumgaMask",r);n.default=i.name}]);