UNPKG

angular-nz-input-formats

Version:

Angular directives to validate and format NZ-specific input types

8 lines 9.23 kB
/*! * angular-nz-input-formats * Angular directives to validate and format NZ-specific input types * @version v0.5.2 * @link https://github.com/nikrolls/angular-nz-input-formats * @license MIT License, http://www.opensource.org/licenses/MIT */ !function(t,i,e){var s;!function(t){t.module=i.module("nzInputFormats",[])}(s||(s={}));var s;!function(t){var e=function(){function t(t,e){void 0===t&&(t=null),void 0===e&&(e=null),this.require="ngModel",this.restrict="A",this.link=null,this.directiveName="nzSimpleInputMask",this.options={mask:null,validateOnLoad:!0},this.mask=null,this.maskChars={"*":/./,A:/[A-Za-z]/,9:/[0-9]/},this.scope=null,this.elem=null,this.ctrl=null,this.document=null,this.lastLen=0,this.setMask(t),e&&(this.maskChars=e),this.link=i.bind(this,this.doLink)}return t.prototype.setMask=function(t){t!==this.options.mask&&(this.mask=t.split(""),this.options.mask=t,this.elem&&this.parser(this.elem.val()))},t.Directive=function(i,e){return void 0===e&&(e=t),t.Document=i[0],{require:"ngModel",restrict:"A",link:function(i,s,r,n,a){var o=new e;return o.document=t.Document,o.doLink(i,s,r,n,a)}}},t.prototype.doLink=function(t,e,s,r,n){this.scope=t,this.elem=e,this.ctrl=r,s.$observe(this.directiveName,i.bind(this,this.processAttributeValue)),this.processAttributeValue(s[this.directiveName]),r.$formatters.push(i.bind(this,this.formatter)),r.$parsers.push(i.bind(this,this.parser)),i.isObject(r.$validators)&&this.options.validateOnLoad&&(r.$validators[this.directiveName]=i.bind(this,this.validator))},t.prototype.processAttributeValue=function(t){var e=this.scope.$eval(t);e&&(e.mask&&this.setMask(e.mask),this.options=i.extend(this.options,e))},t.prototype.updateMask=function(t){},t.prototype.formatter=function(t){return void 0===t&&(t=""),t?(this.updateMask(t),this.mask?this.applyMaskToString(String(t)):t):t},t.prototype.applyMaskToString=function(t){var i=this,e="",s=0;return this.mask.some(function(r){return s>=t.length?!0:void(e+=i.isMaskCharEditable(r)?t.charAt(s++):r)}),e},t.prototype.parser=function(t){if(void 0===t&&(t=""),!t)return this.triggerValidation(t);if(this.updateMask(t),!this.mask)return t;var i=this.removeMaskFromString(t),e=this.formatter(i),s=this.elem[0].selectionEnd,r=this.calculateNewCursorPosition(t,e,s);return this.updateFieldWith(e),this.updateCursorPositionIfElementIsActive(r),this.triggerValidation(i)},t.prototype.removeMaskFromString=function(t){var i=this,e=(t||"").split(""),s=[];return this.mask.every(function(t){return i.isMaskCharEditable(t)?(e=i.discardCharactersUntilNextMatch(e,t),e&&e.length&&s.push(e.shift())):e[0]===t&&e.shift(),e&&e.length>0}),s.join("")},t.prototype.discardCharactersUntilNextMatch=function(t,e){t=i.copy(t);for(var s=this.maskChars[e];t.length;){if(!this.isCharacterValidInMask(t[0],this.mask))return null;if(t[0].match(s))break;t.shift()}return t},t.prototype.isCharacterValidInMask=function(t,i){var e=this;return i.some(function(i){return e.isMaskCharEditable(i)?t.match(e.maskChars[i]):t===i})},t.prototype.isMaskCharEditable=function(t){return this.maskChars.hasOwnProperty(t)},t.prototype.calculateNewCursorPosition=function(t,i,e){return t.length>this.lastLen&&(e=this.isPositionEditable(e)?this.revertCursorMoveIfCharacterWasInvalid(t,e):this.getNextEditablePosition(e,i.length)),e},t.prototype.isPositionEditable=function(t){var i=this.mask[t-1];return this.maskChars.hasOwnProperty(i)},t.prototype.revertCursorMoveIfCharacterWasInvalid=function(t,i){var e=i-1,s=this.maskChars[this.mask[e]];return t.charAt(e).match(s)||i--,i},t.prototype.getNextEditablePosition=function(t,i){for(;i>t&&!this.isMaskCharEditable(this.mask[t-1]);)t++;return t},t.prototype.updateFieldWith=function(t){this.lastLen=t.length,this.elem.val(t),this.ctrl.$viewValue=t,this.ctrl.$commitViewValue()},t.prototype.updateCursorPositionIfElementIsActive=function(t){var i=this.elem[0];this.document.activeElement===i&&(i.selectionStart=i.selectionEnd=t)},t.prototype.triggerValidation=function(t){if(!i.isObject(this.ctrl.$validators)||!this.options.validateOnLoad){var e=this.validator();return this.ctrl.$setValidity(this.directiveName,e),e?t:""}return t},t.prototype.validator=function(){var t=this.ctrl.$viewValue;return i.isUndefined(t)||null===t||""===t||t!==t?!0:this.mask?this.ctrl.$viewValue&&this.ctrl.$viewValue.length===this.mask.length:!0},t.Document=null,t}();t.SimpleInputMask=e,t.module.directive("nzSimpleInputMask",["$document",e.Directive])}(s||(s={}));var s,r=this.__extends||function(t,i){function e(){this.constructor=t}for(var s in i)i.hasOwnProperty(s)&&(t[s]=i[s]);e.prototype=i.prototype,t.prototype=new e};!function(t){var e=function(e){function s(){e.call(this),this.shortMask="99-9999-9999999-99",this.longMask="99-9999-9999999-999",this.directiveName="nzBankNumber",this.options={mask:null,strict:!1},this.banks=["01","02","03","06","08","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","29","30","31","38"],this.checksumWeights=[6,3,7,9,0,10,5,8,4,2,1],this.setMask(this.shortMask)}return r(s,e),s.Directive=function(i){return t.SimpleInputMask.Directive(i,s)},s.prototype.updateMask=function(t){t&&(t=String(t||""),this.setMask(t.replace(/[\s-]/g,"").length<=15?this.shortMask:this.longMask))},s.prototype.validator=function(){var t=e.prototype.validator.call(this),s=this.ctrl.$viewValue;return i.isUndefined(s)||null===s||""===s||s!==s?!0:(s=s.replace(/\D/g,""),s.length<15||s.length>16?!1:this.options.strict?-1===this.banks.indexOf(s.substr(0,2))?!1:this.checksum(s):t)},s.prototype.checksum=function(t){var i=this,e=t.substr(2,11);if(11!==e.length)return!1;var s=e.split("").reduce(function(t,e,s){return t+parseInt(e,10)*i.checksumWeights[s]},0);return s%11===0},s}(t.SimpleInputMask);t.NZBankNumber=e,t.module.directive("nzBankNumber",["$document",e.Directive])}(s||(s={}));var s,r=this.__extends||function(t,i){function e(){this.constructor=t}for(var s in i)i.hasOwnProperty(s)&&(t[s]=i[s]);e.prototype=i.prototype,t.prototype=new e};!function(t){var e=function(e){function s(){e.call(this),this.shortMask="99-999-999",this.longMask="999-999-999",this.directiveName="nzIrdNumber",this.setMask(this.shortMask)}return r(s,e),s.Directive=function(i){return t.SimpleInputMask.Directive(i,s)},s.prototype.updateMask=function(t){t&&(t=String(t||""),this.setMask(t.replace(/\D/g,"").length<=8?this.shortMask:this.longMask))},s.prototype.validator=function(){var t=this.ctrl.$viewValue;if(i.isUndefined(t)||null===t||""===t||t!==t)return!0;var e=s.Extract(t);if(!e)return!1;if(!s.CheckValidRange(e))return!1;var r=e.substr(0,8);return s.CalculateCheckDigit(r,e.substr(-1))},s.Extract=function(t){var i=String(t).trim().match(/(?:\d{8,9}|\d{2,3}-\d{3}-\d{3})/);return i&&i.length?("0"+i[0].replace(/-/g,"")).substr(-9):null},s.CheckValidRange=function(t){var i=Number(t);return i>=1e7&&15e7>=i},s.CalculateCheckDigit=function(t,i){var e=[3,2,7,6,5,4,3,2],r=s.CalculateCheckDigitFor(e,t);return 10===r?s.ReCalculateCheckDigit(t,i):r===Number(i)},s.ReCalculateCheckDigit=function(t,i){var e=[7,4,3,2,5,2,7,6],r=s.CalculateCheckDigitFor(e,t);return 10===r?!1:r===Number(i)},s.CalculateCheckDigitFor=function(t,i){for(var e=0,s=0;s<t.length;s++)e+=Number(i.charAt(s))*t[s];var r=e%11;return 0===r?0:11-r},s}(t.SimpleInputMask);t.NZIrdNumber=e,t.module.directive("nzIrdNumber",["$document",e.Directive])}(s||(s={}));var s,r=this.__extends||function(t,i){function e(){this.constructor=t}for(var s in i)i.hasOwnProperty(s)&&(t[s]=i[s]);e.prototype=i.prototype,t.prototype=new e};!function(t){var e=function(e){function s(){e.call(this),this.defaultMask="9999999999999",this.mobileMask="999 999 99999",this.intlMobileMask="9999 999 99999",this.landlineMask="99 999 9999",this.intlLandlineMask="999 999 9999",this.specialMask="9999 999 999 9999",this.intlSpecialMask="99999 999 999 9999",this.directiveName="nzPhoneNumber",this.minLength=0,this.setMask(this.defaultMask)}return r(s,e),s.Directive=function(i){return t.SimpleInputMask.Directive(i,s)},s.prototype.formatter=function(t){if(!t)return t;var r=s.sanitise(t);i.isDefined(this.options.intl)&&(r=this.options.intl?r.match(/^(?:64.*|6)?/)[0]:r.match(/^(?:0.*)?/)[0]);var n=r.match(/^(64|6$)/);n&&(r="0"+r.substr(2));var a;switch(r.match(/^0[89]0/)?(a="special",this.setMask(n?this.intlSpecialMask:this.specialMask),this.minLength=n?11:10):"02"===r.substr(0,2)?(a="mobile",this.setMask(n?this.intlMobileMask:this.mobileMask),this.minLength=n?10:9):r.match(/^0[345679]/)?(a="landline",this.setMask(n?this.intlLandlineMask:this.landlineMask),this.minLength=n?10:9):(a="other",this.setMask(this.defaultMask),this.minLength=9),this.options.type){case"special":r=r.match(/^(?:0[89]0.*|0[89]|0)?/)[0];break;case"mobile":r=r.match(/^(?:02.*|0)?/)[0];break;case"landline":r=r.match(/^(?:0[345679].*|0)?/)[0]}return t=n?r.replace(/^0/,n[0]):r,e.prototype.formatter.call(this,t)},s.prototype.validator=function(){var t=this.ctrl.$viewValue;return i.isUndefined(t)||null===t||""===t||t!==t?!0:(t=s.sanitise(t),0===t.length||t.length>=this.minLength)},s.sanitise=function(t){return String(t).replace(/\D/g,"")},s}(t.SimpleInputMask);t.NZPhoneNumber=e,t.module.directive("nzPhoneNumber",["$document",e.Directive])}(s||(s={}))}(window,window.angular);