Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements RFC #60 (Component Unification) #11359

Merged
merged 1 commit into from
Jul 8, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions packages/ember-htmlbars/lib/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import lookupHelper from 'ember-htmlbars/hooks/lookup-helper';
import hasHelper from 'ember-htmlbars/hooks/has-helper';
import invokeHelper from 'ember-htmlbars/hooks/invoke-helper';
import element from 'ember-htmlbars/hooks/element';
import attributes from 'ember-htmlbars/hooks/attributes';

import helpers from 'ember-htmlbars/helpers';
import keywords, { registerKeyword } from 'ember-htmlbars/keywords';
Expand All @@ -38,30 +39,31 @@ var emberHooks = merge({}, hooks);
emberHooks.keywords = keywords;

merge(emberHooks, {
linkRenderNode: linkRenderNode,
createFreshScope: createFreshScope,
bindShadowScope: bindShadowScope,
bindSelf: bindSelf,
bindScope: bindScope,
bindLocal: bindLocal,
updateSelf: updateSelf,
getRoot: getRoot,
getChild: getChild,
getValue: getValue,
getCellOrValue: getCellOrValue,
subexpr: subexpr,
concat: concat,
cleanupRenderNode: cleanupRenderNode,
destroyRenderNode: destroyRenderNode,
willCleanupTree: willCleanupTree,
didCleanupTree: didCleanupTree,
didRenderNode: didRenderNode,
classify: classify,
component: component,
lookupHelper: lookupHelper,
hasHelper: hasHelper,
invokeHelper: invokeHelper,
element: element
linkRenderNode,
createFreshScope,
bindShadowScope,
bindSelf,
bindScope,
bindLocal,
updateSelf,
getRoot,
getChild,
getValue,
getCellOrValue,
subexpr,
concat,
cleanupRenderNode,
destroyRenderNode,
willCleanupTree,
didCleanupTree,
didRenderNode,
classify,
component,
lookupHelper,
hasHelper,
invokeHelper,
element,
attributes
});

import debuggerKeyword from 'ember-htmlbars/keywords/debugger';
Expand Down
50 changes: 50 additions & 0 deletions packages/ember-htmlbars/lib/hooks/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { render, internal } from 'htmlbars-runtime';

export default function attributes(morph, env, scope, template, parentNode, visitor) {
let state = morph.state;
let block = state.block;

if (!block) {
let element = findRootElement(parentNode);
if (!element) { return; }

normalizeClassStatement(template.statements, element);

template.element = element;
block = morph.state.block = internal.blockFor(render, template, { scope });
}

block(env, [], undefined, morph, undefined, visitor);
}

function normalizeClassStatement(statements, element) {
let className = element.getAttribute('class');
if (!className) { return; }

for (let i=0, l=statements.length; i<l; i++) {
let statement = statements[i];

if (statement[1] === 'class') {
statement[2][2].unshift(className);
}
}
}

function findRootElement(parentNode) {
let node = parentNode.firstChild;
let found = null;

while (node) {
if (node.nodeType === 1) {
// found more than one top-level element, so there is no "root element"
if (found) { return null; }
found = node;
}
node = node.nextSibling;
}

let className = found && found.getAttribute('class');
if (!className || className.split(' ').indexOf('ember-view') === -1) {
return found;
}
}
50 changes: 36 additions & 14 deletions packages/ember-htmlbars/lib/hooks/component.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ComponentNodeManager from 'ember-htmlbars/node-managers/component-node-manager';
import buildComponentTemplate from 'ember-views/system/build-component-template';

export default function componentHook(renderNode, env, scope, _tagName, params, attrs, templates, visitor) {
var state = renderNode.state;
Expand All @@ -11,25 +12,46 @@ export default function componentHook(renderNode, env, scope, _tagName, params,

let tagName = _tagName;
let isAngleBracket = false;
let isTopLevel;

if (tagName.charAt(0) === '<') {
tagName = tagName.slice(1, -1);
let angles = tagName.match(/^(@?)<(.*)>$/);

if (angles) {
tagName = angles[2];
isAngleBracket = true;
isTopLevel = !!angles[1];
}

var parentView = env.view;

var manager = ComponentNodeManager.create(renderNode, env, {
tagName,
params,
attrs,
parentView,
templates,
isAngleBracket,
parentScope: scope
});

state.manager = manager;
if (!isTopLevel || tagName !== env.view.tagName) {
var manager = ComponentNodeManager.create(renderNode, env, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the use of var here intentional? let is used heavily elsewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not intentional; we were just moving around pre-existing code.

tagName,
params,
attrs,
parentView,
templates,
isAngleBracket,
isTopLevel,
parentScope: scope
});

state.manager = manager;
manager.render(env, visitor);
} else {
let component = env.view;
let templateOptions = {
component,
isAngleBracket: true,
isComponentElement: true,
outerAttrs: scope.attrs,
parentScope: scope
};

let contentOptions = { templates, scope };

let { block } = buildComponentTemplate(templateOptions, attrs, contentOptions);
block(env, [], undefined, renderNode, scope, visitor);
}

manager.render(env, visitor);
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ ComponentNodeManager.create = function(renderNode, env, options) {

extractPositionalParams(renderNode, component, params, attrs);

var results = buildComponentTemplate(
let results = buildComponentTemplate(
{ layout, component, isAngleBracket }, attrs, { templates, scope: parentScope }
);

Expand Down
Loading