-
Notifications
You must be signed in to change notification settings - Fork 16
/
angular-pagination.js
134 lines (122 loc) · 3.83 KB
/
angular-pagination.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
angular-pagination v0.2.2
https://github.com/nervgh/angular-pagination
*/
angular
.module('angularPagination', [])
.value('paginationOptions', {
itemsPerPage: 10,
itemsCount: 100,
maxNumbers: 5,
startPage: 1,
currentPage: 1
})
.factory('Pagination', ['paginationOptions', function(paginationOptions) {
/**
* Creates new pagination object
* @param {Object} options
* @constructor
*/
function Pagination(options) {
var defaults = angular.copy(paginationOptions);
angular.extend(this, defaults, options);
this.endPage = null;
this.pages = [];
this._lastPage = null;
this.setCurrent(this.currentPage);
}
/**
* Sets current page
* @param {Number} page
*/
Pagination.prototype.setCurrent = function(page) {
this.endPage = Math.ceil(this.itemsCount / this.itemsPerPage);
this.currentPage = this._fixPage(Math.floor(page));
this._change(this.currentPage);
this._updatePages();
};
/**
* Returns "true" if page is current
* @param {Number} page
* @returns {Boolean}
*/
Pagination.prototype.isCurrent = function(page) {
return this.currentPage === page;
};
/**
* Returns "true" if page inside of range
* @param {Number} page
* @returns {boolean}
*/
Pagination.prototype.inRange = function(page) {
return this.startPage <= page && this.endPage >= page;
};
/**
* Returns "true" if page is first
* @param {Number} page
* @returns {Boolean}
*/
Pagination.prototype.isFirst = function(page) {
return this.startPage === page;
};
/**
* Returns "true" if page is last
* @param {Number} page
* @returns {Boolean}
*/
Pagination.prototype.isLast = function(page) {
return this.endPage === page;
};
/**
* Callback. Called when page changed
* @param {Number} page
*/
Pagination.prototype.onChange = function(page) {
};
/**
* Fixes number of page if it outside range
* @param {Number} page
* @returns {number}
* @private
*/
Pagination.prototype._fixPage = function(page) {
page = Math.min(page, this.endPage);
page = Math.max(page, this.startPage);
return page;
};
/**
* Calls "onChange" if number of page was changed
* @param {Number} page
* @private
*/
Pagination.prototype._change = function(page) {
if (this._lastPage !== page) {
this._lastPage = page;
this.onChange(page);
}
};
/**
* Updates array of pages
* @private
*/
Pagination.prototype._updatePages = function() {
var delta = Math.floor(this.maxNumbers / 2);
var start = Math.max(this.currentPage - delta, this.startPage);
var end = Math.min(start + this.maxNumbers - 1, this.endPage);
start = this.endPage === end ? end - (this.maxNumbers - 1) : start;
start = Math.max(start, this.startPage);
this.pages.length = 0;
for(var i = start; i <= end; i++) {
this.pages.push(i);
}
};
/**
* Creates and returns a pagination object
* @param {Object} options
* @returns {Pagination}
*/
Pagination.create = function(options) {
return new Pagination(options);
};
return Pagination;
}]);