Wallet/src/js/controllers/walletDetails.js

203 lines
5.3 KiB
JavaScript
Raw Normal View History

2016-08-15 10:25:43 -03:00
'use strict';
angular.module('copayApp.controllers').controller('walletDetailsController', function($scope, $rootScope, $interval, $timeout, $filter, $log, $ionicModal, $ionicPopover, $state, $stateParams, profileService, lodash, configService, gettextCatalog, platformInfo, walletService, txpModalService, externalLinkService, popupService) {
2016-09-08 12:13:37 -03:00
2016-08-18 10:37:08 -03:00
var HISTORY_SHOW_LIMIT = 10;
var currentTxHistoryPage = 0;
var listeners = [];
2016-08-23 17:31:50 -03:00
$scope.txps = [];
$scope.completeTxHistory = [];
$scope.openTxpModal = txpModalService.open;
2016-10-10 15:19:33 -03:00
$scope.isCordova = platformInfo.isCordova;
2016-08-23 17:31:50 -03:00
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
var setPendingTxps = function(txps) {
2016-09-20 10:02:28 -03:00
/* Uncomment to test multiple outputs */
// var txp = {
// message: 'test multi-output',
// fee: 1000,
// createdOn: new Date() / 1000,
// outputs: [],
// wallet: $scope.wallet
2016-09-20 10:02:28 -03:00
// };
//
// function addOutput(n) {
// txp.outputs.push({
// amount: 600,
// toAddress: '2N8bhEwbKtMvR2jqMRcTCQqzHP6zXGToXcK',
// message: 'output #' + (Number(n) + 1)
// });
// };
// lodash.times(15, addOutput);
// txps.push(txp);
2016-08-23 17:31:50 -03:00
if (!txps) {
$scope.txps = [];
return;
}
$scope.txps = lodash.sortBy(txps, 'createdOn').reverse();
};
var updateStatus = function(force) {
2016-08-23 17:31:50 -03:00
$scope.updatingStatus = true;
$scope.updateStatusError = false;
2016-09-08 12:13:37 -03:00
$scope.walletNotRegistered = false;
2016-08-23 17:31:50 -03:00
walletService.getStatus($scope.wallet, {
2016-08-23 17:31:50 -03:00
force: !!force,
}, function(err, status) {
$scope.updatingStatus = false;
if (err) {
2016-09-08 12:13:37 -03:00
if (err === 'WALLET_NOT_REGISTERED') {
$scope.walletNotRegistered = true;
} else {
$scope.updateStatusError = true;
}
2016-08-23 17:31:50 -03:00
$scope.status = null;
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-08-15 10:25:43 -03:00
$scope.openSearchModal = function() {
$scope.color = $scope.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
$scope.btx = lodash.cloneDeep(btx);
$scope.walletId = $scope.wallet.id;
2016-08-15 10:25:43 -03:00
$ionicModal.fromTemplateUrl('views/modals/tx-details.html', {
2016-09-20 16:21:05 -03:00
scope: $scope
2016-08-15 10:25:43 -03:00
}).then(function(modal) {
$scope.txDetailsModal = modal;
$scope.txDetailsModal.show();
});
};
2016-08-18 10:37:08 -03:00
$scope.recreate = function() {
walletService.recreate($scope.wallet, function(err) {
2016-09-08 12:13:37 -03:00
if (err) return;
$timeout(function() {
walletService.startScan($scope.wallet, function() {
2016-09-08 12:13:37 -03:00
$scope.$apply();
});
});
});
2016-08-18 10:37:08 -03:00
};
2016-08-17 18:48:30 -03:00
var updateTxHistory = function(cb) {
2016-09-07 15:35:14 -03:00
if (!cb) cb = function() {};
2016-08-18 10:37:08 -03:00
if ($scope.updatingTxHistory) return;
$scope.updatingTxHistory = true;
$scope.updateTxHistoryError = false;
$scope.updatingTxHistoryProgress = 0;
2016-08-18 10:37:08 -03:00
2016-09-30 15:58:56 -03:00
var progressFn = function(txs, newTxs) {
$scope.updatingTxHistoryProgress = newTxs;
2016-08-22 18:20:01 -03:00
$scope.completeTxHistory = txs;
2016-08-18 10:37:08 -03:00
$scope.showHistory();
2016-09-30 15:58:56 -03:00
$timeout(function() {
$scope.$apply();
});
2016-08-18 10:37:08 -03:00
};
$timeout(function() {
walletService.getTxHistory($scope.wallet, {
2016-08-18 10:37:08 -03:00
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();
$scope.$apply();
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() {
$timeout(function() {
currentTxHistoryPage++;
$scope.showHistory();
$scope.$broadcast('scroll.infiniteScrollComplete');
}, 100);
2016-08-18 10:37:08 -03:00
};
2016-10-10 15:19:33 -03:00
$scope.onRefresh = function() {
$scope.$broadcast('scroll.refreshComplete');
$scope.updateAll(true);
};
$scope.updateAll = function(force, cb)  {
updateStatus(force);
updateTxHistory(cb);
};
2016-08-18 10:37:08 -03:00
2016-08-15 10:25:43 -03:00
$scope.hideToggle = function() {
profileService.toggleHideBalanceFlag($scope.wallet.credentials.walletId, function(err) {
if (err) $log.error(err);
});
};
2016-09-30 11:16:33 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
$scope.wallet = profileService.getWallet(data.stateParams.walletId);
$scope.requiresMultipleSignatures = $scope.wallet.credentials.m > 1;
$scope.updateAll();
listeners = [
$rootScope.$on('bwsEvent', function(e, walletId) {
if (walletId == $scope.wallet.id)
updateStatus();
}),
$rootScope.$on('Local/TxAction', function(e, walletId) {
if (walletId == $scope.wallet.id)
updateStatus();
}),
];
});
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
});
2016-08-15 10:25:43 -03:00
});