11import SavedObjectRegistryProvider from 'ui/saved_objects/saved_object_registry' ;
2+ import 'ui/pager_control' ;
3+ import 'ui/pager' ;
24import _ from 'lodash' ;
35
46export function VisualizeListingController (
7+ $filter ,
58 $scope ,
69 confirmModal ,
710 kbnUrl ,
811 Notifier ,
12+ pagerService ,
913 Private ,
1014 timefilter
1115) {
1216 timefilter . enabled = false ;
1317
18+ const limitTo = $filter ( 'limitTo' ) ;
1419 // TODO: Extract this into an external service.
1520 const services = Private ( SavedObjectRegistryProvider ) . byLoaderPropertiesName ;
1621 const visualizationService = services . visualizations ;
1722 const notify = new Notifier ( { location : 'Visualize' } ) ;
1823
1924 let selectedItems = [ ] ;
2025
26+ /**
27+ * Sorts hits either ascending or descending
28+ * @param {Array } hits Array of saved finder object hits
29+ * @return {Array } Array sorted either ascending or descending
30+ */
31+ const sortItems = ( ) => {
32+ const sortProperty = this . getSortProperty ( ) ;
33+
34+ this . items =
35+ sortProperty . isAscending
36+ ? _ . sortBy ( this . items , sortProperty . getValue )
37+ : _ . sortBy ( this . items , sortProperty . getValue ) . reverse ( ) ;
38+ } ;
39+
40+ const calculateItemsOnPage = ( ) => {
41+ sortItems ( ) ;
42+ this . pager . setTotalItems ( this . items . length ) ;
43+ this . pageOfItems = limitTo ( this . items , this . pager . pageSize , this . pager . startIndex ) ;
44+ } ;
45+
2146 const fetchObjects = ( ) => {
2247 visualizationService . find ( this . filter )
2348 . then ( result => {
2449 this . items = result . hits ;
25- this . sortItems ( ) ;
50+ calculateItemsOnPage ( ) ;
2651 } ) ;
2752 } ;
2853
2954 this . items = [ ] ;
55+ this . pageOfItems = [ ] ;
3056 this . filter = '' ;
3157
58+ this . pager = pagerService . create ( this . items . length , 20 , 1 ) ;
59+
3260 /**
3361 * Remember sort direction per property.
3462 */
@@ -57,20 +85,6 @@ export function VisualizeListingController(
5785 return sortProperty . isAscending ;
5886 } ;
5987
60- /**
61- * Sorts hits either ascending or descending
62- * @param {Array } hits Array of saved finder object hits
63- * @return {Array } Array sorted either ascending or descending
64- */
65- this . sortItems = function sortItems ( ) {
66- const sortProperty = this . getSortProperty ( ) ;
67-
68- this . items =
69- sortProperty . isAscending
70- ? _ . sortBy ( this . items , sortProperty . getValue )
71- : _ . sortBy ( this . items , sortProperty . getValue ) . reverse ( ) ;
72- } ;
73-
7488 this . sortOn = function sortOn ( propertyName ) {
7589 const sortProperty = this . getSortProperty ( ) ;
7690
@@ -81,7 +95,7 @@ export function VisualizeListingController(
8195 this . getSortPropertyByName ( propertyName ) . isSelected = true ;
8296 }
8397
84- this . sortItems ( ) ;
98+ calculateItemsOnPage ( ) ;
8599 } ;
86100
87101 this . toggleAll = function toggleAll ( ) {
@@ -133,6 +147,16 @@ export function VisualizeListingController(
133147 } ) ;
134148 } ;
135149
150+ this . onPageNext = ( ) => {
151+ this . pager . nextPage ( ) ;
152+ calculateItemsOnPage ( ) ;
153+ } ;
154+
155+ this . onPagePrevious = ( ) => {
156+ this . pager . previousPage ( ) ;
157+ calculateItemsOnPage ( ) ;
158+ } ;
159+
136160 this . getUrlForItem = function getUrlForItem ( item ) {
137161 return `#/visualize/edit/${ item . id } ` ;
138162 } ;
0 commit comments