-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
106 lines (95 loc) · 3.61 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
/** File Name: node_modules/railway-pagination/index.js
* Purpose: railway-pagination main file.
* Original author: Anatoliy C.
*
* Update History
* Name Date Description
* --------------- ---------- ------------------------------------------------------------------------------
* Asp3ctus 14/03/2013 - Migrate to compund and new jugglingdb api
* Jude L. 04/26/2012 - Updated the paginateCollection to allow the passing of order option to the Model.all routine.
* Jude L. 05/19/2012 - Updated the paginateCollection to allow the passing of where option to the Model.all routine
if one is provided.
**/
exports.init = function (compound) {
// add view helper
compound.helpers.HelperSet.prototype.paginate = paginateHelper;
// add orm method
// sorry, jugglingdb only for now
compound.on('models', function(){
for(var m in compound.models){
if(compound.models.hasOwnProperty(m)){
compound.models[m].paginate = paginateCollection;
}
}
});
};
// global view helper
function paginateHelper(collection,step) {
if (!step) step = 5;
if (!collection.totalPages || collection.totalPages < 2) return '';
var page = parseInt(collection.currentPage, 10);
var pages = collection.totalPages;
var html = '<div class="pagination">';
var prevClass = 'prev' + (page === 1 ? ' disabled': '');
var nextClass = 'next' + (page === pages ? ' disabled': '');
html += '<ul><li class="' + prevClass + '">';
html += this.link_to('← First', '?page=1');
html += this.link_to('← Previous', '?page=' + (page - 1));
html += '</li>';
var start = ( page <= step ) ? 1 : page-step;
var end = page+step;
if ( page > pages-step )
{
start = pages-(step*2);
}
if ( end < (step*2) )
{
end = step*2;
}
if ( end > pages )
{
end = pages;
}
for (var i = start; i <= end; i++ ) {
if (i == page) {
html += '<li class="active"><a href="#">' + i + '</a></li>';
} else {
html += '<li>' + this.link_to(i, '?page=' + i) + '</li>';
}
}
html += '<li class="' + nextClass + '">';
html += this.link_to('Next →', '?page=' + (page + 1));
html += this.link_to('Last →', '?page=' + pages);
html += '</li></ul></div>';
return html;
};
// orm method
function paginateCollection(opts, callback) {
var limit = opts.limit || 10;
var page = opts.page || 1;
var order = opts.order ||'1';
var where = opts.where;
var include = opts.include;
var Model = this;
if (where != null) {
Model.count({where: where}, function (err, totalRecords) {
Model.all({limit: limit, offset: (page - 1) * limit, order: order, where: where, include: include}, function (err, records) {
if (err) return callback(err);
records.totalRecords = totalRecords;
records.currentPage = page;
records.totalPages = Math.ceil(totalRecords / limit);
callback(null, records);
});
})
} else {
Model.count(function (err, totalRecords) {
Model.all({limit: limit, offset: (page - 1) * limit, order: order, include: include }, function (err, records) {
if (err) return callback(err);
records.totalRecords = totalRecords;
records.currentPage = page;
records.totalPages = Math.ceil(totalRecords / limit);
callback(null, records);
});
})
}
}