complete evaluation of expressions

This commit is contained in:
Javier 2016-07-09 15:36:49 -03:00
commit 65766c6fe5
4 changed files with 134 additions and 68 deletions

View file

@ -12,7 +12,8 @@
<ul class="no-bullet m0" ng-show="!index.noFocusedWallet">
<li href ui-sref="preferencesAlias">
<!-- <li href ui-sref="preferencesAlias"> -->
<li href ui-sref="send2">
<div class="right text-gray">
{{index.alias||index.walletName}}
<i class="icon-arrow-right3 size-24 right"></i>

View file

@ -1,18 +1,25 @@
<ion-content ng-controller="send2Controller">
<ion-content ng-controller="send2Controller" ng-init=init()>
<ion-header-bar align-title="center" class="tab-bar" ng-style="{'background-color':index.backgroundColor}">
<div class="left-small">
<a class="p10" ng-click="close()"><span class="text-close" translate>Close</span></a>
</div>
<h1 class="title ellipsis" translate>Enter amount</h1>
<div class="right-small m5r">
<a class="postfix" ng-show="showAlternative" ng-click="toggleAlternative()">{{alternativeIsoCode}}</a>
<a class="postfix" ng-show="!showAlternative" ng-click="toggleAlternative()">{{unitName}}</a>
<div class="right-small m5r" ng-click="toggleAlternative()">
<a class="postfix" ng-show="showAlternative">{{alternativeIsoCode}}</a>
<a class="postfix" ng-show="!showAlternative">{{unitName}}</a>
</div>
</ion-header-bar>
<div class="send-amount row" ng-style="{'background-color':index.backgroundColor}">
<div class="size-48">${{amount}}</div>
<div class="size-21 text-light">= ${{result}}</div>
<div class="size-48" ng-show="!showAlternative">{{amount}}</div>
<div class="size-21 text-light" ng-show="!showAlternative">{{amountResult}} {{alternativeIsoCode}}</div>
<div class="size-48" ng-show="showAlternative">${{alternativeAmount}}</div>
<div class="size-21 text-light" ng-show="showAlternative">{{alternativeResult}} {{unitName}}</div>
</div>
<div class="button-content ext-center df">
<button class="button expand button-balanced" ng-click="">Request</button>
<button class="button expand button-balanced" ng-click="">Send</button>
</div>
<div class="button-content ext-center df">

View file

@ -1,80 +1,126 @@
'use strict';
angular.module('copayApp.controllers').controller('send2Controller',
function($scope, lodash, configService, go) {
angular.module('copayApp.controllers').controller('send2Controller', function($scope, lodash, configService, go, rateService) {
var unitToSatoshi;
var satToUnit;
var unitDecimals;
$scope.init = function() {
var config = configService.getSync().wallet.settings;
$scope.unitName = config.unitName;
$scope.alternativeIsoCode = config.alternativeIsoCode;
$scope.amount = $scope.result = 0;
unitToSatoshi = config.unitToSatoshi;
satToUnit = 1 / unitToSatoshi;
unitDecimals = config.unitDecimals;
$scope.showAlternative = false;
resetAmount();
};
$scope.toggleAlternative = function() {
$scope.showAlternative = !$scope.showAlternative;
};
$scope.toggleAlternative = function() {
$scope.showAlternative = !$scope.showAlternative;
$scope.close = function() {
go.walletHome();
};
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);
}
};
$scope.pushDigit = function(digit) {
if ($scope.amount.length >= 10) return;
var amount;
if ($scope.amount == 0 && digit == 0) return;
amount = $scope.amount ? $scope.amount + digit : digit;
evaluate(amount);
};
$scope.pushDigit = function(digit) {
var amount = $scope.showAlternative ? $scope.alternativeAmount : $scope.amount;
$scope.pushOperator = function(operator) {
if (amount.toString().length >= 10) return;
if (amount == 0 && digit == 0) return;
var val = amount ? amount + digit : digit;
processAmount(val);
};
$scope.pushOperator = function(operator) {
if ($scope.showAlternative) {
if (!$scope.alternativeAmount || $scope.alternativeAmount.length == 0) return;
$scope.alternativeAmount = _pushOperator($scope.alternativeAmount);
} else {
if (!$scope.amount || $scope.amount.length == 0) return;
if (!isOperator(lodash.last($scope.amount))) {
$scope.amount = $scope.amount + operator;
} else
$scope.amount = $scope.amount.slice(0, -1) + operator;
};
$scope.amount = _pushOperator($scope.amount);
}
$scope.removeDigit = function() {
if (!$scope.amount || $scope.amount.length == 0) {
resetAmount();
return;
function _pushOperator(val) {
if (!isOperator(lodash.last(val))) {
return val + operator;
} else {
return val.slice(0, -1) + operator;
}
var amount;
$scope.amount = amount = $scope.amount.slice(0, -1);
evaluate(amount);
};
};
function isOperator(val) {
var regex = /[\/\-\+\*]/;
var match = regex.exec(val);
if (match) return true;
return false;
};
function isOperator(val) {
var regex = /[\/\-\+\*]/;
var match = regex.exec(val);
if (match) return true;
return false;
};
function resetAmount() {
$scope.amount = $scope.result = 0;
};
$scope.removeDigit = function() {
var amount = $scope.showAlternative ? $scope.alternativeAmount.toString() : $scope.amount.toString();
function evaluate(val) {
if (!val) {
resetAmount();
return;
if (amount && amount.length == 0) {
resetAmount();
return;
}
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 {
if ($scope.amount == 0 && val == '.') {
$scope.amount += val;
return;
}
}
var result;
try {
result = eval(val);
} catch (e) {
return;
}
var result = evaluate(val);
if (lodash.isNumber(result)) {
$scope.result = result;
$scope.amount = val;
}
};
});
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();
};
});

View file

@ -347,18 +347,30 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr
},
}
})
.state('preferencesAlias', {
url: '/preferencesAlias',
templateUrl: 'views/preferencesAlias.html',
.state('send2', {
url: '/send2',
templateUrl: 'views/send2.html',
walletShouldBeComplete: true,
needProfile: true,
views: {
'main': {
templateUrl: 'views/preferencesAlias.html'
templateUrl: 'views/send2.html'
},
}
})
// .state('preferencesAlias', {
// url: '/preferencesAlias',
// templateUrl: 'views/preferencesAlias.html',
// walletShouldBeComplete: true,
// needProfile: true,
// views: {
// 'main': {
// templateUrl: 'views/preferencesAlias.html'
// },
//
// }
// })
.state('preferencesEmail', {
url: '/preferencesEmail',
templateUrl: 'views/preferencesEmail.html',