2016-07-07 16:55:04 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-09-17 18:04:54 -03:00
|
|
|
angular.module('copayApp.controllers').controller('amountController', function($rootScope, $scope, $filter, $timeout, $ionicScrollDelegate, $ionicNavBarDelegate, gettextCatalog, platformInfo, lodash, configService, rateService, $stateParams, $window, $state, $log, txFormatService) {
|
2016-08-30 12:52:28 -03:00
|
|
|
$ionicNavBarDelegate.title(gettextCatalog.getString('Enter Amount'));
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
var unitToSatoshi;
|
|
|
|
|
var satToUnit;
|
|
|
|
|
var unitDecimals;
|
2016-08-09 15:28:03 -03:00
|
|
|
var satToBtc;
|
2016-07-18 14:50:39 -03:00
|
|
|
var self = $scope.self;
|
2016-08-08 16:00:26 -03:00
|
|
|
var SMALL_FONT_SIZE_LIMIT = 13;
|
|
|
|
|
var LENGTH_EXPRESSION_LIMIT = 19;
|
2016-07-09 15:36:49 -03:00
|
|
|
|
|
|
|
|
$scope.init = function() {
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
if (!$stateParams.toAddress) {
|
|
|
|
|
$log.error('Bad params at amount')
|
|
|
|
|
throw ('bad params');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var reNr = /^[1234567890\.]$/;
|
|
|
|
|
var reOp = /^[\*\+\-\/]$/;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var disableKeys = angular.element($window).on('keydown', function(e) {
|
|
|
|
|
if (e.which === 8) { // you can add others here inside brackets.
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
$scope.removeDigit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key && e.key.match(reNr))
|
|
|
|
|
$scope.pushDigit(e.key);
|
|
|
|
|
|
|
|
|
|
else if (e.key && e.key.match(reOp))
|
|
|
|
|
$scope.pushOperator(e.key);
|
|
|
|
|
|
|
|
|
|
else if (e.key && e.key == 'Enter')
|
|
|
|
|
$scope.finish();
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 10);
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$scope.$on('$destroy', function() {
|
|
|
|
|
angular.element($window).off('keydown');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$scope.toAddress = $stateParams.toAddress;
|
|
|
|
|
$scope.toName = $stateParams.toName;
|
2016-09-14 10:04:16 -03:00
|
|
|
$scope.toEmail = $stateParams.toEmail;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
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;
|
2016-08-09 15:28:03 -03:00
|
|
|
satToBtc = 1 / 100000000;
|
2016-07-09 15:36:49 -03:00
|
|
|
unitDecimals = config.unitDecimals;
|
2016-08-16 18:38:18 -03:00
|
|
|
|
|
|
|
|
// in SAT ALWAYS
|
|
|
|
|
if ($stateParams.toAmount) {
|
2016-08-30 12:52:28 -03:00
|
|
|
$scope.amount = (($stateParams.toAmount) * satToUnit).toFixed(unitDecimals);
|
2016-08-16 18:38:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2016-08-10 12:08:39 -03:00
|
|
|
processAmount($scope.amount);
|
2016-08-16 18:38:18 -03:00
|
|
|
|
2016-08-05 15:44:30 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
}, 100);
|
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.toggleAlternative = function() {
|
2016-07-18 14:50:39 -03:00
|
|
|
$scope.showAlternativeAmount = !$scope.showAlternativeAmount;
|
2016-08-01 18:28:39 -03:00
|
|
|
|
2016-08-02 12:44:47 -03:00
|
|
|
if ($scope.amount && isExpression($scope.amount)) {
|
|
|
|
|
var amount = evaluate(format($scope.amount));
|
2016-08-08 16:00:26 -03:00
|
|
|
$scope.globalResult = '= ' + processResult(amount);
|
2016-08-01 18:28:39 -03:00
|
|
|
}
|
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() {
|
2016-08-08 16:00:26 -03:00
|
|
|
if ($scope.amount && $scope.amount.length >= SMALL_FONT_SIZE_LIMIT) $scope.smallFont = true;
|
2016-08-01 17:35:31 -03:00
|
|
|
else $scope.smallFont = false;
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-09 15:36:49 -03:00
|
|
|
$scope.pushDigit = function(digit) {
|
2016-08-08 16:00:26 -03:00
|
|
|
if ($scope.amount && $scope.amount.length >= LENGTH_EXPRESSION_LIMIT) return;
|
2016-07-09 15:36:49 -03:00
|
|
|
|
2016-08-02 12:44:47 -03:00
|
|
|
$scope.amount = ($scope.amount + digit).replace('..', '.');
|
2016-08-08 16:00:26 -03:00
|
|
|
checkFontSize();
|
2016-08-01 17:09:03 -03:00
|
|
|
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-16 18:38:18 -03:00
|
|
|
console.log('[amount.js.90:operator:]', operator); //TODO
|
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-08-08 16:00:26 -03:00
|
|
|
return regex.test(val);
|
2016-07-09 15:36:49 -03:00
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-08-01 17:09:03 -03:00
|
|
|
function isExpression(val) {
|
2016-08-02 12:44:47 -03:00
|
|
|
var regex = /^\.?\d+(\.?\d+)?([\/\-\+\*x]\d?\.?\d+)+$/;
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2016-08-08 16:00:26 -03:00
|
|
|
return regex.test(val);
|
2016-08-01 17:09:03 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.removeDigit = function() {
|
|
|
|
|
$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
|
|
|
};
|
|
|
|
|
|
2016-08-05 15:26:27 -03:00
|
|
|
$scope.resetAmount = function() {
|
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) {
|
2016-08-05 15:26:27 -03:00
|
|
|
$scope.resetAmount();
|
2016-07-09 15:36:49 -03:00
|
|
|
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-08 16:00:26 -03:00
|
|
|
$scope.globalResult = isExpression(val) ? '= ' + processResult(result) : '';
|
2016-08-01 18:28:39 -03:00
|
|
|
$scope.amountResult = $filter('formatFiatAmount')(toFiat(result));
|
2016-08-18 11:45:30 -03:00
|
|
|
$scope.alternativeResult = txFormatService.formatAmount(fromFiat(result) * unitToSatoshi, true);
|
2016-07-09 15:36:49 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-08-08 16:00:26 -03:00
|
|
|
function processResult(val) {
|
|
|
|
|
if ($scope.showAlternativeAmount)
|
|
|
|
|
return $filter('formatFiatAmount')(val);
|
|
|
|
|
else
|
2016-08-18 11:45:30 -03:00
|
|
|
return txFormatService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true);
|
2016-08-08 16:00:26 -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-08 16:00:26 -03:00
|
|
|
if (!lodash.isFinite(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-08-10 14:27:46 -03:00
|
|
|
var _amount = evaluate(format($scope.amount));
|
|
|
|
|
var amount = $scope.showAlternativeAmount ? fromFiat(_amount).toFixed(unitDecimals) : _amount.toFixed(unitDecimals);
|
2016-08-09 15:28:03 -03:00
|
|
|
|
2016-09-16 21:01:19 -03:00
|
|
|
$state.transitionTo('tabs.send.confirm', {
|
2016-08-30 12:52:28 -03:00
|
|
|
toAmount: amount * unitToSatoshi,
|
2016-08-16 18:38:18 -03:00
|
|
|
toAddress: $scope.toAddress,
|
|
|
|
|
toName: $scope.toName,
|
2016-09-14 10:04:16 -03:00
|
|
|
toEmail: $scope.toEmail
|
2016-08-16 18:38:18 -03:00
|
|
|
});
|
2016-07-18 14:50:39 -03:00
|
|
|
};
|
2016-07-09 15:36:49 -03:00
|
|
|
});
|