1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var Base = require('base');
|
5 | var debug = require('debug')('base:templates:views');
|
6 | var plugin = require('./plugins');
|
7 | var utils = require('./utils');
|
8 | var List = require('./list');
|
9 | var routes = require('base-routes');
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports = exports = Views;
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function Views(options) {
|
29 | if (!(this instanceof Views)) {
|
30 | return new Views(options);
|
31 | }
|
32 |
|
33 | Base.call(this);
|
34 |
|
35 | this.is('views');
|
36 | this.define('isCollection', true);
|
37 | this.use(utils.option());
|
38 | this.use(utils.plugin());
|
39 | this.init(options || {});
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | plugin.static(Base, Views, 'Views');
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | Views.prototype.init = function(opts) {
|
57 | debug('initializing');
|
58 |
|
59 |
|
60 | this.define('Item', opts.Item || Views.Item);
|
61 | this.define('View', opts.View || Views.View);
|
62 |
|
63 |
|
64 | this.use(plugin.init);
|
65 | this.use(plugin.renameKey());
|
66 | this.use(plugin.context);
|
67 | this.use(utils.engines());
|
68 | this.use(utils.helpers());
|
69 | this.use(utils.routes());
|
70 | this.use(plugin.item('view', 'View', {emit: false}));
|
71 |
|
72 |
|
73 | this.listen(this);
|
74 | this.views = {};
|
75 |
|
76 |
|
77 | if (Array.isArray(opts) || opts.isList) {
|
78 | this.option(opts.options || {});
|
79 | this.addList(opts.items || opts);
|
80 |
|
81 | } else if (opts.isCollection) {
|
82 | this.option(opts.options || {});
|
83 | this.addViews(opts.views);
|
84 |
|
85 | } else {
|
86 | this.option(opts);
|
87 | }
|
88 | };
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | Views.prototype.listen = function(collection) {
|
95 |
|
96 |
|
97 | this.on('use', function(fn) {
|
98 | if (typeof fn !== 'function') return;
|
99 | for (var key in collection.views) {
|
100 | if (collection.views.hasOwnProperty(key)) {
|
101 | var view = collection.views[key];
|
102 | if (typeof view.use === 'function') {
|
103 | view.use(fn);
|
104 | }
|
105 | }
|
106 | }
|
107 | });
|
108 | };
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | Views.prototype.addView = function(key, value) {
|
127 | var view = this.view(key, value);
|
128 | debug('adding view "%s"', view.path);
|
129 |
|
130 |
|
131 | this.setType(view);
|
132 |
|
133 |
|
134 | var name = this.options.inflection || 'view';
|
135 | utils.setInstanceNames(view, name);
|
136 |
|
137 |
|
138 | if (typeof view.use === 'function') {
|
139 | this.run(view);
|
140 | }
|
141 |
|
142 |
|
143 | this.emit('load', view, this);
|
144 | this.emit('view', view, this);
|
145 | this.emit(name, view, this);
|
146 | this.extendView(view);
|
147 |
|
148 |
|
149 | this.views[view.key] = view;
|
150 | return view;
|
151 | };
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | Views.prototype.setView = function(/*key, value*/) {
|
169 | this.addView.apply(this, arguments);
|
170 | return this;
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | Views.prototype.getView = function(name, options, fn) {
|
186 | if (typeof name !== 'string') {
|
187 | throw new TypeError('expected a string');
|
188 | }
|
189 |
|
190 | debug('getting view "%s"', name);
|
191 | if (typeof options === 'function') {
|
192 | fn = options;
|
193 | options = {};
|
194 | }
|
195 |
|
196 | var view = this.views[name] || this.views[this.renameKey(name)];
|
197 | if (view) return view;
|
198 |
|
199 | view = utils.getView(name, this.views, fn);
|
200 | if (view) return view;
|
201 |
|
202 | if (utils.fileExists(name)) {
|
203 | return this.addView(name, {
|
204 | contents: fs.readFileSync(name)
|
205 | });
|
206 | }
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 | Views.prototype.deleteView = function(view) {
|
221 | if (typeof view === 'string') {
|
222 | view = this.getView(view);
|
223 | }
|
224 | debug('deleting view "%s"', view.key);
|
225 | delete this.views[view.key];
|
226 | return this;
|
227 | };
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | Views.prototype.addViews = function(views, view) {
|
245 | this.emit('addViews', views);
|
246 | if (utils.hasGlob(views)) {
|
247 | var name = '"' + this.options.plural + '"';
|
248 | throw new Error('glob patterns are not supported by the ' + name + ' collection');
|
249 | }
|
250 | if (Array.isArray(views)) {
|
251 | return this.addList.apply(this, arguments);
|
252 | }
|
253 | if (arguments.length > 1 && utils.isView(view)) {
|
254 | this.addView.apply(this, arguments);
|
255 | return this;
|
256 | }
|
257 | this.visit('addView', views);
|
258 | return this;
|
259 | };
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | Views.prototype.addList = function(list, fn) {
|
277 | this.emit('addList', list);
|
278 |
|
279 | if (utils.hasGlob(list)) {
|
280 | var name = '"' + this.options.plural + '"';
|
281 | throw new Error('glob patterns are not supported by the ' + name + ' collection');
|
282 | }
|
283 |
|
284 | if (!Array.isArray(list)) {
|
285 | throw new TypeError('expected list to be an array.');
|
286 | }
|
287 | if (typeof fn !== 'function') {
|
288 | fn = utils.identity;
|
289 | }
|
290 |
|
291 | var len = list.length;
|
292 | var idx = -1;
|
293 | while (++idx < len) {
|
294 | this.addView(fn(list[idx]));
|
295 | }
|
296 | return this;
|
297 | };
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 | Views.prototype.groupBy = function() {
|
314 | var list = new List(this);
|
315 | return list.groupBy.apply(list, arguments);
|
316 | };
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 | Views.prototype.extendView = function(view) {
|
329 | return plugin.view(this, view, this.options);
|
330 | };
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | Views.prototype.isType = function(type) {
|
344 | if (!this.options.viewType || !this.options.viewType.length) {
|
345 | this.viewType();
|
346 | }
|
347 | return this.options.viewType.indexOf(type) !== -1;
|
348 | };
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 | Views.prototype.viewType = function(types) {
|
358 | this.options.viewType = utils.arrayify(this.options.viewType);
|
359 | types = utils.arrayify(types);
|
360 |
|
361 | var validTypes = ['partial', 'layout', 'renderable'];
|
362 | var len = types.length;
|
363 | var idx = -1;
|
364 |
|
365 | while (++idx < len) {
|
366 | var type = types[idx];
|
367 | if (validTypes.indexOf(type) === -1) {
|
368 | var msg = 'Invalid viewType: "' + type
|
369 | + '". viewTypes must be either: "partial", '
|
370 | + '"renderable", or "layout".';
|
371 | throw new Error(msg);
|
372 | }
|
373 | if (this.options.viewType.indexOf(type) === -1) {
|
374 | this.options.viewType.push(type);
|
375 | }
|
376 | }
|
377 |
|
378 | if (this.options.viewType.length === 0) {
|
379 | this.options.viewType.push('renderable');
|
380 | }
|
381 | return this.options.viewType;
|
382 | };
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 | Views.prototype.viewTypes = function() {
|
391 | return this.viewType.apply(this, arguments);
|
392 | };
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | Views.prototype.setType = function(view) {
|
401 | view.options.viewType = utils.arrayify(view.options.viewType);
|
402 | var types = this.viewType();
|
403 | var len = types.length;
|
404 |
|
405 | while (len--) {
|
406 | var type = types[len];
|
407 | if (view.options.viewType.indexOf(type) === -1) {
|
408 | view.options.viewType.push(type);
|
409 | }
|
410 | }
|
411 | view.options.viewType.sort();
|
412 | };
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | Views.prototype.resolveLayout = function(view) {
|
422 | if (!utils.isPartial(view) && typeof view.layout === 'undefined') {
|
423 | return this.option('layout');
|
424 | }
|
425 | return view.layout;
|
426 | };
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 | utils.define(Views, 'Item', require('vinyl-item'));
|
433 | utils.define(Views, 'View', require('vinyl-view'));
|