/
paginate.js
158 lines (135 loc) · 4.15 KB
/
paginate.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
/*
* paginate
* https://github.com/tonymilne/paginate
*
* Copyright (c) 2012 Tony Milne
* Licensed under the MIT license.
*/
var fs = require('fs');
var path = require('path');
var _ = require('underscore');
var jade = require('jade');
var Page = require('./page');
module.exports = function(options) {
options = options || {};
var viewsDirectory = path.join(__dirname, '../views');
var templates = options.templates || {
pagination: path.join(viewsDirectory, 'pagination.jade'),
page: path.join(viewsDirectory, 'page.jade'),
next: path.join(viewsDirectory, 'next.jade'),
previous: path.join(viewsDirectory, 'previous.jade'),
separator: path.join(viewsDirectory, 'separator.jade')
};
function ascendingSortFn(a, b) {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
}
function paginate(itemCount, itemsPerPage, current, query) {
// Sane defaults.
itemsPerPage = itemsPerPage || 10;
current = current || 1;
var pages = [];
var currentPage = null;
var first = 1;
var last = Math.ceil(itemCount / itemsPerPage);
var previous = (current - 1);
var next = (current + 1);
var window = 2;
var outerWindow = 1;
// The outerWindow can be separetely specified by left and right values.
var left = (typeof options.left === 'undefined') ? outerWindow : options.left;
var right = (typeof options.left === 'undefined') ? outerWindow : options.right;
// Populate the pages array with `Page` objects (excluding previous and next).
// The pages array will contain a `Page` object for the separators too.
// 1 ... 3 4 [5] 6 7 ... 10
// The +1 are to account for the separators.
var leftRange = _.range(first, first + left + 1);
var rightRange = _.range(last - right, last + 1);
var innerRange = _.range(current - window, current + window + 1);
// Sort and filter out any "irrelephant" values (e.g. non unique/out of bounds).
var ranges = innerRange.concat(leftRange, rightRange);
ranges.sort(ascendingSortFn);
ranges = _.unique(ranges, true);
ranges = _.reject(ranges, function(value) {
return (value < 1 || value > last);
});
// Create a page for each int in the range.
ranges.forEach(function(value, i) {
var page = new Page(value, {
current: current,
totalPages: last,
left: left,
right: right,
window: window,
query: query
});
// Add a reference to previous/next pages,
// They remain undefined if they don't exist, e.g. previous for page 1.
if (i > 0) {
page.previousPage = pages[i - 1];
page.previousPage.nextPage = page;
}
pages.push(page);
// Keep a reference to `this` page, if it is the currentPage (for view rendering).
if (value === current) {
currentPage = page;
}
});
if(query){
var params = [];
for(var q in query){
if(q !== 'page'){
params.push(q + '=' + query[q]);
}
}
query = '';
if(params.length > 0){
query = '&' + params.join('&');
}
}
return {
pages: pages,
render: function render(options) {
// @TODO: Improve on the template options and add view caching.
var template = fs.readFileSync(templates.pagination);
var fn = jade.compile(template, {
filename: templates.pagination
});
var html = fn({
baseUrl: options.baseUrl || '/',
pages: pages,
currentPage: currentPage,
query: query
});
return html;
}
};
}
// Optionally patch `Mongoose.Query' with a paginate function.
if (typeof options.mongoose === 'undefined' || options.mongoose) {
try {
var Query = null;
if (typeof options.mongoose !== 'undefined' && typeof options.mongoose.Query !== 'undefined') {
Query = options.mongoose.Query;
}
else {
var mongoose = require('mongoose');
Query = mongoose.Query;
}
Query.prototype.paginate = require('./mongoose-paginate')(paginate);
}
catch (exception) {
console.error('Could not patch Mongoose, due to the following exception:');
console.error(exception);
}
}
// Expose the public facing module functionality.
return {
page: paginate,
};
};