Fixes show all/show less button on address list

This commit is contained in:
Matias Pando 2014-09-19 17:12:54 -03:00
commit 12adaa64d2
8 changed files with 110 additions and 161 deletions

View file

@ -3,6 +3,7 @@
angular.module('copayApp.controllers').controller('AddressesController', angular.module('copayApp.controllers').controller('AddressesController',
function($scope, $rootScope, $timeout, $modal, controllerUtils) { function($scope, $rootScope, $timeout, $modal, controllerUtils) {
$scope.loading = false; $scope.loading = false;
$scope.showAll = false;
var w = $rootScope.wallet; var w = $rootScope.wallet;
$scope.newAddr = function() { $scope.newAddr = function() {
@ -16,7 +17,7 @@ angular.module('copayApp.controllers').controller('AddressesController',
}; };
$scope.openAddressModal = function(address) { $scope.openAddressModal = function(address) {
var ModalInstanceCtrl = function ($scope, $modalInstance, address) { var ModalInstanceCtrl = function($scope, $modalInstance, address) {
$scope.address = address; $scope.address = address;
$scope.isMobile = !!window.cordova; $scope.isMobile = !!window.cordova;
@ -25,7 +26,7 @@ angular.module('copayApp.controllers').controller('AddressesController',
window.plugins.toast.showShortBottom('Copied to clipboard'); window.plugins.toast.showShortBottom('Copied to clipboard');
} }
$scope.cancel = function () { $scope.cancel = function() {
$modalInstance.dismiss('cancel'); $modalInstance.dismiss('cancel');
}; };
}; };
@ -35,7 +36,9 @@ angular.module('copayApp.controllers').controller('AddressesController',
windowClass: 'tiny', windowClass: 'tiny',
controller: ModalInstanceCtrl, controller: ModalInstanceCtrl,
resolve: { resolve: {
address: function() { return address; } address: function() {
return address;
}
} }
}); });
}; };
@ -44,20 +47,45 @@ angular.module('copayApp.controllers').controller('AddressesController',
$scope.addressList(); $scope.addressList();
}); });
$scope.toggleShowAll = function() {
$scope.showAll = !$scope.showAll;
$scope.addressList();
};
$scope.limitAddress = function(elements) {
elements = elements.sort(function(a, b) {
return (+a.isChange - +b.isChange);
});
if (elements.length <= 1 || $scope.showAll) {
return elements;
}
// Show last 3 non-change addresses plus those with balance
var addrs = elements.filter(function(e, i) {
return (!e.isChange && i < 3) || (e.balance && e.balance > 0);
});
return addrs;
};
$scope.addressList = function() { $scope.addressList = function() {
$scope.addresses = []; $scope.addresses = [];
var addrInfos = $rootScope.addrInfos;
if (addrInfos) { if ($rootScope.addrInfos) {
var addrInfos = $rootScope.addrInfos;
for (var i = 0; i < addrInfos.length; i++) { for (var i = 0; i < addrInfos.length; i++) {
var addrinfo = addrInfos[i]; var addrinfo = addrInfos[i];
$scope.addresses.push({ $scope.addresses.push({
'address': addrinfo.addressStr, 'address': addrinfo.addressStr,
'balance': $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.addressStr] : 0, 'balance': $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.addressStr] : 0,
'isChange': addrinfo.isChange, 'isChange': addrinfo.isChange,
'owned': addrinfo.owned 'owned': addrinfo.owned,
}); });
} }
$scope.addresses = $scope.limitAddress($scope.addresses);
} }
} };
} }
); );

View file

