Wallet/src/js/controllers/send2.js

126 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-07-07 16:55:04 -03:00
'use strict';
2016-07-09 15:36:49 -03:00
angular.module('copayApp.controllers').controller('send2Controller', function($scope, lodash, configService, go, rateService) {
var unitToSatoshi;
var satToUnit;
var unitDecimals;
$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-07-09 15:36:49 -03:00
unitToSatoshi = config.unitToSatoshi;
satToUnit = 1 / unitToSatoshi;
unitDecimals = config.unitDecimals;
2016-07-07 16:55:04 -03:00
$scope.showAlternative = false;
2016-07-09 15:36:49 -03:00
resetAmount();
};
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
$scope.toggleAlternative = function() {
$scope.showAlternative = !$scope.showAlternative;
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
if ($scope.showAlternative) {
$scope.alternativeAmount = $scope.amountResult;
$scope.alternativeResult = isOperator(lodash.last($scope.amount)) ? evaluate($scope.amount.slice(0, -1)) : evaluate($scope.amount);
} else {
$scope.amount = $scope.alternativeResult;
$scope.amountResult = isOperator(lodash.last($scope.alternativeAmount)) ? evaluate($scope.alternativeAmount.slice(0, -1)) : evaluate($scope.alternativeAmount);
}
};
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
$scope.pushDigit = function(digit) {
var amount = $scope.showAlternative ? $scope.alternativeAmount : $scope.amount;
if (amount.toString().length >= 10) return;
if (amount == 0 && digit == 0) return;
var val = amount ? amount + digit : digit;
processAmount(val);
};
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
$scope.pushOperator = function(operator) {
if ($scope.showAlternative) {
if (!$scope.alternativeAmount || $scope.alternativeAmount.length == 0) return;
$scope.alternativeAmount = _pushOperator($scope.alternativeAmount);
} else {
2016-07-07 16:55:04 -03:00
if (!$scope.amount || $scope.amount.length == 0) return;
2016-07-09 15:36:49 -03:00
$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) {
var regex = /[\/\-\+\*]/;
var match = regex.exec(val);
if (match) return true;
return false;
};
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
$scope.removeDigit = function() {
var amount = $scope.showAlternative ? $scope.alternativeAmount.toString() : $scope.amount.toString();
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
if (amount && amount.length == 0) {
resetAmount();
return;
}
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
amount = amount.slice(0, -1);
if ($scope.showAlternative)
$scope.alternativeAmount = amount;
else
$scope.amount = amount;
processAmount(amount);
};
function resetAmount() {
$scope.amount = $scope.alternativeAmount = $scope.alternativeResult = $scope.amountResult = 0;
};
function processAmount(val) {
if (!val) {
resetAmount();
return;
}
if ($scope.showAlternative) {
if ($scope.alternativeAmount == 0 && val == '.') {
$scope.alternativeAmount += val;
}
} else {
2016-07-07 16:55:04 -03:00
if ($scope.amount == 0 && val == '.') {
$scope.amount += val;
}
2016-07-09 15:36:49 -03:00
}
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
var result = evaluate(val);
2016-07-07 16:55:04 -03:00
2016-07-09 15:36:49 -03:00
if (lodash.isNumber(result)) {
$scope.amount = $scope.alternativeAmount = val;
$scope.alternativeResult = parseFloat((rateService.fromFiat(result, $scope.alternativeIsoCode) * satToUnit).toFixed(unitDecimals), 10);
$scope.amountResult = parseFloat((rateService.toFiat(result * unitToSatoshi, $scope.alternativeIsoCode)).toFixed(2), 10);
}
};
function evaluate(val) {
var result;
try {
result = eval(val);
} catch (e) {
return null;
}
return result;
};
$scope.close = function() {
go.walletHome();
};
});