Skip to content

Commit

Permalink
Put $ on dom, and assign to element as needed. Eliminate _templateInf…
Browse files Browse the repository at this point in the history
…o reference.
  • Loading branch information
kevinpschaaf committed Apr 7, 2017
1 parent 396c102 commit 03bed19
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions lib/elements/dom-bind.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
return;
}
this.root = this._stampBoundTemplate(template);
this.$ = this.root.$;
this.__children = [];
for (let n=this.root.firstChild; n; n=n.nextSibling) {
this.__children[this.__children.length] = n;
Expand Down
9 changes: 9 additions & 0 deletions lib/mixins/element-mixin.html
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@
hostStack.beginHosting(this);
this.root = this._stampBoundTemplate(this._template);
hostStack.endHosting(this);
this.$ = this.root.$;
}
super.ready();
}
Expand Down Expand Up @@ -747,6 +748,14 @@
return Polymer.ResolveUrl.resolveUrl(url, base);
}

/**
* Overrides `PropertyAccessors` to add map of dynamic functions on
* template info, for consumption by `PropertyEffects` template binding
* code. This map determines which method templates should have accessors
* created for them.
*
* @override
*/
static _parseTemplateContent(template, templateInfo, nodeInfo) {
templateInfo.dynamicFns = templateInfo.dynamicFns || propertiesForClass(this);
return super._parseTemplateContent(template, templateInfo, nodeInfo);
Expand Down
25 changes: 22 additions & 3 deletions lib/mixins/property-effects.html
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@
* its prototype, the property effect lists will be cloned and added as
* own properties of the caller.
*
* @param {string} path Property that should trigger the effect
* @param {string} property Property that should trigger the effect
* @param {string} type Effect type, from this.PROPERTY_EFFECT_TYPES
* @param {Object=} effect Effect metadata object
* @protected
Expand Down Expand Up @@ -2015,12 +2015,23 @@
return this.__templateInfo = templateInfo;
}

static _addTemplatePropertyEffect(templateInfo, prop, info) {
/**
* Adds a property effect to the given template metadata, which is run
* at the "propagate" stage of `_propertiesChanged` when the template
* has been bound to the element via `_bindTemplate`.
*
* The `effect` object should match the format in `_addPropertyEffect`.
*
* @param {string} prop Property that should trigger the effect
* @param {Object=} effect Effect metadata object
* @protected
*/
static _addTemplatePropertyEffect(templateInfo, prop, effect) {
let hostProps = templateInfo.hostProps = templateInfo.hostProps || {};
hostProps[prop] = true;
let effects = templateInfo.propertyEffects = templateInfo.propertyEffects || {};
let propEffects = effects[prop] = effects[prop] || [];
propEffects.push(info);
propEffects.push(effect);
}

/**
Expand All @@ -2030,6 +2041,14 @@
* is returned containing the stamped DOM, ready for insertion into the
* DOM.
*
* This method may be called more than once; however note that due to
* `shadycss` polyfill limitations, only styles from templates prepared
* using `ShadyCSS.prepareTemplate` will be correctly polyfilled (scoped
* to the shadow root and support CSS custom properties), and note that
* `ShadyCSS.prepareTemplate` may only be called once per element. As such,
* any styles required by in runtime-stamped templates must be included
* in the main element template.
*
* @param {HTMLTemplateElement} template Template to stamp
* @return {DocumentFragment} Cloned template content
* @protected
Expand Down
9 changes: 5 additions & 4 deletions lib/mixins/template-stamp.html
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@
* The template is parsed (once and memoized) using this library's
* template parsing features, and provides the following value-added
* features:
* * Adds declarative event listners for `on-event="handler"` attributes
* * Generates an "id map" for all nodes with id's under `this.$`
* * Adds declarative event listeners for `on-event="handler"` attributes
* * Generates an "id map" for all nodes with id's under `$` on returned
* document fragment
* * Passes template info including `content` back to templates as
* `_templateInfo` (a performance optimization to avoid deep template
* cloning)
Expand All @@ -408,10 +409,10 @@
// NOTE: ShadyDom optimization indicating there is an insertion point
dom.__noInsertionPoint = !templateInfo.hasInsertionPoint;
let nodes = dom.nodeList = new Array(nodeInfo.length);
this.$ = {};
dom.$ = {};
for (let i=0, l=nodeInfo.length, info; (i<l) && (info=nodeInfo[i]); i++) {
let node = nodes[i] = findTemplateNode(dom, info);
applyIdToMap(this, this.$, node, info);
applyIdToMap(this, dom.$, node, info);
applyTemplateContent(this, node, info);
applyEventListener(this, node, info);
}
Expand Down
22 changes: 10 additions & 12 deletions lib/utils/templatize.html
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
return templateHost && templateHost._methodHost || templateHost;
}

function createTemplatizerClass(template, options) {
function createTemplatizerClass(template, templateInfo, options) {
// Anonymous class created by the templatize
/**
* @unrestricted
Expand All @@ -203,12 +203,11 @@
let klass = class extends base { }
klass.prototype.__templatizeOptions = options;
klass.prototype._bindTemplate(template);
addNotifyEffects(klass, template, options);
addNotifyEffects(klass, template, templateInfo, options);
return klass;
}

function addPropagateEffects(template, options) {
let templateInfo = template._templateInfo;
function addPropagateEffects(template, templateInfo, options) {
let userForwardHostProp = options.forwardHostProp;
if (userForwardHostProp) {
// Provide data API and property effects on memoized template class
Expand Down Expand Up @@ -250,8 +249,7 @@
}
}

function addNotifyEffects(klass, template, options) {
let templateInfo = template._templateInfo;
function addNotifyEffects(klass, template, templateInfo, options) {
let hostProps = templateInfo.hostProps || {};
for (let iprop in options.instanceProps) {
delete hostProps[iprop];
Expand Down Expand Up @@ -381,26 +379,26 @@
* @param {HTMLTemplateElement} template Template to templatize
* @param {*} owner Owner of the template instances; any optional callbacks
* will be bound to this owner.
* @param {*} options Options dictionary (see summary for details)
* @param {*=} options Options dictionary (see summary for details)
* @return {TemplateInstanceBase} Generated class bound to the template
* provided
*/
templatize(template, owner, options) {
options = options || {};
if (template.__templatizeOwner) {
throw new Error('A <template> can only be templatized once');
}
template.__templatizeOwner = owner;
let templateInfo = template._templateInfo;
let templateInfo = owner.constructor._parseTemplate(template);
// Get memoized base class for the prototypical template, which
// includes property effects for binding template & forwarding
let baseClass = templateInfo && templateInfo.templatizeInstanceClass;
let baseClass = templateInfo.templatizeInstanceClass;
if (!baseClass) {
baseClass = createTemplatizerClass(template, options);
templateInfo = template._templateInfo;
baseClass = createTemplatizerClass(template, templateInfo, options);
templateInfo.templatizeInstanceClass = baseClass;
}
// Host property forwarding must be installed onto template instance
addPropagateEffects(template, options);
addPropagateEffects(template, templateInfo, options);
// Subclass base class and add reference for this specific template
let klass = class TemplateInstance extends baseClass {};
klass.prototype._methodHost = findMethodHost(template);
Expand Down

0 comments on commit 03bed19

Please sign in to comment.