Wallet/src/js/controllers/modals/inputAmount.js

150 lines
4.2 KiB
JavaScript
Raw Normal View History

2016-07-07 16:55:04 -03:00
'use strict';
2016-07-18 14:50:39 -03:00
angular.module('copayApp.controllers').controller('inputAmountController', function($scope, 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-07-09 15:36:49 -03:00
unitToSatoshi = config.unitToSatoshi;
satToUnit = 1 / unitToSatoshi;
unitDecimals = config.unitDecimals;
2016-07-18 14:50:39 -03:00
console.log($scope.showAlternativeAmount);
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() {
2016-07-18 14:50:39 -03:00
$scope.showAlternativeAmount = !$scope.showAlternativeAmount;
2016-07-15 15:36:00 -03:00
var amount;
2016-07-07 16:55:04 -03:00
2016-07-18 14:50:39 -03:00
if ($scope.showAlternativeAmount) {
2016-07-09 15:36:49 -03:00
$scope.alternativeAmount = $scope.amountResult;
2016-07-15 15:36:00 -03:00
amount = processFormat($scope.amount);
$scope.alternativeResult = isOperator(lodash.last(amount)) ? evaluate(amount.slice(0, -1)) : evaluate(amount);
2016-07-09 15:36:49 -03:00
} else {
$scope.amount = $scope.alternativeResult;
2016-07-15 15:36:00 -03:00
amount = processFormat($scope.alternativeAmount);
$scope.amountResult = isOperator(lodash.last(amount)) ? evaluate(amount.slice(0, -1)) : evaluate(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.pushDigit = function(digit) {
2016-07-18 14:50:39 -03:00
var amount = $scope.showAlternativeAmount ? $scope.alternativeAmount : $scope.amount;
2016-07-09 15:36:49 -03:00
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) {
2016-07-18 14:50:39 -03:00
if ($scope.showAlternativeAmount) {
2016-07-09 15:36:49 -03:00
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) {
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-07-09 15:36:49 -03:00
$scope.removeDigit = function() {
2016-07-18 14:50:39 -03:00
var amount = $scope.showAlternativeAmount ? $scope.alternativeAmount : $scope.amount;
2016-07-07 16:55:04 -03:00
2016-07-15 15:36:00 -03:00
if (amount && amount.toString().length == 0) {
2016-07-09 15:36:49 -03:00
resetAmount();
return;
}
2016-07-07 16:55:04 -03:00
2016-07-18 14:50:39 -03:00
amount = amount.toString().slice(0, -1);
2016-07-09 15:36:49 -03:00
2016-07-18 14:50:39 -03:00
if ($scope.showAlternativeAmount)
2016-07-09 15:36:49 -03:00
$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;
}
2016-07-18 14:50:39 -03:00
if ($scope.showAlternativeAmount) {
2016-07-09 15:36:49 -03:00
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-15 15:36:00 -03:00
var formatedValue = processFormat(val);
var result = evaluate(formatedValue);
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;
};
2016-07-15 15:36:00 -03:00
function processFormat(val) {
return val.toString().replace('x', '*');
};
2016-07-18 14:50:39 -03:00
$scope.save = function() {
if ($scope.showAlternativeAmount) {
if ($scope.alternativeResult == 0) return;
self.showAlternativeAmount = true;
self.setForm(null, $scope.alternativeResult, null);
} else {
if ($scope.amountResult == 0) return;
self.showAlternativeAmount = false;
self.setForm(null, $scope.amountResult, null);
}
$scope.cancel();
};
$scope.cancel = function() {
$scope.inputAmountModal.hide();
2016-07-09 15:36:49 -03:00
};
});