-
Notifications
You must be signed in to change notification settings - Fork 10
/
knockout-paging.js
268 lines (231 loc) · 7.72 KB
/
knockout-paging.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*!
Knockout paged extender {{ version }}
By: Erik Schierboom (C) 2015
License: Apache 2
Adds the paged extender that can add paging functionality to an
observable array.
*/
(function(root, factory) {
if (typeof define === "function" && define.amd) {
define(["knockout"], factory);
} else if (typeof exports === "object") {
module.exports = factory(require("knockout"));
} else {
root.KnockoutElse = factory(root.ko);
}
})(this, function(ko) {
// index.js
// --------
// paged extender.
// --------
"use strict";
// polyfill for IE < 9
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === "[object Array]";
};
}
// Utilities
function isObservableArray(value) {
return ko.isObservable(value) && Array.isArray(ko.unwrap(value));
}
function createRange(min, max) {
var list = [];
for (var i = min; i <= max; i++) {
list.push(i);
}
return list;
}
// This page generator just returns a integer for all pages in the pages
// observable. For a large number of pages, this soon becomes impractical
// and you are better of with the sliding window page generator or your
// own, custom one.
function DefaultPageGenerator() {
this.generate = function(pagedObservable) {
return createRange(1, pagedObservable.pageCount());
};
}
// This page generators presents a sliding window that displays the current
// page and the windows around it. The default window displays 5 pages, but
// you can customize it through by settings the windowSize observable
function SlidingPageGenerator() {
var self = this;
self.windowSize = ko.observable(5);
self.generate = function(pagedObservable) {
var leftBasedStartIndex =
pagedObservable.pageNumber() - Math.floor(self.windowSize() / 2),
rightBasedStartIndex =
pagedObservable.pageCount() - self.windowSize() + 1,
startIndex = Math.max(
1,
Math.min(leftBasedStartIndex, rightBasedStartIndex)
),
stopIndex = Math.min(
pagedObservable.pageCount(),
startIndex + self.windowSize() - 1
);
return createRange(startIndex, stopIndex);
};
}
// This object contains global paging options as well as page generators.
//
// There are two default paging options:
// ko.paging.defaults.pageNumber: the default page number (1)
// ko.paging.defaults.pageSize: the default page size (50)
//
// The paging generators are stored as followed:
// ko.paging.generators.default: default page generator that returns all pages
// ko.paging.generators.sliding: sliding page generator that displays a sliding
// window with the current page and the pages around it
//
// You can extend the available page generators by simply adding a property to
// the generators object:
//
// // This generator uses a zero-based paged index
// ko.paging.generators.zeroBased = {
// generate: function(pagedObservable) {
// return [createRange(0, pagedObservable.pageCount() - 1)];
// }
// }
//
// You can then use this custom pager by supplying the name of the custom generator
// when extending the observable array:
// target.extend({ paged: { pageGenerator: 'zeroBased' } });
ko.paging = {
defaults: {
pageNumber: 1,
pageSize: 50
},
generators: {
default: new DefaultPageGenerator(),
sliding: new SlidingPageGenerator()
}
};
// This extender adds paging functionality to a Knockout observable array.
// The target must be an observable array, otherwise an error is thrown.
// The options parameter can contain two values: the page number and/or
// the page size:
// { pageNumber: the current page number, pageSize: the page size }
ko.extenders.paged = function(target, options) {
if (!isObservableArray(target)) {
throw new Error(
"The paged extender can only be applied to observable arrays."
);
}
if (options && options["pageNumber"] < 1) {
throw new Error("The page number must be greater than zero.");
}
if (options && options["pageSize"] < 1) {
throw new Error("The page size must be greater than zero.");
}
if (
options &&
options["pageGenerator"] !== undefined &&
ko.paging.generators[options["pageGenerator"]] === undefined
) {
throw new Error("The page generator could not be found.");
}
// We use this field as a backing field for the pageNumber computed observable,
// as we want to do some additional validation
var _pageNumber = ko.observable(
(options && options["pageNumber"]) || ko.paging.defaults.pageNumber || 1
);
target.pageNumber = ko.pureComputed({
read: function() {
return _pageNumber();
},
write: function(value) {
_pageNumber(Math.min(Math.max(1, value), target.pageCount()));
},
owner: target
});
// We use this field as a backing field for the pageNumber computed observable,
// as we want to do some additional validation
var _pageSize = ko.observable(
(options && options["pageSize"]) || ko.paging.defaults.pageSize || 50
);
target.pageSize = ko.pureComputed({
read: function() {
return _pageSize();
},
write: function(value) {
_pageSize(value);
if (target.pageNumber() > target.pageCount()) {
target.pageNumber(target.pageCount());
}
},
owner: target
});
target.pageGenerator =
ko.paging.generators[(options && options["pageGenerator"]) || "default"];
target.pageItems = ko.pureComputed(function() {
return target().slice(
target.firstItemOnPage() - 1,
target.lastItemOnPage()
);
});
target.pageItems.extend({ rateLimit: 1 });
target.pageCount = ko.pureComputed(function() {
if (target.itemCount() <= 0) {
return 1;
}
return Math.ceil(target.itemCount() / target.pageSize());
});
target.itemCount = ko.pureComputed(function() {
return target().length;
});
target.firstItemOnPage = ko.pureComputed(function() {
return (target.pageNumber() - 1) * target.pageSize() + 1;
});
target.lastItemOnPage = ko.pureComputed(function() {
if (target.itemCount() == 0) {
return 1;
}
return Math.min(
target.pageNumber() * target.pageSize(),
target.itemCount()
);
});
target.hasPreviousPage = ko.pureComputed(function() {
return !target.isFirstPage();
});
target.hasNextPage = ko.pureComputed(function() {
return !target.isLastPage();
});
target.isFirstPage = ko.pureComputed(function() {
return target.pageNumber() == 1;
});
target.isLastPage = ko.pureComputed(function() {
return target.pageNumber() == target.pageCount();
});
target.pages = ko.pureComputed(function() {
return target.pageGenerator.generate(target);
});
target.toNextPage = function() {
if (target.hasNextPage()) {
target.pageNumber(target.pageNumber() + 1);
}
};
target.toPreviousPage = function() {
if (target.hasPreviousPage()) {
target.pageNumber(target.pageNumber() - 1);
}
};
target.toFirstPage = function() {
if (!target.isFirstPage()) {
target.pageNumber(1);
}
};
target.toLastPage = function() {
if (!target.isLastPage()) {
target.pageNumber(target.pageCount());
}
};
return target;
};
// Add a wrapper function to the main ko object to allow for easier creation of
// paged observable arrays
ko.pagedObservableArray = function(initialValue, options) {
return ko.observableArray(initialValue).extend({ paged: options });
};
});