1 | 'use strict';
|
2 |
|
3 | var Base = require('base');
|
4 | var debug = require('debug')('base:templates:list');
|
5 | var plugin = require('./plugins');
|
6 | var Group = require('./group');
|
7 | var utils = require('./utils');
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | module.exports = exports = List;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function List(options) {
|
30 | if (!(this instanceof List)) {
|
31 | return new List(options);
|
32 | }
|
33 |
|
34 | Base.call(this);
|
35 |
|
36 | this.is('list');
|
37 | this.define('isCollection', true);
|
38 | this.use(utils.option());
|
39 | this.use(utils.plugin());
|
40 | this.init(options || {});
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | plugin.static(Base, List, 'List');
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | List.prototype.init = function(options) {
|
54 | debug('initalizing');
|
55 |
|
56 |
|
57 | this.define('Item', options.Item || List.Item);
|
58 | this.define('View', options.View || List.View);
|
59 |
|
60 |
|
61 | this.use(plugin.init);
|
62 | this.use(plugin.renameKey());
|
63 | this.use(plugin.context);
|
64 | this.use(utils.engines());
|
65 | this.use(utils.helpers());
|
66 | this.use(utils.routes());
|
67 | this.use(plugin.item('item', 'Item', {emit: false}));
|
68 | this.use(plugin.item('view', 'View', {emit: false}));
|
69 |
|
70 |
|
71 | plugin.is(this.Item);
|
72 | plugin.is(this.View);
|
73 |
|
74 | this.queue = [];
|
75 | this.items = [];
|
76 | this.keys = [];
|
77 |
|
78 |
|
79 | decorate(this, 'paginate');
|
80 | decorate(this, 'filter', 'items');
|
81 | decorate(this, 'groupBy', 'items');
|
82 | decorate(this, 'sortBy', 'items');
|
83 |
|
84 |
|
85 | if (Array.isArray(options)) {
|
86 | this.options = {};
|
87 | this.addList(options);
|
88 |
|
89 | } else if (options.isList) {
|
90 | this.options = options.options;
|
91 | this.addList(options.items);
|
92 |
|
93 | } else if (options.isCollection) {
|
94 | this.options = options.options;
|
95 | this.addItems(options.views);
|
96 |
|
97 | } else {
|
98 | this.options = options;
|
99 | }
|
100 | };
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | List.prototype.addItem = function(key, value) {
|
119 | var item = this.item(key, value);
|
120 | utils.setInstanceNames(item, 'Item');
|
121 |
|
122 | debug('adding item "%s"', item.key);
|
123 |
|
124 | if (this.options.pager === true) {
|
125 | List.addPager(item, this.items);
|
126 | }
|
127 |
|
128 | this.keys.push(item.key);
|
129 | if (typeof item.use === 'function') {
|
130 | this.run(item);
|
131 | }
|
132 |
|
133 | this.extendItem(item);
|
134 | this.emit('load', item, this);
|
135 | this.emit('item', item, this);
|
136 |
|
137 | this.items.push(item);
|
138 | return item;
|
139 | };
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 | List.prototype.setItem = function(/*key, value*/) {
|
157 | this.addItem.apply(this, arguments);
|
158 | return this;
|
159 | };
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | List.prototype.addItems = function(items) {
|
177 | if (Array.isArray(items)) {
|
178 | return this.addList.apply(this, arguments);
|
179 | }
|
180 |
|
181 | this.emit('addItems', items);
|
182 | if (this.loaded) {
|
183 | this.loaded = false;
|
184 | return this;
|
185 | }
|
186 |
|
187 | this.visit('addItem', items);
|
188 | return this;
|
189 | };
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 | List.prototype.addList = function(list, fn) {
|
206 | this.emit.call(this, 'addList', list);
|
207 | if (this.loaded) {
|
208 | this.loaded = false;
|
209 | return this;
|
210 | }
|
211 |
|
212 | if (!Array.isArray(list)) {
|
213 | throw new TypeError('expected list to be an array.');
|
214 | }
|
215 |
|
216 | var len = list.length, i = -1;
|
217 | while (++i < len) {
|
218 | var item = list[i];
|
219 | if (typeof fn === 'function') {
|
220 | item = fn(item) || item;
|
221 | }
|
222 | this.addItem(item.path, item);
|
223 | }
|
224 | return this;
|
225 | };
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | List.prototype.hasItem = function(key) {
|
241 | return this.getIndex(key) !== -1;
|
242 | };
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 | List.prototype.getIndex = function(key) {
|
257 | if (typeof key === 'undefined') {
|
258 | throw new Error('expected a string or instance of Item');
|
259 | }
|
260 |
|
261 | if (List.isItem(key)) {
|
262 | key = key.key;
|
263 | }
|
264 |
|
265 | var idx = this.keys.indexOf(key);
|
266 | if (idx !== -1) {
|
267 | return idx;
|
268 | }
|
269 |
|
270 | idx = this.keys.indexOf(this.renameKey(key));
|
271 | if (idx !== -1) {
|
272 | return idx;
|
273 | }
|
274 |
|
275 | var items = this.items;
|
276 | var len = items.length;
|
277 |
|
278 | while (len--) {
|
279 | var item = items[len];
|
280 | var prop = this.renameKey(key, item);
|
281 | if (utils.matchFile(prop, item)) {
|
282 | return len;
|
283 | }
|
284 | }
|
285 | return -1;
|
286 | };
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 | List.prototype.getItem = function(key) {
|
301 | var idx = this.getIndex(key);
|
302 | if (idx !== -1) {
|
303 | return this.items[idx];
|
304 | }
|
305 | };
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | List.prototype.getView = function() {
|
320 | return this.getItem.apply(this, arguments);
|
321 | };
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | List.prototype.deleteItem = function(item) {
|
334 | var idx = this.getIndex(item);
|
335 | if (idx !== -1) {
|
336 | this.items.splice(idx, 1);
|
337 | this.keys.splice(idx, 1);
|
338 | }
|
339 | return this;
|
340 | };
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 | List.prototype.deleteItems = function(items) {
|
353 | if (!Array.isArray(items)) {
|
354 | return this.deleteItem.apply(this, arguments);
|
355 | }
|
356 | for (var i = 0; i < items.length; i++) {
|
357 | this.deleteItem(items[i]);
|
358 | }
|
359 | return this.items;
|
360 | };
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 | List.prototype.extendItem = function(item) {
|
373 | plugin.view(this, item);
|
374 | return this;
|
375 | };
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | List.prototype.filter = function(fn) {
|
391 | var list = new List(this.options);
|
392 | list.addItems(this.items.slice().filter(fn));
|
393 | return list;
|
394 | };
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | List.prototype.sortBy = function(items) {
|
412 | var args = [].slice.call(arguments);
|
413 | var last = args[args.length - 1];
|
414 | var opts = this.options.sort || {};
|
415 |
|
416 |
|
417 | if (last && typeof last === 'object' && !Array.isArray(last)) {
|
418 | opts = utils.extend({}, opts, args.pop());
|
419 | }
|
420 |
|
421 | if (!Array.isArray(items)) {
|
422 | args.unshift(this.items.slice());
|
423 | }
|
424 |
|
425 |
|
426 | args.push(opts);
|
427 |
|
428 |
|
429 | items = utils.sortBy.apply(utils.sortBy, args);
|
430 | var list = new List(this.options);
|
431 | list.addItems(items);
|
432 | return list;
|
433 | };
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 | List.prototype.groupBy = function(items) {
|
450 | var args = arguments;
|
451 | var fn = utils.groupBy;
|
452 |
|
453 | if (!Array.isArray(items)) {
|
454 |
|
455 | args = [].slice.call(arguments);
|
456 | args.unshift(this.items.slice());
|
457 | }
|
458 |
|
459 |
|
460 | return new Group(fn.apply(fn, args));
|
461 | };
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 | List.prototype.paginate = function(items, options) {
|
477 | var fn = utils.paginationator;
|
478 | var args = arguments;
|
479 |
|
480 | if (!Array.isArray(items)) {
|
481 | args = [].slice.call(arguments);
|
482 |
|
483 | args.unshift(this.items.slice());
|
484 | }
|
485 |
|
486 |
|
487 | items = fn.apply(fn, args);
|
488 | return items.pages;
|
489 | };
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 | List.prototype.resolveLayout = function(item) {
|
499 | if (utils.isRenderable(item) && typeof item.layout === 'undefined') {
|
500 | return this.option('layout');
|
501 | }
|
502 | return item.layout;
|
503 | };
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 | List.addPager = function addPager(item, items) {
|
514 | var len = items.length;
|
515 | var prev = items[len - 1] || null;
|
516 |
|
517 | item.data.pager = {};
|
518 | utils.define(item.data.pager, 'isPager', true);
|
519 | utils.define(item.data.pager, 'current', item);
|
520 |
|
521 | item.data.pager.index = len;
|
522 | item.data.pager.isFirst = len === 0;
|
523 | item.data.pager.first = items[0] || item;
|
524 | item.data.pager.prev = prev;
|
525 |
|
526 | if (prev) {
|
527 | prev.data.pager.next = item;
|
528 | }
|
529 |
|
530 | Object.defineProperty(item.data.pager, 'isLast', {
|
531 | configurable: true,
|
532 | enumerable: true,
|
533 | get: function() {
|
534 | return this.index === items.length - 1;
|
535 | }
|
536 | });
|
537 |
|
538 | Object.defineProperty(item.data.pager, 'last', {
|
539 | configurable: true,
|
540 | enumerable: true,
|
541 | get: function() {
|
542 | return items[items.length - 1];
|
543 | }
|
544 | });
|
545 | };
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | utils.define(List, 'Item', require('vinyl-item'));
|
552 | utils.define(List, 'View', require('vinyl-view'));
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 | function decorate(list, method, prop) {
|
559 | utils.define(list.items, method, function() {
|
560 | var res = list[method].apply(list, arguments);
|
561 | return prop ? res[prop] : res;
|
562 | });
|
563 | }
|