2018-08-01 16:59:13 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @desc amount directive that can be used to display formatted financial values
|
|
|
|
|
* size-equal attribute is optional, defaults to false.
|
|
|
|
|
* @example fee = {
|
|
|
|
|
* value: 12.49382901,
|
|
|
|
|
* currency: 'BCH'
|
|
|
|
|
* }
|
|
|
|
|
* @example <formatted-amount value="fee.value" currency="fee.currency"></formatted-amount>
|
|
|
|
|
* @example <formatted-amount value="fee.value" currency="fee.currency" size-equal="true"></formatted-amount>
|
|
|
|
|
*/
|
|
|
|
|
angular.module('bitcoincom.directives')
|
|
|
|
|
.directive('formattedAmount', function(configService, uxLanguage) {
|
|
|
|
|
return {
|
|
|
|
|
restrict: 'E',
|
|
|
|
|
scope: {
|
2018-08-07 16:11:47 +02:00
|
|
|
value: '@',
|
|
|
|
|
currency: '@',
|
|
|
|
|
sizeEqual: '@'
|
2018-08-01 16:59:13 +08:00
|
|
|
},
|
|
|
|
|
templateUrl: 'views/includes/formatted-amount.html',
|
|
|
|
|
controller: function($scope, $timeout) {
|
2018-08-07 16:11:47 +02:00
|
|
|
if (!$scope.currency && $scope.value) { // If there is no currency available..
|
|
|
|
|
// Try to extract currency from value..
|
|
|
|
|
var currencySplit = $scope.value.split(" ");
|
|
|
|
|
if (currencySplit.length === 2) {
|
|
|
|
|
$scope.value = currencySplit[0];
|
|
|
|
|
$scope.currency = currencySplit[1];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-01 16:59:13 +08:00
|
|
|
$scope.displaySizeEqual = !!$scope.sizeEqual;
|
|
|
|
|
|
|
|
|
|
configService.whenAvailable(function(config) {
|
2018-08-07 16:11:47 +02:00
|
|
|
console.log("WAIT!!");
|
2018-08-01 16:59:13 +08:00
|
|
|
$timeout(function() {
|
2018-08-07 16:11:47 +02:00
|
|
|
console.log("FIRED!!");
|
2018-08-01 16:59:13 +08:00
|
|
|
var decimalPlaces = {
|
|
|
|
|
'0': ['BIF', 'CLP', 'DJF', 'GNF', 'ILS', 'JPY', 'KMF', 'KRW', 'MGA', 'PYG', 'RWF', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF'],
|
|
|
|
|
'3': ['BHD', 'IQD', 'JOD', 'KWD', 'OMR', 'TND'],
|
|
|
|
|
'8': ['BCH', 'BTC']
|
|
|
|
|
};
|
|
|
|
|
var localizeNumbers = function(x, minimumFractionDigits = 0, useGrouping = true) {
|
|
|
|
|
return parseFloat(x).toLocaleString(uxLanguage.getCurrentLanguage(), {
|
|
|
|
|
minimumFractionDigits: minimumFractionDigits,
|
|
|
|
|
useGrouping: useGrouping
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var buildAmount = function(start, middle, end) {
|
|
|
|
|
$scope.start = start;
|
|
|
|
|
$scope.middle = middle;
|
|
|
|
|
$scope.end = end;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var getDecimalPlaces = function(currency) {
|
2018-08-07 16:11:47 +02:00
|
|
|
if (decimalPlaces['0'].indexOf(currency.toUpperCase()) > -1) return '0';
|
|
|
|
|
if (decimalPlaces['3'].indexOf(currency.toUpperCase()) > -1) return '3';
|
|
|
|
|
if (decimalPlaces['8'].indexOf(currency.toUpperCase()) > -1) return '8';
|
2018-08-01 16:59:13 +08:00
|
|
|
return '2';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var formatNumbers = function(currency, value) {
|
|
|
|
|
switch (getDecimalPlaces(currency)) {
|
|
|
|
|
case '0':
|
|
|
|
|
var valueFormatted = localizeNumbers(Math.round(parseFloat(value)));
|
|
|
|
|
buildAmount(valueFormatted, '', '');
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '3':
|
|
|
|
|
var valueProcessing = parseFloat(parseFloat(value).toFixed(3));
|
2018-08-07 16:11:47 +02:00
|
|
|
var valueFormatted = localizeNumbers(valueProcessing, 3);
|
2018-08-01 16:59:13 +08:00
|
|
|
buildAmount(valueFormatted, '', '');
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '8':
|
|
|
|
|
var valueFormatted = parseFloat(value).toFixed(8);
|
|
|
|
|
if (parseFloat(value) == 0) {
|
|
|
|
|
buildAmount('0', '', '');
|
|
|
|
|
} else {
|
|
|
|
|
var valueFormatted = localizeNumbers(valueFormatted, 8);
|
|
|
|
|
var start = valueFormatted.slice(0, -5);
|
|
|
|
|
var middle = valueFormatted.slice(-5, -2);
|
|
|
|
|
var end = valueFormatted.substr(valueFormatted.length - 2);
|
|
|
|
|
buildAmount(start, middle, end);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
var valueProcessing = parseFloat(parseFloat(value).toFixed(2));
|
2018-08-07 16:11:47 +02:00
|
|
|
var valueFormatted = localizeNumbers(valueProcessing, 2);
|
2018-08-01 16:59:13 +08:00
|
|
|
buildAmount(valueFormatted, '', '');
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-08-07 16:11:47 +02:00
|
|
|
};
|
2018-08-01 16:59:13 +08:00
|
|
|
|
|
|
|
|
formatNumbers($scope.currency, $scope.value);
|
|
|
|
|
$scope.$watchGroup(['currency', 'value'], function() {
|
|
|
|
|
formatNumbers($scope.currency, $scope.value);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
);
|