Wallet/src/js/controllers/walletDetails.js

209 lines
5.2 KiB
JavaScript
Raw Normal View History

2016-08-15 10:25:43 -03:00
'use strict';
2016-09-05 14:59:11 -03:00
angular.module('copayApp.controllers').controller('walletDetailsController', function($scope, $rootScope, $interval, $timeout, $filter, $log, $ionicModal, $ionicPopover, $ionicNavBarDelegate, $state, $stateParams, bwcError, profileService, lodash, configService, gettext, gettextCatalog, platformInfo, walletService, $ionicPopup, txpModalService, externalLinkService) {
2016-08-18 10:37:08 -03:00
var HISTORY_SHOW_LIMIT = 10;
2016-09-06 11:22:10 -03:00
var currentTxHistoryPage;
var wallet;
2016-08-23 17:31:50 -03:00
$scope.txps = [];
2016-09-05 14:59:11 -03:00
$scope.openExternalLink = function(url, target) {
externalLinkService.open(url, target);
};
2016-08-23 17:31:50 -03:00
2016-09-06 11:22:10 -03:00
$scope.init = function() {
currentTxHistoryPage = 0;
$scope.completeTxHistory = [];
wallet = profileService.getWallet($stateParams.walletId);
/* Set color for header bar */
$rootScope.walletDetailsColor = wallet.color;
$rootScope.walletDetailsName = wallet.name;
$scope.wallet = wallet;
$scope.requiresMultipleSignatures = wallet.credentials.m > 1;
$scope.newTx = false;
$ionicNavBarDelegate.title(wallet.name);
$scope.updateAll(function() {
if ($stateParams.txid) {
var tx = lodash.find($scope.completeTxHistory, {
txid: $stateParams.txid
});
if (tx) {
$scope.openTxModal(tx);
} else {
$ionicPopup.alert({
title: gettext('TX not available'),
});
}
} else if ($stateParams.txpId) {
var txp = lodash.find($scope.txps, {
id: $stateParams.txpId
});
if (txp) {
$scope.openTxpModal(txp);
} else {
$ionicPopup.alert({
title: gettext('Proposal not longer available'),
});
}
}
});
}
2016-08-23 17:31:50 -03:00
var setPendingTxps = function(txps) {
if (!txps) {
$scope.txps = [];
return;
}
$scope.txps = lodash.sortBy(txps, 'createdOn').reverse();
};
$scope.updateStatus = function(force) {
$scope.updatingStatus = true;
$scope.updateStatusError = false;
walletService.getStatus(wallet, {
force: !!force,
}, function(err, status) {
$scope.updatingStatus = false;
if (err) {
$scope.status = null;
$scope.updateStatusError = true;
return;
}
setPendingTxps(status.pendingTxps);
$scope.status = status;
2016-08-24 17:54:01 -03:00
$timeout(function() {
2016-08-23 17:45:23 -03:00
$scope.$apply();
}, 1);
2016-08-24 17:54:01 -03:00
2016-08-23 17:31:50 -03:00
});
};
2016-09-01 19:14:18 -03:00
$scope.openTxpModal = txpModalService.open;
2016-08-23 17:31:50 -03:00
var listeners = [
$rootScope.$on('bwsEvent', function(e, walletId) {
if (walletId == wallet.id)
$scope.updateStatus();
}),
$rootScope.$on('Local/TxAction', function(e, walletId) {
if (walletId == wallet.id)
$scope.updateStatus();
}),
];
$scope.$on('$destroy', function() {
2016-08-24 17:54:01 -03:00
lodash.each(listeners, function(x) {
2016-08-23 17:31:50 -03:00
x();
});
});
2016-08-18 10:37:08 -03:00
2016-08-15 10:25:43 -03:00
$scope.openSearchModal = function() {
2016-08-19 13:09:27 -03:00
$scope.color = wallet.color;
2016-08-15 10:25:43 -03:00
$ionicModal.fromTemplateUrl('views/modals/search.html', {
scope: $scope,
focusFirstInput: true
}).then(function(modal) {
$scope.searchModal = modal;
$scope.searchModal.show();
});
2016-09-02 14:29:34 -03:00
$scope.close = function() {
$scope.searchModal.hide();
}
2016-08-15 10:25:43 -03:00
};
2016-08-18 17:56:04 -03:00
$scope.openTxModal = function(btx) {
2016-08-15 10:25:43 -03:00
var self = this;
$scope.btx = lodash.cloneDeep(btx);
$scope.self = self;
$ionicModal.fromTemplateUrl('views/modals/tx-details.html', {
scope: $scope,
hideDelay: 500
}).then(function(modal) {
$scope.txDetailsModal = modal;
$scope.txDetailsModal.show();
});
};
2016-08-18 10:37:08 -03:00
$scope.recreate = function() {
walletService.recreate();
};
2016-08-17 18:48:30 -03:00
2016-08-24 17:54:01 -03:00
$scope.updateTxHistory = function(cb) {
2016-08-18 10:37:08 -03:00
if ($scope.updatingTxHistory) return;
$scope.updatingTxHistory = true;
$scope.updateTxHistoryError = false;
$scope.updatingTxHistoryProgress = null;
var progressFn = function(txs) {
$scope.updatingTxHistoryProgress = txs ? txs.length : 0;
2016-08-22 18:20:01 -03:00
$scope.completeTxHistory = txs;
2016-08-18 10:37:08 -03:00
$scope.showHistory();
2016-08-24 17:54:01 -03:00
$timeout(function() {
2016-08-23 17:45:23 -03:00
$scope.$apply();
}, 1);
2016-08-24 17:54:01 -03:00
2016-08-18 10:37:08 -03:00
};
$timeout(function() {
walletService.getTxHistory(wallet, {
progressFn: progressFn,
}, function(err, txHistory) {
$scope.updatingTxHistory = false;
if (err) {
$scope.txHistory = null;
$scope.updateTxHistoryError = true;
return;
}
2016-08-22 18:20:01 -03:00
$scope.completeTxHistory = txHistory;
2016-08-18 10:37:08 -03:00
$scope.showHistory();
2016-08-24 17:54:01 -03:00
$timeout(function() {
2016-08-23 17:45:23 -03:00
$scope.$apply();
}, 1);
2016-08-24 17:54:01 -03:00
return cb();
2016-08-18 10:37:08 -03:00
});
2016-08-15 16:07:30 -03:00
});
2016-08-15 10:25:43 -03:00
};
2016-08-18 10:37:08 -03:00
$scope.showHistory = function() {
2016-08-22 18:20:01 -03:00
if ($scope.completeTxHistory) {
$scope.txHistory = $scope.completeTxHistory.slice(0, (currentTxHistoryPage + 1) * HISTORY_SHOW_LIMIT);
$scope.txHistoryShowMore = $scope.completeTxHistory.length > $scope.txHistory.length;
2016-08-18 10:37:08 -03:00
}
};
$scope.showMore = function() {
currentTxHistoryPage++;
$scope.showHistory();
$scope.$broadcast('scroll.infiniteScrollComplete');
};
2016-08-24 17:54:01 -03:00
$scope.updateAll = function(cb)  {
2016-08-18 10:37:08 -03:00
$scope.updateStatus(false);
2016-08-24 17:54:01 -03:00
$scope.updateTxHistory(cb);
2016-08-18 10:37:08 -03:00
}
2016-08-15 10:25:43 -03:00
$scope.hideToggle = function() {
2016-08-31 12:18:04 -03:00
profileService.toggleHideBalanceFlag(wallet.credentials.walletId, function(err) {
if (err) $log.error(err);
});
2016-08-23 12:38:21 -03:00
}
2016-08-15 10:25:43 -03:00
});