2014-10-31 07:49:52 -07:00
|
|
|
'use strict';
|
|
|
|
|
2014-12-09 10:13:17 -08:00
|
|
|
angular.module('copayApp.controllers').controller('HomeWalletController', function($scope, $rootScope, $timeout, $filter, $modal, rateService, notification, txStatus, identityService) {
|
2014-12-09 06:52:01 -08:00
|
|
|
$scope.initHome = function() {
|
|
|
|
var w = $rootScope.wallet;
|
2014-12-04 15:43:27 -08:00
|
|
|
|
2014-12-09 06:52:01 -08:00
|
|
|
$rootScope.title = 'Home';
|
2014-12-04 10:06:17 -08:00
|
|
|
$scope.rateService = rateService;
|
|
|
|
$scope.isRateAvailable = false;
|
|
|
|
|
2014-12-09 06:52:01 -08:00
|
|
|
if (w.isShared())
|
|
|
|
$scope.copayers = w.getRegisteredPeerIds();
|
|
|
|
|
|
|
|
w.on('txProposalEvent', _updateTxs);
|
2014-12-04 13:12:29 -08:00
|
|
|
_updateTxs();
|
2014-12-04 10:06:17 -08:00
|
|
|
|
|
|
|
rateService.whenAvailable(function() {
|
|
|
|
$scope.isRateAvailable = true;
|
|
|
|
$scope.$digest();
|
2014-12-04 13:12:29 -08:00
|
|
|
});
|
|
|
|
};
|
2014-12-04 10:06:17 -08:00
|
|
|
|
2014-12-09 06:52:01 -08:00
|
|
|
// This is necessary, since wallet can change in homeWallet,
|
|
|
|
// without running init() again.
|
|
|
|
|
2014-12-04 13:12:29 -08:00
|
|
|
var removeWatch;
|
|
|
|
removeWatch = $rootScope.$watch('wallet.id', function(newWallet, oldWallet) {
|
2014-12-04 10:06:17 -08:00
|
|
|
if ($rootScope.wallet && $rootScope.wallet.isComplete() && newWallet !== oldWallet) {
|
2014-12-04 13:12:29 -08:00
|
|
|
|
|
|
|
if (removeWatch)
|
|
|
|
removeWatch();
|
|
|
|
|
|
|
|
if (oldWallet) {
|
|
|
|
var oldw = $rootScope.iden.getWalletById(oldWallet);
|
|
|
|
if (oldw)
|
|
|
|
oldw.removeListener('txProposalEvent', _updateTxs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
$rootScope.pendingTxCount = 0;
|
|
|
|
w.on('txProposalEvent', _updateTxs);
|
2014-12-04 10:06:17 -08:00
|
|
|
_updateTxs();
|
|
|
|
}
|
|
|
|
});
|
2014-12-04 13:12:29 -08:00
|
|
|
|
2014-12-09 10:13:17 -08:00
|
|
|
$scope.$on("$destroy", function() {
|
2014-12-04 10:06:17 -08:00
|
|
|
var w = $rootScope.wallet;
|
2014-12-05 09:23:33 -08:00
|
|
|
if (w) {
|
|
|
|
removeWatch();
|
|
|
|
w.removeListener('txProposalEvent', _updateTxs);
|
|
|
|
};
|
2014-12-06 10:30:32 -08:00
|
|
|
});
|
2014-12-04 10:06:17 -08:00
|
|
|
|
|
|
|
$scope.setAlternativeAmount = function(w, tx, cb) {
|
|
|
|
rateService.whenAvailable(function() {
|
|
|
|
_.each(tx.outs, function(out) {
|
|
|
|
var valueSat = out.valueSat * w.settings.unitToSatoshi;
|
2014-12-04 13:12:29 -08:00
|
|
|
out.alternativeAmount = $filter('noFractionNumber')(rateService.toFiat(valueSat, $scope.alternativeIsoCode), 2);
|
2014-12-04 10:06:17 -08:00
|
|
|
out.alternativeIsoCode = $scope.alternativeIsoCode;
|
|
|
|
});
|
|
|
|
if (cb) return cb(tx);
|
|
|
|
});
|
2014-12-02 09:33:46 -08:00
|
|
|
};
|
2014-12-04 10:06:17 -08:00
|
|
|
|
|
|
|
var _updateTxs = _.throttle(function() {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
if (!w) return;
|
|
|
|
|
|
|
|
$scope.alternativeIsoCode = w.settings.alternativeIsoCode;
|
|
|
|
$scope.myId = w.getMyCopayerId();
|
2014-12-04 13:12:29 -08:00
|
|
|
|
2014-12-04 10:06:17 -08:00
|
|
|
var res = w.getPendingTxProposals();
|
|
|
|
_.each(res.txs, function(tx) {
|
|
|
|
$scope.setAlternativeAmount(w, tx);
|
|
|
|
if (tx.merchant) {
|
|
|
|
var url = tx.merchant.request_url;
|
|
|
|
var domain = /^(?:https?)?:\/\/([^\/:]+).*$/.exec(url)[1];
|
|
|
|
tx.merchant.domain = domain;
|
|
|
|
}
|
|
|
|
if (tx.outs) {
|
|
|
|
_.each(tx.outs, function(out) {
|
|
|
|
out.valueSat = out.value;
|
|
|
|
out.value = $filter('noFractionNumber')(out.value);
|
|
|
|
});
|
2014-12-04 13:12:29 -08:00
|
|
|
}
|
2014-12-04 10:06:17 -08:00
|
|
|
});
|
|
|
|
$scope.txps = res.txs;
|
2014-12-04 13:12:29 -08:00
|
|
|
$timeout(function(){
|
|
|
|
$scope.$digest();
|
|
|
|
},1)
|
|
|
|
}, 100);
|
|
|
|
|
2014-12-04 10:06:17 -08:00
|
|
|
$scope.sign = function(ntxid) {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
$scope.loading = true;
|
|
|
|
$scope.error = $scope.success = null;
|
|
|
|
w.signAndSend(ntxid, function(err, id, status) {
|
|
|
|
$scope.loading = false;
|
2014-12-09 10:13:17 -08:00
|
|
|
if (!txStatus.notify(status))
|
|
|
|
$scope.error = status;
|
2014-12-04 10:06:17 -08:00
|
|
|
_updateTxs();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.reject = function(ntxid) {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
w.reject(ntxid);
|
2014-12-09 10:13:17 -08:00
|
|
|
txStatus.notify('txRejected');
|
2014-12-04 10:06:17 -08:00
|
|
|
_updateTxs();
|
|
|
|
};
|
|
|
|
|
2014-12-09 09:27:09 -08:00
|
|
|
|
2014-12-09 10:13:17 -08:00
|
|
|
$scope.openTxModal = function(tx) {
|
2014-12-09 09:27:09 -08:00
|
|
|
var ModalInstanceCtrl = function($scope, $modalInstance) {
|
2014-12-09 10:13:17 -08:00
|
|
|
$scope.tx = tx;
|
2014-12-09 09:27:09 -08:00
|
|
|
|
|
|
|
$scope.getShortNetworkName = function() {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
return w.getNetworkName().substring(0, 4);
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.cancel = function() {
|
|
|
|
$modalInstance.dismiss('cancel');
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
$modal.open({
|
2014-12-09 10:13:17 -08:00
|
|
|
templateUrl: 'views/modals/txp-details.html',
|
2014-12-09 09:27:09 -08:00
|
|
|
windowClass: 'tiny',
|
|
|
|
controller: ModalInstanceCtrl,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-12-02 09:33:46 -08:00
|
|
|
});
|