Updated amount directive.

This commit is contained in:
Brendon Duncan 2018-08-01 14:29:37 +12:00
commit 65ac476aaa
2 changed files with 38 additions and 31 deletions

View file

@ -23,7 +23,7 @@ angular.module('bitcoincom.directives')
}, },
templateUrl: 'views/includes/amount.html', templateUrl: 'views/includes/amount.html',
controller: ['$scope', function($scope) { controller: ['$scope', function($scope) {
if (typeof $scope.sizeEqual == 'undefined') $scope.sizeEqual = false; $scope.displaySizeEqual = typeof $scope.sizeEqual == 'undefined' ? false : true;
var decimalPlaces = { var decimalPlaces = {
'0': ['BIF', 'CLP', 'DJF', 'GNF', 'ILS', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'], '0': ['BIF', 'CLP', 'DJF', 'GNF', 'ILS', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'],
@ -48,27 +48,28 @@ angular.module('bitcoincom.directives')
return '2'; return '2';
}; };
switch (getDecimalPlaces($scope.currency)) { var formatNumbers = function(currency, value) {
switch (getDecimalPlaces(currency)) {
case '0': case '0':
var valueFormatted = numberWithCommas(Math.round(parseFloat($scope.value))); var valueFormatted = numberWithCommas(Math.round(parseFloat(value)));
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
break; break;
case '2': case '2':
var valueProcessing = parseFloat(parseFloat($scope.value).toFixed(2)); var valueProcessing = parseFloat(parseFloat(value).toFixed(2));
var valueFormatted = numberWithCommas(valueProcessing); var valueFormatted = numberWithCommas(valueProcessing);
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
break; break;
case '3': case '3':
var valueProcessing = parseFloat(parseFloat($scope.value).toFixed(3)); var valueProcessing = parseFloat(parseFloat(value).toFixed(3));
var valueFormatted = numberWithCommas(valueProcessing); var valueFormatted = numberWithCommas(valueProcessing);
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
break; break;
case '8': case '8':
var valueFormatted = parseFloat($scope.value).toFixed(8); var valueFormatted = parseFloat(value).toFixed(8);
if (parseFloat($scope.value) == 0) { if (parseFloat(value) == 0) {
buildAmount('0', '', ''); buildAmount('0', '', '');
} else { } else {
buildAmount(valueFormatted, '', ''); buildAmount(valueFormatted, '', '');
@ -79,6 +80,12 @@ angular.module('bitcoincom.directives')
} }
break; break;
} }
}
formatNumbers($scope.currency, $scope.value);
$scope.$watchGroup(['currency', 'value'], function() {
formatNumbers($scope.currency, $scope.value);
});
}] }]
}; };
} }

View file

@ -1,4 +1,4 @@
<div class="amount" <div class="amount"
ng-class="{ 'size-equal': sizeEqual }"> ng-class="{ 'size-equal': displaySizeEqual }">
<span ng-if="start.length > 0" class="start">{{start}}</span><span ng-if="middle.length > 0" class="middle">{{middle}}</span><span ng-if="end.length > 0" class="end">{{end}}</span><span ng-if="currency.length > 0" class="currency">{{currency}}</span> <span ng-if="start.length > 0" class="start">{{start}}</span><span ng-if="middle.length > 0" class="middle">{{middle}}</span><span ng-if="end.length > 0" class="end">{{end}}</span><span ng-if="currency.length > 0" class="currency">{{currency}}</span>
</div> </div>