1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | 'use strict';
|
9 |
|
10 | var Base = require('base');
|
11 | var debug = require('debug')('base:templates');
|
12 | var helpers = require('./lib/helpers/');
|
13 | var plugin = require('./lib/plugins/');
|
14 | var utils = require('./lib/utils');
|
15 | var lib = require('./lib/');
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | module.exports = exports = Templates;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function Templates(options) {
|
37 | if (!(this instanceof Templates)) {
|
38 | return new Templates(options);
|
39 | }
|
40 |
|
41 | Base.call(this, null, options);
|
42 | this.is('templates');
|
43 | this.define('isApp', true);
|
44 | this.use(utils.option());
|
45 | this.use(utils.plugin());
|
46 | this.initTemplates();
|
47 | }
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | plugin.static(Base, Templates, 'Templates');
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | Templates.prototype.initTemplates = function() {
|
60 | debug('initializing <%s>, called from <%s>', __filename, module.parent.id);
|
61 | Templates.emit('templates.preInit', this);
|
62 |
|
63 | this.items = {};
|
64 | this.views = {};
|
65 | this.inflections = {};
|
66 |
|
67 |
|
68 | this.listen(this);
|
69 |
|
70 | this.define('utils', utils);
|
71 | this.use(plugin.init);
|
72 | this.use(plugin.renameKey());
|
73 | this.use(plugin.context);
|
74 | this.use(plugin.lookup);
|
75 | this.use(utils.engines());
|
76 | this.use(utils.helpers());
|
77 | this.use(utils.routes())
|
78 |
|
79 | this.use(plugin.item('item', 'Item'));
|
80 | this.use(plugin.item('view', 'View'));
|
81 |
|
82 | for (var key in this.options.mixins) {
|
83 | this.mixin(key, this.options.mixins[key]);
|
84 | }
|
85 |
|
86 |
|
87 | helpers.view(this);
|
88 |
|
89 |
|
90 | this.expose('Item');
|
91 | this.expose('View');
|
92 | this.expose('List');
|
93 | this.expose('Collection');
|
94 | this.expose('Group');
|
95 | this.expose('Views');
|
96 |
|
97 | Templates.setup(this, 'Templates');
|
98 | Templates.emit('templates.postInit', this);
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | Templates.prototype.expose = function(name) {
|
107 | this.define(name, {
|
108 | configurable: true,
|
109 | enumerable: true,
|
110 | get: function() {
|
111 | return this.options[name] || Templates[name];
|
112 | }
|
113 | });
|
114 | };
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 | Templates.prototype.listen = function(app) {
|
121 | this.on('option', function(key, value) {
|
122 | utils.updateOptions(app, key, value);
|
123 | });
|
124 |
|
125 |
|
126 |
|
127 | this.on('use', function(fn, app) {
|
128 | if (!fn) return;
|
129 | for (var key in app.views) {
|
130 | if (app.views.hasOwnProperty(key)) {
|
131 | app[key].use(fn);
|
132 | }
|
133 | }
|
134 | });
|
135 | };
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 | Templates.prototype.list = function(opts) {
|
155 | opts = opts || {};
|
156 |
|
157 | if (!opts.isList) {
|
158 | utils.defaults(opts, this.options);
|
159 | }
|
160 |
|
161 | var List = opts.List || this.get('List');
|
162 | var list = {};
|
163 |
|
164 | if (opts.isList === true || opts instanceof List) {
|
165 | list = opts;
|
166 | } else {
|
167 | opts.Item = opts.Item || opts.View || this.get('Item');
|
168 | list = new List(opts);
|
169 | }
|
170 |
|
171 |
|
172 | this.extendList(list, opts);
|
173 |
|
174 |
|
175 | this.emit('list', list, opts);
|
176 | return list;
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | Templates.prototype.collection = function(opts, created) {
|
194 | opts = opts || {};
|
195 |
|
196 | if (!opts.isCollection) {
|
197 | utils.defaults(opts, this.options);
|
198 | }
|
199 |
|
200 | var Collection = opts.Collection || opts.Views || this.get('Views');
|
201 | var collection = {};
|
202 |
|
203 | if (opts.isCollection === true) {
|
204 | collection = opts;
|
205 | } else {
|
206 | opts.Item = opts.Item || opts.View || this.get('View');
|
207 | collection = new Collection(opts);
|
208 | }
|
209 |
|
210 | if (created !== true) {
|
211 |
|
212 | if (collection.isViews) {
|
213 | collection.viewType();
|
214 | }
|
215 |
|
216 |
|
217 | this.run(collection);
|
218 |
|
219 |
|
220 | this.emit('collection', collection, opts);
|
221 | this.extendViews(collection, opts);
|
222 |
|
223 |
|
224 | helpers.singular(this, collection);
|
225 | helpers.plural(this, collection);
|
226 | } else {
|
227 |
|
228 |
|
229 | this.emit('collection', collection, opts);
|
230 | }
|
231 | return collection;
|
232 | };
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | Templates.prototype.create = function(name, opts) {
|
246 | debug('creating view collection: "%s"', name);
|
247 | opts = opts || {};
|
248 |
|
249 | if (!opts.isCollection) {
|
250 | opts = utils.merge({}, this.options, opts);
|
251 | }
|
252 |
|
253 |
|
254 | this.emit('create', name, opts);
|
255 |
|
256 |
|
257 | var collection = this.collection(opts, true);
|
258 | utils.setInstanceNames(collection, name);
|
259 |
|
260 |
|
261 | var single = utils.single(name);
|
262 | var plural = utils.plural(name);
|
263 |
|
264 |
|
265 | this.inflections[single] = plural;
|
266 |
|
267 |
|
268 | collection.option('inflection', single);
|
269 | collection.option('plural', plural);
|
270 |
|
271 |
|
272 | this.viewType(plural, collection.viewType());
|
273 |
|
274 |
|
275 | this.views[plural] = collection.items || collection.views;
|
276 |
|
277 |
|
278 | this.define(plural, function() {
|
279 | return collection.addViews.apply(collection, arguments);
|
280 | });
|
281 | this.define(single, function() {
|
282 | return collection.addView.apply(collection, arguments);
|
283 | });
|
284 |
|
285 |
|
286 |
|
287 | this[plural].__proto__ = collection;
|
288 | this[single].__proto__ = collection;
|
289 |
|
290 |
|
291 |
|
292 | collection.define(plural, this[plural]);
|
293 | collection.define(single, this[single]);
|
294 |
|
295 |
|
296 | this.run(collection);
|
297 |
|
298 |
|
299 |
|
300 | this.extendViews(collection, opts);
|
301 |
|
302 |
|
303 | this.emit('postCreate', collection, opts);
|
304 |
|
305 |
|
306 | helpers.singular(this, collection);
|
307 | helpers.plural(this, collection);
|
308 | return collection;
|
309 | };
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 | Templates.prototype.extendView = function(view, options) {
|
316 | plugin.view(this, view, options);
|
317 | return this;
|
318 | };
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | Templates.prototype.extendViews = function(views, options) {
|
325 | plugin.views(this, views, options);
|
326 | return this;
|
327 | };
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | Templates.prototype.extendList = function(views, options) {
|
334 | plugin.list(this, views, options);
|
335 | return this;
|
336 | };
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | Templates.prototype.resolveLayout = function(view) {
|
343 | debug('resolving layout for "%s"', view.key);
|
344 |
|
345 | if (!utils.isPartial(view) && typeof view.layout === 'undefined') {
|
346 | if (view.options && view.options.collection) {
|
347 | var collection = this[view.options.collection];
|
348 | var layout = collection.resolveLayout(view);
|
349 | if (typeof layout === 'undefined') {
|
350 | layout = this.option('layout');
|
351 | }
|
352 | return layout;
|
353 | }
|
354 | }
|
355 | return view.layout;
|
356 | };
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | Templates.setup = function(app, name) {
|
376 | var setup = app.options['init' + name || app.constructor.name];
|
377 | if (typeof setup === 'function') {
|
378 | setup.call(app, app, app.options);
|
379 | }
|
380 | };
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 | Templates.Base = Base;
|
387 | Templates.Collection = lib.collection;
|
388 | Templates.List = lib.list;
|
389 | Templates.Group = lib.group;
|
390 | Templates.Views = lib.views;
|
391 | Templates.Item = require('vinyl-item');
|
392 | Templates.View = require('vinyl-view');
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 | utils.define(Templates, 'utils', utils);
|
399 | utils.define(Templates, '_', { lib: lib, plugin: plugin });
|