@@ -9,6 +9,7 @@ angular.module('copayApp.controllers').controller('HistoryController',
99
1010 $rootScope . title = 'History' ;
1111 $scope . loading = false ;
12+ $scope . generating = false ;
1213 $scope . lastShowed = false ;
1314
1415 $scope . currentPage = 1 ;
@@ -19,11 +20,83 @@ angular.module('copayApp.controllers').controller('HistoryController',
1920 $scope . alternativeCurrency = [ ] ;
2021
2122
23+
2224 $scope . selectPage = function ( page ) {
2325 $scope . currentPage = page ;
2426 $scope . update ( ) ;
2527 } ;
2628
29+
30+
31+ $scope . downloadHistory = function ( ) {
32+
33+ if ( window . cordova ) {
34+ log . info ( 'Not available on mobile' ) ;
35+ return ;
36+ }
37+ var w = $rootScope . wallet ;
38+ if ( ! w ) return ;
39+
40+ $scope . generating = true ;
41+ w . getTransactionHistory ( function ( err , res ) {
42+ if ( err ) throw err ;
43+
44+ if ( ! res ) {
45+ return ;
46+ }
47+
48+ var unit = w . settings . unitName ;
49+ var data = res . items ;
50+ var filename = "copay_history.csv" ;
51+ var csvContent = "data:text/csv;charset=utf-8," ;
52+ csvContent += "Date,Amount(" + unit + "),Action,AddressTo,Comment\n" ;
53+
54+ data . forEach ( function ( it , index ) {
55+ var dataString = formatDate ( it . minedTs || it . sentTs ) + ',' + it . amount + ',' + it . action + ',' + it . addressTo + ',' + formatString ( it . comment ) ;
56+ csvContent += index < data . length ? dataString + "\n" : dataString ;
57+ } ) ;
58+
59+ var encodedUri = encodeURI ( csvContent ) ;
60+ var link = document . createElement ( "a" ) ;
61+ link . setAttribute ( "href" , encodedUri ) ;
62+ link . setAttribute ( "download" , filename ) ;
63+
64+ link . click ( ) ;
65+ $scope . generating = false ;
66+ $scope . $digest ( ) ;
67+
68+ function formatDate ( date ) {
69+ var dateObj = new Date ( date ) ;
70+ if ( ! dateObj ) {
71+ log . error ( 'Error formating a date' ) ;
72+ return 'DateError'
73+ }
74+ if ( ! dateObj . toJSON ( ) ) {
75+ return '' ;
76+ }
77+
78+ return dateObj . toJSON ( ) . substring ( 0 , 10 ) ;
79+ }
80+
81+ function formatString ( str ) {
82+ if ( ! str ) return '' ;
83+
84+ if ( str . indexOf ( '"' ) !== - 1 ) {
85+ //replace all
86+ str = str . replace ( new RegExp ( '"' , 'g' ) , '\'' ) ;
87+ }
88+
89+ //escaping commas
90+ str = '\"' + str + '\"' ;
91+
92+ return str ;
93+ }
94+ } ) ;
95+
96+ } ;
97+
98+
99+
27100 $scope . update = function ( ) {
28101 $scope . getTransactions ( ) ;
29102 } ;
0 commit comments