forked from EloquentStudio/filter.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaginator.js
More file actions
159 lines (122 loc) · 3.43 KB
/
Copy pathpaginator.js
File metadata and controls
159 lines (122 loc) · 3.43 KB
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
var Paginator = function(recordsCount, opts, onPagination) {
var paginationView;
this.recordsCount = recordsCount;;
this.opts = opts;
this.$container = $(this.opts.container);
if(this.opts.paginationView){
paginationView = $(this.opts.paginationView).html();
} else {
paginationView = views.pagination;
}
this.paginationTmpl = templateBuilder(paginationView);
this.currentPage = 1;
this.onPagination = onPagination;
this.initPerPage();
this.render();
this.bindEvents();
};
var P = Paginator.prototype;
P.bindEvents = function(){
var self = this;
$(this.opts.container).on('click', '[data-page]', function(e){
self.setCurrentPage($(this).data('page'));
e.preventDefault();
});
};
P.totalPages = function(){
return Math.ceil(this.recordsCount/this.perPageCount);
};
P.setCurrentPage = function(page){
page = this.toPage(page)
this.prevCurrentPage = this.currentPage;
this.currentPage = page;
this.paginate(page);
};
P.setRecordCount = function(total){
this.recordsCount = total;
this.setCurrentPage(this.currentPage);
}
P.toPage = function(page){
if(page == 'first'){
return 1;
}
if(page == 'last'){
return this.totalPages();
}
if(page == 'next'){
var next_page = this.currentPage + 1;
return (next_page > this.totalPages() ? this.currentPage : next_page);
}
if(page == 'prev'){
var prev_page = this.currentPage - 1;
return (prev_page <= 0 ? this.currentPage : prev_page);
}
return parseInt(page);
};
P.paginate = function(page){
this.render();
this.onPagination(this.currentPage, this.perPageCount);
};
P.render = function(){
var pages = this.getPages();
if(this.currentPage > pages.totalPages){
this.currentPage = pages.totalPages;
}
if(this.currentPage == 0){
this.currentPage = 1;
}
pages.currentPage = this.currentPage;
this.$container.html(this.paginationTmpl(pages))
};
function makePageArray(start, end){
var i = start, pages = [];
for(i; i <= end; i++){
pages.push(i);
}
return pages;
}
P.getPages = function () {
var total = this.totalPages();
if(!this.opts.visiblePages){
return { totalPages: total, pages: makePageArray(0, total), self: this };
}
var half = Math.floor(this.opts.visiblePages / 2);
var start = this.currentPage - half + 1 - this.opts.visiblePages % 2;
var end = this.currentPage + half;
// handle boundary case
if (start <= 0) {
start = 1;
end = this.opts.visiblePages;
}
if (end > total) {
start = total - this.opts.visiblePages;
if(start <= 0){
start = 1;
}
end = total;
}
return { currentPage: this.currentPage, totalPages: total, pages: makePageArray(start, end), self: this };
},
P.initPerPage = function(){
var opts = this.opts.perPage,
template,
html,
ele,
event_type,
self = this;
this.perPageCount = opts.values[0];
template = opts.perPageView ? $(opts.perPageView).html() : views.per_page;
html = templateBuilder(template)({ values: opts.values });
$(opts.container).html(html);
ele = $(opts.container).find('[data-perpage]')
event_type = ele.get(0).tagName == 'SELECT' ? 'change' : 'click';
$(opts.container).on(event_type, '[data-perpage]', function(e){
var value = parseInt($(this).val() || $(this).data('value'));
self.setPerPage(value)
e.preventDefault();
});
};
P.setPerPage = function(value){
this.perPageCount = value;
this.setCurrentPage(this.currentPage);
}