-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpages.js
155 lines (132 loc) · 2.72 KB
/
pages.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
'use strict';
var Page = require('./page');
/**
* Pages constructor
*
* ```js
* var pages = new Pages();
* ```
*
* @param {Array} `pages` Optional array of pages to initialize the `pages` array.
* @api public
*/
function Pages(pages) {
this.pages = [];
if (typeof pages === 'undefined') return;
if (!Array.isArray(pages)) {
throw new TypeError('expected pages to be an Array');
}
this.addPages(pages);
}
/**
* Add a page to the list.
*
* ```js
* pages.addPage({items: [1, 2, 3]});
* ```
*
* @param {Object} `page` Plain object or instance of a `Page`
* @return {Object} Returns the instance for chaining
* @api public
*/
Pages.prototype.addPage = function(page) {
if (!(page instanceof Page)) page = new Page(page);
this.pages.push(decorate(this, page));
return this;
};
/**
* Add an array of pages to the list.
*
* ```js
* pages.addPages([...]);
* ```
*
* @param {Object} `pages` Array of page objects
* @return {Object} Returns the instance for chaining
* @api public
*/
Pages.prototype.addPages = function(pages) {
for (var i = 0; i < pages.length; i++) {
this.addPage(pages[i]);
}
return this;
};
/**
* Decorates a page with additional properties.
*
* @param {Object} `page` Instance of page to decorate
* @return {Object} Returns the decorated page to be added to the list
*/
function decorate(pages, page) {
Object.defineProperties(page, {
first: {
enumerable: true,
set: function() {},
get: function() {
return pages.first && pages.first.current;
}
},
current: {
enumerable: true,
set: function() {},
get: function() {
return this.idx + 1;
}
},
last: {
enumerable: true,
set: function() {},
get: function() {
return pages.last && pages.last.current;
}
},
total: {
enumerable: true,
set: function() {},
get: function() {
return pages.total;
}
}
});
var prev = pages.last;
var idx = pages.total;
page.idx = idx;
if (prev) {
page.prev = prev.current;
prev.next = page.current;
}
return page;
}
/**
* Getters
*/
Object.defineProperties(Pages.prototype, {
/**
* Helper property to calculate the total pages in the array.
*/
total: {
get: function() {
return this.pages.length;
}
},
/**
* Helper property to get the first page from the array.
*/
first: {
get: function() {
return this.total > 0 ? this.pages[0] : null;
}
},
/**
* Helper property to get the last page from the array.
*/
last: {
get: function() {
return this.total > 0 ? this.pages[this.total - 1] : null;
}
}
});
/**
* Expose `Pages`
*/
module.exports = Pages;