-
Notifications
You must be signed in to change notification settings - Fork 11
/
layout.js
426 lines (365 loc) · 11.7 KB
/
layout.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*****************************************************************************/
/* Imports */
/*****************************************************************************/
var DynamicTemplate = Iron.DynamicTemplate;
var inherits = Iron.utils.inherits;
/*****************************************************************************/
/* Helpers */
/*****************************************************************************/
/**
* Find the first Layout in the rendered parent hierarchy.
*/
findFirstLayout = function (view) {
while (view) {
if (view.name === 'Iron.Layout')
return view.__dynamicTemplate__;
else
view = view.parentView;
}
return null;
};
/*****************************************************************************/
/* Layout */
/*****************************************************************************/
/**
* Dynamically render templates into regions.
*
* Layout inherits from Iron.DynamicTemplate and provides the ability to create
* regions that a user can render templates or content blocks into. The layout
* and each region is an instance of DynamicTemplate so the template and data
* contexts are completely dynamic and programmable in javascript.
*/
Layout = function (options) {
var self = this;
Layout.__super__.constructor.apply(this, arguments);
options = options || {};
this.name = 'Iron.Layout';
this._regions = {};
this._regionHooks = {};
this.defaultTemplate('__IronDefaultLayout__');
// if there's block content then render that
// to the main region
if (options.content)
this.render(options.content);
};
/**
* The default region for a layout where the main content will go.
*/
DEFAULT_REGION = Layout.DEFAULT_REGION = 'main';
/**
* Inherits from Iron.DynamicTemplate which gives us the ability to set the
* template and data context dynamically.
*/
inherits(Layout, Iron.DynamicTemplate);
/**
* Return the DynamicTemplate instance for a given region. If the region doesn't
* exist it is created.
*
* The regions object looks like this:
*
* {
* "main": DynamicTemplate,
* "footer": DynamicTemplate,
* .
* .
* .
* }
*/
Layout.prototype.region = function (name, options) {
return this._ensureRegion(name, options);
};
/**
* Destroy all child regions and reset the regions map.
*/
Layout.prototype.destroyRegions = function () {
_.each(this._regions, function (dynamicTemplate) {
dynamicTemplate.destroy();
});
this._regions = {};
};
/**
* Set the template for a region.
*/
Layout.prototype.render = function (template, options) {
// having options is usually good
options = options || {};
// let the user specify the region to render the template into
var region = options.to || options.region || DEFAULT_REGION;
// get the DynamicTemplate for this region
var dynamicTemplate = this.region(region);
// if we're in a rendering transaction, track that we've rendered this
// particular region
this._trackRenderedRegion(region);
// set the template value for the dynamic template
dynamicTemplate.template(template);
// set the data for the region. If options.data is not defined, this will
// clear the data, which is what we want
dynamicTemplate.data(options.data);
};
/**
* Returns true if the given region is defined and false otherwise.
*/
Layout.prototype.has = function (region) {
region = region || Layout.DEFAULT_REGION;
return !!this._regions[region];
};
/**
* Returns an array of region keys.
*/
Layout.prototype.regionKeys = function () {
return _.keys(this._regions);
};
/**
* Clear a given region or the "main" region by default.
*/
Layout.prototype.clear = function (region) {
region = region || Layout.DEFAULT_REGION;
// we don't want to create a region if it didn't exist before
if (this.has(region))
this.region(region).template(null);
// chain it up
return this;
};
/**
* Clear all regions.
*/
Layout.prototype.clearAll = function () {
_.each(this._regions, function (dynamicTemplate) {
dynamicTemplate.template(null);
});
// chain it up
return this;
};
/**
* Start tracking rendered regions.
*/
Layout.prototype.beginRendering = function (onComplete) {
var self = this;
if (this._finishRenderingTransaction)
this._finishRenderingTransaction();
this._finishRenderingTransaction = _.once(function () {
var regions = self._endRendering({flush: false});
onComplete && onComplete(regions);
});
Deps.afterFlush(this._finishRenderingTransaction);
if (this._renderedRegions)
throw new Error("You called beginRendering again before calling endRendering");
this._renderedRegions = {};
};
/**
* Track a rendered region if we're in a transaction.
*/
Layout.prototype._trackRenderedRegion = function (region) {
if (!this._renderedRegions)
return;
this._renderedRegions[region] = true;
};
/**
* Stop a rendering transaction and retrieve the rendered regions. This
* shouldn't be called directly. Instead, pass an onComplete callback to the
* beginRendering method.
*/
Layout.prototype._endRendering = function (opts) {
// we flush here to ensure all of the {{#contentFor}} inclusions have had a
// chance to render from our templates, otherwise we'll never know about
// them.
opts = opts || {};
if (opts.flush !== false)
Deps.flush();
var renderedRegions = this._renderedRegions || {};
this._renderedRegions = null;
return _.keys(renderedRegions);
};
/**
* View lifecycle hooks for regions.
*/
_.each(
[
'onRegionCreated',
'onRegionRendered',
'onRegionDestroyed'
],
function (hook) {
Layout.prototype[hook] = function (cb) {
var hooks = this._regionHooks[hook] = this._regionHooks[hook] || [];
hooks.push(cb);
return this;
}
}
);
/**
* Returns the DynamicTemplate for a given region or creates it if it doesn't
* exists yet.
*/
Layout.prototype._ensureRegion = function (name, options) {
return this._regions[name] = this._regions[name] || this._createDynamicTemplate(name, options);
};
/**
* Create a new DynamicTemplate instance.
*/
Layout.prototype._createDynamicTemplate = function (name, options) {
var self = this;
var tmpl = new Iron.DynamicTemplate(options);
var capitalize = Iron.utils.capitalize;
tmpl._region = name;
_.each(['viewCreated', 'viewReady', 'viewDestroyed'], function (hook) {
hook = capitalize(hook);
tmpl['on' + hook](function (dynamicTemplate) {
// "this" is the view instance
var view = this;
var regionHook = ({
viewCreated: "regionCreated",
viewReady: "regionRendered",
viewDestroyed: "regionDestroyed"
})[hook];
self._runRegionHooks('on' + regionHook, view, dynamicTemplate);
});
});
return tmpl;
};
Layout.prototype._runRegionHooks = function (name, regionView, regionDynamicTemplate) {
var layout = this;
var hooks = this._regionHooks[name] || [];
var hook;
for (var i = 0; i < hooks.length; i++) {
hook = hooks[i];
// keep the "thisArg" pointing to the view, but make the first parameter to
// the callback teh dynamic template instance.
hook.call(regionView, regionDynamicTemplate.region, regionDynamicTemplate, this);
}
};
/*****************************************************************************/
/* UI Helpers */
/*****************************************************************************/
if (typeof Template !== 'undefined') {
/**
* Create a region in the closest layout ancestor.
*
* Examples:
* <aside>
* {{> yield "aside"}}
* </aside>
*
* <article>
* {{> yield}}
* </article>
*
* <footer>
* {{> yield "footer"}}
* </footer>
*/
UI.registerHelper('yield', new Template('yield', function () {
var layout = findFirstLayout(this);
if (!layout)
throw new Error("No Iron.Layout found so you can't use yield!");
// Example options: {{> yield region="footer"}} or {{> yield "footer"}}
var options = DynamicTemplate.getInclusionArguments(this);
var region;
var dynamicTemplate;
if (_.isString(options)) {
region = options;
} else if (_.isObject(options)) {
region = options.region;
}
// if there's no region specified we'll assume you meant the main region
region = region || DEFAULT_REGION;
// get or create the region
dynamicTemplate = layout.region(region);
// if the dynamicTemplate had already been inserted, let's
// destroy it before creating a new one.
if (dynamicTemplate.isCreated)
dynamicTemplate.destroy();
// now return a newly created view
return dynamicTemplate.create();
}));
/**
* Render a template into a region in the closest layout ancestor from within
* your template markup.
*
* Examples:
*
* {{#contentFor "footer"}}
* Footer stuff
* {{/contentFor}}
*
* {{> contentFor region="footer" template="SomeTemplate" data=someData}}
*
* Note: The helper is a UI.Component object instead of a function so that
* Meteor UI does not create a Deps.Dependency.
*
* XXX what happens if the parent that calls contentFor gets destroyed?
* XXX the layout.region should be reset to be empty?
* XXX but how do we control order of setting the region? what if it gets destroyed but then something else sets it?
*
*/
UI.registerHelper('contentFor', new Template('contentFor', function () {
var layout = findFirstLayout(this);
if (!layout)
throw new Error("No Iron.Layout found so you can't use contentFor!");
var options = DynamicTemplate.getInclusionArguments(this) || {}
var content = this.templateContentBlock;
var template = options.template;
var data = options.data;
var region;
if (_.isString(options))
region = options;
else if (_.isObject(options))
region = options.region;
else
throw new Error("Which region is this contentFor block supposed to be for?");
// set the region to a provided template or the content directly.
layout.region(region).template(template || content);
// tell the layout to track this as a rendered region if we're in a
// rendering transaction.
layout._trackRenderedRegion(region);
// if we have some data then set the data context
if (data)
layout.region(region).data(data);
// just render nothing into this area of the page since the dynamic template
// will do the actual rendering into the right region.
return null;
}));
/**
* Check to see if a given region is currently rendered to.
*
* Example:
* {{#if hasRegion 'aside'}}
* <aside>
* {{> yield "aside"}}
* </aside>
* {{/if}}
*/
UI.registerHelper('hasRegion', function (region) {
var layout = findFirstLayout(Blaze.getView());
if (!layout)
throw new Error("No Iron.Layout found so you can't use hasRegion!");
if (!_.isString(region))
throw new Error("You need to provide an region argument to hasRegion");
return !! layout.region(region).template();
});
/**
* Let people use Layout directly from their templates!
*
* Example:
* {{#Layout template="MyTemplate"}}
* Main content goes here
*
* {{#contentFor "footer"}}
* footer goes here
* {{/contentFor}}
* {{/Layout}}
*/
UI.registerHelper('Layout', new Template('layout', function () {
var args = Iron.DynamicTemplate.args(this);
var layout = new Layout({
template: function () { return args('template'); },
data: function () { return args('data'); },
content: this.templateContentBlock
});
return layout.create();
}));
}
/*****************************************************************************/
/* Namespacing */
/*****************************************************************************/
Iron.Layout = Layout;