@ -14,32 +14,15 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
}; };
$rootScope.fromSetup = false; $rootScope.fromSetup = false;
$scope.loading = false; $scope.loading = false;
$scope.retreiving = true; $scope.wallets = walletFactory.getWallets().sort(cmp);
$scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id);
walletFactory.getWallets(function(err, wallets) {
if (err || !wallets || !wallets.length) {
$location.path('/');
} else {
$scope.retreiving = false;
$scope.wallets = wallets.sort(cmp);
walletFactory.storage.getLastOpened(function(ret) {
if (ret && _.indexOf(_.pluck($scope.wallets, 'id')) == -1)
ret = null;
$scope.selectedWalletId = ret || ($scope.wallets[0] && $scope.wallets[0].id);
setTimeout(function() {
$rootScope.$digest();
}, 0);
});
}
});
$scope.openPassword = ''; $scope.openPassword = '';
$scope.isMobile = !!window.cordova; $scope.isMobile = !!window.cordova;
if (!$scope.wallets.length) {
$location.path('/');
}
$scope.open = function(form) { $scope.open = function(form) {
if (form && form.$invalid) { if (form && form.$invalid) {
notification.error('Error', 'Please enter the required fields'); notification.error('Error', 'Please enter the required fields');
@ -51,16 +34,19 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc
Passphrase.getBase64Async(password, function(passphrase) { Passphrase.getBase64Async(password, function(passphrase) {
var w, errMsg; var w, errMsg;
walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) { try {
if (!w) { w = walletFactory.open($scope.selectedWalletId, passphrase);
$scope.loading = false; } catch (e) {
notification.error('Error', err.errMsg || 'Wrong password'); errMsg = e.message;
$rootScope.$digest(); };
} else { if (!w) {
$rootScope.updatingBalance = true; $scope.loading = false;
controllerUtils.startNetwork(w, $scope); notification.error('Error', errMsg || 'Wrong password');
} $rootScope.$digest();
}); return;
}
$rootScope.updatingBalance = true;
controllerUtils.startNetwork(w, $scope);
}); });
}; };

View file

@ -26,62 +26,45 @@ angular.module('copayApp.filters', [])
}); });
} }
}) })
.filter('limitAddress', function() {
return function(elements, showAll) {
elements = elements.sort(function(a, b) {
return (+b.owned) - (+a.owned);
});
if (elements.length <= 1 || showAll) { .filter('noFractionNumber', ['$filter', '$locale', '$rootScope',
return elements; function(filter, locale, $rootScope) {
} var numberFilter = filter('number');
var formats = locale.NUMBER_FORMATS;
return function(amount, n) {
if (typeof(n) === 'undefined' && !$rootScope.wallet) return amount;
// Show last 3 non-change addresses plus those with balance var fractionSize = (typeof(n) !== 'undefined') ?
var addrs = elements.filter(function(e, i) { n : $rootScope.wallet.settings.unitToSatoshi.toString().length - 1;
return (!e.isChange && i < 3) || (e.balance && e.balance > 0); var value = numberFilter(amount, fractionSize);
}); var sep = value.indexOf(formats.DECIMAL_SEP);
var group = value.indexOf(formats.GROUP_SEP);
return addrs; if (amount >= 0) {
}; if (group > 0) {
}) if (sep < 0) {
.filter('noFractionNumber', ['$filter', '$locale', '$rootScope',
function(filter, locale, $rootScope) {
var numberFilter = filter('number');
var formats = locale.NUMBER_FORMATS;
return function(amount, n) {
if (typeof(n) === 'undefined' && !$rootScope.wallet) return amount;
var fractionSize = (typeof(n) !== 'undefined') ?
n : $rootScope.wallet.settings.unitToSatoshi.toString().length - 1;
var value = numberFilter(amount, fractionSize);
var sep = value.indexOf(formats.DECIMAL_SEP);
var group = value.indexOf(formats.GROUP_SEP);
if (amount >= 0) {
if (group > 0) {
if (sep < 0) {
return value;
}
var intValue = value.substring(0, sep);
var floatValue = parseFloat(value.substring(sep));
if (floatValue === 0) {
floatValue = '';
} else {
if (floatValue % 1 === 0) {
floatValue = floatValue.toFixed(0);
}
floatValue = floatValue.toString().substring(1);
}
var finalValue = intValue + floatValue;
return finalValue;
} else {
value = parseFloat(value);
if (value % 1 === 0) {
value = value.toFixed(0);
}
return value; return value;
} }
var intValue = value.substring(0, sep);
var floatValue = parseFloat(value.substring(sep));
if (floatValue === 0) {
floatValue = '';
} else {
if (floatValue % 1 === 0) {
floatValue = floatValue.toFixed(0);
}
floatValue = floatValue.toString().substring(1);
}
var finalValue = intValue + floatValue;
return finalValue;
} else {
value = parseFloat(value);
if (value % 1 === 0) {
value = value.toFixed(0);
}
return value;
} }
return 0; }
}; return 0;
} };
]); }
]);

