11'use strict' ;
22
33angular . 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 ) {
66 //Current title
77 $scope . title = 'Project Management' ;
88
@@ -15,6 +15,15 @@ angular.module('topcoderX')
1515 $scope . state = {
1616 status : 'active' ,
1717 } ;
18+ $scope . tableConfig = {
19+ pageNumber : 1 ,
20+ pageSize : 10 ,
21+ isLoading : false ,
22+ initialized : false ,
23+ query : '' ,
24+ lastKey : [ ] ,
25+ pages : 1
26+ } ;
1827
1928 //go to a project detail
2029 $scope . goProject = function ( project ) {
@@ -44,13 +53,27 @@ angular.module('topcoderX')
4453 $scope . state . status = status ;
4554 $scope . isLoaded = false ;
4655 $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+ }
4876 $scope . isLoaded = true ;
49- $scope . projects = response . data ;
50- $scope . allProjects = angular . copy ( $scope . projects ) ;
51- $timeout ( function ( ) {
52- $scope . init ( ) ;
53- } , 1000 ) ;
5477 } ) . catch ( function ( error ) {
5578 $scope . isLoaded = true ;
5679 if ( error . data ) {
@@ -61,6 +84,51 @@ angular.module('topcoderX')
6184 } ) ;
6285 } ;
6386
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+
64132 $scope . repoType = function ( repo ) {
65133 if ( repo . toLocaleLowerCase ( ) . indexOf ( "github" ) >= 0 ) {
66134 return "Github" ;
@@ -82,31 +150,19 @@ angular.module('topcoderX')
82150 $scope . getProjects ( $scope . state . status ) ;
83151 } ;
84152
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-
97153 $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 ) ;
101158 } ;
102159
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+ } ;
112168 } ] ) ;
0 commit comments