-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathcomponent-node-manager.js
304 lines (239 loc) · 10.4 KB
/
component-node-manager.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
import { assert, warn, runInDebug } from 'ember-metal/debug';
import buildComponentTemplate from 'ember-views/system/build-component-template';
import getCellOrValue from 'ember-htmlbars/hooks/get-cell-or-value';
import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import { MUTABLE_CELL } from 'ember-views/compat/attrs-proxy';
import { instrument } from 'ember-htmlbars/system/instrumentation-support';
import LegacyEmberComponent from 'ember-views/components/component';
import GlimmerComponent from 'ember-htmlbars/glimmer-component';
import extractPositionalParams from 'ember-htmlbars/utils/extract-positional-params';
import symbol from 'ember-metal/symbol';
import { setOwner } from 'container/owner';
// These symbols will be used to limit link-to's public API surface area.
export let HAS_BLOCK = symbol('HAS_BLOCK');
// In theory this should come through the env, but it should
// be safe to import this until we make the hook system public
// and it gets actively used in addons or other downstream
// libraries.
import getValue from 'ember-htmlbars/hooks/get-value';
function ComponentNodeManager(component, isAngleBracket, scope, renderNode, attrs, block, expectElement) {
this.component = component;
this.isAngleBracket = isAngleBracket;
this.scope = scope;
this.renderNode = renderNode;
this.attrs = attrs;
this.block = block;
this.expectElement = expectElement;
}
export default ComponentNodeManager;
ComponentNodeManager.create = function ComponentNodeManager_create(renderNode, env, options) {
let { tagName,
params,
attrs,
parentView,
parentScope,
isAngleBracket,
component,
layout,
templates } = options;
attrs = attrs || {};
component = component || (isAngleBracket ? GlimmerComponent : LegacyEmberComponent);
let createOptions = {
parentView,
[HAS_BLOCK]: !!templates.default
};
configureTagName(attrs, tagName, component, isAngleBracket, createOptions);
// Map passed attributes (e.g. <my-component id="foo">) to component
// properties ({ id: "foo" }).
configureCreateOptions(attrs, createOptions);
// If there is a controller on the scope, pluck it off and save it on the
// component. This allows the component to target actions sent via
// `sendAction` correctly.
if (parentScope.hasLocal('controller')) {
createOptions._controller = getValue(parentScope.getLocal('controller'));
} else {
createOptions._targetObject = getValue(parentScope.getSelf());
}
extractPositionalParams(renderNode, component, params, attrs);
// Instantiate the component
component = createComponent(component, isAngleBracket, createOptions, renderNode, env, attrs);
// If the component specifies its layout via the `layout` property
// instead of using the template looked up in the container, get it
// now that we have the component instance.
if (!layout) {
layout = get(component, 'layout');
}
runInDebug(() => {
if (isAngleBracket) {
assert(`You cannot invoke the '${tagName}' component with angle brackets, because it's a subclass of Component. Please upgrade to GlimmerComponent. Alternatively, you can invoke as '{{${tagName}}}'.`, component.isGlimmerComponent);
} else {
assert(`You cannot invoke the '${tagName}' component with curly braces, because it's a subclass of GlimmerComponent. Please invoke it as '<${tagName}>' instead.`, !component.isGlimmerComponent);
}
if (!layout) { return; }
let fragmentReason = layout.meta.fragmentReason;
if (isAngleBracket && fragmentReason) {
switch (fragmentReason.name) {
case 'missing-wrapper':
assert(`The <${tagName}> template must have a single top-level element because it is a GlimmerComponent.`);
break;
case 'modifiers':
let modifiers = fragmentReason.modifiers.map(m => `{{${m} ...}}`);
assert(`You cannot use ${ modifiers.join(', ') } in the top-level element of the <${tagName}> template because it is a GlimmerComponent.`);
break;
case 'triple-curlies':
assert(`You cannot use triple curlies (e.g. style={{{ ... }}}) in the top-level element of the <${tagName}> template because it is a GlimmerComponent.`);
break;
}
}
});
let results = buildComponentTemplate(
{ layout, component, isAngleBracket }, attrs, { templates, scope: parentScope }
);
return new ComponentNodeManager(component, isAngleBracket, parentScope, renderNode, attrs, results.block, results.createdElement);
};
function configureTagName(attrs, tagName, component, isAngleBracket, createOptions) {
if (isAngleBracket) {
createOptions.tagName = tagName;
} else if (attrs.tagName) {
createOptions.tagName = getValue(attrs.tagName);
}
}
function configureCreateOptions(attrs, createOptions) {
// Some attrs are special and need to be set as properties on the component
// instance. Make sure we use getValue() to get them from `attrs` since
// they are still streams.
if (attrs.id) { createOptions.elementId = getValue(attrs.id); }
if (attrs._defaultTagName) { createOptions._defaultTagName = getValue(attrs._defaultTagName); }
if (attrs.viewName) { createOptions.viewName = getValue(attrs.viewName); }
}
ComponentNodeManager.prototype.render = function ComponentNodeManager_render(_env, visitor) {
var { component } = this;
return instrument(component, function ComponentNodeManager_render_instrument() {
let env = _env.childWithView(component);
env.renderer.componentWillRender(component);
env.renderedViews.push(component.elementId);
if (this.block) {
this.block.invoke(env, [], undefined, this.renderNode, this.scope, visitor);
}
let element;
if (this.expectElement || component.isGlimmerComponent) {
// This code assumes that Glimmer components are never fragments. When
// Glimmer components gain fragment powers, we will need to communicate
// whether the layout produced a single top-level node or fragment
// somehow (either via static information on the template/component, or
// dynamically as the layout is being rendered).
element = this.renderNode.firstNode;
// Glimmer components may have whitespace or boundary nodes around the
// top-level element.
if (element && element.nodeType !== 1) {
element = nextElementSibling(element);
}
}
// In environments like FastBoot, disable any hooks that would cause the component
// to access the DOM directly.
if (env.destinedForDOM) {
env.renderer.didCreateElement(component, element);
env.renderer.willInsertElement(component, element);
env.lifecycleHooks.push({ type: 'didInsertElement', view: component });
}
}, this);
};
function nextElementSibling(node) {
let current = node;
while (current) {
if (current.nodeType === 1) { return current; }
current = node.nextSibling;
}
}
ComponentNodeManager.prototype.rerender = function ComponentNodeManager_rerender(_env, attrs, visitor) {
var component = this.component;
return instrument(component, function ComponentNodeManager_rerender_instrument() {
let env = _env.childWithView(component);
var snapshot = takeSnapshot(attrs);
if (component._renderNode.shouldReceiveAttrs) {
if (component._propagateAttrsToThis) {
component._propagateAttrsToThis(takeLegacySnapshot(attrs));
}
env.renderer.componentUpdateAttrs(component, snapshot);
component._renderNode.shouldReceiveAttrs = false;
}
// Notify component that it has become dirty and is about to change.
env.renderer.componentWillUpdate(component, snapshot);
env.renderer.componentWillRender(component);
env.renderedViews.push(component.elementId);
if (this.block) {
this.block.invoke(env, [], undefined, this.renderNode, this.scope, visitor);
}
env.lifecycleHooks.push({ type: 'didUpdate', view: component });
return env;
}, this);
};
ComponentNodeManager.prototype.destroy = function ComponentNodeManager_destroy() {
let component = this.component;
// Clear component's render node. Normally this gets cleared
// during view destruction, but in this case we're re-assigning the
// node to a different view and it will get cleaned up automatically.
component._renderNode = null;
component.destroy();
};
export function createComponent(_component, isAngleBracket, props, renderNode, env, attrs = {}) {
if (!isAngleBracket) {
assert('controller= is no longer supported', !('controller' in attrs));
snapshotAndUpdateTarget(attrs, props);
} else {
props.attrs = takeSnapshot(attrs);
props._isAngleBracket = true;
}
setOwner(props, env.owner);
props.renderer = props.parentView ? props.parentView.renderer : env.owner.lookup('renderer:-dom');
props._viewRegistry = props.parentView ? props.parentView._viewRegistry : env.owner.lookup('-view-registry:main');
let component = _component.create(props);
if (props.parentView) {
props.parentView.appendChild(component);
if (props.viewName) {
set(props.parentView, props.viewName, component);
}
}
component._renderNode = renderNode;
renderNode.emberView = component;
renderNode.buildChildEnv = buildChildEnv;
return component;
}
function takeSnapshot(attrs) {
let hash = {};
for (var prop in attrs) {
hash[prop] = getCellOrValue(attrs[prop]);
}
return hash;
}
export function takeLegacySnapshot(attrs) {
let hash = {};
for (var prop in attrs) {
hash[prop] = getValue(attrs[prop]);
}
return hash;
}
function snapshotAndUpdateTarget(rawAttrs, target) {
let attrs = {};
for (var prop in rawAttrs) {
let value = getCellOrValue(rawAttrs[prop]);
attrs[prop] = value;
// when `attrs` is an actual value being set in the
// attrs hash (`{{foo-bar attrs="blah"}}`) we cannot
// set `"blah"` to the root of the target because
// that would replace all attrs with `attrs.attrs`
if (prop === 'attrs') {
warn(`Invoking a component with a hash attribute named \`attrs\` is not supported. Please refactor usage of ${target} to avoid passing \`attrs\` as a hash parameter.`, false, { id: 'ember-htmlbars.component-unsupported-attrs' });
continue;
}
if (value && value[MUTABLE_CELL]) {
value = value.value;
}
target[prop] = value;
}
return target.attrs = attrs;
}
function buildChildEnv(state, env) {
return env.childWithView(this.emberView);
}