angular-dynamic-number
Advanced tools
Comparing version 2.1.1 to 2.2.0
{ | ||
"name": "angular-dynamic-number", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"homepage": "https://github.com/uhlryk/angular-dynamic-number", | ||
@@ -5,0 +5,0 @@ "authors": [ |
{ | ||
"name": "angular-dynamic-number", | ||
"version": "2.1.1", | ||
"version": "2.2.0", | ||
"description": "Highly customizable angular directive for numbers", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -109,2 +109,6 @@ # Angular Dynamic Number | ||
**num-fixed | ||
If true then there is fixed number of fraction digets - (useful when fraction part is 00 and we need to show this zeros e.g. 12,00 ) | ||
**num-thousand**: | ||
@@ -111,0 +115,0 @@ |
@@ -29,3 +29,3 @@ /*jslint node: true */ | ||
} | ||
return addPrependAppend(newViewValue, prepend, append); | ||
return newViewValue; | ||
} | ||
@@ -76,3 +76,3 @@ function convViewToModel(viewValue, viewSeparator, thousandSeparator) { | ||
if(attrs_num_sep === ','){ | ||
return ','; | ||
return ','; | ||
} else if(attrs_num_sep === '.'){ | ||
@@ -199,3 +199,19 @@ return '.'; | ||
} | ||
function changeViewValue(ngModelController, value, prepend, append, state, disable){ | ||
function addFixedZeros(value, parameters) { | ||
var newValue = value; | ||
if(parameters.isFixed) { | ||
var fractionPart = newValue.split(parameters.fractionSeparator)[1]; | ||
var fractionLength = (fractionPart && fractionPart.length) ? fractionPart.length : 0; | ||
if(fractionLength === 0) { | ||
newValue += parameters.fractionSeparator; | ||
} | ||
for(var i=fractionLength; i < parameters.fractionPart; i++ ) { | ||
newValue += "0"; | ||
} | ||
} | ||
return newValue; | ||
} | ||
function changeViewValue(ngModelController, value, parameters, state, disable){ | ||
// https://github.com/angular/angular.js/issues/13068 | ||
@@ -206,3 +222,6 @@ // ngModelController.$viewValue = value; | ||
} | ||
ngModelController.$setViewValue(addPrependAppend(String(value), prepend, append)); | ||
var valueString = String(value); | ||
var valueWithFixedZeros = addFixedZeros(valueString, parameters); | ||
var valueWithPrependAppend = addPrependAppend(valueWithFixedZeros, parameters.prepend, parameters.append); | ||
ngModelController.$setViewValue(valueWithPrependAppend); | ||
ngModelController.$render(); | ||
@@ -232,2 +251,3 @@ } | ||
} | ||
value = addPrependAppend(value, prepend, append); | ||
if(isThousandSeparator){ | ||
@@ -275,3 +295,3 @@ value = addThousandSeparator(value, fractionSeparator, thousandSeparator); | ||
} else | ||
elem.focus(); | ||
elem.focus(); | ||
} | ||
@@ -294,2 +314,15 @@ } | ||
function cutSurplusFractionPart(value, parameters) { | ||
var newValue = value; | ||
var splitedValue = newValue.split(parameters.fractionSeparator); | ||
var integerPart = splitedValue[0]; | ||
var fractionPart = splitedValue[1]; | ||
if (fractionPart && fractionPart.length > parameters.fractionPart) { | ||
fractionPart = fractionPart.slice(0, parameters.fractionPart); | ||
newValue = [integerPart, fractionPart].join(parameters.fractionSeparator); | ||
} | ||
return newValue; | ||
} | ||
function prepareResponse(value) { | ||
@@ -311,3 +344,4 @@ return Number(value); | ||
numPrepend: scope.numPrepend, | ||
numAppend: scope.numAppend | ||
numAppend: scope.numAppend, | ||
numFixed: scope.numFixed | ||
}; | ||
@@ -335,2 +369,3 @@ if(key) { | ||
var append = initNumAppendPrepend(properties.numAppend !== undefined ? properties.numAppend : strategy.numAppend); | ||
var isFixed = initIsFixed(properties.numFixed !== undefined ? properties.numFixed : strategy.numFixed, false); | ||
if(isPositiveNumber === false && isNegativeNumber === false) { | ||
@@ -354,3 +389,4 @@ throw new Error('Number is set to not be positive and not be negative. Change num_pos attr or/and num_neg attr to true'); | ||
prepend: prepend, | ||
append: append | ||
append: append, | ||
isFixed: isFixed | ||
} | ||
@@ -374,2 +410,3 @@ } | ||
var append = parameters.append; | ||
var isFixed = parameters.isFixed; | ||
@@ -396,3 +433,3 @@ var parsedValue = String(value); | ||
if(new RegExp('^[\.,'+thousandSeparator+']{2,}').test(parsedValue)) { | ||
changeViewValue(ngModelController, 0, prepend, append, state); | ||
changeViewValue(ngModelController, 0, parameters, state); | ||
return 0; | ||
@@ -412,7 +449,7 @@ } | ||
if(fractionPart) { | ||
changeViewValue(ngModelController, '0' + fractionSeparator, prepend, append, state, true); | ||
changeViewValue(ngModelController, '0' + fractionSeparator, parameters, state, true); | ||
setCaretPosition(element[0], 2); | ||
return 0; | ||
} else { | ||
changeViewValue(ngModelController, '', prepend, append, state); | ||
changeViewValue(ngModelController, '', parameters, state); | ||
return 0; | ||
@@ -422,3 +459,3 @@ } | ||
if(parsedValue === '' && String(value).charAt(0)=== '0'){ | ||
changeViewValue(ngModelController, 0, prepend, append); | ||
changeViewValue(ngModelController, 0, parameters); | ||
return 0; | ||
@@ -431,8 +468,10 @@ } | ||
if(isPositiveNumber && !isNegativeNumber) { | ||
changeViewValue(ngModelController, '', prepend, append, state); | ||
changeViewValue(ngModelController, '', parameters, state); | ||
} else { | ||
changeViewValue(ngModelController, '-', prepend, append, state); | ||
changeViewValue(ngModelController, '-', parameters, state); | ||
} | ||
return 0; | ||
} | ||
parsedValue = cutSurplusFractionPart(parsedValue, parameters); | ||
/** | ||
@@ -443,7 +482,8 @@ * view value failed 'correct view format' test | ||
if(viewRegexTest.test(parsedValue) === false){ | ||
var modelValue = convModelToView(ngModelController.$modelValue, fractionSeparator, prepend, append); | ||
var modelValue = convModelToView(ngModelController.$modelValue, fractionSeparator, parameters); | ||
if(isThousandSeparator){ | ||
modelValue = addThousandSeparator(modelValue, fractionSeparator, thousandSeparator); | ||
} | ||
changeViewValue(ngModelController, modelValue, prepend, append, state); | ||
changeViewValue(ngModelController, modelValue, parameters, state); | ||
setCaretPosition(element[0],cursorPosition-1); | ||
@@ -472,3 +512,3 @@ return ngModelController.$modelValue; | ||
} | ||
changeViewValue(ngModelController, parsedValue, prepend, append, state); | ||
changeViewValue(ngModelController, parsedValue, parameters, state); | ||
setCaretPosition(element[0], currentPosition + dots); | ||
@@ -491,3 +531,3 @@ return convViewToModel(parsedValue, fractionSeparator, thousandSeparator); | ||
initObject.roundFunction, | ||
false, | ||
initObject.isFixed, | ||
initObject.isThousandSeparator, | ||
@@ -516,3 +556,4 @@ initObject.thousandSeparator, | ||
numPrepend: "@", | ||
numAppend: "@" | ||
numAppend: "@", | ||
numFixed: "@" | ||
}, | ||
@@ -611,2 +652,9 @@ link: function(scope, element, attrs, ngModelController) { | ||
}); | ||
scope.$watch('numFixed', function(newProperty, oldProperty ){ | ||
if(oldProperty === newProperty) { | ||
return; | ||
} | ||
initObject = initAllProperties(createPropertyObject(scope, 'numFixed', newProperty), element, attrs, ngModelController, dynamicNumberStrategy); | ||
onPropertyWatch(ngModelController, initObject); | ||
}); | ||
var state = { | ||
@@ -634,3 +682,3 @@ enable: true, | ||
initObject.roundFunction, | ||
false, | ||
initObject.isFixed, | ||
initObject.isThousandSeparator, | ||
@@ -649,44 +697,44 @@ initObject.thousandSeparator, | ||
angular.module(moduleName,[]) | ||
.provider('dynamicNumberStrategy', function() { | ||
var strategies = {}; | ||
this.addStrategy = function(name, strategy){ | ||
strategies[name]=strategy; | ||
}; | ||
this.getStrategy = function(name) { | ||
return strategies[name]; | ||
}; | ||
this.$get = function(){ | ||
return { | ||
getStrategy: function(name) { | ||
return strategies[name]; | ||
.provider('dynamicNumberStrategy', function() { | ||
var strategies = {}; | ||
this.addStrategy = function(name, strategy){ | ||
strategies[name]=strategy; | ||
}; | ||
this.getStrategy = function(name) { | ||
return strategies[name]; | ||
}; | ||
this.$get = function(){ | ||
return { | ||
getStrategy: function(name) { | ||
return strategies[name]; | ||
} | ||
}; | ||
}; | ||
}) | ||
.filter('awnum', ['dynamicNumberStrategy', function(dynamicNumberStrategy) { | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand, numThousandSep, numPrepend, numAppend) { | ||
var strategy = {}; | ||
var fractionPart; | ||
if(angular.isString(numFract)) { | ||
strategy = dynamicNumberStrategy.getStrategy(numFract); | ||
numFract = strategy.numFract; | ||
} | ||
var fractionPart = initFractionPart(numFract, 2); | ||
var fractionSeparator = initSeparator(numSep !== undefined ? numSep : strategy.numSep, '.'); | ||
var roundFunction = initRound(numRound !== undefined ? numRound : strategy.numRound, Math.round); | ||
var isFixed = initIsFixed(numFixed !== undefined ? numFixed : strategy.numFixed, false); | ||
var isThousandSeparator = initIsThousand(numThousand !== undefined ? numThousand : strategy.numThousand, false); | ||
var thousandSeparator = initThousandSeparator(numThousandSep !== undefined ? numThousandSep : strategy.numThousandSep, fractionSeparator, fractionSeparator==='.'?',':'.'); | ||
var prepend = initNumAppendPrepend(numPrepend !== undefined ? numPrepend : strategy.numPrepend); | ||
var append = initNumAppendPrepend(numAppend !== undefined ? numAppend : strategy.numAppend); | ||
var filteredValue = filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator, thousandSeparator, prepend, append); | ||
if(filteredValue === '') { | ||
return '0'; | ||
} | ||
return filteredValue; | ||
}; | ||
}; | ||
}) | ||
.filter('awnum', ['dynamicNumberStrategy', function(dynamicNumberStrategy) { | ||
return function(value, numFract, numSep, numRound, numFixed, numThousand, numThousandSep, numPrepend, numAppend) { | ||
var strategy = {}; | ||
var fractionPart; | ||
if(angular.isString(numFract)) { | ||
strategy = dynamicNumberStrategy.getStrategy(numFract); | ||
numFract = strategy.numFract; | ||
} | ||
var fractionPart = initFractionPart(numFract, 2); | ||
var fractionSeparator = initSeparator(numSep !== undefined ? numSep : strategy.numSep, '.'); | ||
var roundFunction = initRound(numRound !== undefined ? numRound : strategy.numRound, Math.round); | ||
var isFixed = initIsFixed(numFixed !== undefined ? numFixed : strategy.numFixed, false); | ||
var isThousandSeparator = initIsThousand(numThousand !== undefined ? numThousand : strategy.numThousand, false); | ||
var thousandSeparator = initThousandSeparator(numThousandSep !== undefined ? numThousandSep : strategy.numThousandSep, fractionSeparator, fractionSeparator==='.'?',':'.'); | ||
var prepend = initNumAppendPrepend(numPrepend !== undefined ? numPrepend : strategy.numPrepend); | ||
var append = initNumAppendPrepend(numAppend !== undefined ? numAppend : strategy.numAppend); | ||
var filteredValue = filterModelValue(value, fractionPart, fractionSeparator, roundFunction, isFixed, isThousandSeparator, thousandSeparator, prepend, append); | ||
if(filteredValue === '') { | ||
return '0'; | ||
} | ||
return filteredValue; | ||
}; | ||
}]) | ||
.directive('awnum', ['dynamicNumberStrategy',dynamicNumberDirective]); | ||
}]) | ||
.directive('awnum', ['dynamicNumberStrategy',dynamicNumberDirective]); | ||
return moduleName; | ||
})); |
@@ -1,1 +0,1 @@ | ||
!function(n,e){"use strict";if("function"==typeof define&&define.amd)define(["angular"],e);else{if("undefined"==typeof module||"object"!=typeof module.exports)return e(n.angular);module.exports=e(require("angular"))}}(window,function(n){"use strict";function e(n,e,t,u){if(void 0===n||null===n||""===n)return 0;var a="";return a=","===e?String(n).replace(".",","):String(n),r(a,t,u)}function t(n,e,t){return","===e?String(n).replace(/['\.\s]/g,"").replace(",","."):"."===e?String(n).replace(/[',\s]/g,""):void 0}function r(n,e,t){var r=n;return t&&(r+=t),e&&(r=/^\-.+/.test(r)?r.replace("-","-"+e):/^\-/.test(r)?r:e+r),r}function u(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function a(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function o(n,e){return","===n?",":"."===n?".":e}function i(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function c(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function d(n,e){return"floor"===n?Math.floor:"ceil"===n?Math.ceil:"round"===n?Math.round:e}function m(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function p(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function s(n,e,t){if(!n)return t;var r;return r="."===e?new RegExp("^[',\\s]$"):new RegExp("^['\\.\\s]$"),r.test(n)?n:t}function f(n){var e=new RegExp("[^\\d,\\.\\s\\-]{1}");return e.test(n)?n:null}function l(n,e,t,r,u){var a="-?";r===!1&&u===!0?a="-":r===!0&&u===!1&&(a="");var o="[0-9]{0,"+n+"}";0===n&&(o="0");var i="(\\"+t+"([0-9]){0,"+e+"})";return 0===e&&(i=""),new RegExp("^"+a+o+i+"?$")}function g(n){return String(n).replace(/^0+/g,"").replace(/^-0(\d+)/g,"-$1").replace(new RegExp("^-([\\.,\\s])","g"),"-0$1").replace(new RegExp("^[\\.,\\s]","g"),"0$&")}function v(n,e,t){var r=n;return e&&(r=r.replace(new RegExp("[\\"+e+"]","g"),"")),t&&(r=r.replace(new RegExp("[\\"+t+"]","g"),"")),r}function S(n,e){return"."===e?String(n).replace(/\./g,""):","===e?String(n).replace(/,/g,""):String(n).replace(new RegExp("['\\s]","g"),"")}function h(n,e,t){return n=String(n).split(e),n[0]=n[0].replace(/\B(?=(\d{3})+(?!\d))/g,t),n.join(e)}function w(n,e,t,u,a,o){o&&(a.enable=!1),n.$setViewValue(r(String(e),t,u)),n.$render()}function $(n,t,r,u,a,o,i,c,d){if(""===n||void 0===n||null===n)return"";if(n=Number(n),!isNaN(n)&&isFinite(n)){var m=Math.pow(10,t);return n=a?e((u(n*m)/m).toFixed(t),r,c,d):e(String(u(n*m)/m),r,c,d),o&&(n=h(n,r,i)),n}return a?0..toFixed(t):"0"}function N(n){var e=0;if(document.selection){n.focus();var t=document.selection.createRange();t.moveStart("character",-n.value.length),e=t.text.length}else(n.selectionStart||"0"==n.selectionStart)&&(e="backward"==n.selectionDirection?n.selectionStart:n.selectionEnd);return e}function R(n,e){if(null!==n)if(n.createTextRange){var t=n.createTextRange();t.move("character",e),t.select()}else n.selectionStart?(n.focus(),n.setSelectionRange(e,e)):n.focus()}function x(n,e,t){for(var r=0,u=0,a=0;a<n.length;a++)if(n[a]!==e){if(r++,r>=t)break}else u++;return u}function T(n){return Number(n)}function P(n,e,t){var r={awnum:n.awnum,numInt:n.numInt,numFract:n.numFract,numSep:n.numSep,numPos:n.numPos,numNeg:n.numNeg,numRound:n.numRound,numThousand:n.numThousand,numThousandSep:n.numThousandSep,numPrepend:n.numPrepend,numAppend:n.numAppend};return e&&(r[e]=t),r}function b(n,e,t,r,m){var g={};n.awnum&&(g=m.getStrategy(n.awnum));var v=u(void 0!==n.numInt?n.numInt:g.numInt,6),S=a(void 0!==n.numFract?n.numFract:g.numFract,2),h=o(void 0!==n.numSep?n.numSep:g.numSep,"."),w=i(void 0!==n.numPos?n.numPos:g.numPos,!0),$=c(void 0!==n.numNeg?n.numNeg:g.numNeg,!0),N=d(void 0!==n.numRound?n.numRound:g.numRound,Math.round),R=p(void 0!==n.numThousand?n.numThousand:g.numThousand,!1),x=s(void 0!==n.numThousandSep?n.numThousandSep:g.numThousandSep,h,"."===h?",":"."),T=f(void 0!==n.numPrepend?n.numPrepend:g.numPrepend),P=f(void 0!==n.numAppend?n.numAppend:g.numAppend);if(w===!1&&$===!1)throw new Error("Number is set to not be positive and not be negative. Change num_pos attr or/and num_neg attr to true");var b=l(v,S,h,w,$);return{element:e,attrs:t,ngModelController:r,viewRegexTest:b,integerPart:v,fractionPart:S,fractionSeparator:h,isPositiveNumber:w,isNegativeNumber:$,roundFunction:N,isThousandSeparator:R,thousandSeparator:x,prepend:T,append:P}}function y(n,r,u){var a=r.element,o=(r.attrs,r.ngModelController),i=r.viewRegexTest,c=(r.integerPart,r.fractionPart),d=r.fractionSeparator,m=r.isPositiveNumber,p=r.isNegativeNumber,s=(r.roundFunction,r.isThousandSeparator),f=r.thousandSeparator,l=r.prepend,$=r.append,T=String(n);if(I){I=!1;var P=new RegExp("[^"+(p?"-":"")+d+f+"0-9]+","g");T=T.replace(P,""),P=new RegExp("^["+d+f+"]"),T=T.replace(P,""),P=new RegExp("["+d+f+"]([0-9]{"+c+"})$"),T=T.replace(P,d+"$1")}if(T=v(T,l,$),new RegExp("^[.,"+f+"]{2,}").test(T))return w(o,0,l,$,u),0;var b=N(a[0]);l&&b--;var y=T.slice(0,b);y=S(y,f),T=S(T,f),y=g(y);var F=T;if(T=g(T),T==="0"+d&&F===d&&m)return c?(w(o,"0"+d,l,$,u,!0),R(a[0],2),0):(w(o,"",l,$,u),0);if(""===T&&"0"===String(n).charAt(0))return w(o,0,l,$),0;if(void 0===T||""===T)return 0;if("-"===T)return m&&!p?w(o,"",l,$,u):w(o,"-",l,$,u),0;if(i.test(T)===!1){var E=e(o.$modelValue,d,l,$);return s&&(E=h(E,d,f)),w(o,E,l,$,u),R(a[0],b-1),o.$modelValue}var A=0,M=y.length;return s&&(T=h(T,d,f),A=x(T,f,M)),l&&(A++,new RegExp("^(\\-\\d)$").test(T)&&(A+=2),new RegExp("^(\\d)$").test(T)&&A++),w(o,T,l,$,u),R(a[0],M+A),t(T,d,f)}function F(n,e){n.$setViewValue(""),n.$render(),n.$setViewValue(e),n.$render()}function E(n,e){var t=$(n.$modelValue,e.fractionPart,e.fractionSeparator,e.roundFunction,!1,e.isThousandSeparator,e.thousandSeparator,e.prepend,e.append);F(n,t)}function A(n){return{restrict:"A",require:"?ngModel",scope:{awnum:"@",numInt:"@",numFract:"@",numSep:"@",numPos:"@",numNeg:"@",numRound:"@",numThousand:"@",numThousandSep:"@",numPrepend:"@",numAppend:"@"},link:function(e,t,r,u){if(!t[0]||"INPUT"!==t[0].tagName||"text"!==t[0].type&&"tel"!==t[0].type)return void console.warn("Directive angular-dynamic-number works only for 'input' tag with type = 'text' or type = 'tel'");if(!u)return void console.warn("Directive angular-dynamic-number need ngModel attribute");var a=b(P(e),t,r,u,n);t.on("paste",function(){I=!0}),e.$watch("numInt",function(o,i){i!==o&&(a=b(P(e,"numInt",o),t,r,u,n),E(u,a))}),e.$watch("numFract",function(o,i){i!==o&&(a=b(P(e,"numFract",o),t,r,u,n),E(u,a))}),e.$watch("numSep",function(o,i){i!==o&&(a=b(P(e,"numSep",o),t,r,u,n),E(u,a))}),e.$watch("numPos",function(o,i){i!==o&&(a=b(P(e,"numPos",o),t,r,u,n),E(u,a))}),e.$watch("numNeg",function(o,i){i!==o&&(a=b(P(e,"numNeg",o),t,r,u,n),E(u,a))}),e.$watch("numThousand",function(o,i){i!==o&&(a=b(P(e,"numThousand",o),t,r,u,n),E(u,a))}),e.$watch("numThousandSep",function(o,i){i!==o&&(a=b(P(e,"numThousandSep",o),t,r,u,n),E(u,a))}),e.$watch("numAppend",function(o,i){i!==o&&(a=b(P(e,"numAppend",o),t,r,u,n),E(u,a))}),e.$watch("numPrepend",function(o,i){i!==o&&(a=b(P(e,"numPrepend",o),t,r,u,n),E(u,a))});var o={enable:!0,count:0};u.$parsers.unshift(function(n){return o.enable?(o.count++,T(y(n,a,o))):(o.enable=!0,n)}),u.$formatters.push(function(n){return $(n,a.fractionPart,a.fractionSeparator,a.roundFunction,!1,a.isThousandSeparator,a.thousandSeparator,a.prepend,a.append)})}}}var I=!1,M="dynamicNumber";return n.module(M,[]).provider("dynamicNumberStrategy",function(){var n={};this.addStrategy=function(e,t){n[e]=t},this.getStrategy=function(e){return n[e]},this.$get=function(){return{getStrategy:function(e){return n[e]}}}}).filter("awnum",["dynamicNumberStrategy",function(e){return function(t,r,u,i,c,l,g,v,S){var h,w={};n.isString(r)&&(w=e.getStrategy(r),r=w.numFract);var h=a(r,2),N=o(void 0!==u?u:w.numSep,"."),R=d(void 0!==i?i:w.numRound,Math.round),x=m(void 0!==c?c:w.numFixed,!1),T=p(void 0!==l?l:w.numThousand,!1),P=s(void 0!==g?g:w.numThousandSep,N,"."===N?",":"."),b=f(void 0!==v?v:w.numPrepend),y=f(void 0!==S?S:w.numAppend),F=$(t,h,N,R,x,T,P,b,y);return""===F?"0":F}}]).directive("awnum",["dynamicNumberStrategy",A]),M}); | ||
!function(n,e){"use strict";if("function"==typeof define&&define.amd)define(["angular"],e);else{if("undefined"==typeof module||"object"!=typeof module.exports)return e(n.angular);module.exports=e(require("angular"))}}(window,function(n){"use strict";function e(n,e,t,r){if(void 0===n||null===n||""===n)return 0;var u="";return u=","===e?String(n).replace(".",","):String(n)}function t(n,e,t){return","===e?String(n).replace(/['\.\s]/g,"").replace(",","."):"."===e?String(n).replace(/[',\s]/g,""):void 0}function r(n,e,t){var r=n;return t&&(r+=t),e&&(r=/^\-.+/.test(r)?r.replace("-","-"+e):/^\-/.test(r)?r:e+r),r}function u(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function a(n,e){if(n>=0){var t=parseInt(n,10);if(isNaN(t)===!1&&isFinite(t)&&t>=0)return t}return e}function i(n,e){return","===n?",":"."===n?".":e}function o(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function c(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function d(n,e){return"floor"===n?Math.floor:"ceil"===n?Math.ceil:"round"===n?Math.round:e}function m(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function p(n,e){return"false"===n||n===!1?!1:"true"===n||n===!0?!0:e}function s(n,e,t){if(!n)return t;var r;return r="."===e?new RegExp("^[',\\s]$"):new RegExp("^['\\.\\s]$"),r.test(n)?n:t}function f(n){var e=new RegExp("[^\\d,\\.\\s\\-]{1}");return e.test(n)?n:null}function l(n,e,t,r,u){var a="-?";r===!1&&u===!0?a="-":r===!0&&u===!1&&(a="");var i="[0-9]{0,"+n+"}";0===n&&(i="0");var o="(\\"+t+"([0-9]){0,"+e+"})";return 0===e&&(o=""),new RegExp("^"+a+i+o+"?$")}function g(n){return String(n).replace(/^0+/g,"").replace(/^-0(\d+)/g,"-$1").replace(new RegExp("^-([\\.,\\s])","g"),"-0$1").replace(new RegExp("^[\\.,\\s]","g"),"0$&")}function v(n,e,t){var r=n;return e&&(r=r.replace(new RegExp("[\\"+e+"]","g"),"")),t&&(r=r.replace(new RegExp("[\\"+t+"]","g"),"")),r}function S(n,e){return"."===e?String(n).replace(/\./g,""):","===e?String(n).replace(/,/g,""):String(n).replace(new RegExp("['\\s]","g"),"")}function h(n,e,t){return n=String(n).split(e),n[0]=n[0].replace(/\B(?=(\d{3})+(?!\d))/g,t),n.join(e)}function w(n,e){var t=n;if(e.isFixed){var r=t.split(e.fractionSeparator)[1],u=r&&r.length?r.length:0;0===u&&(t+=e.fractionSeparator);for(var a=u;a<e.fractionPart;a++)t+="0"}return t}function x(n,e,t,u,a){a&&(u.enable=!1);var i=String(e),o=w(i,t),c=r(o,t.prepend,t.append);n.$setViewValue(c),n.$render()}function F(n,t,u,a,i,o,c,d,m){if(""===n||void 0===n||null===n)return"";if(n=Number(n),!isNaN(n)&&isFinite(n)){var p=Math.pow(10,t);return n=i?e((a(n*p)/p).toFixed(t),u,d,m):e(String(a(n*p)/p),u,d,m),n=r(n,d,m),o&&(n=h(n,u,c)),n}return i?0..toFixed(t):"0"}function $(n){var e=0;if(document.selection){n.focus();var t=document.selection.createRange();t.moveStart("character",-n.value.length),e=t.text.length}else(n.selectionStart||"0"==n.selectionStart)&&(e="backward"==n.selectionDirection?n.selectionStart:n.selectionEnd);return e}function N(n,e){if(null!==n)if(n.createTextRange){var t=n.createTextRange();t.move("character",e),t.select()}else n.selectionStart?(n.focus(),n.setSelectionRange(e,e)):n.focus()}function P(n,e,t){for(var r=0,u=0,a=0;a<n.length;a++)if(n[a]!==e){if(r++,r>=t)break}else u++;return u}function R(n,e){var t=n,r=t.split(e.fractionSeparator),u=r[0],a=r[1];return a&&a.length>e.fractionPart&&(a=a.slice(0,e.fractionPart),t=[u,a].join(e.fractionSeparator)),t}function T(n){return Number(n)}function b(n,e,t){var r={awnum:n.awnum,numInt:n.numInt,numFract:n.numFract,numSep:n.numSep,numPos:n.numPos,numNeg:n.numNeg,numRound:n.numRound,numThousand:n.numThousand,numThousandSep:n.numThousandSep,numPrepend:n.numPrepend,numAppend:n.numAppend,numFixed:n.numFixed};return e&&(r[e]=t),r}function y(n,e,t,r,g){var v={};n.awnum&&(v=g.getStrategy(n.awnum));var S=u(void 0!==n.numInt?n.numInt:v.numInt,6),h=a(void 0!==n.numFract?n.numFract:v.numFract,2),w=i(void 0!==n.numSep?n.numSep:v.numSep,"."),x=o(void 0!==n.numPos?n.numPos:v.numPos,!0),F=c(void 0!==n.numNeg?n.numNeg:v.numNeg,!0),$=d(void 0!==n.numRound?n.numRound:v.numRound,Math.round),N=p(void 0!==n.numThousand?n.numThousand:v.numThousand,!1),P=s(void 0!==n.numThousandSep?n.numThousandSep:v.numThousandSep,w,"."===w?",":"."),R=f(void 0!==n.numPrepend?n.numPrepend:v.numPrepend),T=f(void 0!==n.numAppend?n.numAppend:v.numAppend),b=m(void 0!==n.numFixed?n.numFixed:v.numFixed,!1);if(x===!1&&F===!1)throw new Error("Number is set to not be positive and not be negative. Change num_pos attr or/and num_neg attr to true");var y=l(S,h,w,x,F);return{element:e,attrs:t,ngModelController:r,viewRegexTest:y,integerPart:S,fractionPart:h,fractionSeparator:w,isPositiveNumber:x,isNegativeNumber:F,roundFunction:$,isThousandSeparator:N,thousandSeparator:P,prepend:R,append:T,isFixed:b}}function E(n,r,u){var a=r.element,i=(r.attrs,r.ngModelController),o=r.viewRegexTest,c=(r.integerPart,r.fractionPart),d=r.fractionSeparator,m=r.isPositiveNumber,p=r.isNegativeNumber,s=(r.roundFunction,r.isThousandSeparator),f=r.thousandSeparator,l=r.prepend,w=r.append,F=(r.isFixed,String(n));if(V){V=!1;var T=new RegExp("[^"+(p?"-":"")+d+f+"0-9]+","g");F=F.replace(T,""),T=new RegExp("^["+d+f+"]"),F=F.replace(T,""),T=new RegExp("["+d+f+"]([0-9]{"+c+"})$"),F=F.replace(T,d+"$1")}if(F=v(F,l,w),new RegExp("^[.,"+f+"]{2,}").test(F))return x(i,0,r,u),0;var b=$(a[0]);l&&b--;var y=F.slice(0,b);y=S(y,f),F=S(F,f),y=g(y);var E=F;if(F=g(F),F==="0"+d&&E===d&&m)return c?(x(i,"0"+d,r,u,!0),N(a[0],2),0):(x(i,"",r,u),0);if(""===F&&"0"===String(n).charAt(0))return x(i,0,r),0;if(void 0===F||""===F)return 0;if("-"===F)return m&&!p?x(i,"",r,u):x(i,"-",r,u),0;if(F=R(F,r),o.test(F)===!1){var A=e(i.$modelValue,d,r);return s&&(A=h(A,d,f)),x(i,A,r,u),N(a[0],b-1),i.$modelValue}var I=0,M=y.length;return s&&(F=h(F,d,f),I=P(F,f,M)),l&&(I++,new RegExp("^(\\-\\d)$").test(F)&&(I+=2),new RegExp("^(\\d)$").test(F)&&I++),x(i,F,r,u),N(a[0],M+I),t(F,d,f)}function A(n,e){n.$setViewValue(""),n.$render(),n.$setViewValue(e),n.$render()}function I(n,e){var t=F(n.$modelValue,e.fractionPart,e.fractionSeparator,e.roundFunction,e.isFixed,e.isThousandSeparator,e.thousandSeparator,e.prepend,e.append);A(n,t)}function M(n){return{restrict:"A",require:"?ngModel",scope:{awnum:"@",numInt:"@",numFract:"@",numSep:"@",numPos:"@",numNeg:"@",numRound:"@",numThousand:"@",numThousandSep:"@",numPrepend:"@",numAppend:"@",numFixed:"@"},link:function(e,t,r,u){if(!t[0]||"INPUT"!==t[0].tagName||"text"!==t[0].type&&"tel"!==t[0].type)return void console.warn("Directive angular-dynamic-number works only for 'input' tag with type = 'text' or type = 'tel'");if(!u)return void console.warn("Directive angular-dynamic-number need ngModel attribute");var a=y(b(e),t,r,u,n);t.on("paste",function(){V=!0}),e.$watch("numInt",function(i,o){o!==i&&(a=y(b(e,"numInt",i),t,r,u,n),I(u,a))}),e.$watch("numFract",function(i,o){o!==i&&(a=y(b(e,"numFract",i),t,r,u,n),I(u,a))}),e.$watch("numSep",function(i,o){o!==i&&(a=y(b(e,"numSep",i),t,r,u,n),I(u,a))}),e.$watch("numPos",function(i,o){o!==i&&(a=y(b(e,"numPos",i),t,r,u,n),I(u,a))}),e.$watch("numNeg",function(i,o){o!==i&&(a=y(b(e,"numNeg",i),t,r,u,n),I(u,a))}),e.$watch("numThousand",function(i,o){o!==i&&(a=y(b(e,"numThousand",i),t,r,u,n),I(u,a))}),e.$watch("numThousandSep",function(i,o){o!==i&&(a=y(b(e,"numThousandSep",i),t,r,u,n),I(u,a))}),e.$watch("numAppend",function(i,o){o!==i&&(a=y(b(e,"numAppend",i),t,r,u,n),I(u,a))}),e.$watch("numPrepend",function(i,o){o!==i&&(a=y(b(e,"numPrepend",i),t,r,u,n),I(u,a))}),e.$watch("numFixed",function(i,o){o!==i&&(a=y(b(e,"numFixed",i),t,r,u,n),I(u,a))});var i={enable:!0,count:0};u.$parsers.unshift(function(n){return i.enable?(i.count++,T(E(n,a,i))):(i.enable=!0,n)}),u.$formatters.push(function(n){return F(n,a.fractionPart,a.fractionSeparator,a.roundFunction,a.isFixed,a.isThousandSeparator,a.thousandSeparator,a.prepend,a.append)})}}}var V=!1,k="dynamicNumber";return n.module(k,[]).provider("dynamicNumberStrategy",function(){var n={};this.addStrategy=function(e,t){n[e]=t},this.getStrategy=function(e){return n[e]},this.$get=function(){return{getStrategy:function(e){return n[e]}}}}).filter("awnum",["dynamicNumberStrategy",function(e){return function(t,r,u,o,c,l,g,v,S){var h,w={};n.isString(r)&&(w=e.getStrategy(r),r=w.numFract);var h=a(r,2),x=i(void 0!==u?u:w.numSep,"."),$=d(void 0!==o?o:w.numRound,Math.round),N=m(void 0!==c?c:w.numFixed,!1),P=p(void 0!==l?l:w.numThousand,!1),R=s(void 0!==g?g:w.numThousandSep,x,"."===x?",":"."),T=f(void 0!==v?v:w.numPrepend),b=f(void 0!==S?S:w.numAppend),y=F(t,h,x,$,N,P,R,T,b);return""===y?"0":y}}]).directive("awnum",["dynamicNumberStrategy",M]),k}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
89603
719
252