@@ -4,11 +4,33 @@ angular.module('topcoderX')
4
4
. controller ( 'CopilotPaymentsController' , [ '$scope' , '$rootScope' , '$state' , 'CopilotPaymentService' , '$filter' , 'Alert' , 'Dialog' , '$log' , '$timeout' ,
5
5
function ( $scope , $rootScope , $state , CopilotPaymentService , $filter , Alert , Dialog , $log , $timeout ) {
6
6
$scope . title = 'Copilot payment page' ;
7
- $scope . payments = [ ] ;
8
- $scope . isLoaded = false ;
9
7
$scope . totalPendingAmounts = 0 ;
10
8
$scope . topcoderUrl = '' ;
11
9
$scope . status = '' ;
10
+
11
+ $scope . tableConfig = {
12
+ active : {
13
+ pageNumber : 1 ,
14
+ pageSize : 20 ,
15
+ isLoading : false ,
16
+ sortBy : 'project' ,
17
+ sortDir : 'desc' ,
18
+ totalPages : 1 ,
19
+ initialized : false ,
20
+ closed : false ,
21
+ } ,
22
+ inactive : {
23
+ pageNumber : 1 ,
24
+ pageSize : 20 ,
25
+ isLoading : false ,
26
+ sortBy : 'project' ,
27
+ sortDir : 'desc' ,
28
+ totalPages : 1 ,
29
+ initialized : false ,
30
+ closed : true ,
31
+ } ,
32
+ } ;
33
+
12
34
$scope . goPayment = function ( payment ) {
13
35
if ( payment ) {
14
36
$rootScope . payment = payment ;
@@ -28,27 +50,31 @@ angular.module('topcoderX')
28
50
} ) ;
29
51
} ;
30
52
31
- $scope . getPayments = function ( status ) {
32
- $scope . isLoaded = false ;
33
- CopilotPaymentService . getAll ( 'project' ) . then ( function ( res ) {
34
- if ( status === 'active' ) {
35
- $scope . payments = res . data . activePayments ;
36
- $scope . totalPendingAmounts = 0 ;
37
- for ( var i = 0 ; i < $scope . payments . length ; i ++ ) {
38
- $scope . totalPendingAmounts += $scope . payments [ i ] . amount ;
53
+ /**
54
+ * gets the payment
55
+ * @param {String } provider the provider
56
+ */
57
+ $scope . getPayments = function ( provider ) {
58
+ $scope . totalPendingAmounts = 0 ;
59
+ $scope . status = provider ;
60
+ var config = $scope . tableConfig [ provider ] ;
61
+ config . isLoading = true ;
62
+ CopilotPaymentService . search ( config . closed , config . sortBy , config . sortDir , config . pageNumber , config . pageSize )
63
+ . then ( function ( res ) {
64
+ if ( provider === 'active' ) {
65
+ for ( var i = 0 ; i < res . data . docs . length ; i ++ ) {
66
+ $scope . totalPendingAmounts += res . data . docs [ i ] . amount ;
67
+ }
39
68
}
40
- $scope . isLoaded = true ;
41
- $scope . status = 'active' ;
42
- }
43
- if ( status === 'closed' ) {
44
- $scope . payments = res . data . closedPayments ;
45
- $scope . isLoaded = true ;
46
- $scope . status = 'closed' ;
47
- }
48
- } ) . catch ( function ( err ) {
49
- $scope . isLoaded = true ;
50
- _handleError ( err , 'Error getting payments.' ) ;
51
- } ) ;
69
+ config . items = res . data . docs ;
70
+ config . pages = res . data . pages ;
71
+ config . initialized = true ;
72
+ config . isLoading = false ;
73
+ } ) . catch ( function ( err ) {
74
+ config . isLoading = false ;
75
+ config . initialized = true ;
76
+ _handleError ( err , 'An error occurred while getting the data for ' + provider + '.' ) ;
77
+ } ) ;
52
78
} ;
53
79
54
80
$scope . getPayments ( 'active' ) ;
@@ -90,19 +116,72 @@ angular.module('topcoderX')
90
116
Dialog . show ( 'Are you sure you want to delete this payment?' , $scope ) ;
91
117
} ;
92
118
93
- $scope . sort = function ( criteria ) {
94
- CopilotPaymentService . getAll ( criteria ) . then ( function ( res ) {
95
- if ( status === 'active' ) {
96
- $scope . payments = res . data . activePayments ;
97
- $scope . isLoaded = true ;
98
- $scope . status = 'active' ;
119
+ /**
120
+ * handles the sort click
121
+ * @param criteria the criteria
122
+ * @param provider the provider
123
+ */
124
+ $scope . sort = function ( criteria , provider ) {
125
+ if ( criteria === $scope . tableConfig [ provider ] . sortBy ) {
126
+ if ( $scope . tableConfig [ provider ] . sortDir === 'asc' ) {
127
+ $scope . tableConfig [ provider ] . sortDir = 'desc' ;
128
+ } else {
129
+ $scope . tableConfig [ provider ] . sortDir = 'asc' ;
99
130
}
100
- if ( status === 'closed' ) {
101
- $scope . payments = res . data . closedPayments ;
102
- $scope . isLoaded = true ;
103
- $scope . status = 'closed' ;
131
+ } else {
132
+ $scope . tableConfig [ provider ] . sortDir = 'asc' ;
133
+ }
134
+ $scope . tableConfig [ provider ] . sortBy = criteria ;
135
+ $scope . tableConfig [ provider ] . pageNumber = 1 ;
136
+ $scope . getPayments ( provider ) ;
137
+ } ;
138
+
139
+ /**
140
+ * handles the change page click
141
+ * @param {Number } pageNumber the page number
142
+ * @param {String } provider the selected provider
143
+ */
144
+ $scope . changePage = function ( pageNumber , provider ) {
145
+ if ( pageNumber === 0 || pageNumber > $scope . tableConfig [ provider ] . pages ||
146
+ ( pageNumber === $scope . tableConfig [ provider ] . pages &&
147
+ $scope . tableConfig [ provider ] . pageNumber === pageNumber ) ) {
148
+ return false ;
149
+ }
150
+ $scope . tableConfig [ provider ] . pageNumber = pageNumber ;
151
+ $scope . getPayments ( provider ) ;
152
+ } ;
153
+
154
+ /**
155
+ * handles the tab change click
156
+ * @param {String } provider the selected provider
157
+ */
158
+ $scope . tabChanged = function ( provider ) {
159
+ $scope . tableConfig [ provider ] . sortBy = 'project' ;
160
+ $scope . tableConfig [ provider ] . sortDir = 'desc' ;
161
+ $scope . tableConfig [ provider ] . pageNumber = 1 ;
162
+ $scope . tableConfig [ provider ] . initialized = false ;
163
+ $scope . getPayments ( provider ) ;
164
+ } ;
165
+
166
+ /**
167
+ * get the number array that shows the pagination bar
168
+ * @param {String } provider the provider
169
+ */
170
+ $scope . getPageArray = function ( provider ) {
171
+ var res = [ ] ;
172
+
173
+ var pageNo = $scope . tableConfig [ provider ] . pageNumber ;
174
+ var i = pageNo - 5 ;
175
+ for ( i ; i <= pageNo ; i ++ ) {
176
+ if ( i > 0 ) {
177
+ res . push ( i ) ;
104
178
}
105
- } ) ;
179
+ }
180
+ var j = pageNo + 1 ;
181
+ for ( j ; j <= $scope . tableConfig [ provider ] . pages && j <= pageNo + 5 ; j ++ ) {
182
+ res . push ( j ) ;
183
+ }
184
+ return res ;
106
185
} ;
107
186
108
187
function onInit ( ) {
@@ -115,9 +194,5 @@ angular.module('topcoderX')
115
194
}
116
195
117
196
onInit ( ) ;
118
-
119
- $scope . init = function ( ) {
120
- $ ( '.footable' ) . footable ( ) ;
121
- } ;
122
197
}
123
198
] ) ;
0 commit comments