View file

@ -377,6 +377,7 @@ Network.prototype.send = function(dest, payload, cb) {
var message = this.encode(to, payload); var message = this.encode(to, payload);
this.socket.emit('message', message); this.socket.emit('message', message);
} }
if (typeof cb === 'function') cb(); if (typeof cb === 'function') cb();
@ -405,4 +406,6 @@ Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) {
} }
}; };
module.exports = Network; module.exports = Network;

View file

@ -45,6 +45,7 @@ function PublicKeyRing(opts) {
this.copayerIds = []; this.copayerIds = [];
this.copayersBackup = opts.copayersBackup || []; this.copayersBackup = opts.copayersBackup || [];
this.addressToPath = {}; this.addressToPath = {};
}; };
/** /**
@ -322,6 +323,8 @@ PublicKeyRing.prototype.getRedeemScript = function(index, isChange, copayerIndex
return script; return script;
}; };
/** /**
* @desc * @desc
* Get the address for a multisig based on the given params. * Get the address for a multisig based on the given params.
@ -462,9 +465,11 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) {
var ret = []; var ret = [];
var self = this; var self = this;
var copayerIndex = pubkey && this.getCosigner(pubkey); var copayerIndex = pubkey && this.getCosigner(pubkey);
this.indexes.forEach(function(index) { this.indexes.forEach(function(index) {
ret = ret.concat(self.getAddressesInfoForIndex(index, opts, copayerIndex)); ret = ret.concat(self.getAddressesInfoForIndex(index, opts, copayerIndex));
}); });
return ret; return ret;
}; };
@ -489,18 +494,19 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) {
*/ */
PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayerIndex) { PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayerIndex) {
opts = opts || {}; opts = opts || {};
var isOwned = index.copayerIndex === HDPath.SHARED_INDEX || index.copayerIndex === copayerIndex; var isOwned = index.copayerIndex === HDPath.SHARED_INDEX || index.copayerIndex === copayerIndex;
var ret = []; var ret = [];
var appendAddressInfo = function(address, isChange) { var appendAddressInfo = function(address, isChange, index) {
ret.unshift({ ret.unshift({
address: address, address: address,
addressStr: address.toString(), addressStr: address.toString(),
isChange: isChange, isChange: isChange,
owned: isOwned owned: isOwned,
//index: index.copayerIndex
}); });
}; };
for (var i = 0; !opts.excludeChange && i < index.changeIndex; i++) { for (var i = 0; !opts.excludeChange && i < index.changeIndex; i++) {
appendAddressInfo(this.getAddress(i, true, index.copayerIndex), true); appendAddressInfo(this.getAddress(i, true, index.copayerIndex), true);
} }

View file

@ -136,6 +136,7 @@ angular.module('copayApp.services')
}); });
w.on('close', root.onErrorDigest); w.on('close', root.onErrorDigest);
w.on('locked', root.onErrorDigest.bind(this)); w.on('locked', root.onErrorDigest.bind(this));
}; };
root.setupRootVariables = function() { root.setupRootVariables = function() {
@ -159,6 +160,7 @@ angular.module('copayApp.services')
root.updateAddressList(); root.updateAddressList();
notification.enableHtml5Mode(); // for chrome: if support, enable it notification.enableHtml5Mode(); // for chrome: if support, enable it
w.netStart(); w.netStart();
}; };
// TODO movie this to wallet // TODO movie this to wallet
@ -281,5 +283,7 @@ angular.module('copayApp.services')
}); });
} }
return root; return root;
}); });

