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-09-23 22:13:56 -07:00
|
|
|
function($scope, $rootScope, $timeout, controllerUtils, notification, rateService) {
|
2014-10-10 13:58:19 -07:00
|
|
|
controllerUtils.redirIfNotComplete();
|
|
|
|
|
2014-09-04 07:12:08 -07:00
|
|
|
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;
|
2014-05-16 21:19:52 -07:00
|
|
|
$scope.lastShowed = false;
|
2014-04-25 13:53:19 -07:00
|
|
|
|
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;
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.blockchain_txs = [];
|
2014-09-23 22:13:56 -07:00
|
|
|
$scope.alternativeCurrency = [];
|
2014-05-20 11:14:52 -07:00
|
|
|
|
2014-11-10 14:10:24 -08:00
|
|
|
|
2014-11-10 13:00:43 -08:00
|
|
|
|
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() {
|
2014-11-11 12:56:12 -08:00
|
|
|
var w = $rootScope.wallet;
|
|
|
|
if (!w) return;
|
2014-11-11 06:19:08 -08:00
|
|
|
|
2014-11-12 12:54:18 -08:00
|
|
|
$scope.generating = true;
|
2014-11-13 05:24:54 -08:00
|
|
|
w.getTransactionHistory(function(err, res) {
|
2014-11-11 12:56:12 -08:00
|
|
|
if (err) throw err;
|
2014-11-10 13:00:43 -08:00
|
|
|
|
2014-11-19 10:47:28 -08:00
|
|
|
if (!res) return;
|
2014-11-10 13:00:43 -08:00
|
|
|
|
2014-11-12 06:02:26 -08:00
|
|
|
var unit = w.settings.unitName;
|
2014-11-11 12:56:12 -08:00
|
|
|
var data = res.items;
|
|
|
|
var filename = "copay_history.csv";
|
2014-11-12 06:02:26 -08:00
|
|
|
var csvContent = "data:text/csv;charset=utf-8,";
|
2014-11-19 10:47:28 -08:00
|
|
|
csvContent += "Date,Amount(" + unit + "),Action,AddressTo,Comment";
|
|
|
|
|
|
|
|
if (w.isShared()) {
|
|
|
|
csvContent += ",Signers\n";
|
|
|
|
} else {
|
|
|
|
csvContent += "\n";
|
|
|
|
}
|
2014-11-10 13:00:43 -08:00
|
|
|
|
2014-11-11 12:56:12 -08:00
|
|
|
data.forEach(function(it, index) {
|
2014-11-19 10:47:28 -08:00
|
|
|
var dataString = formatDate(it.minedTs || it.sentTs) + ',' + it.amount + ',' + it.action + ',' + formatString(it.addressTo) + ',' + formatString(it.comment);
|
|
|
|
if (it.actionList) {
|
|
|
|
dataString += ',' + formatSigners(it.actionList);
|
|
|
|
}
|
2014-11-11 12:56:12 -08:00
|
|
|
csvContent += index < data.length ? dataString + "\n" : dataString;
|
|
|
|
});
|
2014-11-11 06:19:08 -08:00
|
|
|
|
2014-11-11 12:56:12 -08:00
|
|
|
var encodedUri = encodeURI(csvContent);
|
|
|
|
var link = document.createElement("a");
|
|
|
|
link.setAttribute("href", encodedUri);
|
|
|
|
link.setAttribute("download", filename);
|
2014-11-11 06:19:08 -08:00
|
|
|
|
2014-11-11 12:56:12 -08:00
|
|
|
link.click();
|
2014-11-12 12:54:18 -08:00
|
|
|
$scope.generating = false;
|
|
|
|
$scope.$digest();
|
2014-11-11 07:29:10 -08:00
|
|
|
|
2014-11-11 12:56:12 -08:00
|
|
|
function formatDate(date) {
|
|
|
|
var dateObj = new Date(date);
|
|
|
|
if (!dateObj) {
|
|
|
|
log.error('Error formating a date');
|
|
|
|
return 'DateError'
|
|
|
|
}
|
|
|
|
if (!dateObj.toJSON()) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return dateObj.toJSON().substring(0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatString(str) {
|
|
|
|
if (!str) return '';
|
2014-11-12 12:54:18 -08:00
|
|
|
|
|
|
|
if (str.indexOf('"') !== -1) {
|
|
|
|
//replace all
|
|
|
|
str = str.replace(new RegExp('"', 'g'), '\'');
|
|
|
|
}
|
|
|
|
|
2014-11-12 06:02:26 -08:00
|
|
|
//escaping commas
|
|
|
|
str = '\"' + str + '\"';
|
|
|
|
|
2014-11-11 12:56:12 -08:00
|
|
|
return str;
|
|
|
|
}
|
2014-11-11 07:29:10 -08:00
|
|
|
|
2014-11-19 10:47:28 -08:00
|
|
|
function formatSigners(item) {
|
|
|
|
if (!item) return '';
|
|
|
|
var str = '';
|
|
|
|
item.forEach(function(it, index) {
|
|
|
|
str += index == 0 ? w.publicKeyRing.nicknameForCopayer(it.cId) : '|' + w.publicKeyRing.nicknameForCopayer(it.cId);
|
|
|
|
});
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2014-11-10 13:00:43 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-11-19 10:47:28 -08:00
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
$scope.update = function() {
|
2014-11-07 07:22:05 -08:00
|
|
|
$scope.getTransactions();
|
2014-05-16 14:33:06 -07:00
|
|
|
};
|
|
|
|
|
2014-08-06 09:59:33 -07:00
|
|
|
$scope.show = function() {
|
2014-06-16 08:44:18 -07:00
|
|
|
$scope.loading = true;
|
|
|
|
setTimeout(function() {
|
2014-05-16 21:19:52 -07:00
|
|
|
$scope.update();
|
2014-11-10 05:40:03 -08:00
|
|
|
}, 1);
|
2014-05-16 21:19:52 -07:00
|
|
|
};
|
|
|
|
|
2014-11-10 05:40:03 -08:00
|
|
|
$scope.getTransactions = function() {
|
2014-06-03 12:54:09 -07:00
|
|
|
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 || [];
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.loading = true;
|
2014-10-30 12:20:25 -07:00
|
|
|
|
2014-11-10 05:40:03 -08:00
|
|
|
w.getTransactionHistory({
|
|
|
|
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;
|
|
|
|
|
|
|
|
_.each(items, function(r) {
|
2014-10-30 12:46:39 -07:00
|
|
|
r.ts = r.minedTs || r.sentTs;
|
|
|
|
});
|
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-04-22 21:55:00 -07:00
|
|
|
};
|
|
|
|
|
2014-10-22 09:10:41 -07:00
|
|
|
|
2014-07-31 16:34:21 -07:00
|
|
|
$scope.hasAction = function(actions, action) {
|
|
|
|
return actions.hasOwnProperty('create');
|
2014-11-07 07:22:05 -08:00
|
|
|
};
|
2014-07-31 16:34:21 -07:00
|
|
|
|
2014-05-08 14:14:49 -07:00
|
|
|
$scope.getShortNetworkName = function() {
|
2014-10-30 12:38:08 -07:00
|
|
|
var w = $rootScope.wallet;
|
2014-09-11 11:25:32 -07:00
|
|
|
return w.getNetworkName().substring(0, 4);
|
2014-05-08 14:14:49 -07:00
|
|
|
};
|
2014-10-30 12:20:25 -07:00
|
|
|
|
2014-03-26 05:18:42 -07:00
|
|
|
});
|