nexus site path corrected
[portal.git] / ecomp-portal-FE / client / bower_components / angular-smart-table / src / stPagination.js
1 ng.module('smart-table')
2   .directive('stPagination', ['stConfig', function (stConfig) {
3     return {
4       restrict: 'EA',
5       require: '^stTable',
6       scope: {
7         stItemsByPage: '=?',
8         stDisplayedPages: '=?',
9         stPageChange: '&'
10       },
11       templateUrl: function (element, attrs) {
12         if (attrs.stTemplate) {
13           return attrs.stTemplate;
14         }
15         return stConfig.pagination.template;
16       },
17       link: function (scope, element, attrs, ctrl) {
18
19         scope.stItemsByPage = scope.stItemsByPage ? +(scope.stItemsByPage) : stConfig.pagination.itemsByPage;
20         scope.stDisplayedPages = scope.stDisplayedPages ? +(scope.stDisplayedPages) : stConfig.pagination.displayedPages;
21
22         scope.currentPage = 1;
23         scope.pages = [];
24
25         function redraw () {
26           var paginationState = ctrl.tableState().pagination;
27           var start = 1;
28           var end;
29           var i;
30           var prevPage = scope.currentPage;
31           scope.totalItemCount = paginationState.totalItemCount;
32           scope.currentPage = Math.floor(paginationState.start / paginationState.number) + 1;
33
34           start = Math.max(start, scope.currentPage - Math.abs(Math.floor(scope.stDisplayedPages / 2)));
35           end = start + scope.stDisplayedPages;
36
37           if (end > paginationState.numberOfPages) {
38             end = paginationState.numberOfPages + 1;
39             start = Math.max(1, end - scope.stDisplayedPages);
40           }
41
42           scope.pages = [];
43           scope.numPages = paginationState.numberOfPages;
44
45           for (i = start; i < end; i++) {
46             scope.pages.push(i);
47           }
48
49           if (prevPage !== scope.currentPage) {
50             scope.stPageChange({newPage: scope.currentPage});
51           }
52         }
53
54         //table state --> view
55         scope.$watch(function () {
56           return ctrl.tableState().pagination;
57         }, redraw, true);
58
59         //scope --> table state  (--> view)
60         scope.$watch('stItemsByPage', function (newValue, oldValue) {
61           if (newValue !== oldValue) {
62             scope.selectPage(1);
63           }
64         });
65
66         scope.$watch('stDisplayedPages', redraw);
67
68         //view -> table state
69         scope.selectPage = function (page) {
70           if (page > 0 && page <= scope.numPages) {
71             ctrl.slice((page - 1) * scope.stItemsByPage, scope.stItemsByPage);
72           }
73         };
74
75         if (!ctrl.tableState().pagination.number) {
76           ctrl.slice(0, scope.stItemsByPage);
77         }
78       }
79     };
80   }]);