Merge branch 'testacular' of mjesec.ffzg.hr:/git/angular-drzb into testacular
[angular-drzb] / app / js / pagination.js
1 angular.module('ui.bootstrap.pagination', [])
2
3 .directive('pagination', function() {
4   return {
5     restrict: 'E',
6     scope: {
7       numPages: '=',
8       currentPage: '=',
9       maxSize: '=',
10       hidePagination: '=',
11       onSelectPage: '&'
12     },
13     templateUrl: 'template/pagination/pagination.html',
14     replace: true,
15     link: function(scope) {
16       scope.$watch('numPages + currentPage + maxSize', function() {
17         scope.pages = [];
18         
19         //set the default maxSize to numPages
20         var maxSize = ( scope.maxSize && scope.maxSize < scope.numPages ) ? scope.maxSize : scope.numPages;
21         var startPage = scope.currentPage - Math.floor(maxSize/2);
22         
23         //adjust the startPage within boundary
24         if(startPage < 1) {
25             startPage = 1;
26         }
27         if ((startPage + maxSize - 1) > scope.numPages) {
28             startPage = startPage - ((startPage + maxSize - 1) - scope.numPages );
29         }
30
31         for(var i=0; i < maxSize && i < scope.numPages ;i++) {
32           scope.pages.push(startPage + i);
33         }
34         if ( scope.currentPage > scope.numPages ) {
35           scope.selectPage(scope.numPages);
36         }
37       });
38       scope.noPrevious = function() {
39         return scope.currentPage === 1;
40       };
41       scope.noNext = function() {
42         return scope.currentPage === scope.numPages;
43       };
44       scope.isActive = function(page) {
45         return scope.currentPage === page;
46       };
47
48       scope.selectPage = function(page) {
49         if ( ! scope.isActive(page) ) {
50           scope.currentPage = page;
51           scope.onSelectPage({ page: page });
52         }
53       };
54
55       scope.selectPrevious = function() {
56         if ( !scope.noPrevious() ) {
57           scope.selectPage(scope.currentPage-1);
58         }
59       };
60       scope.selectNext = function() {
61         if ( !scope.noNext() ) {
62           scope.selectPage(scope.currentPage+1);
63         }
64       };
65     }
66   };
67 });