From 12adaa64d26b04ed3c88fda5573387625d7f48fb Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Fri, 19 Sep 2014 17:12:54 -0300 Subject: [PATCH 1/5] Fixes show all/show less button on address list --- js/controllers/addresses.js | 42 ++++++++++++--- js/controllers/open.js | 52 +++++++----------- js/filters.js | 91 +++++++++++++------------------- js/models/Async.js | 3 ++ js/models/PublicKeyRing.js | 12 +++-- js/services/controllerUtils.js | 4 ++ test/unit/filters/filtersSpec.js | 61 --------------------- views/addresses.html | 8 +-- 8 files changed, 111 insertions(+), 162 deletions(-) diff --git a/js/controllers/addresses.js b/js/controllers/addresses.js index 03f6f5408..f2fa26dac 100644 --- a/js/controllers/addresses.js +++ b/js/controllers/addresses.js @@ -3,6 +3,7 @@ angular.module('copayApp.controllers').controller('AddressesController', function($scope, $rootScope, $timeout, $modal, controllerUtils) { $scope.loading = false; + $scope.showAll = false; var w = $rootScope.wallet; $scope.newAddr = function() { @@ -16,7 +17,7 @@ angular.module('copayApp.controllers').controller('AddressesController', }; $scope.openAddressModal = function(address) { - var ModalInstanceCtrl = function ($scope, $modalInstance, address) { + var ModalInstanceCtrl = function($scope, $modalInstance, address) { $scope.address = address; $scope.isMobile = !!window.cordova; @@ -25,7 +26,7 @@ angular.module('copayApp.controllers').controller('AddressesController', window.plugins.toast.showShortBottom('Copied to clipboard'); } - $scope.cancel = function () { + $scope.cancel = function() { $modalInstance.dismiss('cancel'); }; }; @@ -35,7 +36,9 @@ angular.module('copayApp.controllers').controller('AddressesController', windowClass: 'tiny', controller: ModalInstanceCtrl, resolve: { - address: function() { return address; } + address: function() { + return address; + } } }); }; @@ -44,20 +47,45 @@ angular.module('copayApp.controllers').controller('AddressesController', $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.addresses = []; - var addrInfos = $rootScope.addrInfos; - if (addrInfos) { + + if ($rootScope.addrInfos) { + var addrInfos = $rootScope.addrInfos; for (var i = 0; i < addrInfos.length; i++) { var addrinfo = addrInfos[i]; $scope.addresses.push({ 'address': addrinfo.addressStr, 'balance': $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.addressStr] : 0, 'isChange': addrinfo.isChange, - 'owned': addrinfo.owned + 'owned': addrinfo.owned, }); } + $scope.addresses = $scope.limitAddress($scope.addresses); } - } + }; } ); diff --git a/js/controllers/open.js b/js/controllers/open.js index a98ff6a5e..95091cdbe 100644 --- a/js/controllers/open.js +++ b/js/controllers/open.js @@ -14,32 +14,15 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc }; $rootScope.fromSetup = false; $scope.loading = false; - $scope.retreiving = true; - - 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.wallets = walletFactory.getWallets().sort(cmp); + $scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id); $scope.openPassword = ''; $scope.isMobile = !!window.cordova; + if (!$scope.wallets.length) { + $location.path('/'); + } + $scope.open = function(form) { if (form && form.$invalid) { 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) { var w, errMsg; - walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) { - if (!w) { - $scope.loading = false; - notification.error('Error', err.errMsg || 'Wrong password'); - $rootScope.$digest(); - } else { - $rootScope.updatingBalance = true; - controllerUtils.startNetwork(w, $scope); - } - }); + try { + w = walletFactory.open($scope.selectedWalletId, passphrase); + } catch (e) { + errMsg = e.message; + }; + if (!w) { + $scope.loading = false; + notification.error('Error', errMsg || 'Wrong password'); + $rootScope.$digest(); + return; + } + $rootScope.updatingBalance = true; + controllerUtils.startNetwork(w, $scope); }); }; diff --git a/js/filters.js b/js/filters.js index 9cd467bbf..2c562663c 100644 --- a/js/filters.js +++ b/js/filters.js @@ -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) { - return elements; - } +.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; - // 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; - }; - }) - .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); - } + 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 0; - }; - } - ]); + } + return 0; + }; + } +]); diff --git a/js/models/Async.js b/js/models/Async.js index 217eb9869..0a429295a 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -377,6 +377,7 @@ Network.prototype.send = function(dest, payload, cb) { var message = this.encode(to, payload); this.socket.emit('message', message); + } if (typeof cb === 'function') cb(); @@ -405,4 +406,6 @@ Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) { } }; + + module.exports = Network; diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index 84a08ed16..071d35133 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -45,6 +45,7 @@ function PublicKeyRing(opts) { this.copayerIds = []; this.copayersBackup = opts.copayersBackup || []; this.addressToPath = {}; + }; /** @@ -322,6 +323,8 @@ PublicKeyRing.prototype.getRedeemScript = function(index, isChange, copayerIndex return script; }; + + /** * @desc * Get the address for a multisig based on the given params. @@ -462,9 +465,11 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) { var ret = []; var self = this; var copayerIndex = pubkey && this.getCosigner(pubkey); + this.indexes.forEach(function(index) { ret = ret.concat(self.getAddressesInfoForIndex(index, opts, copayerIndex)); }); + return ret; }; @@ -489,18 +494,19 @@ PublicKeyRing.prototype.getAddressesInfo = function(opts, pubkey) { */ PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayerIndex) { opts = opts || {}; - var isOwned = index.copayerIndex === HDPath.SHARED_INDEX || index.copayerIndex === copayerIndex; var ret = []; - var appendAddressInfo = function(address, isChange) { + var appendAddressInfo = function(address, isChange, index) { ret.unshift({ address: address, addressStr: address.toString(), isChange: isChange, - owned: isOwned + owned: isOwned, + //index: index.copayerIndex }); }; + for (var i = 0; !opts.excludeChange && i < index.changeIndex; i++) { appendAddressInfo(this.getAddress(i, true, index.copayerIndex), true); } diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index 5200b4e1c..7b70f660c 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -136,6 +136,7 @@ angular.module('copayApp.services') }); w.on('close', root.onErrorDigest); w.on('locked', root.onErrorDigest.bind(this)); + }; root.setupRootVariables = function() { @@ -159,6 +160,7 @@ angular.module('copayApp.services') root.updateAddressList(); notification.enableHtml5Mode(); // for chrome: if support, enable it w.netStart(); + }; // TODO movie this to wallet @@ -281,5 +283,7 @@ angular.module('copayApp.services') }); } + + return root; }); diff --git a/test/unit/filters/filtersSpec.js b/test/unit/filters/filtersSpec.js index 1f2e52809..45607ba63 100644 --- a/test/unit/filters/filtersSpec.js +++ b/test/unit/filters/filtersSpec.js @@ -15,68 +15,7 @@ describe('Unit: Testing Filters', function() { 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() { it('should work with empty lists', inject(function($filter) { diff --git a/views/addresses.html b/views/addresses.html index 011e817bc..0101bd86f 100644 --- a/views/addresses.html +++ b/views/addresses.html @@ -6,8 +6,8 @@
-
-
+
+
@@ -22,7 +22,7 @@
- +