View file

@ -15,68 +15,7 @@ describe('Unit: Testing Filters', function() {
alternativeIsoCode: 'LOL' alternativeIsoCode: 'LOL'
}; };
describe('limitAddress', function() {
it('should handle emtpy list', inject(function($filter) {
var limitAddress = $filter('limitAddress');
expect(limitAddress([], false)).to.be.empty;
}));
it('should honor show all', inject(function($filter) {
var limitAddress = $filter('limitAddress');
var addresses = [{}, {}, {}, {}, {}];
expect(limitAddress(addresses, true).length).to.equal(5);
expect(limitAddress([{}], false).length).to.equal(1);
}));
it('should filter correctly', inject(function($filter) {
var limitAddress = $filter('limitAddress');
var addresses = [{
isChange: true,
balance: 0
}, {
isChange: false,
balance: 0
}, {
isChange: true,
balance: 0
}, {
isChange: false,
balance: 0
}, {
isChange: true,
balance: 0
}, {
isChange: false,
balance: 0
}, {
isChange: true,
balance: 0
}, {
isChange: false,
balance: 0
}];
expect(limitAddress(addresses, false).length).to.equal(1);
addresses[0].isChange = false;
expect(limitAddress(addresses, false).length).to.equal(2);
addresses[2].isChange = false;
expect(limitAddress(addresses, false).length).to.equal(3);
addresses[3].isChange = false;
expect(limitAddress(addresses, false).length).to.equal(3);
addresses[0].balance = 20;
expect(limitAddress(addresses, false).length).to.equal(3);
addresses[0].balance = 20;
expect(limitAddress(addresses, false).length).to.equal(3);
addresses[7].balance = 20;
expect(limitAddress(addresses, false).length).to.equal(4);
}));
});
describe('removeEmpty addresses', function() { describe('removeEmpty addresses', function() {
it('should work with empty lists', inject(function($filter) { it('should work with empty lists', inject(function($filter) {

View file

@ -6,8 +6,8 @@
</h1> </h1>
<div class="large-12 medium-12" ng-if="!!(addresses|removeEmpty).length"> <div class="large-12 medium-12" ng-if="!!(addresses|removeEmpty).length">
<div class="large-12 medium-12" ng-init="showAll=0"> <div class="large-12 medium-12">
<div class="oh" ng-repeat="addr in addresses|removeEmpty|limitAddress:showAll"> <div class="oh" ng-repeat="addr in addresses|removeEmpty">
<div class="panel radius show-for-large-up"> <div class="panel radius show-for-large-up">
<div class="row collapse"> <div class="row collapse">
<div class="large-7 medium-9 column"> <div class="large-7 medium-9 column">
@ -22,7 +22,7 @@
<div class="large-3 medium-3 column text-right"> <div class="large-3 medium-3 column text-right">
<span ng-if="$root.updatingBalance"> <span ng-if="$root.updatingBalance">
<i class="fi-bitcoin-circle icon-rotate spinner"></i> <i class="fi-bitcoin-circle icon-rotate spinner"></i>
</span> </span>
<p class="size-14" ng-if="!$root.updatingBalance"> <p class="size-14" ng-if="!$root.updatingBalance">
<b>{{addr.balance || 0|noFractionNumber}} {{$root.wallet.settings.unitName}}</b> <b>{{addr.balance || 0|noFractionNumber}} {{$root.wallet.settings.unitName}}</b>
</p> </p>
@ -51,7 +51,7 @@
</div> </div>
<a class="secondary radius" ng-click="showAll=!showAll" ng-show="(addresses|removeEmpty).length != (addresses|removeEmpty|limitAddress).length"> <a class="secondary radius" ng-click="toggleShowAll()" ng-show="(addresses|removeEmpty).length == (addresses|removeEmpty).length">
<span translate ng-if="!showAll">Show all</span> <span translate ng-if="!showAll">Show all</span>
<span translate ng-if="showAll">Show less</span> <span translate ng-if="showAll">Show less</span>
</a> </a>