UNPKG

angular-input-masks

Version:
1,437 lines (1,135 loc) 40.7 kB
/** * angular-input-masks * Personalized input masks for AngularJS * @version v4.2.1 * @link http://github.com/assisrafael/angular-input-masks * @license MIT */ (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ 'use strict'; module.exports = angular.module('ui.utils.masks', [ require('./global/global-masks'), require('./br/br-masks'), require('./ch/ch-masks'), require('./fr/fr-masks'), require('./us/us-masks') ]).name; },{"./br/br-masks":3,"./ch/ch-masks":12,"./fr/fr-masks":14,"./global/global-masks":18,"./us/us-masks":29}],2:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var boletoBancarioMask = new StringMask('00000.00000 00000.000000 00000.000000 0 00000000000000'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.replace(/[^0-9]/g, '').slice(0, 47); }, format: function(cleanValue) { if (cleanValue.length === 0) { return cleanValue; } return boletoBancarioMask.apply(cleanValue).replace(/[^0-9]$/, ''); }, validations: { brBoletoBancario: function(value) { return value.length === 47; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],3:[function(require,module,exports){ 'use strict'; var m = angular.module('ui.utils.masks.br', []) .directive('uiBrBoletoBancarioMask', require('./boleto-bancario/boleto-bancario')) .directive('uiBrCarPlateMask', require('./car-plate/car-plate')) .directive('uiBrCepMask', require('./cep/cep')) .directive('uiBrCnpjMask', require('./cnpj/cnpj')) .directive('uiBrCpfMask', require('./cpf/cpf')) .directive('uiBrCpfcnpjMask', require('./cpf-cnpj/cpf-cnpj')) .directive('uiBrIeMask', require('./inscricao-estadual/ie')) .directive('uiNfeAccessKeyMask', require('./nfe/nfe')) .directive('uiBrPhoneNumberMask', require('./phone/br-phone')); module.exports = m.name; },{"./boleto-bancario/boleto-bancario":2,"./car-plate/car-plate":4,"./cep/cep":5,"./cnpj/cnpj":6,"./cpf-cnpj/cpf-cnpj":7,"./cpf/cpf":8,"./inscricao-estadual/ie":9,"./nfe/nfe":10,"./phone/br-phone":11}],4:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var carPlateMask = new StringMask('UUU-0000'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.replace(/[^a-zA-Z0-9]/g, '').slice(0, 7); }, format: function(cleanValue) { return (carPlateMask.apply(cleanValue) || '').replace(/[^a-zA-Z0-9]$/, ''); }, validations: { carPlate: function(value) { return value.length === 7; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],5:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var cepMask = new StringMask('00000-000'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 8); }, format: function(cleanValue) { return (cepMask.apply(cleanValue) || '').replace(/[^0-9]$/, ''); }, validations: { cep: function(value) { return value.toString().trim().length === 8; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],6:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var BrV = require('br-validations'); var maskFactory = require('../../helpers/mask-factory'); var cnpjPattern = new StringMask('00.000.000\/0000-00'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.replace(/[^\d]/g, '').slice(0, 14); }, format: function(cleanValue) { return (cnpjPattern.apply(cleanValue) || '').trim().replace(/[^0-9]$/, ''); }, validations: { cnpj: function(value) { return BrV.cnpj.validate(value); } } }); },{"../../helpers/mask-factory":24,"br-validations":undefined,"string-mask":undefined}],7:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var BrV = require('br-validations'); var maskFactory = require('../../helpers/mask-factory'); var cnpjPattern = new StringMask('00.000.000\/0000-00'); var cpfPattern = new StringMask('000.000.000-00'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.replace(/[^\d]/g, '').slice(0, 14); }, format: function(cleanValue) { var formatedValue; if (cleanValue.length > 11) { formatedValue = cnpjPattern.apply(cleanValue); } else { formatedValue = cpfPattern.apply(cleanValue) || ''; } return formatedValue.trim().replace(/[^0-9]$/, ''); }, validations: { cpf: function(value) { return value.length > 11 || BrV.cpf.validate(value); }, cnpj: function(value) { return value.length <= 11 || BrV.cnpj.validate(value); } } }); },{"../../helpers/mask-factory":24,"br-validations":undefined,"string-mask":undefined}],8:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var BrV = require('br-validations'); var maskFactory = require('../../helpers/mask-factory'); var cpfPattern = new StringMask('000.000.000-00'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.replace(/[^\d]/g, '').slice(0, 11); }, format: function(cleanValue) { return (cpfPattern.apply(cleanValue) || '').trim().replace(/[^0-9]$/, ''); }, validations: { cpf: function(value) { return BrV.cpf.validate(value); } } }); },{"../../helpers/mask-factory":24,"br-validations":undefined,"string-mask":undefined}],9:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var BrV = require('br-validations'); var ieMasks = { 'AC': [{mask: new StringMask('00.000.000/000-00')}], 'AL': [{mask: new StringMask('000000000')}], 'AM': [{mask: new StringMask('00.000.000-0')}], 'AP': [{mask: new StringMask('000000000')}], 'BA': [{chars: 8, mask: new StringMask('000000-00')}, {mask: new StringMask('0000000-00')}], 'CE': [{mask: new StringMask('00000000-0')}], 'DF': [{mask: new StringMask('00000000000-00')}], 'ES': [{mask: new StringMask('00000000-0')}], 'GO': [{mask: new StringMask('00.000.000-0')}], 'MA': [{mask: new StringMask('000000000')}], 'MG': [{mask: new StringMask('000.000.000/0000')}], 'MS': [{mask: new StringMask('000000000')}], 'MT': [{mask: new StringMask('0000000000-0')}], 'PA': [{mask: new StringMask('00-000000-0')}], 'PB': [{mask: new StringMask('00000000-0')}], 'PE': [{chars: 9, mask: new StringMask('0000000-00')}, {mask: new StringMask('00.0.000.0000000-0')}], 'PI': [{mask: new StringMask('000000000')}], 'PR': [{mask: new StringMask('000.00000-00')}], 'RJ': [{mask: new StringMask('00.000.00-0')}], 'RN': [{chars: 9, mask: new StringMask('00.000.000-0')}, {mask: new StringMask('00.0.000.000-0')}], 'RO': [{mask: new StringMask('0000000000000-0')}], 'RR': [{mask: new StringMask('00000000-0')}], 'RS': [{mask: new StringMask('000/0000000')}], 'SC': [{mask: new StringMask('000.000.000')}], 'SE': [{mask: new StringMask('00000000-0')}], 'SP': [{mask: new StringMask('000.000.000.000')}, {mask: new StringMask('-00000000.0/000')}], 'TO': [{mask: new StringMask('00000000000')}] }; function BrIeMaskDirective($parse) { function clearValue(value) { if (!value) { return value; } return value.replace(/[^0-9]/g, ''); } function getMask(uf, value) { if (!uf || !ieMasks[uf]) { return; } if (uf === 'SP' && /^P/i.test(value)) { return ieMasks.SP[1].mask; } var masks = ieMasks[uf]; var i = 0; while (masks[i].chars && masks[i].chars < clearValue(value).length && i < masks.length - 1) { i++; } return masks[i].mask; } function applyIEMask(value, uf) { var mask = getMask(uf, value); if (!mask) { return value; } var processed = mask.process(clearValue(value)); var formatedValue = processed.result || ''; formatedValue = formatedValue.trim().replace(/[^0-9]$/, ''); if (uf === 'SP' && /^p/i.test(value)) { return 'P' + formatedValue; } return formatedValue; } return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var state = ($parse(attrs.uiBrIeMask)(scope) || '').toUpperCase(); function formatter(value) { if (ctrl.$isEmpty(value)) { return value; } return applyIEMask(value, state); } function parser(value) { if (ctrl.$isEmpty(value)) { return value; } var formatedValue = applyIEMask(value, state); var actualValue = clearValue(formatedValue); if (ctrl.$viewValue !== formatedValue) { ctrl.$setViewValue(formatedValue); ctrl.$render(); } if (state && state.toUpperCase() === 'SP' && /^p/i.test(value)) { return 'P' + actualValue; } return actualValue; } ctrl.$formatters.push(formatter); ctrl.$parsers.push(parser); ctrl.$validators.ie = function validator(modelValue) { return ctrl.$isEmpty(modelValue) || BrV.ie(state).validate(modelValue); }; scope.$watch(attrs.uiBrIeMask, function(newState) { state = (newState || '').toUpperCase(); parser(ctrl.$viewValue); ctrl.$validate(); }); } }; } BrIeMaskDirective.$inject = ['$parse']; module.exports = BrIeMaskDirective; },{"br-validations":undefined,"string-mask":undefined}],10:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var nfeAccessKeyMask = new StringMask('0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.replace(/[^0-9]/g, '').slice(0, 44); }, format: function(cleanValue) { return (nfeAccessKeyMask.apply(cleanValue) || '').replace(/[^0-9]$/, ''); }, validations: { nfeAccessKey: function(value) { return value.length === 44; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],11:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); /** * FIXME: all numbers will have 9 digits after 2016. * see http://portal.embratel.com.br/embratel/9-digito/ */ var phoneMask8D = { countryCode : new StringMask('+00 (00) 0000-0000'), //with country code areaCode : new StringMask('(00) 0000-0000'), //with area code simple : new StringMask('0000-0000') //without area code }, phoneMask9D = { countryCode : new StringMask('+00 (00) 00000-0000'), //with country code areaCode : new StringMask('(00) 00000-0000'), //with area code simple : new StringMask('00000-0000') //without area code }, phoneMask0800 = { countryCode : null, //N/A areaCode : null, //N/A simple : new StringMask('0000-000-0000') //N/A, so it's "simple" }; module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 13); }, format: function(cleanValue) { var formattedValue; if (cleanValue.indexOf('0800') === 0) { formattedValue = phoneMask0800.simple.apply(cleanValue); } else if (cleanValue.length < 9) { formattedValue = phoneMask8D.simple.apply(cleanValue) || ''; } else if (cleanValue.length < 10) { formattedValue = phoneMask9D.simple.apply(cleanValue); } else if (cleanValue.length < 11) { formattedValue = phoneMask8D.areaCode.apply(cleanValue); } else if (cleanValue.length < 12) { formattedValue = phoneMask9D.areaCode.apply(cleanValue); } else if (cleanValue.length < 13) { formattedValue = phoneMask8D.countryCode.apply(cleanValue); } else { formattedValue = phoneMask9D.countryCode.apply(cleanValue); } return formattedValue.trim().replace(/[^0-9]$/, ''); }, getModelValue: function(formattedValue, originalModelType) { var cleanValue = this.clearValue(formattedValue); return originalModelType === 'number' ? parseInt(cleanValue) : cleanValue; }, validations: { brPhoneNumber: function(value) { var valueLength = value && value.toString().length; //8- 8D without AC //9- 9D without AC //10- 8D with AC //11- 9D with AC and 0800 //12- 8D with AC plus CC //13- 9D with AC plus CC return valueLength >= 8 && valueLength <= 13; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],12:[function(require,module,exports){ 'use strict'; var m = angular.module('ui.utils.masks.ch', []) .directive('uiChPhoneNumberMask', require('./phone/ch-phone')); module.exports = m.name; },{"./phone/ch-phone":13}],13:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var phoneMask = new StringMask('+00 00 000 00 00'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 11); }, format: function(cleanValue) { var formatedValue; formatedValue = phoneMask.apply(cleanValue) || ''; return formatedValue.trim().replace(/[^0-9]$/, ''); }, validations: { chPhoneNumber: function(value) { var valueLength = value && value.toString().length; return valueLength === 11; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],14:[function(require,module,exports){ 'use strict'; var m = angular.module('ui.utils.masks.fr', []) .directive('uiFrPhoneNumberMask', require('./phone/fr-phone')); module.exports = m.name; },{"./phone/fr-phone":15}],15:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var phoneMaskFR = new StringMask('00 00 00 00 00'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, '').slice(0, 10); }, format: function(cleanValue) { var formattedValue; formattedValue = phoneMaskFR.apply(cleanValue) || ''; return formattedValue.trim().replace(/[^0-9]$/, ''); }, validations: { frPhoneNumber: function(value) { var valueLength = value && value.toString().length; return valueLength === 10; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],16:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var ccSize = 16; var ccMask = new StringMask('0000 0000 0000 0000'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, '').slice(0, ccSize); }, format: function(cleanValue) { var formatedValue; formatedValue = ccMask.apply(cleanValue) || ''; return formatedValue.trim().replace(/[^0-9]$/, ''); }, validations: { creditCard: function(value) { var valueLength = value && value.toString().length; return valueLength === ccSize; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],17:[function(require,module,exports){ 'use strict'; var formatDate = require('date-fns/format'); var parseDate = require('date-fns/parse'); var isValidDate = require('date-fns/isValid'); var StringMask = require('string-mask'); function isISODateString(date) { return /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]{3}([-+][0-9]{2}:[0-9]{2}|Z)$/ .test(date.toString()); } var dateFormatMapByLocale = { 'pt-br': 'DD/MM/YYYY', 'es-ar': 'DD/MM/YYYY', 'es-mx': 'DD/MM/YYYY', 'es' : 'DD/MM/YYYY', 'en-us': 'MM/DD/YYYY', 'en' : 'MM/DD/YYYY', 'fr-fr': 'DD/MM/YYYY', 'fr' : 'DD/MM/YYYY', 'ru' : 'DD.MM.YYYY' }; function DateMaskDirective($locale) { var dateFormat = dateFormatMapByLocale[$locale.id] || 'YYYY-MM-DD'; return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { attrs.parse = attrs.parse || 'true'; dateFormat = attrs.uiDateMask || dateFormat; var dateMask = new StringMask(dateFormat.replace(/[YMD]/g,'0')); function formatter(value) { if (ctrl.$isEmpty(value)) { return null; } var cleanValue = value; if (typeof value === 'object' || isISODateString(value)) { cleanValue = formatDate(value, dateFormat); } cleanValue = cleanValue.replace(/[^0-9]/g, ''); var formatedValue = dateMask.apply(cleanValue) || ''; return formatedValue.trim().replace(/[^0-9]$/, ''); } ctrl.$formatters.push(formatter); ctrl.$parsers.push(function parser(value) { if (ctrl.$isEmpty(value)) { return value; } var formatedValue = formatter(value); if (ctrl.$viewValue !== formatedValue) { ctrl.$setViewValue(formatedValue); ctrl.$render(); } return attrs.parse === 'false' ? formatedValue : parseDate(formatedValue, dateFormat, new Date()); }); ctrl.$validators.date = function validator(modelValue, viewValue) { if (ctrl.$isEmpty(modelValue)) { return true; } return isValidDate(parseDate(viewValue, dateFormat, new Date())) && viewValue.length === dateFormat.length; }; } }; } DateMaskDirective.$inject = ['$locale']; module.exports = DateMaskDirective; },{"date-fns/format":undefined,"date-fns/isValid":undefined,"date-fns/parse":undefined,"string-mask":undefined}],18:[function(require,module,exports){ 'use strict'; var m = angular.module('ui.utils.masks.global', []) .directive('uiCreditCardMask', require('./credit-card/credit-card')) .directive('uiDateMask', require('./date/date')) .directive('uiMoneyMask', require('./money/money')) .directive('uiNumberMask', require('./number/number')) .directive('uiPercentageMask', require('./percentage/percentage')) .directive('uiScientificNotationMask', require('./scientific-notation/scientific-notation')) .directive('uiTimeMask', require('./time/time')); module.exports = m.name; },{"./credit-card/credit-card":16,"./date/date":17,"./money/money":19,"./number/number":20,"./percentage/percentage":21,"./scientific-notation/scientific-notation":22,"./time/time":23}],19:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var validators = require('../../helpers/validators'); var PreFormatters = require('../../helpers/pre-formatters'); function MoneyMaskDirective($locale, $parse) { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP, currencySym = $locale.NUMBER_FORMATS.CURRENCY_SYM, symbolSeparation = ' ', decimals = $parse(attrs.uiMoneyMask)(scope), backspacePressed = false; element.bind('keydown keypress', function(event) { backspacePressed = event.which === 8; }); function maskFactory(decimals) { var decimalsPattern = decimals > 0 ? decimalDelimiter + new Array(decimals + 1).join('0') : ''; var maskPattern = '#' + thousandsDelimiter + '##0' + decimalsPattern; if (angular.isDefined(attrs.uiCurrencyAfter)) { maskPattern += symbolSeparation; } else { maskPattern = symbolSeparation + maskPattern; } return new StringMask(maskPattern, {reverse: true}); } if (angular.isDefined(attrs.uiDecimalDelimiter)) { decimalDelimiter = attrs.uiDecimalDelimiter; } if (angular.isDefined(attrs.uiThousandsDelimiter)) { thousandsDelimiter = attrs.uiThousandsDelimiter; } if (angular.isDefined(attrs.uiHideGroupSep)) { thousandsDelimiter = ''; } if (angular.isDefined(attrs.uiHideSpace)) { symbolSeparation = ''; } if (angular.isDefined(attrs.currencySymbol)) { currencySym = attrs.currencySymbol; if (attrs.currencySymbol.length === 0) { symbolSeparation = ''; } } if (isNaN(decimals)) { decimals = 2; } decimals = parseInt(decimals); var moneyMask = maskFactory(decimals); function formatter(value) { if (ctrl.$isEmpty(value)) { return ''; } if (angular.isDefined(attrs.uiIntegerModel)) { value /= Math.pow(10, decimals); } var prefix = (angular.isDefined(attrs.uiNegativeNumber) && value < 0) ? '-' : ''; var valueToFormat = PreFormatters.prepareNumberToFormatter(value, decimals); if (angular.isDefined(attrs.uiCurrencyAfter)) { return prefix + moneyMask.apply(valueToFormat) + currencySym; } return prefix + currencySym + moneyMask.apply(valueToFormat); } function parser(value) { if (ctrl.$isEmpty(value)) { return null; } var actualNumber = value.replace(/[^\d]+/g,''), formatedValue; actualNumber = actualNumber.replace(/^[0]+([1-9])/,'$1'); actualNumber = actualNumber || '0'; if (backspacePressed && angular.isDefined(attrs.uiCurrencyAfter) && actualNumber !== 0) { actualNumber = actualNumber.substring(0, actualNumber.length - 1); backspacePressed = false; } if (angular.isDefined(attrs.uiCurrencyAfter)) { formatedValue = moneyMask.apply(actualNumber) + currencySym; } else { formatedValue = currencySym + moneyMask.apply(actualNumber); } if (angular.isDefined(attrs.uiNegativeNumber)) { var isNegative = (value[0] === '-'), needsToInvertSign = (value.slice(-1) === '-'); //only apply the minus sign if it is negative or(exclusive) //needs to be negative and the number is different from zero if (needsToInvertSign ^ isNegative && !!actualNumber) { actualNumber *= -1; formatedValue = '-' + formatedValue; } } if (value !== formatedValue) { ctrl.$setViewValue(formatedValue); ctrl.$render(); } var retValue = parseInt(formatedValue.replace(/[^\d\-]+/g,'')); if (!isNaN(retValue)) { if (!angular.isDefined(attrs.uiIntegerModel)) { retValue /= Math.pow(10, decimals); } return retValue; } return null; } ctrl.$formatters.push(formatter); ctrl.$parsers.push(parser); if (attrs.uiMoneyMask) { scope.$watch(attrs.uiMoneyMask, function(_decimals) { decimals = isNaN(_decimals) ? 2 : _decimals; decimals = parseInt(decimals); moneyMask = maskFactory(decimals); parser(ctrl.$viewValue); }); } if (attrs.currency) { scope.$watch(attrs.currency, function(_currency) { currencySym = _currency; moneyMask = maskFactory(decimals); parser(ctrl.$viewValue); }); } if (attrs.min) { var minVal; ctrl.$validators.min = function(modelValue) { return validators.minNumber(ctrl, modelValue, minVal); }; scope.$watch(attrs.min, function(value) { minVal = value; ctrl.$validate(); }); } if (attrs.max) { var maxVal; ctrl.$validators.max = function(modelValue) { return validators.maxNumber(ctrl, modelValue, maxVal); }; scope.$watch(attrs.max, function(value) { maxVal = value; ctrl.$validate(); }); } } }; } MoneyMaskDirective.$inject = ['$locale', '$parse']; module.exports = MoneyMaskDirective; },{"../../helpers/pre-formatters":26,"../../helpers/validators":27,"string-mask":undefined}],20:[function(require,module,exports){ 'use strict'; var validators = require('../../helpers/validators'); var NumberMasks = require('../../helpers/number-mask-builder'); var PreFormatters = require('../../helpers/pre-formatters'); function NumberMaskDirective($locale, $parse) { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP, decimals = $parse(attrs.uiNumberMask)(scope); if (angular.isDefined(attrs.uiHideGroupSep)) { thousandsDelimiter = ''; } if (isNaN(decimals)) { decimals = 2; } var viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter), modelMask = NumberMasks.modelMask(decimals); function parser(value) { if (ctrl.$isEmpty(value)) { return null; } var valueToFormat = PreFormatters.clearDelimitersAndLeadingZeros(value) || '0'; var formatedValue = viewMask.apply(valueToFormat); var actualNumber = parseFloat(modelMask.apply(valueToFormat)); if (angular.isDefined(attrs.uiNegativeNumber)) { var isNegative = (value[0] === '-'), needsToInvertSign = (value.slice(-1) === '-'); //only apply the minus sign if it is negative or(exclusive) or the first character //needs to be negative and the number is different from zero if ((needsToInvertSign ^ isNegative) || value === '-') { actualNumber *= -1; formatedValue = '-' + ((actualNumber !== 0) ? formatedValue : ''); } } if (ctrl.$viewValue !== formatedValue) { ctrl.$setViewValue(formatedValue); ctrl.$render(); } return actualNumber; } function formatter(value) { if (ctrl.$isEmpty(value)) { return value; } var prefix = (angular.isDefined(attrs.uiNegativeNumber) && value < 0) ? '-' : ''; var valueToFormat = PreFormatters.prepareNumberToFormatter(value, decimals); return prefix + viewMask.apply(valueToFormat); } function clearViewValueIfMinusSign() { if (ctrl.$viewValue === '-') { ctrl.$setViewValue(''); ctrl.$render(); } } element.on('blur', clearViewValueIfMinusSign); ctrl.$formatters.push(formatter); ctrl.$parsers.push(parser); if (attrs.uiNumberMask) { scope.$watch(attrs.uiNumberMask, function(_decimals) { decimals = isNaN(_decimals) ? 2 : _decimals; viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter); modelMask = NumberMasks.modelMask(decimals); parser(ctrl.$viewValue); }); } if (attrs.min) { var minVal; ctrl.$validators.min = function(modelValue) { return validators.minNumber(ctrl, modelValue, minVal); }; scope.$watch(attrs.min, function(value) { minVal = value; ctrl.$validate(); }); } if (attrs.max) { var maxVal; ctrl.$validators.max = function(modelValue) { return validators.maxNumber(ctrl, modelValue, maxVal); }; scope.$watch(attrs.max, function(value) { maxVal = value; ctrl.$validate(); }); } } }; } NumberMaskDirective.$inject = ['$locale', '$parse']; module.exports = NumberMaskDirective; },{"../../helpers/number-mask-builder":25,"../../helpers/pre-formatters":26,"../../helpers/validators":27}],21:[function(require,module,exports){ 'use strict'; var validators = require('../../helpers/validators'); var NumberMasks = require('../../helpers/number-mask-builder'); var PreFormatters = require('../../helpers/pre-formatters'); function preparePercentageToFormatter(value, decimals, modelMultiplier) { return PreFormatters.clearDelimitersAndLeadingZeros((parseFloat(value)*modelMultiplier).toFixed(decimals)); } function PercentageMaskDirective($locale) { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP; var backspacePressed = false; element.bind('keydown keypress', function(event) { backspacePressed = event.which === 8; }); var thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP; if (angular.isDefined(attrs.uiHideGroupSep)) { thousandsDelimiter = ''; } var percentageSymbol = ' %'; if (angular.isDefined(attrs.uiHidePercentageSign)) { percentageSymbol = ''; } else if (angular.isDefined(attrs.uiHideSpace)) { percentageSymbol = '%'; } var decimals = parseInt(attrs.uiPercentageMask); if (isNaN(decimals)) { decimals = 2; } var modelValue = { multiplier : 100, decimalMask: 2 }; if (angular.isDefined(attrs.uiPercentageValue)) { modelValue.multiplier = 1; modelValue.decimalMask = 0; } var numberDecimals = decimals + modelValue.decimalMask; var viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter), modelMask = NumberMasks.modelMask(numberDecimals); function formatter(value) { if (ctrl.$isEmpty(value)) { return value; } var prefix = (angular.isDefined(attrs.uiNegativeNumber) && value < 0) ? '-' : ''; var valueToFormat = preparePercentageToFormatter(value, decimals, modelValue.multiplier); var formatedValue = prefix + viewMask.apply(valueToFormat) + percentageSymbol; return formatedValue; } function parser(value) { if (ctrl.$isEmpty(value)) { return null; } var valueToFormat = PreFormatters.clearDelimitersAndLeadingZeros(value) || '0'; if (percentageSymbol !== '' && value.length > 1 && value.indexOf('%') === -1) { valueToFormat = valueToFormat.slice(0, valueToFormat.length - 1); } if (backspacePressed && value.length === 1 && value !== '%') { valueToFormat = '0'; } var formatedValue = viewMask.apply(valueToFormat) + percentageSymbol; var actualNumber = parseFloat(modelMask.apply(valueToFormat)); if (angular.isDefined(attrs.uiNegativeNumber)) { var isNegative = (value[0] === '-'), needsToInvertSign = (value.slice(-1) === '-'); //only apply the minus sign if it is negative or(exclusive) or the first character //needs to be negative and the number is different from zero if ((needsToInvertSign ^ isNegative) || value === '-') { actualNumber *= -1; formatedValue = '-' + ((actualNumber !== 0) ? formatedValue : ''); } } if (ctrl.$viewValue !== formatedValue) { ctrl.$setViewValue(formatedValue); ctrl.$render(); } return actualNumber; } ctrl.$formatters.push(formatter); ctrl.$parsers.push(parser); if (attrs.uiPercentageMask) { scope.$watch(attrs.uiPercentageMask, function(_decimals) { decimals = isNaN(_decimals) ? 2 : _decimals; numberDecimals = decimals + modelValue.decimalMask; viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter); modelMask = NumberMasks.modelMask(numberDecimals); parser(formatter(ctrl.$modelValue)); }); } if (attrs.min) { var minVal; ctrl.$validators.min = function(modelValue) { return validators.minNumber(ctrl, modelValue, minVal); }; scope.$watch(attrs.min, function(value) { minVal = value; ctrl.$validate(); }); } if (attrs.max) { var maxVal; ctrl.$validators.max = function(modelValue) { return validators.maxNumber(ctrl, modelValue, maxVal); }; scope.$watch(attrs.max, function(value) { maxVal = value; ctrl.$validate(); }); } } }; } PercentageMaskDirective.$inject = ['$locale']; module.exports = PercentageMaskDirective; },{"../../helpers/number-mask-builder":25,"../../helpers/pre-formatters":26,"../../helpers/validators":27}],22:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); function ScientificNotationMaskDirective($locale, $parse) { var decimalDelimiter = $locale.NUMBER_FORMATS.DECIMAL_SEP, defaultPrecision = 2; function significandMaskBuilder(decimals) { var mask = '0'; if (decimals > 0) { mask += decimalDelimiter; for (var i = 0; i < decimals; i++) { mask += '0'; } } return new StringMask(mask, { reverse: true }); } return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var decimals = $parse(attrs.uiScientificNotationMask)(scope); if (isNaN(decimals)) { decimals = defaultPrecision; } var significandMask = significandMaskBuilder(decimals); function splitNumber(value) { var stringValue = value.toString(), splittedNumber = stringValue.match(/(-?[0-9]*)[\.]?([0-9]*)?[Ee]?([\+-]?[0-9]*)?/); return { integerPartOfSignificand: splittedNumber[1], decimalPartOfSignificand: splittedNumber[2], exponent: splittedNumber[3] | 0 }; } function formatter(value) { if (ctrl.$isEmpty(value)) { return value; } if (typeof value === 'number') { value = value.toExponential(decimals); } else { value = value.toString().replace(decimalDelimiter, '.'); } var formattedValue, exponent; var splittedNumber = splitNumber(value); var integerPartOfSignificand = splittedNumber.integerPartOfSignificand || 0; var numberToFormat = integerPartOfSignificand.toString(); if (angular.isDefined(splittedNumber.decimalPartOfSignificand)) { numberToFormat += splittedNumber.decimalPartOfSignificand; } var needsNormalization = (integerPartOfSignificand >= 1 || integerPartOfSignificand <= -1) && ( (angular.isDefined(splittedNumber.decimalPartOfSignificand) && splittedNumber.decimalPartOfSignificand.length > decimals) || (decimals === 0 && numberToFormat.length >= 2) ); if (needsNormalization) { exponent = numberToFormat.slice(decimals + 1, numberToFormat.length); numberToFormat = numberToFormat.slice(0, decimals + 1); } formattedValue = significandMask.apply(numberToFormat); if (splittedNumber.exponent !== 0) { exponent = splittedNumber.exponent; } if (angular.isDefined(exponent)) { formattedValue += 'e' + exponent; } var prefix = (angular.isDefined(attrs.uiNegativeNumber) && value[0] === '-') ? '-' : ''; return prefix + formattedValue; } function parser(value) { if (ctrl.$isEmpty(value)) { return value; } var isExponentNegative = /e-/.test(value); var cleanValue = value.replace('e-', 'e'); var viewValue = formatter(cleanValue); var needsToInvertSign = (value.slice(-1) === '-'); if (needsToInvertSign ^ isExponentNegative) { viewValue = viewValue.replace(/(e[-]?)/, 'e-'); } if (needsToInvertSign && isExponentNegative) { viewValue = viewValue[0] !== '-' ? ('-' + viewValue) : viewValue.replace(/^(-)/,''); } var modelValue = parseFloat(viewValue.replace(decimalDelimiter, '.')); if (ctrl.$viewValue !== viewValue) { ctrl.$setViewValue(viewValue); ctrl.$render(); } return modelValue; } ctrl.$formatters.push(formatter); ctrl.$parsers.push(parser); ctrl.$validators.max = function validator(value) { return ctrl.$isEmpty(value) || value < Number.MAX_VALUE; }; } }; } ScientificNotationMaskDirective.$inject = ['$locale', '$parse']; module.exports = ScientificNotationMaskDirective; },{"string-mask":undefined}],23:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); module.exports = function TimeMaskDirective() { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { var timeFormat = '00:00:00'; if (angular.isDefined(attrs.uiTimeMask) && attrs.uiTimeMask === 'short') { timeFormat = '00:00'; } var formattedValueLength = timeFormat.length; var unformattedValueLength = timeFormat.replace(':', '').length; var timeMask = new StringMask(timeFormat); function formatter(value) { if (ctrl.$isEmpty(value)) { return value; } var cleanValue = value.replace(/[^0-9]/g, '').slice(0, unformattedValueLength) || ''; return (timeMask.apply(cleanValue) || '').replace(/[^0-9]$/, ''); } ctrl.$formatters.push(formatter); ctrl.$parsers.push(function parser(value) { if (ctrl.$isEmpty(value)) { return value; } var viewValue = formatter(value); var modelValue = viewValue; if (ctrl.$viewValue !== viewValue) { ctrl.$setViewValue(viewValue); ctrl.$render(); } return modelValue; }); ctrl.$validators.time = function(modelValue) { if (ctrl.$isEmpty(modelValue)) { return true; } var splittedValue = modelValue.toString().split(/:/).filter(function(v) { return !!v; }); var hours = parseInt(splittedValue[0]), minutes = parseInt(splittedValue[1]), seconds = parseInt(splittedValue[2] || 0); return modelValue.toString().length === formattedValueLength && hours < 24 && minutes < 60 && seconds < 60; }; } }; }; },{"string-mask":undefined}],24:[function(require,module,exports){ 'use strict'; module.exports = function maskFactory(maskDefinition) { return function MaskDirective() { return { restrict: 'A', require: 'ngModel', link: function(scope, element, attrs, ctrl) { ctrl.$formatters.push(function formatter(value) { if (ctrl.$isEmpty(value)) { return value; } var cleanValue = maskDefinition.clearValue(value.toString()); return maskDefinition.format(cleanValue); }); ctrl.$parsers.push(function parser(value) { if (ctrl.$isEmpty(value)) { return value; } var cleanValue = maskDefinition.clearValue(value.toString()); var formattedValue = maskDefinition.format(cleanValue); if (ctrl.$viewValue !== formattedValue) { ctrl.$setViewValue(formattedValue); ctrl.$render(); } if (angular.isUndefined(maskDefinition.getModelValue)) { return cleanValue; } var actualModelType = typeof ctrl.$modelValue; return maskDefinition.getModelValue(formattedValue, actualModelType); }); angular.forEach(maskDefinition.validations, function(validatorFn, validationErrorKey) { ctrl.$validators[validationErrorKey] = function validator(modelValue, viewValue) { return ctrl.$isEmpty(modelValue) || validatorFn(modelValue, viewValue); }; }); } }; }; }; },{}],25:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); function viewMask(decimals, decimalDelimiter, thousandsDelimiter) { var mask = '#' + thousandsDelimiter + '##0'; if (decimals > 0) { mask += decimalDelimiter; for (var i = 0; i < decimals; i++) { mask += '0'; } } return new StringMask(mask, { reverse: true }); } function modelMask(decimals) { var mask = '###0'; if (decimals > 0) { mask += '.'; for (var i = 0; i < decimals; i++) { mask += '0'; } } return new StringMask(mask, { reverse: true }); } module.exports = { viewMask: viewMask, modelMask: modelMask }; },{"string-mask":undefined}],26:[function(require,module,exports){ 'use strict'; function clearDelimitersAndLeadingZeros(value) { if (value === '0') { return '0'; } var cleanValue = value.toString().replace(/^-/,'').replace(/^0*/, ''); return cleanValue.replace(/[^0-9]/g, ''); } function prepareNumberToFormatter(value, decimals) { return clearDelimitersAndLeadingZeros((parseFloat(value)).toFixed(decimals)); } module.exports = { clearDelimitersAndLeadingZeros: clearDelimitersAndLeadingZeros, prepareNumberToFormatter: prepareNumberToFormatter }; },{}],27:[function(require,module,exports){ 'use strict'; module.exports = { maxNumber: function(ctrl, value, limit) { var max = parseFloat(limit, 10); return ctrl.$isEmpty(value) || isNaN(max) || value <= max; }, minNumber: function(ctrl, value, limit) { var min = parseFloat(limit, 10); return ctrl.$isEmpty(value) || isNaN(min) || value >= min; } }; },{}],28:[function(require,module,exports){ 'use strict'; var StringMask = require('string-mask'); var maskFactory = require('../../helpers/mask-factory'); var phoneMaskUS = new StringMask('(000) 000-0000'), phoneMaskINTL = new StringMask('+00-00-000-000000'); module.exports = maskFactory({ clearValue: function(rawValue) { return rawValue.toString().replace(/[^0-9]/g, ''); }, format: function(cleanValue) { var formattedValue; if (cleanValue.length < 11) { formattedValue = phoneMaskUS.apply(cleanValue) || ''; } else { formattedValue = phoneMaskINTL.apply(cleanValue); } return formattedValue.trim().replace(/[^0-9]$/, ''); }, validations: { usPhoneNumber: function(value) { return value && value.toString().length > 9; } } }); },{"../../helpers/mask-factory":24,"string-mask":undefined}],29:[function(require,module,exports){ 'use strict'; var m = angular.module('ui.utils.masks.us', []) .directive('uiUsPhoneNumberMask', require('./phone/us-phone')); module.exports = m.name; },{"./phone/us-phone":28}]},{},[1]);