2016-07-07 16:55:04 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-07-19 17:10:40 -03:00
|
|
|
angular.module('copayApp.controllers').controller('inputAmountController', function($rootScope, $scope, platformInfo, lodash, configService, go, rateService) {
|
2016-07-09 15:36:49 -03:00
|
|
|
var unitToSatoshi;
|
|
|
|
|
var satToUnit;
|
|
|
|
|
var unitDecimals;
|
2016-07-18 14:50:39 -03:00
|
|
|
var self = $scope.self;
|
2016-07-09 15:36:49 -03:00
|
|
|
|
|
|
|
|
$scope.init = function() {
|
2016-07-07 16:55:04 -03:00
|
|
|
var config = configService.getSync().wallet.settings;
|
|
|
|
|
$scope.unitName = config.unitName;
|
|
|
|
|
$scope.alternativeIsoCode = config.alternativeIsoCode;
|
2016-08-01 17:09:03 -03:00
|
|
|
$scope.specificAmount = $scope.specificAlternativeAmount = '';
|
2016-07-19 17:10:40 -03:00
|
|
|
$scope.isCordova = platformInfo.isCordova;
|
2016-07-09 15:36:49 -03:00
|
|
|
unitToSatoshi = config.unitToSatoshi;
|
|
|
|
|
satToUnit = 1 / unitToSatoshi;
|
|
|
|
|
unitDecimals = config.unitDecimals;
|
|
|
|
|
resetAmount();
|
|
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-07-19 17:10:40 -03:00
|
|
|
$scope.shareAddress = function(uri) {
|
|
|
|
|
if ($scope.isCordova) {
|
|
|
|
|
window.plugins.socialsharing.share(uri, null, null, null);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
$scope.toggleAlternative = function() {
|
2016-07-18 14:50:39 -03:00
|
|
|
$scope.showAlternativeAmount = !$scope.showAlternativeAmount;
|
2016-08-01 18:14:57 -03:00
|
|
|
var decimals = $scope.showAlternativeAmount ? 2 : unitDecimals;
|
|
|
|
|
$scope.globalResult = evaluate(format($scope.amount)).toFixed(decimals);
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-08-01 17:35:31 -03:00
|
|
|
function checkFontSize() {
|
|
|
|
|
if ($scope.amount && $scope.amount.length >= 13) $scope.smallFont = true;
|
|
|
|
|
else $scope.smallFont = false;
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
$scope.pushDigit = function(digit) {
|
2016-08-01 17:35:31 -03:00
|
|
|
checkFontSize();
|
2016-08-01 17:09:03 -03:00
|
|
|
if ($scope.amount && $scope.amount.length >= 20) return;
|
2016-07-09 15:36:49 -03:00
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
$scope.amount = $scope.amount + digit;
|
|
|
|
|
processAmount($scope.amount);
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
$scope.pushOperator = function(operator) {
|
2016-08-01 17:09:03 -03:00
|
|
|
if (!$scope.amount || $scope.amount.length == 0) return;
|
|
|
|
|
$scope.amount = _pushOperator($scope.amount);
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
function _pushOperator(val) {
|
|
|
|
|
if (!isOperator(lodash.last(val))) {
|
|
|
|
|
return val + operator;
|
|
|
|
|
} else {
|
|
|
|
|
return val.slice(0, -1) + operator;
|
2016-07-07 16:55:04 -03:00
|
|
|
}
|
|
|
|
|
};
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
function isOperator(val) {
|
2016-07-15 15:36:00 -03:00
|
|
|
var regex = /[\/\-\+\x\*]/;
|
2016-07-09 15:36:49 -03:00
|
|
|
var match = regex.exec(val);
|
|
|
|
|
if (match) return true;
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
function isExpression(val) {
|
|
|
|
|
var regex = /^\d+(\.?\d?)+$/;
|
|
|
|
|
var match = regex.exec(val);
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
if (match) return false;
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.removeDigit = function() {
|
|
|
|
|
if ($scope.amount.toString().length == 1) {
|
2016-07-09 15:36:49 -03:00
|
|
|
resetAmount();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
$scope.amount = $scope.amount.slice(0, -1);
|
|
|
|
|
processAmount($scope.amount);
|
2016-08-01 17:35:31 -03:00
|
|
|
checkFontSize();
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function resetAmount() {
|
2016-08-01 17:09:03 -03:00
|
|
|
$scope.amount = $scope.alternativeResult = $scope.amountResult = $scope.globalResult = '';
|
2016-08-01 17:35:31 -03:00
|
|
|
checkFontSize();
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function processAmount(val) {
|
|
|
|
|
if (!val) {
|
|
|
|
|
resetAmount();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 12:00:58 -03:00
|
|
|
var formatedValue = format(val);
|
2016-07-15 15:36:00 -03:00
|
|
|
var result = evaluate(formatedValue);
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
if (lodash.isNumber(result)) {
|
2016-08-01 18:14:57 -03:00
|
|
|
if ($scope.showAlternativeAmount)
|
|
|
|
|
$scope.globalResult = isExpression(val) ? '= ' + result.toFixed(2) : '';
|
|
|
|
|
else
|
|
|
|
|
$scope.globalResult = isExpression(val) ? '= ' + result.toFixed(unitDecimals) : '';
|
|
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
$scope.amountResult = toFiat(result).toFixed(2);
|
2016-08-01 18:14:57 -03:00
|
|
|
$scope.alternativeResult = fromFiat(result).toFixed(unitDecimals);
|
2016-07-09 15:36:49 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-19 12:00:58 -03:00
|
|
|
function fromFiat(val) {
|
|
|
|
|
return parseFloat((rateService.fromFiat(val, $scope.alternativeIsoCode) * satToUnit).toFixed(unitDecimals), 10);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function toFiat(val) {
|
|
|
|
|
return parseFloat((rateService.toFiat(val * unitToSatoshi, $scope.alternativeIsoCode)).toFixed(2), 10);
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
function evaluate(val) {
|
|
|
|
|
var result;
|
|
|
|
|
try {
|
2016-07-20 16:16:39 -03:00
|
|
|
result = $scope.$eval(val);
|
2016-07-09 15:36:49 -03:00
|
|
|
} catch (e) {
|
2016-08-01 17:09:03 -03:00
|
|
|
return 0;
|
2016-07-09 15:36:49 -03:00
|
|
|
}
|
2016-08-01 17:09:03 -03:00
|
|
|
if (result == 'Infinity' || lodash.isNaN(result)) return 0;
|
2016-07-09 15:36:49 -03:00
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-19 12:00:58 -03:00
|
|
|
function format(val) {
|
2016-07-20 16:26:15 -03:00
|
|
|
var result = val.toString();
|
|
|
|
|
|
|
|
|
|
if (isOperator(lodash.last(val)))
|
|
|
|
|
result = result.slice(0, -1);
|
|
|
|
|
|
|
|
|
|
return result.replace('x', '*');
|
2016-07-15 15:36:00 -03:00
|
|
|
};
|
|
|
|
|
|
2016-07-19 12:00:58 -03:00
|
|
|
$scope.finish = function() {
|
2016-07-19 17:10:40 -03:00
|
|
|
var amount;
|
|
|
|
|
var alternativeAmount;
|
|
|
|
|
|
|
|
|
|
if ($scope.showAlternativeAmount) {
|
|
|
|
|
amount = $scope.alternativeResult;
|
2016-08-01 17:09:03 -03:00
|
|
|
alternativeAmount = evaluate(format($scope.amount));
|
2016-07-19 17:10:40 -03:00
|
|
|
} else {
|
|
|
|
|
amount = evaluate(format($scope.amount));
|
|
|
|
|
alternativeAmount = toFiat(amount);
|
|
|
|
|
}
|
2016-07-18 14:50:39 -03:00
|
|
|
|
2016-07-19 12:00:58 -03:00
|
|
|
if ($scope.address) {
|
2016-07-19 17:10:40 -03:00
|
|
|
var satToBtc = 1 / 100000000;
|
|
|
|
|
var amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
|
|
|
|
|
if ($scope.unitName == 'bits') {
|
|
|
|
|
$scope.specificAmountBtc = (amountSat * satToBtc).toFixed(8);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
$scope.specificAmount = amount;
|
|
|
|
|
$scope.specificAlternativeAmount = alternativeAmount;
|
2016-07-18 14:50:39 -03:00
|
|
|
} else {
|
2016-08-01 17:09:03 -03:00
|
|
|
self.setAmount(amount, alternativeAmount, $scope.showAlternativeAmount);
|
2016-07-19 12:00:58 -03:00
|
|
|
$scope.cancel();
|
2016-07-18 14:50:39 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.cancel = function() {
|
|
|
|
|
$scope.inputAmountModal.hide();
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
|
|
|
|
});
|