1
1
'use strict' ;
2
2
3
3
angular . module ( 'topcoderX' )
4
- . controller ( 'ProjectsController' , [ 'currentUser' , '$scope' , '$state' , 'ProjectService' , '$filter' , '$rootScope' , 'Alert' , 'Helper' , '$timeout' ,
5
- function ( currentUser , $scope , $state , ProjectService , $filter , $rootScope , Alert , Helper , $timeout ) {
4
+ . controller ( 'ProjectsController' , [ 'currentUser' , '$scope' , '$state' , 'ProjectService' , '$filter' , '$rootScope' , 'Alert' , 'Helper' ,
5
+ function ( currentUser , $scope , $state , ProjectService , $filter , $rootScope , Alert , Helper ) {
6
6
//Current title
7
7
$scope . title = 'Project Management' ;
8
8
@@ -15,6 +15,15 @@ angular.module('topcoderX')
15
15
$scope . state = {
16
16
status : 'active' ,
17
17
} ;
18
+ $scope . tableConfig = {
19
+ pageNumber : 1 ,
20
+ pageSize : 10 ,
21
+ isLoading : false ,
22
+ initialized : false ,
23
+ query : '' ,
24
+ lastKey : [ ] ,
25
+ pages : 1
26
+ } ;
18
27
19
28
//go to a project detail
20
29
$scope . goProject = function ( project ) {
@@ -44,13 +53,27 @@ angular.module('topcoderX')
44
53
$scope . state . status = status ;
45
54
$scope . isLoaded = false ;
46
55
$scope . projects = [ ] ;
47
- ProjectService . getProjects ( status , $scope . filter . showAll ) . then ( function ( response ) {
56
+ ProjectService . getProjects (
57
+ status , $scope . filter . showAll ,
58
+ $scope . tableConfig . pageSize , $scope . tableConfig . lastKey [ $scope . tableConfig . pageNumber ] ,
59
+ $scope . tableConfig . query ) . then ( function ( response ) {
60
+ var config = $scope . tableConfig ;
61
+
62
+ if ( config . query ) {
63
+ config . allItems = response . data . docs ;
64
+ $scope . projects = config . allItems . slice ( 0 , config . pageSize ) ;
65
+ config . pages = Math . ceil ( config . allItems . length / config . pageSize ) ;
66
+ }
67
+ else {
68
+ $scope . projects = response . data . docs ;
69
+ }
70
+ if ( response . data . lastKey ) {
71
+ config . lastKey [ config . pageNumber + 1 ] = response . data . lastKey ;
72
+ if ( ! config . pages || config . pages <= config . pageNumber ) {
73
+ config . pages = config . pageNumber + 1 ;
74
+ }
75
+ }
48
76
$scope . isLoaded = true ;
49
- $scope . projects = response . data ;
50
- $scope . allProjects = angular . copy ( $scope . projects ) ;
51
- $timeout ( function ( ) {
52
- $scope . init ( ) ;
53
- } , 1000 ) ;
54
77
} ) . catch ( function ( error ) {
55
78
$scope . isLoaded = true ;
56
79
if ( error . data ) {
@@ -61,6 +84,51 @@ angular.module('topcoderX')
61
84
} ) ;
62
85
} ;
63
86
87
+ /**
88
+ * get the number array that shows the pagination bar
89
+ */
90
+ $scope . getPageArray = function ( ) {
91
+ var res = [ ] ;
92
+
93
+ var pageNo = $scope . tableConfig . pageNumber ;
94
+ var i = pageNo - 5 ;
95
+ for ( i ; i <= pageNo ; i ++ ) {
96
+ if ( i > 0 ) {
97
+ res . push ( i ) ;
98
+ }
99
+ }
100
+ var j = pageNo + 1 ;
101
+ for ( j ; j <= $scope . tableConfig . pages && j <= pageNo + 5 ; j ++ ) {
102
+ res . push ( j ) ;
103
+ }
104
+ return res ;
105
+ } ;
106
+
107
+ /**
108
+ * handles the change page click
109
+ * @param {Number } pageNumber the page number
110
+ */
111
+ $scope . changePage = function ( pageNumber ) {
112
+ if ( pageNumber === 0 || pageNumber > $scope . tableConfig . pages ||
113
+ ( pageNumber === $scope . tableConfig . pages &&
114
+ $scope . tableConfig . pageNumber === pageNumber ) ) {
115
+ return false ;
116
+ }
117
+ $scope . tableConfig . pageNumber = pageNumber ;
118
+ if ( $scope . tableConfig . query && $scope . tableConfig . allItems ) {
119
+ var start = ( $scope . tableConfig . pageNumber - 1 ) * $scope . tableConfig . pageSize - 1 ;
120
+ if ( pageNumber === 1 ) {
121
+ start = 0 ;
122
+ }
123
+ $scope . projects = $scope . tableConfig . allItems . slice (
124
+ start , $scope . tableConfig . pageSize ) ;
125
+ $scope . isLoaded = true ;
126
+ }
127
+ else {
128
+ $scope . getProjects ( $scope . state . status ) ;
129
+ }
130
+ } ;
131
+
64
132
$scope . repoType = function ( repo ) {
65
133
if ( repo . toLocaleLowerCase ( ) . indexOf ( "github" ) >= 0 ) {
66
134
return "Github" ;
@@ -82,31 +150,19 @@ angular.module('topcoderX')
82
150
$scope . getProjects ( $scope . state . status ) ;
83
151
} ;
84
152
85
-
86
- $scope . onSearchChange = function ( obj ) {
87
- $scope . searchText = obj . searchText ;
88
- if ( ! obj . searchText || obj . searchText . length === 0 ) {
89
- $scope . getProjects ( $scope . state . status ) ;
90
- }
91
-
92
- if ( $scope . allProjects . length > 0 ) {
93
- _searchLocal ( obj . searchText ) ;
94
- }
95
- } ;
96
-
97
153
$scope . onSearchIconClicked = function ( ) {
98
- if ( $scope . allProjects . length > 0 && $scope . searchText ) {
99
- _searchLocal ( $scope . searchText ) ;
100
- }
154
+ $scope . tableConfig . pageNumber = 1 ;
155
+ $scope . tableConfig . pages = 1 ;
156
+ $scope . tableConfig . allItems = [ ] ;
157
+ $scope . getProjects ( $scope . state . status ) ;
101
158
} ;
102
159
103
- function _searchLocal ( query ) {
104
- $scope . projects = $scope . allProjects . filter ( function ( value ) {
105
- return value [ 'title' ] . toLowerCase ( ) . includes ( query . toLowerCase ( ) ) ;
106
- } )
107
- $timeout ( function ( ) {
108
- $ ( '.footable' ) . filter ( '[data-page="0"]' ) . trigger ( 'click' ) ;
109
- } , 1000 ) ;
110
- }
111
-
160
+ $scope . onSearchReset = function ( ) {
161
+ var config = $scope . tableConfig ;
162
+ config . query = '' ;
163
+ config . pageNumber = 1 ;
164
+ config . pages = 1 ;
165
+ config . allItems = [ ] ;
166
+ $scope . getProjects ( $scope . state . status ) ;
167
+ } ;
112
168
} ] ) ;
0 commit comments