From f1adde8e444f087f5131e6af4177a26f84f589d2 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Mon, 10 Nov 2014 18:00:43 -0300 Subject: [PATCH 1/8] Added csv file generation --- js/controllers/history.js | 24 ++++++++++++++++++++++++ views/history.html | 12 +++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 391dfd51c..58d450a95 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -19,11 +19,33 @@ angular.module('copayApp.controllers').controller('HistoryController', $scope.alternativeCurrency = []; + $scope.selectPage = function(page) { $scope.currentPage = page; $scope.update(); }; + + + $scope.downloadHistory = function() { + var data = $scope.blockchain_txs; + var csvContent = "data:text/csv;charset=utf-8,"; + + data.forEach(function(it, index) { + var dataString = it.ts + ',' + it.amount + ',' + it.action + ',' + it.addressTo; + csvContent += index < data.length ? dataString + "\n" : dataString; + }); + + var encodedUri = encodeURI(csvContent); + var link = document.createElement("a"); + link.setAttribute("href", encodedUri); + link.setAttribute("download", "my_data.csv"); + + link.click(); + }; + + + $scope.update = function() { $scope.getTransactions(); }; @@ -73,6 +95,8 @@ angular.module('copayApp.controllers').controller('HistoryController', $scope.hasAction = function(actions, action) { + + return actions.hasOwnProperty('create'); }; diff --git a/views/history.html b/views/history.html index 99adf5746..5ee062a86 100644 --- a/views/history.html +++ b/views/history.html @@ -12,7 +12,7 @@ class="row" ng-if="blockchain_txs[0].txid">
- +
@@ -99,6 +99,16 @@ More details
+ +
+ + Generating file... +
+
+ + + Download +
From 54a7caeb828d28ee342686ff8499b469eb86e1d6 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 11 Nov 2014 11:19:08 -0300 Subject: [PATCH 2/8] Improved csv format --- js/controllers/history.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 58d450a95..85c221d40 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -28,20 +28,37 @@ angular.module('copayApp.controllers').controller('HistoryController', $scope.downloadHistory = function() { + + if (window.cordova) { + log.info('Not available on mobile'); + return; + } + var data = $scope.blockchain_txs; - var csvContent = "data:text/csv;charset=utf-8,"; + var filename = "copay_history.csv"; + var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo\n"; data.forEach(function(it, index) { - var dataString = it.ts + ',' + it.amount + ',' + it.action + ',' + it.addressTo; + var dataString = formatDate(it.ts) + ',' + it.amount + ',' + it.action + ',' + it.addressTo; csvContent += index < data.length ? dataString + "\n" : dataString; }); var encodedUri = encodeURI(csvContent); var link = document.createElement("a"); link.setAttribute("href", encodedUri); - link.setAttribute("download", "my_data.csv"); + link.setAttribute("download", filename); link.click(); + + + function formatDate(date) { + var dateObj = new Date(date); + if (!dateObj) { + log.error('Error formating a date'); + return 'DateError' + } + return dateObj.toJSON().substring(0, 10); + } }; From 888d9735e249499d79abda8793addc8397043406 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 11 Nov 2014 12:29:10 -0300 Subject: [PATCH 3/8] UI fixed --- js/controllers/history.js | 14 ++++++++++++-- views/history.html | 3 +-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 85c221d40..6cdee8a9f 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -36,10 +36,10 @@ angular.module('copayApp.controllers').controller('HistoryController', var data = $scope.blockchain_txs; var filename = "copay_history.csv"; - var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo\n"; + var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo,Comment\n"; data.forEach(function(it, index) { - var dataString = formatDate(it.ts) + ',' + it.amount + ',' + it.action + ',' + it.addressTo; + var dataString = formatDate(it.ts) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment); csvContent += index < data.length ? dataString + "\n" : dataString; }); @@ -57,8 +57,18 @@ angular.module('copayApp.controllers').controller('HistoryController', log.error('Error formating a date'); return 'DateError' } + if (!dateObj.toJSON()) { + return ''; + } + return dateObj.toJSON().substring(0, 10); } + + + function formatString(str) { + if (!str) return ''; + return str; + } }; diff --git a/views/history.html b/views/history.html index 5ee062a86..cc1e1d957 100644 --- a/views/history.html +++ b/views/history.html @@ -11,8 +11,7 @@
-
-
+
From 36b6636ce41c9c3122a280ac35a317e3f3b8df43 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 11 Nov 2014 13:33:18 -0300 Subject: [PATCH 4/8] Rebased --- views/history.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/history.html b/views/history.html index cc1e1d957..9b89d5669 100644 --- a/views/history.html +++ b/views/history.html @@ -101,7 +101,7 @@
- Generating file... + Generating file....
From f8fb16563026d66e0c9508cd880423d90a3d53f6 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Tue, 11 Nov 2014 17:56:12 -0300 Subject: [PATCH 5/8] Using getTransactionHistory from wallet --- js/controllers/history.js | 76 +++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 34 deletions(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 6cdee8a9f..18c17359b 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -33,42 +33,52 @@ angular.module('copayApp.controllers').controller('HistoryController', log.info('Not available on mobile'); return; } + var w = $rootScope.wallet; + if (!w) return; - var data = $scope.blockchain_txs; - var filename = "copay_history.csv"; - var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo,Comment\n"; + var data = w.getTransactionHistory(null, function(err, res) { + if (err) throw err; - data.forEach(function(it, index) { - var dataString = formatDate(it.ts) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment); - csvContent += index < data.length ? dataString + "\n" : dataString; + if (!res) { + return; + } + + var data = res.items; + var filename = "copay_history.csv"; + var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo,Comment\n"; + + data.forEach(function(it, index) { + var dataString = formatDate(it.minedTs || it.sentTs) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment); + csvContent += index < data.length ? dataString + "\n" : dataString; + }); + + var encodedUri = encodeURI(csvContent); + var link = document.createElement("a"); + link.setAttribute("href", encodedUri); + link.setAttribute("download", filename); + + link.click(); + + + 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 ''; + return str; + } }); - var encodedUri = encodeURI(csvContent); - var link = document.createElement("a"); - link.setAttribute("href", encodedUri); - link.setAttribute("download", filename); - - link.click(); - - - 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 ''; - return str; - } }; @@ -122,8 +132,6 @@ angular.module('copayApp.controllers').controller('HistoryController', $scope.hasAction = function(actions, action) { - - return actions.hasOwnProperty('create'); }; From 11e5bb4305bac7a205e66f426013f3af65d8e2bf Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Wed, 12 Nov 2014 11:02:26 -0300 Subject: [PATCH 6/8] Escaping commas --- js/controllers/history.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 18c17359b..6f40c2b09 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -43,9 +43,11 @@ angular.module('copayApp.controllers').controller('HistoryController', return; } + var unit = w.settings.unitName; var data = res.items; var filename = "copay_history.csv"; - var csvContent = "data:text/csv;charset=utf-8,Date,Amount,Action,AddressTo,Comment\n"; + var csvContent = "data:text/csv;charset=utf-8,"; + csvContent += "Date,Amount(" + unit + "),Action,AddressTo,Comment\n"; data.forEach(function(it, index) { var dataString = formatDate(it.minedTs || it.sentTs) + ',' + it.amount + ',' + it.action + ',' + it.addressTo + ',' + formatString(it.comment); @@ -75,6 +77,9 @@ angular.module('copayApp.controllers').controller('HistoryController', function formatString(str) { if (!str) return ''; + //escaping commas + str = '\"' + str + '\"'; + return str; } }); From d31dbb23f431a72074874415eb95b6e7d42bb8e1 Mon Sep 17 00:00:00 2001 From: Matias Pando Date: Wed, 12 Nov 2014 17:54:18 -0300 Subject: [PATCH 7/8] Added spinner --- js/controllers/history.js | 11 ++++++++++- views/history.html | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/js/controllers/history.js b/js/controllers/history.js index 6f40c2b09..0e9f736d0 100644 --- a/js/controllers/history.js +++ b/js/controllers/history.js @@ -9,6 +9,7 @@ angular.module('copayApp.controllers').controller('HistoryController', $rootScope.title = 'History'; $scope.loading = false; + $scope.generating = false; $scope.lastShowed = false; $scope.currentPage = 1; @@ -36,6 +37,7 @@ angular.module('copayApp.controllers').controller('HistoryController', var w = $rootScope.wallet; if (!w) return; + $scope.generating = true; var data = w.getTransactionHistory(null, function(err, res) { if (err) throw err; @@ -60,7 +62,8 @@ angular.module('copayApp.controllers').controller('HistoryController', link.setAttribute("download", filename); link.click(); - + $scope.generating = false; + $scope.$digest(); function formatDate(date) { var dateObj = new Date(date); @@ -77,6 +80,12 @@ angular.module('copayApp.controllers').controller('HistoryController', function formatString(str) { if (!str) return ''; + + if (str.indexOf('"') !== -1) { + //replace all + str = str.replace(new RegExp('"', 'g'), '\''); + } + //escaping commas str = '\"' + str + '\"'; diff --git a/views/history.html b/views/history.html index 9b89d5669..cc1e1d957 100644 --- a/views/history.html +++ b/views/history.html @@ -101,7 +101,7 @@
- Generating file.... + Generating file...