copay/js/controllers/history.js

203 lines
5.5 KiB
JavaScript
Raw Normal View History

2014-03-26 05:18:42 -07:00
'use strict';
2014-06-12 13:42:26 -07:00
var bitcore = require('bitcore');
2014-03-26 05:18:42 -07:00
2014-10-30 10:13:40 -07:00
angular.module('copayApp.controllers').controller('HistoryController',
2014-12-09 11:11:56 -08:00
function($scope, $rootScope, $filter, $timeout, $modal, rateService, notification, go) {
var w = $rootScope.wallet;
2014-10-30 14:17:41 -07:00
$rootScope.title = 'History';
2014-04-24 18:43:19 -07:00
$scope.loading = false;
2014-11-12 12:54:18 -08:00
$scope.generating = false;
$scope.lastShowed = false;
2014-11-10 05:40:03 -08:00
$scope.currentPage = 1;
$scope.itemsPerPage = 10;
$scope.nbPages = 0;
2014-11-10 14:10:24 -08:00
$scope.totalItems = 0;
$scope.blockchain_txs = [];
$scope.alternativeCurrency = [];
2014-11-10 14:10:24 -08:00
$scope.selectPage = function(page) {
$scope.currentPage = page;
$scope.update();
};
2014-11-10 13:00:43 -08:00
$scope.downloadHistory = function() {
var w = $rootScope.wallet;
if (!w) return;
2014-11-11 06:19:08 -08:00
2014-12-03 11:39:21 -08:00
var filename = "copay_history.csv";
var descriptor = {
2014-12-04 08:42:50 -08:00
columns: [{
label: 'Date',
property: 'ts',
type: 'date'
}, {
label: 'Amount (' + w.settings.unitName + ')',
property: 'amount',
type: 'number'
}, {
label: 'Amount (' + w.settings.alternativeIsoCode + ')',
property: 'alternativeAmount'
}, {
label: 'Action',
property: 'action'
}, {
label: 'AddressTo',
property: 'addressTo'
}, {
label: 'Comment',
property: 'comment'
}, ],
2014-12-03 11:39:21 -08:00
};
if (w.isShared()) {
descriptor.columns.push({
label: 'Signers',
property: function(obj) {
if (!obj.actionList) return '';
return _.map(obj.actionList, function(action) {
return w.publicKeyRing.nicknameForCopayer(action.cId);
}).join('|');
}
});
}
2014-11-11 06:19:08 -08:00
2014-12-03 11:39:21 -08:00
$scope.generating = true;
2014-11-27 05:56:41 -08:00
2014-12-03 11:39:21 -08:00
$scope._getTransactions(w, null, function(err, res) {
if (err) {
$scope.generating = false;
logger.error(err);
notification.error('Could not get transaction history');
return;
2014-11-27 05:56:41 -08:00
}
2014-12-04 08:42:50 -08:00
$scope._addRates(w, res.items, function(err) {
copay.csv.toCsv(res.items, descriptor, function(err, res) {
2014-12-03 11:39:21 -08:00
if (err) {
$scope.generating = false;
logger.error(err);
notification.error('Could not generate csv file');
return;
}
var csvContent = "data:text/csv;charset=utf-8," + res;
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", filename);
link.click();
$scope.generating = false;
$scope.$digest();
});
});
});
2014-11-10 13:00:43 -08:00
};
$scope.update = function() {
$scope.getTransactions();
};
$scope.show = function() {
$scope.loading = true;
setTimeout(function() {
$scope.update();
2014-11-10 05:40:03 -08:00
}, 1);
};
2014-12-04 08:42:50 -08:00
$scope._getTransactions = function(w, opts, cb) {
2014-12-03 11:39:21 -08:00
w.getTransactionHistory(opts, function(err, res) {
if (err) return cb(err);
if (!res) return cb();
var now = new Date();
var items = res.items;
_.each(items, function(tx) {
tx.ts = tx.minedTs || tx.sentTs;
tx.rateTs = Math.floor((tx.ts || now) / 1000);
tx.amount = $filter('noFractionNumber')(tx.amount);
});
return cb(null, res);
});
};
2014-12-04 08:42:50 -08:00
$scope._addRates = function(w, txs, cb) {
2014-12-03 11:39:21 -08:00
if (!txs || txs.length == 0) return cb();
var index = _.indexBy(txs, 'rateTs');
rateService.getHistoricRates(w.settings.alternativeIsoCode, _.keys(index), function(err, res) {
if (err) return cb(err);
if (!res) return cb();
_.each(res, function(r) {
var tx = index[r.ts];
var alternativeAmount = (r.rate != null ? tx.amountSat * rateService.SAT_TO_BTC * r.rate : null);
tx.alternativeAmount = alternativeAmount ? $filter('noFractionNumber')(alternativeAmount, 2) : null;
});
return cb();
});
};
2014-12-04 08:42:50 -08:00
$scope.openTxModal = function(btx) {
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.btx = btx;
$scope.getShortNetworkName = function() {
var w = $rootScope.wallet;
return w.getNetworkName().substring(0, 4);
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
};
$modal.open({
templateUrl: 'views/modals/tx-details.html',
windowClass: 'medium',
2014-12-04 08:42:50 -08:00
controller: ModalInstanceCtrl,
});
};
2014-11-10 05:40:03 -08:00
$scope.getTransactions = function() {
var w = $rootScope.wallet;
2014-10-22 09:10:41 -07:00
if (!w) return;
2014-10-22 12:57:29 -07:00
$scope.blockchain_txs = w.cached_txs || [];
$scope.loading = true;
2014-10-30 12:20:25 -07:00
2014-12-03 11:39:21 -08:00
$scope._getTransactions(w, {
2014-11-10 05:40:03 -08:00
currentPage: $scope.currentPage,
itemsPerPage: $scope.itemsPerPage,
}, function(err, res) {
2014-10-22 09:10:41 -07:00
if (err) throw err;
if (!res) {
$scope.loading = false;
$scope.lastShowed = false;
return;
2014-04-24 19:13:55 -07:00
}
2014-10-22 09:10:41 -07:00
2014-11-10 05:40:03 -08:00
var items = res.items;
2014-12-04 08:42:50 -08:00
$scope._addRates(w, items, function(err) {
$timeout(function() {
$scope.$digest();
}, 1);
2014-12-03 11:39:21 -08:00
})
2014-11-25 10:09:51 -08:00
2014-11-10 05:40:03 -08:00
$scope.blockchain_txs = w.cached_txs = items;
$scope.nbPages = res.nbPages;
2014-11-10 14:10:24 -08:00
$scope.totalItems = res.nbItems;
2014-11-10 05:40:03 -08:00
2014-10-22 09:10:41 -07:00
$scope.loading = false;
2014-10-27 13:16:24 -07:00
setTimeout(function() {
$scope.$digest();
}, 1);
2014-10-22 09:10:41 -07:00
});
};
2014-10-22 09:10:41 -07:00
$scope.hasAction = function(actions, action) {
return actions.hasOwnProperty('create');
};
2014-03-26 05:18:42 -07:00
});