forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuiltins.js
354 lines (319 loc) · 12.4 KB
/
builtins.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
Blaze._calculateCondition = function (cond) {
if (cond instanceof Array && cond.length === 0)
cond = false;
return !! cond;
};
/**
* @summary Constructs a View that renders content with a data context.
* @locus Client
* @param {Object|Function} data An object to use as the data context, or a function returning such an object. If a function is provided, it will be reactively re-run.
* @param {Function} contentFunc A Function that returns [*renderable content*](#renderable_content).
*/
Blaze.With = function (data, contentFunc) {
var view = Blaze.View('with', contentFunc);
view.dataVar = new ReactiveVar;
view.onViewCreated(function () {
if (typeof data === 'function') {
// `data` is a reactive function
view.autorun(function () {
view.dataVar.set(data());
}, view.parentView, 'setData');
} else {
view.dataVar.set(data);
}
});
return view;
};
/**
* Attaches bindings to the instantiated view.
* @param {Object} bindings A dictionary of bindings, each binding name
* corresponds to a value or a function that will be reactively re-run.
* @param {View} view The target.
*/
Blaze._attachBindingsToView = function (bindings, view) {
view.onViewCreated(function () {
_.each(bindings, function (binding, name) {
view._scopeBindings[name] = new ReactiveVar;
if (typeof binding === 'function') {
view.autorun(function () {
view._scopeBindings[name].set(binding());
}, view.parentView);
} else {
view._scopeBindings[name].set(binding);
}
});
});
};
/**
* @summary Constructs a View setting the local lexical scope in the block.
* @param {Function} bindings Dictionary mapping names of bindings to
* values or computations to reactively re-run.
* @param {Function} contentFunc A Function that returns [*renderable content*](#renderable_content).
*/
Blaze.Let = function (bindings, contentFunc) {
var view = Blaze.View('let', contentFunc);
Blaze._attachBindingsToView(bindings, view);
return view;
};
/**
* @summary Constructs a View that renders content conditionally.
* @locus Client
* @param {Function} conditionFunc A function to reactively re-run. Whether the result is truthy or falsy determines whether `contentFunc` or `elseFunc` is shown. An empty array is considered falsy.
* @param {Function} contentFunc A Function that returns [*renderable content*](#renderable_content).
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#renderable_content). If no `elseFunc` is supplied, no content is shown in the "else" case.
*/
Blaze.If = function (conditionFunc, contentFunc, elseFunc, _not) {
var conditionVar = new ReactiveVar;
var view = Blaze.View(_not ? 'unless' : 'if', function () {
return conditionVar.get() ? contentFunc() :
(elseFunc ? elseFunc() : null);
});
view.__conditionVar = conditionVar;
view.onViewCreated(function () {
this.autorun(function () {
var cond = Blaze._calculateCondition(conditionFunc());
conditionVar.set(_not ? (! cond) : cond);
}, this.parentView, 'condition');
});
return view;
};
/**
* @summary An inverted [`Blaze.If`](#blaze_if).
* @locus Client
* @param {Function} conditionFunc A function to reactively re-run. If the result is falsy, `contentFunc` is shown, otherwise `elseFunc` is shown. An empty array is considered falsy.
* @param {Function} contentFunc A Function that returns [*renderable content*](#renderable_content).
* @param {Function} [elseFunc] Optional. A Function that returns [*renderable content*](#renderable_content). If no `elseFunc` is supplied, no content is shown in the "else" case.
*/
Blaze.Unless = function (conditionFunc, contentFunc, elseFunc) {
return Blaze.If(conditionFunc, contentFunc, elseFunc, true /*_not*/);
};
/**
* @summary Constructs a View that renders `contentFunc` for each item in a sequence.
* @locus Client
* @param {Function} argFunc A function to reactively re-run. The function can
* return one of two options:
*
* 1. An object with two fields: '_variable' and '_sequence'. Each iterates over
* '_sequence', it may be a Cursor, an array, null, or undefined. Inside the
* Each body you will be able to get the current item from the sequence using
* the name specified in the '_variable' field.
*
* 2. Just a sequence (Cursor, array, null, or undefined) not wrapped into an
* object. Inside the Each body, the current item will be set as the data
* context.
* @param {Function} contentFunc A Function that returns [*renderable
* content*](#renderable_content).
* @param {Function} [elseFunc] A Function that returns [*renderable
* content*](#renderable_content) to display in the case when there are no items
* in the sequence.
*/
Blaze.Each = function (argFunc, contentFunc, elseFunc) {
var eachView = Blaze.View('each', function () {
var subviews = this.initialSubviews;
this.initialSubviews = null;
if (this._isCreatedForExpansion) {
this.expandedValueDep = new Tracker.Dependency;
this.expandedValueDep.depend();
}
return subviews;
});
eachView.initialSubviews = [];
eachView.numItems = 0;
eachView.inElseMode = false;
eachView.stopHandle = null;
eachView.contentFunc = contentFunc;
eachView.elseFunc = elseFunc;
eachView.argVar = new ReactiveVar;
eachView.variableName = null;
// update the @index value in the scope of all subviews in the range
var updateIndices = function (from, to) {
if (to === undefined) {
to = eachView.numItems - 1;
}
for (var i = from; i <= to; i++) {
var view = eachView._domrange.members[i].view;
view._scopeBindings['@index'].set(i);
}
};
eachView.onViewCreated(function () {
// We evaluate argFunc in an autorun to make sure
// Blaze.currentView is always set when it runs (rather than
// passing argFunc straight to ObserveSequence).
eachView.autorun(function () {
// argFunc can return either a sequence as is or a wrapper object with a
// _sequence and _variable fields set.
var arg = argFunc();
if (_.isObject(arg) && _.has(arg, '_sequence')) {
eachView.variableName = arg._variable || null;
arg = arg._sequence;
}
eachView.argVar.set(arg);
}, eachView.parentView, 'collection');
eachView.stopHandle = ObserveSequence.observe(function () {
return eachView.argVar.get();
}, {
addedAt: function (id, item, index) {
Tracker.nonreactive(function () {
var newItemView;
if (eachView.variableName) {
// new-style #each (as in {{#each item in items}})
// doesn't create a new data context
newItemView = Blaze.View('item', eachView.contentFunc);
} else {
newItemView = Blaze.With(item, eachView.contentFunc);
}
eachView.numItems++;
var bindings = {};
bindings['@index'] = index;
if (eachView.variableName) {
bindings[eachView.variableName] = item;
}
Blaze._attachBindingsToView(bindings, newItemView);
if (eachView.expandedValueDep) {
eachView.expandedValueDep.changed();
} else if (eachView._domrange) {
if (eachView.inElseMode) {
eachView._domrange.removeMember(0);
eachView.inElseMode = false;
}
var range = Blaze._materializeView(newItemView, eachView);
eachView._domrange.addMember(range, index);
updateIndices(index);
} else {
eachView.initialSubviews.splice(index, 0, newItemView);
}
});
},
removedAt: function (id, item, index) {
Tracker.nonreactive(function () {
eachView.numItems--;
if (eachView.expandedValueDep) {
eachView.expandedValueDep.changed();
} else if (eachView._domrange) {
eachView._domrange.removeMember(index);
updateIndices(index);
if (eachView.elseFunc && eachView.numItems === 0) {
eachView.inElseMode = true;
eachView._domrange.addMember(
Blaze._materializeView(
Blaze.View('each_else',eachView.elseFunc),
eachView), 0);
}
} else {
eachView.initialSubviews.splice(index, 1);
}
});
},
changedAt: function (id, newItem, oldItem, index) {
Tracker.nonreactive(function () {
if (eachView.expandedValueDep) {
eachView.expandedValueDep.changed();
} else {
var itemView;
if (eachView._domrange) {
itemView = eachView._domrange.getMember(index).view;
} else {
itemView = eachView.initialSubviews[index];
}
if (eachView.variableName) {
itemView._scopeBindings[eachView.variableName].set(newItem);
} else {
itemView.dataVar.set(newItem);
}
}
});
},
movedTo: function (id, item, fromIndex, toIndex) {
Tracker.nonreactive(function () {
if (eachView.expandedValueDep) {
eachView.expandedValueDep.changed();
} else if (eachView._domrange) {
eachView._domrange.moveMember(fromIndex, toIndex);
updateIndices(
Math.min(fromIndex, toIndex), Math.max(fromIndex, toIndex));
} else {
var subviews = eachView.initialSubviews;
var itemView = subviews[fromIndex];
subviews.splice(fromIndex, 1);
subviews.splice(toIndex, 0, itemView);
}
});
}
});
if (eachView.elseFunc && eachView.numItems === 0) {
eachView.inElseMode = true;
eachView.initialSubviews[0] =
Blaze.View('each_else', eachView.elseFunc);
}
});
eachView.onViewDestroyed(function () {
if (eachView.stopHandle)
eachView.stopHandle.stop();
});
return eachView;
};
Blaze._TemplateWith = function (arg, contentFunc) {
var w;
var argFunc = arg;
if (typeof arg !== 'function') {
argFunc = function () {
return arg;
};
}
// This is a little messy. When we compile `{{> Template.contentBlock}}`, we
// wrap it in Blaze._InOuterTemplateScope in order to skip the intermediate
// parent Views in the current template. However, when there's an argument
// (`{{> Template.contentBlock arg}}`), the argument needs to be evaluated
// in the original scope. There's no good order to nest
// Blaze._InOuterTemplateScope and Spacebars.TemplateWith to achieve this,
// so we wrap argFunc to run it in the "original parentView" of the
// Blaze._InOuterTemplateScope.
//
// To make this better, reconsider _InOuterTemplateScope as a primitive.
// Longer term, evaluate expressions in the proper lexical scope.
var wrappedArgFunc = function () {
var viewToEvaluateArg = null;
if (w.parentView && w.parentView.name === 'InOuterTemplateScope') {
viewToEvaluateArg = w.parentView.originalParentView;
}
if (viewToEvaluateArg) {
return Blaze._withCurrentView(viewToEvaluateArg, argFunc);
} else {
return argFunc();
}
};
var wrappedContentFunc = function () {
var content = contentFunc.call(this);
// Since we are generating the Blaze._TemplateWith view for the
// user, set the flag on the child view. If `content` is a template,
// construct the View so that we can set the flag.
if (content instanceof Blaze.Template) {
content = content.constructView();
}
if (content instanceof Blaze.View) {
content._hasGeneratedParent = true;
}
return content;
};
w = Blaze.With(wrappedArgFunc, wrappedContentFunc);
w.__isTemplateWith = true;
return w;
};
Blaze._InOuterTemplateScope = function (templateView, contentFunc) {
var view = Blaze.View('InOuterTemplateScope', contentFunc);
var parentView = templateView.parentView;
// Hack so that if you call `{{> foo bar}}` and it expands into
// `{{#with bar}}{{> foo}}{{/with}}`, and then `foo` is a template
// that inserts `{{> Template.contentBlock}}`, the data context for
// `Template.contentBlock` is not `bar` but the one enclosing that.
if (parentView.__isTemplateWith)
parentView = parentView.parentView;
view.onViewCreated(function () {
this.originalParentView = this.parentView;
this.parentView = parentView;
this.__childDoesntStartNewLexicalScope = true;
});
return view;
};
// XXX COMPAT WITH 0.9.0
Blaze.InOuterTemplateScope = Blaze._InOuterTemplateScope;