From cb00a5b6295e7b927222c0d3c48cecf879cc5402 Mon Sep 17 00:00:00 2001 From: Gustavo Cortez Date: Tue, 13 May 2014 04:03:09 -0300 Subject: [PATCH] Transactions counter in navbar. Moved updateTx from controller to service. --- index.html | 8 +++-- js/controllers/header.js | 1 + js/controllers/transactions.js | 56 +++------------------------------- js/services/controllerUtils.js | 40 ++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 54 deletions(-) diff --git a/index.html b/index.html index 8796e5d1a..b553aa377 100644 --- a/index.html +++ b/index.html @@ -55,7 +55,9 @@
@@ -385,10 +387,10 @@
- -
diff --git a/js/controllers/header.js b/js/controllers/header.js index e97010e1e..3ceb1d865 100644 --- a/js/controllers/header.js +++ b/js/controllers/header.js @@ -23,6 +23,7 @@ angular.module('copay.header').controller('HeaderController', $rootScope.$watch('wallet', function(wallet) { if (wallet) { + controllerUtils.updateTxs(); } }); diff --git a/js/controllers/transactions.js b/js/controllers/transactions.js index f1ea1952e..2e083f2ae 100644 --- a/js/controllers/transactions.js +++ b/js/controllers/transactions.js @@ -1,46 +1,11 @@ 'use strict'; angular.module('copay.transactions').controller('TransactionsController', - function($scope, $rootScope, $location) { - var bitcore = require('bitcore'); + function($scope, $rootScope, controllerUtils) { $scope.title = 'Transactions'; $scope.loading = false; - - - var _updateTxs = function() { - var w =$rootScope.wallet; - if (!w) return; - - var inT = w.getTxProposals(); - var txs = []; - - inT.forEach(function(i){ - var tx = i.builder.build(); - var outs = []; - - tx.outs.forEach(function(o) { - var addr = bitcore.Address.fromScriptPubKey(o.getScript(), config.networkName)[0].toString(); - if (!w.addressIsOwn(addr, {excludeMain:true})) { - outs.push({ - address: addr, - value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.COIN, - }); - } - }); - // extra fields - i.outs = outs; - i.fee = i.builder.feeSat/bitcore.util.COIN; - i.missingSignatures = tx.countInputMissingSignatures(0); - txs.push(i); - }); - $scope.txs = txs; - w.removeListener('txProposalsUpdated',_updateTxs) - w.once('txProposalsUpdated',_updateTxs); - $scope.loading = false; - }; - $scope.send = function (ntxid) { $scope.loading = true; var w = $rootScope.wallet; @@ -50,7 +15,7 @@ angular.module('copay.transactions').controller('TransactionsController', ? {type:'success', message: 'Transaction broadcasted. txid: ' + txid} : {type:'error', message: 'There was an error sending the Transaction'} ; - _updateTxs(); + controllerUtils.updateTxs(); $rootScope.$digest(); }); }; @@ -62,17 +27,16 @@ angular.module('copay.transactions').controller('TransactionsController', if (!ret) { $rootScope.flashMessage = {type:'error', message: 'There was an error signing the Transaction'}; - _updateTxs(); + controllerUtils.updateTxs(); $rootScope.$digest(); return; } var p = w.txProposals.getTxProposal(ntxid); if (p.builder.isFullySigned()) { $scope.send(ntxid); - _updateTxs(); } else { - _updateTxs(); + controllerUtils.updateTxs(); $rootScope.$digest(); } }; @@ -100,18 +64,8 @@ angular.module('copay.transactions').controller('TransactionsController', var w = $rootScope.wallet; w.reject(ntxid); $rootScope.flashMessage = {type:'warning', message: 'Transaction rejected by you'}; - _updateTxs(); + controllerUtils.updateTxs(); // $rootScope.$digest(); }; - _updateTxs(); - - var w = $rootScope.wallet; - if (w) { - w.on('txProposalsUpdated', function() { - console.log('[transactions.js.108: txProposalsUpdated:]'); //TODO - _updateTxs(); - $rootScope.$digest(); - }); - } }); diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index 30d90a1be..addde1589 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -82,6 +82,10 @@ angular.module('copay.controllerUtils') root.updateBalance(); $rootScope.$digest(); }); + w.on('txProposalsUpdated', function() { + console.log('[ txProposalsUpdated ]'); //TODO + root.updateTxs(); + }); w.on('openError', root.onErrorDigest); w.on('connect', function(peerID) { if (peerID) { @@ -116,10 +120,46 @@ angular.module('copay.controllerUtils') console.log('New available balance:', balance); $rootScope.availableBalance = balance; $rootScope.loading = false; + $rootScope.$digest(); if (cb) cb(); }); }; + root.updateTxs = function() { + var bitcore = require('bitcore'); + var w = $rootScope.wallet; + if (!w) return; + + var inT = w.getTxProposals(); + var txs = []; + + inT.forEach(function(i){ + var tx = i.builder.build(); + var outs = []; + + tx.outs.forEach(function(o) { + var addr = bitcore.Address.fromScriptPubKey(o.getScript(), config.networkName)[0].toString(); + if (!w.addressIsOwn(addr, {excludeMain:true})) { + outs.push({ + address: addr, + value: bitcore.util.valueToBigInt(o.getValue())/bitcore.util.COIN, + }); + } + }); + // extra fields + i.outs = outs; + i.fee = i.builder.feeSat/bitcore.util.COIN; + i.missingSignatures = tx.countInputMissingSignatures(0); + txs.push(i); + }); + $rootScope.txs = txs; +console.log('[controllerUtils.js:155]',txs); //TODO + $rootScope.txslength = txs.length; + w.removeListener('txProposalsUpdated',root.updateTxs) + w.once('txProposalsUpdated',root.updateTxs); + $rootScope.loading = false; + }; + root.setSocketHandlers = function() { // TODO: optimize this? Socket.removeAllListeners();