-
Notifications
You must be signed in to change notification settings - Fork 4
/
angular-pagination.js
executable file
·196 lines (190 loc) · 5 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(function() {
/* global angular: false */
"use strict";
var paginationModule = angular.module("pagination", [])
/**
* The main pagination service that should be injected into the controller
* to set the information needed from the backend
*/
paginationModule.factory("Pagination", function(){
/**
* Constructor accepts an object of params that get passed to set
* @param obj
*/
var pagination = function(obj){
this.set(obj)
}
/**
* Limit per page
* @type {number}
*/
pagination.prototype.limit = 10
/**
* Total number of records
* @type {number}
*/
pagination.prototype.total = 0
/**
* Starting record number
* @type {number}
*/
pagination.prototype.start = 0
/**
* Total number of pages
* @type {number}
*/
pagination.prototype.pages = 0
/**
* Current page
* @type {number}
*/
pagination.prototype.page = 1
/**
* Max number of page buttons to show
* @type {number}
*/
pagination.prototype.buttons_max = 5
/**
* Range variables for displaying record stats eg:
* {{ pg.range.start }} - {{ pg.range.end }} of {{ pg.range.total }} entries
* @type {{start: number, end: number, total: number}}
*/
pagination.prototype.range = {
start: 0,
end: 0,
total: 0
}
/**
* Startup pagination (do page math)
*/
pagination.prototype.process = function(){
this.pages = Math.ceil(this.total / this.limit)
this.page = Math.ceil(this.start / this.limit) + 1
this.range.start = (this.total > 0) ? (this.start + 1) : 0
if(this.range.start > this.total)
this.range.start = this.total
if(this.start + this.limit < this.total)
this.range.end = this.start + this.limit
else
this.range.end = this.total
this.range.total = this.total
}
/**
* Determine if the first page is currently selected
* @returns {boolean}
*/
pagination.prototype.isFirst = function(){
return (1 === this.page)
}
/**
* Determine if this last page is current selected
* @returns {boolean}
*/
pagination.prototype.isLast = function(){
return ((0 === this.pages) || (this.page === this.pages))
}
/**
* Get the starting point of the first page
* @returns {number}
*/
pagination.prototype.first = function(){
return 0
}
/**
* Get the safe starting point of the previous page
* @returns {number}
*/
pagination.prototype.previous = function(){
return this.isFirst() ? 0 : this.start - this.limit
}
/**
* Get the safe starting point of the next page
* @returns {number}
*/
pagination.prototype.next = function(){
return this.isLast() ? this.start : this.start + this.limit
}
/**
* Get the starting point of the last page
* @returns {number}
*/
pagination.prototype.last = function(){
return Math.max(0,Math.floor((this.pages - 1) * this.limit))
}
/**
* Get the starting point of a specific page
* @param page
* @returns {number}
*/
pagination.prototype.forPage = function(page){
page = parseInt(page,10) || 1
if(page < 1) page = 1
if(page > this.pages) page = this.pages
return Math.max(0,(page - 1) * this.limit)
}
/**
* When changing the limit this returns the new start value
* @returns {number}
*/
pagination.prototype.forLimitChange = function(limit){
limit = limit || this.limit
return parseInt(Math.floor(this.start / limit) * limit,10)
}
/**
* Get a range of buttons
* Use the buttons_max setting to change the amount, defaults to 5
* @returns {Array}
*/
pagination.prototype.buttons = function(){
var buttons = []
, start, end
if(this.isFirst()){
start = 1
end = (this.buttons_max <= this.pages) ? this.buttons_max : this.pages
} else if(this.isLast()){
start = this.pages - (this.buttons_max - 1)
end = this.pages
if(start < 1) start = 1
} else {
start = this.page - Math.floor(this.buttons_max / 2)
end = this.page + Math.floor(this.buttons_max / 2)
if(start < 1){
start = 1
end++
}
if(end > this.pages){
end = this.pages
start = this.pages - (this.buttons_max - 1)
if(start < 1) start = 1
}
}
for(var i=start; i <= end; i++) buttons.push(i)
return buttons
}
/**
* Set the properties of the pagination
* @param obj
*/
pagination.prototype.set = function(obj){
if("object" !== typeof obj) obj = {}
if("undefined" !== typeof obj.start) this.start = parseInt(obj.start,10)
if("undefined" !== typeof obj.limit) this.limit = parseInt(obj.limit,10)
if("undefined" !== typeof obj.total) this.total = parseInt(obj.total,10)
this.process()
}
return pagination
})
paginationModule.filter("paginationStart", function(){
return function(input, start){
start = parseInt(start,10)
return input.slice(+start)
}
})
paginationModule.filter("paginationRange", function(){
return function(input, total){
total = parseInt(total,10)
for(var i = 1; i <= total; i++) input.push(i)
return input
}
})
})();