clean code

This commit is contained in:
Javier 2016-08-08 16:00:26 -03:00
parent 0f7f6d9493
commit ec56a7c044
3 changed files with 35 additions and 65 deletions

View File

@ -6,10 +6,13 @@
</a>
</div>
<h1 class="title ellipsis" translate>Enter amount</h1>
<div class="buttons m5r m3t" ng-show="!specificAmount" ng-click="toggleAlternative()">
<div class="buttons m5r m3t" ng-if="!specificAmount" ng-click="toggleAlternative()">
<button class="button black" ng-show="showAlternativeAmount">{{alternativeIsoCode}}</button>
<button class="button" ng-style="{'background-color':color}" ng-show="!showAlternativeAmount">{{unitName}}</button>
</div>
<div class="buttons m3t" ng-if="specificAmount" ng-click="init()">
<button class="button right" ng-style="{'background-color':color}"><span translate>Cancel</span></button>
</div>
</ion-header-bar>
<div>
@ -63,10 +66,10 @@
<h4 class="title m10l" translate>QR Code</h4>
<ul class="no-bullet size-14 m0">
<li class="line-b p10 oh text-center">
<qrcode size="220" data="bitcoin:{{address + '?amount=' + specificAmountBtc}}"></qrcode>
<qrcode size="220" data="bitcoin:{{addr + '?amount=' + specificAmount}}"></qrcode>
<div class="m10t text-center" ng-show="isCordova">
<span class="button outline dark-gray tiny round"
ng-click="shareAddress('bitcoin:' + address + '?amount=' + specificAmountBtc)">
ng-click="shareAddress('bitcoin:' + addr + '?amount=' + specificAmount)">
<i class="fi-share"></i>
<span translate>Share address</span>
</span>
@ -90,16 +93,6 @@
</span>
</li>
</ul>
<div class="row">
<div class="columns">
<button class="round expand m20t"
ng-style="{'background-color':index.backgroundColor}"
ng-click="init()">
<span translate>Cancel</span>
</button>
</div>
</div>
</div>
</div>
</ion-modal-view>

View File

