/
index.js
174 lines (142 loc) · 2.72 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
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
/**
* Module dependencies.
*/
var Emitter = require('emitter')
, html = require('./template')
, dom = require('dom');
/**
* Expose `Pager`.
*/
module.exports = Pager;
/**
* Initialize a new `Pager`.
*
* @api public
*/
function Pager() {
Emitter.call(this);
this.el = dom(html);
this.el.on('click', 'li > a', this.onclick.bind(this));
this.perpage(5);
this.total(0);
this.show(0);
}
/**
* Mixin emitter.
*/
Emitter(Pager.prototype);
/**
* Handle delegated clicks.
*
* @api private
*/
Pager.prototype.onclick = function(e){
e.preventDefault();
var el = dom(e.target.parentNode);
if (el.hasClass('prev')) return this.prev();
if (el.hasClass('next')) return this.next();
this.show(el.text() - 1);
};
/**
* Return the total number of pages.
*
* @return {Number}
* @api public
*/
Pager.prototype.pages = function(){
return Math.ceil(this._total / this._perpage);
};
/**
* Select the previous page.
*
* @api public
*/
Pager.prototype.prev = function(){
this.show(Math.max(0, this.current - 1));
};
/**
* Select the next page.
*
* @api public
*/
Pager.prototype.next = function(){
this.show(Math.min(this.pages() - 1, this.current + 1));
};
/**
* Select the page `n`.
*
* @param {Number} n
* @return {Pager}
* @api public
*/
Pager.prototype.show = function(n){
this.select(n);
this.emit('show', n)
return this;
};
/**
* Select page `n` without emitting "show".
*
* @param {Number} n
* @return {Pager}
* @api public
*/
Pager.prototype.select = function(n){
this.current = n;
this.render();
return this;
};
/**
* Set the number of items perpage to `n`.
*
* @param {Number} n
* @return {Pager}
* @api public
*/
Pager.prototype.perpage = function(n){
this._perpage = n;
return this;
};
/**
* Set the total number of items to `n`.
*
* @param {Number} n
* @return {Pager}
* @api public
*/
Pager.prototype.total = function(n){
this._total = n;
return this;
};
/**
* Render the pager.
*
* @api public
*/
Pager.prototype.render = function(){
var total = this._total;
var curr = this.current;
var per = this._perpage;
var pages = this.pages();
var el = this.el;
var prev = el.find('.prev');
var next = el.find('.next');
var links = '';
// remove old
el.find('li.page').remove();
// page links
for (var i = 0; i < pages; ++i) {
var n = i + 1;
links += curr == i
? '<li class="page active"><a href="#">' + n + '</a></li>'
: '<li class="page"><a href="#">' + n + '</a></li>';
}
// insert
if (links) dom(links).insertAfter(prev);
// prev
if (curr) prev.removeClass('pager-hide')
else prev.addClass('pager-hide');
// next
if (curr < pages - 1) next.removeClass('pager-hide')
else next.addClass('pager-hide');
};