-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
102 lines (92 loc) · 3.14 KB
/
index.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
'use strict';
/**
* @typedef {Object} PaginationData
* @property {( Number|undefined )} next - next page number. `undefined` if there is no next page
* @property {( Number|undefined )} prev - previous page number. `undefined` if there is no previous page
* @property {Number} currentPage - current page number
* @property {Number} totalPages - total number of pages
* @property {Number} totalItems - total number of items
* @property {Number} limit - items per page. copied from argument
* @property {Number} skip - no of skipped records. copied from argument
* @property {Array<Object>} buttons - array of buttons in the pagination list
* @property {Number} buttons[].page - page number of button
* @property {string} buttons[].class - html class of button.<br> current page will have `opts.inactiveClass` and all others will have `opts.activeClass`
*/
/**
* Calculate pagination data using given params
*
* @param {Number} total - Total number of records
* @param {Number} skip - Number of records to be skiped. AKA offset
* @param {Number} [ limit=10 ] - Number of items per page
* @param {Object} [ opts={} ] - additional options
* @param {Number} [ opts.buttonCount=5 ] - length of pagination button list.
<b>Eg:</b>
<ul>
<li>
< 2, *3*, 4, > -> Means buttonCount=3
</li>activeClass
<li>
< 2, *3*, 4, 5, 6 > -> Means buttonCount=5
</li>
<li>
< 2, *3*, 4, 5, 6, 7, 9 > -> Means buttonCount=7
</li>
</ul>
* @param {string} [ opts.activeClass='active' ] - HTML class attribute to be applied for current page button
* @param {string} [ opts.inactiveClass='' ] - HTML class attribute to be applied for button other than current page button
* @returns {PaginationData}
*/
function paginate( total, skip, limit, opts ){
var pageNumber,
buttonCount,
activeClass,
inactiveClass,
buttons = [],
carry = 0,
pageCount ,
start,
end,
i;
total = parseInt(total);
skip = parseInt(skip);
limit = parseInt(limit) || 10;
opts = opts || {};
buttonCount = opts.buttonCount || 5;
activeClass = opts.hasOwnProperty( 'activeClass' ) ? opts.activeClass : 'active';
inactiveClass = opts.hasOwnProperty( 'inactiveClass' ) ? opts.inactiveClass : '';
pageNumber = Math.floor( skip/limit )+1;
buttonCount = Math.floor( buttonCount/2 );
pageCount = Math.ceil( total/limit );
start = pageNumber - buttonCount;
if ( start < 1 ){
start = 1;
carry = buttonCount - pageNumber + 1;
}
end = ( pageNumber + buttonCount + carry );
if ( end > pageCount ){
carry = end - pageCount;
end = pageCount;
start = ( start - carry ) < 1 ? 1 : ( start - carry );
}
for ( i=start; i <= end ; i++) {
buttons.push({
page: i,
class: i === pageNumber? activeClass : inactiveClass,
});
}
return {
next : ( pageNumber < pageCount ? pageNumber + 1 : null ),
prev : ( pageNumber > 1 ? pageNumber - 1 : null ),
currentPage : pageNumber,
buttons : buttons,
totalPages : pageCount,
totalItems : total,
limit : limit,
skip : skip,
};
}
if( typeof module !== 'undefined' ){
module.exports = paginate;
} else {
window.GenericPaginate = paginate;
}