@ -5,6 +5,8 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
var satToUnit;
var unitDecimals;
var self = $scope.self;
var SMALL_FONT_SIZE_LIMIT = 13;
var LENGTH_EXPRESSION_LIMIT = 19;
$scope.init = function() {
var config = configService.getSync().wallet.settings;
@ -32,23 +34,20 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
if ($scope.amount && isExpression($scope.amount)) {
var amount = evaluate(format($scope.amount));
if ($scope.showAlternativeAmount)
$scope.globalResult = '= ' + $filter('formatFiatAmount')(amount);
else
$scope.globalResult = '= ' + profileService.formatAmount(amount * unitToSatoshi, true);
$scope.globalResult = '= ' + processResult(amount);
}
};
function checkFontSize() {
if ($scope.amount && $scope.amount.length >= 13) $scope.smallFont = true;
if ($scope.amount && $scope.amount.length >= SMALL_FONT_SIZE_LIMIT) $scope.smallFont = true;
else $scope.smallFont = false;
};
$scope.pushDigit = function(digit) {
checkFontSize();
if ($scope.amount && $scope.amount.length >= 19) return;
if ($scope.amount && $scope.amount.length >= LENGTH_EXPRESSION_LIMIT) return;
$scope.amount = ($scope.amount + digit).replace('..', '.');
checkFontSize();
processAmount($scope.amount);
};
@ -67,25 +66,16 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
function isOperator(val) {
var regex = /[\/\-\+\x\*]/;
var match = regex.exec(val);
if (match) return true;
return false;
return regex.test(val);
};
function isExpression(val) {
var regex = /^\.?\d+(\.?\d+)?([\/\-\+\*x]\d?\.?\d+)+$/;
var match = regex.exec(val);
if (match) return true;
return false;
return regex.test(val);
};
$scope.removeDigit = function() {
if ($scope.amount.toString().length == 1) {
$scope.resetAmount();
return;
}
$scope.amount = $scope.amount.slice(0, -1);
processAmount($scope.amount);
checkFontSize();
@ -106,16 +96,19 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
var result = evaluate(formatedValue);
if (lodash.isNumber(result)) {
if ($scope.showAlternativeAmount)
$scope.globalResult = isExpression(val) ? '= ' + $filter('formatFiatAmount')(result) : '';
else
$scope.globalResult = isExpression(val) ? '= ' + profileService.formatAmount(result * unitToSatoshi, true) : '';
$scope.globalResult = isExpression(val) ? '= ' + processResult(result) : '';
$scope.amountResult = $filter('formatFiatAmount')(toFiat(result));
$scope.alternativeResult = profileService.formatAmount(fromFiat(result) * unitToSatoshi, true);
}
};
function processResult(val) {
if ($scope.showAlternativeAmount)
return $filter('formatFiatAmount')(val);
else
return profileService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true);
};
function fromFiat(val) {
return parseFloat((rateService.fromFiat(val, $scope.alternativeIsoCode) * satToUnit).toFixed(unitDecimals), 10);
};
@ -131,7 +124,7 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
} catch (e) {
return 0;
}
if (result == 'Infinity' || lodash.isNaN(result)) return 0;
if (!lodash.isFinite(result)) return 0;
return result;
};
@ -145,26 +138,15 @@ angular.module('copayApp.controllers').controller('inputAmountController', funct
};
$scope.finish = function() {
var amount;
var alternativeAmount;
var amount = evaluate(format($scope.amount)).toFixed(unitDecimals);
var alternativeAmount = fromFiat(amount).toFixed(unitDecimals);
if ($scope.showAlternativeAmount) {
amount = fromFiat($scope.globalResult);
alternativeAmount = profileService.formatAmount(evaluate(format($scope.amount)) * unitToSatoshi, true);
} else {
amount = profileService.formatAmount(evaluate(format($scope.amount)) * unitToSatoshi, true);
alternativeAmount = toFiat(amount);
}
if (amount % 1 == 0) amount = parseInt(amount);
if ($scope.address) {
var satToBtc = 1 / 100000000;
var amountSat = parseInt((amount * unitToSatoshi).toFixed(0));
if ($scope.unitName == 'bits') {
$scope.specificAmountBtc = (amountSat * satToBtc).toFixed(8);
}
if ($scope.addr) {
$scope.specificAmount = amount;
$scope.specificAlternativeAmount = $filter('formatFiatAmount')(toFiat(amount));
$scope.specificAmount = profileService.formatAmount(amount * unitToSatoshi, true);
$scope.specificAlternativeAmount = $filter('formatFiatAmount')(alternativeAmount);
$timeout(function() {
$ionicScrollDelegate.resize();
}, 100);

View File

@ -348,14 +348,9 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
};
this.setAmount = function(amount, alternativeAmount, useAlternativeAmount) {
var amountResult = useAlternativeAmount ? alternativeAmount : amount;
$scope.showAlternative = useAlternativeAmount;
var amountResult;
if (useAlternativeAmount) {
amountResult = parseFloat((rateService.fromFiat(alternativeAmount, self.alternativeIsoCode) * self.satToUnit).toFixed(self.unitDecimals), 10);
} else {
amountResult = amount;
}
self.fromInputAmount = true;
self.setForm(null, amountResult, null);
};
@ -543,7 +538,7 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
});
};
$scope.openCustomAmountModal = function(addr) {
$scope.openCustomInputAmountModal = function(addr) {
var fc = profileService.focusedClient;
$scope.color = fc.backgroundColor;
$scope.self = self;
@ -557,19 +552,19 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
});
};
$scope.openAmountModal = function(address) {
$scope.openAmountModal = function(addr) {
if (isCordova)
$scope.openInputAmountModal(address);
$scope.openInputAmountModal(addr);
else
$scope.openCustomAmountModal(address);
$scope.openCustomInputAmountModal(addr);
};
$scope.openInputAmountModal = function(addr) {
var fc = profileService.focusedClient;
$scope.color = fc.backgroundColor;
$scope.showAlternativeAmount = $scope.showAlternative || null;
$scope.address = addr || null;
$scope.self = self;
$scope.addr = addr;
$ionicModal.fromTemplateUrl('views/modals/inputAmount.html', {
scope: $scope