Wallet/js/controllers/history.js

135 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-03-26 09:18:42 -03:00
'use strict';
2014-06-12 17:42:26 -03:00
var bitcore = require('bitcore');
2014-03-26 09:18:42 -03:00
2014-10-30 14:13:40 -03:00
angular.module('copayApp.controllers').controller('HistoryController',
function($scope, $rootScope, $timeout, controllerUtils, notification, rateService) {
2014-10-10 17:58:19 -03:00
controllerUtils.redirIfNotComplete();
var w = $rootScope.wallet;
2014-10-30 18:17:41 -03:00
$rootScope.title = 'History';
2014-04-24 22:43:19 -03:00
$scope.loading = false;
$scope.lastShowed = false;
2014-11-10 10:40:03 -03:00
$scope.currentPage = 1;
$scope.itemsPerPage = 10;
$scope.nbPages = 0;
2014-11-10 19:10:24 -03:00
$scope.totalItems = 0;
$scope.blockchain_txs = [];
$scope.alternativeCurrency = [];
2014-11-10 19:10:24 -03:00
2014-11-10 18:00:43 -03:00
2014-11-10 19:10:24 -03:00
$scope.selectPage = function(page) {
$scope.currentPage = page;
$scope.update();
};
2014-11-10 18:00:43 -03:00
$scope.downloadHistory = function() {
2014-11-11 11:19:08 -03:00
if (window.cordova) {
log.info('Not available on mobile');
return;
}
2014-11-10 18:00:43 -03:00
var data = $scope.blockchain_txs;
2014-11-11 11:19:08 -03:00
var filename = "copay_history.csv";
2014-11-11 12:29:10 -03:00
var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo,Comment\n";
2014-11-10 18:00:43 -03:00
data.forEach(function(it, index) {
2014-11-11 12:29:10 -03:00
var dataString = formatDate(it.ts) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment);
2014-11-10 18:00:43 -03:00
csvContent += index < data.length ? dataString + "\n" : dataString;
});
var encodedUri = encodeURI(csvContent);
var link = document.createElement("a");
link.setAttribute("href", encodedUri);
2014-11-11 11:19:08 -03:00
link.setAttribute("download", filename);
2014-11-10 18:00:43 -03:00
link.click();
2014-11-11 11:19:08 -03:00
function formatDate(date) {
var dateObj = new Date(date);
if (!dateObj) {
log.error('Error formating a date');
return 'DateError'
}
2014-11-11 12:29:10 -03:00
if (!dateObj.toJSON()) {
return '';
}
2014-11-11 11:19:08 -03:00
return dateObj.toJSON().substring(0, 10);
}
2014-11-11 12:29:10 -03:00
function formatString(str) {
if (!str) return '';
return str;
}
2014-11-10 18:00:43 -03:00
};
$scope.update = function() {
$scope.getTransactions();
};
$scope.show = function() {
$scope.loading = true;
setTimeout(function() {
$scope.update();
2014-11-10 10:40:03 -03:00
}, 1);
};
2014-11-10 10:40:03 -03:00
$scope.getTransactions = function() {
var w = $rootScope.wallet;
2014-10-22 13:10:41 -03:00
if (!w) return;
2014-10-22 16:57:29 -03:00
$scope.blockchain_txs = w.cached_txs || [];
$scope.loading = true;
2014-10-30 16:20:25 -03:00
2014-11-10 10:40:03 -03:00
w.getTransactionHistory({
currentPage: $scope.currentPage,
itemsPerPage: $scope.itemsPerPage,
}, function(err, res) {
2014-10-22 13:10:41 -03:00
if (err) throw err;
if (!res) {
$scope.loading = false;
$scope.lastShowed = false;
return;
2014-04-24 23:13:55 -03:00
}
2014-10-22 13:10:41 -03:00
2014-11-10 10:40:03 -03:00
var items = res.items;
_.each(items, function(r) {
2014-10-30 16:46:39 -03:00
r.ts = r.minedTs || r.sentTs;
});
2014-11-10 10:40:03 -03:00
$scope.blockchain_txs = w.cached_txs = items;
$scope.nbPages = res.nbPages;
2014-11-10 19:10:24 -03:00
$scope.totalItems = res.nbItems;
2014-11-10 10:40:03 -03:00
2014-10-22 13:10:41 -03:00
$scope.loading = false;
2014-10-27 17:16:24 -03:00
setTimeout(function() {
$scope.$digest();
}, 1);
2014-10-22 13:10:41 -03:00
});
};
2014-10-22 13:10:41 -03:00
$scope.hasAction = function(actions, action) {
2014-11-10 18:00:43 -03:00
return actions.hasOwnProperty('create');
};
$scope.getShortNetworkName = function() {
var w = $rootScope.wallet;
2014-09-11 11:25:32 -07:00
return w.getNetworkName().substring(0, 4);
};
2014-10-30 16:20:25 -03:00
2014-03-26 09:18:42 -03:00
});