forked from cmather/blaze-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.js
362 lines (307 loc) · 10 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
var isLogging = false;
var log = function (msg) {
if (!isLogging)
return;
if (arguments.length > 1)
msg = _.toArray(arguments).join(' ');
console.log('%c<BlazeLayout> ' + msg, 'color: green; font-weight: bold; font-size: 1.3em;');
};
/*****************************************************************************/
/* Meteor Functions */
/*
* These are copied from Core because we need to throw an error at lookup time
* if a template is not found. The Component.lookup method does not give us a
* way to do that. We should construct a proper pull request and send to Meteor.
* Probably the ability to pass a not found callback or something to the lookup
* method as an option.
/*****************************************************************************/
var findComponentWithProp = function (id, comp) {
while (comp) {
if (typeof comp[id] !== 'undefined')
return comp;
comp = comp.parent;
}
return null;
};
var getComponentData = function (comp) {
comp = findComponentWithProp('data', comp);
return (comp ?
(typeof comp.data === 'function' ?
comp.data() : comp.data) :
null);
};
/*****************************************************************************/
/* End Meteor Functions */
/*****************************************************************************/
/**
* Find a template object.
*
* Similar to Component.lookupTemplate with two differences:
*
* 1. Throw an error if we can't find the template. This is useful in debugging
* vs. silently failing.
*
* 2. If the template is a property on the component, don't call
* getComponentData(self), thereby creating an unnecessary data dependency. This
* was initially causing problems with {{> yield}}
*/
var lookupTemplate = function (name) {
// self should be an instance of Layout
var self = this;
var comp;
var result;
var contentBlocksByRegion = self.get('_contentBlocksByRegion');
if (!name)
throw new Error("BlazeLayout: You must pass a name to lookupTemplate");
if (contentBlocksByRegion[name]) {
result = contentBlocksByRegion[name];
} else if ((comp = findComponentWithProp(name, self))) {
result = comp[name];
} else if (_.has(Template, name)) {
result = Template[name];
} else if (Handlebars._globalHelpers[name]) {
result = Handlebars._globalHelpers[name];
}
if (typeof result === 'function' && !result._isEmboxedConstant) {
return function (/* args */ ) {
// modified from Core to call function in context of the
// component, not a data context.
return result.apply(self, arguments);
}
} else if (result) {
return result
} else {
throw new Error("BlazeLayout: Sorry, couldn't find a template named " + name + ". Are you sure you defined it?");
}
}
Layout = UI.Component.extend({
init: function () {
var self = this;
var layout = this;
var tmpl = Deps.nonreactive(function () {
return self.get('template') || '_defaultLayout';
});
var tmplDep = new Deps.Dependency;
// get the initial data value
var data = Deps.nonreactive(function () { return self.get(); });
var dataDep = new Deps.Dependency;
var regions = this._regions = new ReactiveDict;
var content = this.__content;
// a place to put content defined like this:
// {{#contentFor region="footer"}}content{{/contentFor}}
// this will be searched in the lookup chain.
var contentBlocksByRegion = this._contentBlocksByRegion = {};
// the default main region will be the content block
// for this component. example:
// {{#Layout template="MyLayout"}}
// Content block in here becomes main region
// {{/Layout}}
regions.set('main', '_defaultMainRegion');
/**
* instance methods
*/
this.template = function (value) {
if (typeof value !== 'undefined') {
// make sure we convert false and null
// values to the _defaultLayout so when
// we compare to our existing template
// we don't re-render the default layout
// unnecessarily.
if (value === false || value === null)
value = '_defaultLayout';
if (!EJSON.equals(value, tmpl)) {
tmpl = value;
tmplDep.changed();
}
} else {
tmplDep.depend();
return tmpl || '_defaultLayout';
}
};
var emboxedData = UI.emboxValue(function () {
log('return data()');
dataDep.depend();
return data;
});
this.setData = function (value) {
log('setData', value);
if (!EJSON.equals(value, data)) {
data = value;
dataDep.changed();
}
};
this.getData = function () {
return emboxedData();
};
this.data = function () {
return self.getData();
};
/**
* Set a region template.
*
* If you want to get the template for a region
* you need to call this._regions.get('key');
*
*/
this.setRegion = function (key, value) {
if (arguments.length < 2) {
value = key;
key = 'main';
} else if (typeof key === 'undefined') {
key = 'main';
}
regions.set(key, value);
};
//TODO add test
this.getRegionKeys = function () {
return _.keys(regions.keys);
};
//TODO add test
this.clearRegion = function (key) {
regions.set(key, null);
};
// define a yield region to render templates into
this.yield = UI.Component.extend({
init: function () {
var self = this;
var data = Deps.nonreactive(function () { return self.get(); });
var region = self.region = (data && data.region) || 'main';
// reset the data function to use the layout's
// data
this.data = function () {
return layout.getData();
};
},
render: function () {
var self = this;
var region = self.region;
// returning a function tells UI.materialize to
// create a computation. then, if the region template
// changes, this comp will be rerun and the new template
// will get put on the screen.
return function () {
var regions = Deps.nonreactive(function () {
return self.get('_regions');
});
// create a reactive dep
var tmpl = regions.get(region);
// don't call lookup if tmpl is undefined
if (tmpl) {
return lookupTemplate.call(self, tmpl);
}
};
}
});
// render content into a yield region using markup. when you call setRegion
// manually, you specify a string, not a content block. And the
// lookupTemplate method uses this string name to find the template. Since
// contentFor creates anonymous content we need a way to add this into the
// lookup chain. But then we need to destroy it if it's not used anymore.
// not sure how to do this.
this.contentFor = UI.Component.extend({
init: function () {
var self = this;
var data = Deps.nonreactive(function () { return self.get(); });
var region = self.region = data.region;
if (!region)
throw new Error("{{#contentFor}} requires a region argument like this: {{#contentFor region='footer'}}");
},
render: function () {
var self = this;
var region = self.region;
var contentBlocksByRegion = layout._contentBlocksByRegion;
if (contentBlocksByRegion[region]) {
delete contentBlocksByRegion[region];
}
// store away the content block so we can find it during lookup
// which happens in the yield function.
contentBlocksByRegion[region] = self.__content;
// this will just set the region to itself but when the lookupTemplate
// function searches it will check contentBlocksByRegion first, so we'll
// find the content block there.
layout.setRegion(region, region);
// don't render anything for now. let the yield template control this.
return null;
}
});
/**
* By default the main region will be the content block
* if the layout was used directly in your template like this:
*
* {{#Layout}}
* content block goes into main {{> yield}}
* {{/Layout}}
*/
this._defaultMainRegion = function () {
return content || null;
};
this._defaultLayout = function () {
return UI.block(function () {
return lookupTemplate.call(layout, 'yield');
});
};
},
render: function () {
var self = this;
// return a function to create a reactive
// computation. so if the template changes
// the layout is re-endered.
return function () {
// reactive
var tmplName = self.template();
var tmpl = Deps.nonreactive(function () {
return lookupTemplate.call(self, tmplName);
});
log('rendering layout: ' + tmplName);
return tmpl;
};
}
});
/**
* Put Layout into the template lookup chain so
* we can do this:
* {{#Layout template="MyLayout"}}
* Some content
* {{/Layout}}
*/
Template.Layout = Layout;
BlazeUIManager = function (router) {
var self = this;
this.router = router;
this._component = null;
_.each([
'setRegion',
'clearRegion',
'getRegionKeys',
'getData',
'setData'
], function (method) {
self[method] = function () {
if (self._component) {
return self._component[method].apply(this, arguments);
}
};
});
// proxy the "layout" method to the underlying component's
// "template" method.
self.layout = function () {
if (self._component)
return self._component.template.apply(this, arguments);
else
throw new Error('Layout has not been rendered yet');
};
};
BlazeUIManager.prototype = {
render: function (props) {
this._component = UI.render(Layout.extend(props || {}));
return this._component;
},
insert: function (parent) {
UI.DomRange.insert(this.render().dom, parent || document.body);
}
};
if (Package['iron-router']) {
Package['iron-router'].Router.configure({
uiManager: new BlazeUIManager
});
}