Skip to content

Commit

Permalink
feat(ui-percentage-value): add attribute ui-percentage-value to use a…
Browse files Browse the repository at this point in the history
…nd as the same
  • Loading branch information
dvbeato committed May 30, 2015
1 parent 344a931 commit df8f941
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_size = 4
indent_style = tab

5 changes: 5 additions & 0 deletions .gitignore
Expand Up @@ -4,3 +4,8 @@ node_modules
coverage
npm-debug.log
releases

.DS_Store
*.swp
.idea
*.iml
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -67,12 +67,18 @@ bower install --save angular-input-masks
<input type="text" name="field" ng-model="percentage" ui-percentage-mask="4">
```

- The $modelValue is the $viewValue / 100, so $viewValue - 100% = $modelValue - 1

- You can use the same value in $modelValue and $viewValue using ```ui-percentage-value```:

```html
<input type="text" name="field" ng-model="percentage" ui-percentage-mask ui-percentage-value>
```

- Support to the ```min```, ```max``` and ```ui-hide-group-sep``` attributes.

- Internationalized: Used the decimal separator and thousands separator defined in the client browser configuration.

- The $modelValue is the $viewValue / 100, so $viewValue - 100% = $modelValue - 1

### ui-money-mask ###

- Example:
Expand Down
8 changes: 6 additions & 2 deletions demo/index.html
Expand Up @@ -63,8 +63,8 @@ <h2>ui-number-mask</h2>
<br>

<h2>ui-percentage-mask</h2>
<input type="text" name="field5" ng-model="percentageWithDefaultDecimals" ui-percentage-mask> <br>
<span ng-bind="percentageWithDefaultDecimals"></span>
$viewValue - <input type="text" name="field5" ng-model="percentageWithDefaultDecimals" ui-percentage-mask> <br>
$modelValue - <span ng-bind="percentageWithDefaultDecimals"></span>
<br>
<input type="text" name="field6" ng-model="percentageWith4Decimals" ui-percentage-mask="4" min="percentageWithDefaultDecimals" max="115"> <br>
<span ng-bind="percentageWith4Decimals"></span> - {{form.field6.$error}}
Expand All @@ -73,6 +73,10 @@ <h2>ui-percentage-mask</h2>
Decimals: <input type="text" name="field26" ng-model="pdecimals" ui-number-mask=0><br>
<span ng-bind="percentWithDynamicDecimals"></span> - {{form.field25.$error}}
<br>
<h4>ui-percentage-value</h4>
$viewValue - <input type="text" name="field6" ng-model="percentageValue" ui-percentage-mask="4" ui-percentage-value min="percentageWithDefaultDecimals" max="115"> <br>
$modelValue - <span ng-bind="percentageValue"></span>
<br />

<h2>ui-br-cpf-mask</h2>
<i>Same as the deprecated <b>ui-cpf-mask</b></i><br><br>
Expand Down
26 changes: 21 additions & 5 deletions src/global/percentage/percentage.js
@@ -1,6 +1,6 @@
function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks, NumberValidators) {
function preparePercentageToFormatter (value, decimals) {
return PreFormatters.clearDelimitersAndLeadingZeros((parseFloat(value)*100).toFixed(decimals));
function preparePercentageToFormatter (value, decimals, modelMultiplier) {
return PreFormatters.clearDelimitersAndLeadingZeros((parseFloat(value)*modelMultiplier).toFixed(decimals));
}

return {
Expand All @@ -11,15 +11,25 @@ function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks, Nu
thousandsDelimiter = $locale.NUMBER_FORMATS.GROUP_SEP,
decimals = parseInt(attrs.uiPercentageMask);

var modelValue = {
multiplier : 100,
decimalMask: 2
};

if (angular.isDefined(attrs.uiHideGroupSep)){
thousandsDelimiter = '';
}

if (angular.isDefined(attrs.uiPercentageValue)) {
modelValue.multiplier = 1;
modelValue.decimalMask = 0;
}

if(isNaN(decimals)) {
decimals = 2;
}

var numberDecimals = decimals + 2;
var numberDecimals = decimals + modelValue.decimalMask;
var viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter),
modelMask = NumberMasks.modelMask(numberDecimals);

Expand All @@ -28,7 +38,7 @@ function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks, Nu
return value;
}

var valueToFormat = preparePercentageToFormatter(value, decimals);
var valueToFormat = preparePercentageToFormatter(value, decimals, modelValue.multiplier);
return viewMask.apply(valueToFormat) + ' %';
}

Expand Down Expand Up @@ -60,7 +70,13 @@ function PercentageMaskDirective($locale, $parse, PreFormatters, NumberMasks, Nu
if(isNaN(decimals)) {
decimals = 2;
}
numberDecimals = decimals + 2;

if (angular.isDefined(attrs.uiPercentageValue)) {
modelValue.multiplier = 1;
modelValue.decimalMask = 0;
}

numberDecimals = decimals + modelValue.decimalMask;
viewMask = NumberMasks.viewMask(decimals, decimalDelimiter, thousandsDelimiter);
modelMask = NumberMasks.modelMask(numberDecimals);

Expand Down
26 changes: 26 additions & 0 deletions src/global/percentage/percentage.test.js
Expand Up @@ -99,4 +99,30 @@ describe('ui-percentage-mask', function() {
input.val('99990').triggerHandler('input');
expect(model.$valid).toBe(true);
});

it('should format initial model values with percentage value', function() {
var input = TestUtil.compile('<input ng-model="model" ui-percentage-mask ui-percentage-value>', {
model: '1234.5'
});

var model = input.controller('ngModel');
expect(model.$viewValue).toBe('1,234.50 %');
});

it('should allow changing the number of decimals', inject(function($rootScope) {
var input = TestUtil.compile('<input ng-model="model" ui-percentage-mask="decimals" ui-percentage-value>', {
model: '1234.501',
decimals: 2
});

var model = input.controller('ngModel');
expect(model.$viewValue).toBe('1,234.50 %');
$rootScope.decimals = 3;
$rootScope.$digest();
expect(model.$viewValue).toBe('123.450 %');
$rootScope.decimals = 'invalid value';
$rootScope.$digest();
expect(model.$viewValue).toBe('1,234.50 %');
}));

});

0 comments on commit df8f941

Please sign in to comment.