{{addr.balance || 0|noFractionNumber}} {{$root.wallet.settings.unitName}}

@@ -51,7 +51,7 @@
- + Show all Show less From ce976a1b42a7f743900cc3f65ebed1e1206a5ed4 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 22 Sep 2014 12:13:51 -0300 Subject: [PATCH 2/5] Rebased --- js/controllers/open.js | 52 +++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/js/controllers/open.js b/js/controllers/open.js index 95091cdbe..a98ff6a5e 100644 --- a/js/controllers/open.js +++ b/js/controllers/open.js @@ -14,15 +14,32 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc }; $rootScope.fromSetup = false; $scope.loading = false; - $scope.wallets = walletFactory.getWallets().sort(cmp); - $scope.selectedWalletId = walletFactory.storage.getLastOpened() || ($scope.wallets[0] && $scope.wallets[0].id); + $scope.retreiving = true; + + 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.isMobile = !!window.cordova; - if (!$scope.wallets.length) { - $location.path('/'); - } - $scope.open = function(form) { if (form && form.$invalid) { notification.error('Error', 'Please enter the required fields'); @@ -34,19 +51,16 @@ angular.module('copayApp.controllers').controller('OpenController', function($sc Passphrase.getBase64Async(password, function(passphrase) { var w, errMsg; - try { - w = walletFactory.open($scope.selectedWalletId, passphrase); - } catch (e) { - errMsg = e.message; - }; - if (!w) { - $scope.loading = false; - notification.error('Error', errMsg || 'Wrong password'); - $rootScope.$digest(); - return; - } - $rootScope.updatingBalance = true; - controllerUtils.startNetwork(w, $scope); + walletFactory.open($scope.selectedWalletId, passphrase, function(err, w) { + if (!w) { + $scope.loading = false; + notification.error('Error', err.errMsg || 'Wrong password'); + $rootScope.$digest(); + } else { + $rootScope.updatingBalance = true; + controllerUtils.startNetwork(w, $scope); + } + }); }); }; From 86c8137da0e7bb2b601941ccbe6666af6a1fce3a Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 23 Sep 2014 11:24:08 -0300 Subject: [PATCH 3/5] Removed whitespaces --- js/controllers/addresses.js | 2 +- js/models/Async.js | 3 --- js/models/PublicKeyRing.js | 6 ++---- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/js/controllers/addresses.js b/js/controllers/addresses.js index f2fa26dac..c75783bab 100644 --- a/js/controllers/addresses.js +++ b/js/controllers/addresses.js @@ -81,7 +81,7 @@ angular.module('copayApp.controllers').controller('AddressesController', 'address': addrinfo.addressStr, 'balance': $rootScope.balanceByAddr ? $rootScope.balanceByAddr[addrinfo.addressStr] : 0, 'isChange': addrinfo.isChange, - 'owned': addrinfo.owned, + 'owned': addrinfo.owned }); } $scope.addresses = $scope.limitAddress($scope.addresses); diff --git a/js/models/Async.js b/js/models/Async.js index 0a429295a..217eb9869 100644 --- a/js/models/Async.js +++ b/js/models/Async.js @@ -377,7 +377,6 @@ Network.prototype.send = function(dest, payload, cb) { var message = this.encode(to, payload); this.socket.emit('message', message); - } if (typeof cb === 'function') cb(); @@ -406,6 +405,4 @@ Network.prototype.lockIncommingConnections = function(allowedCopayerIdsArray) { } }; - - module.exports = Network; diff --git a/js/models/PublicKeyRing.js b/js/models/PublicKeyRing.js index 071d35133..9512986ed 100644 --- a/js/models/PublicKeyRing.js +++ b/js/models/PublicKeyRing.js @@ -496,17 +496,15 @@ PublicKeyRing.prototype.getAddressesInfoForIndex = function(index, opts, copayer opts = opts || {}; var isOwned = index.copayerIndex === HDPath.SHARED_INDEX || index.copayerIndex === copayerIndex; var ret = []; - var appendAddressInfo = function(address, isChange, index) { + var appendAddressInfo = function(address, isChange) { ret.unshift({ address: address, addressStr: address.toString(), isChange: isChange, - owned: isOwned, - //index: index.copayerIndex + owned: isOwned }); }; - for (var i = 0; !opts.excludeChange && i < index.changeIndex; i++) { appendAddressInfo(this.getAddress(i, true, index.copayerIndex), true); } From a3691c02a5c8ed03bc94b5966d0811578c62d4f2 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 23 Sep 2014 12:05:04 -0300 Subject: [PATCH 4/5] Only shows addresses after updating balance --- js/controllers/addresses.js | 1 + 1 file changed, 1 insertion(+) diff --git a/js/controllers/addresses.js b/js/controllers/addresses.js index c75783bab..3ac7c23ea 100644 --- a/js/controllers/addresses.js +++ b/js/controllers/addresses.js @@ -44,6 +44,7 @@ angular.module('copayApp.controllers').controller('AddressesController', }; $rootScope.$watch('addrInfos', function() { + if ($rootScope.updatingBalance) return; $scope.addressList(); }); From 462e6788bce424195e7164faee771356f36cfce1 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 23 Sep 2014 17:12:25 -0300 Subject: [PATCH 5/5] . --- js/controllers/addresses.js | 1 + views/addresses.html | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/js/controllers/addresses.js b/js/controllers/addresses.js index 3ac7c23ea..3432ef8c5 100644 --- a/js/controllers/addresses.js +++ b/js/controllers/addresses.js @@ -76,6 +76,7 @@ angular.module('copayApp.controllers').controller('AddressesController', if ($rootScope.addrInfos) { var addrInfos = $rootScope.addrInfos; + $scope.addrLength = addrInfos.length; for (var i = 0; i < addrInfos.length; i++) { var addrinfo = addrInfos[i]; $scope.addresses.push({ diff --git a/views/addresses.html b/views/addresses.html index 0101bd86f..688f3d628 100644 --- a/views/addresses.html +++ b/views/addresses.html @@ -4,7 +4,9 @@ Addresses - +
+ +
@@ -50,8 +52,7 @@
- - + Show all Show less