Commit 896eeb35 authored by tsergeant's avatar tsergeant Committed by Commit bot

MD WebUI: Simplify usage of Templatizer in cr-lazy-render

This changes the template stamping parts of cr-lazy-render to look more
like iron-list. This simplifies the code and makes it play nicely with
Closure.

BUG=642200
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2344803002
Cr-Commit-Position: refs/heads/master@{#419058}
parent 18db6b0c
......@@ -5388,7 +5388,7 @@ Polymer({
"extends": 'template',
behaviors: [ Polymer.Templatizer ],
_renderPromise: null,
_instance: null,
_child: null,
get: function() {
if (!this._renderPromise) {
this._renderPromise = new Promise(function(resolve) {
......@@ -5402,28 +5402,22 @@ Polymer({
return this._renderPromise;
},
getIfExists: function() {
if (this._instance) {
var children = this._instance._children;
for (var i = 0; i < children.length; i++) {
if (children[i].nodeType == Node.ELEMENT_NODE) return children[i];
}
}
return null;
return this._child;
},
_render: function() {
if (!this.ctor) this.templatize(this);
var parentNode = this.parentNode;
if (parentNode && !this._instance) {
this._instance = this.stamp({});
var root = this._instance.root;
parentNode.insertBefore(root, this);
if (parentNode && !this._child) {
var instance = this.stamp({});
this._child = instance.root.querySelector('*');
parentNode.insertBefore(instance.root, this);
}
},
_forwardParentProp: function(prop, value) {
if (this._instance) this._instance.__setProperty(prop, value, true);
if (this._child) this._child._templateInstance[prop] = value;
},
_forwardParentPath: function(path, value) {
if (this._instance) this._instance._notifyPath(path, value, true);
if (this._child) this._child._templateInstance.notifyPath(path, value, true);
}
});
......
......@@ -16,30 +16,6 @@
* });
*/
/**
* A template instance created by Polymer.Templatizer.
* @constructor
* @extends {PolymerElement}
*/
var TemplateInstance = function() {};
/** @type {Array<Element>} */
TemplateInstance.prototype._children;
/**
* @param {string} prop
* @param {Object} value
* @param {boolean} quiet
*/
TemplateInstance.prototype.__setProperty = function(prop, value, quiet) {};
/**
* @param {string} path
* @param {Object} value
* @param {boolean} quiet
*/
TemplateInstance.prototype._notifyPath = function(path, value, quiet) {};
Polymer({
is: 'cr-lazy-render',
extends: 'template',
......@@ -49,10 +25,10 @@ Polymer({
],
/** @private {Promise<Element>} */
_renderPromise: null,
renderPromise_: null,
/** @private {TemplateInstance} */
_instance: null,
/** @private {TemplatizerNode} */
child_: null,
/**
* Stamp the template into the DOM tree asynchronously
......@@ -60,16 +36,16 @@ Polymer({
* been stamped.
*/
get: function() {
if (!this._renderPromise) {
this._renderPromise = new Promise(function(resolve) {
if (!this.renderPromise_) {
this.renderPromise_ = new Promise(function(resolve) {
this._debounceTemplate(function() {
this._render();
this._renderPromise = null;
this.render_();
this.renderPromise_ = null;
resolve(this.getIfExists());
}.bind(this));
}.bind(this));
}
return this._renderPromise;
return this.renderPromise_;
},
/**
......@@ -77,25 +53,18 @@ Polymer({
* already been stamped.
*/
getIfExists: function() {
if (this._instance) {
var children = this._instance._children;
for (var i = 0; i < children.length; i++) {
if (children[i].nodeType == Node.ELEMENT_NODE)
return children[i];
}
}
return null;
return this.child_;
},
_render: function() {
/** @private */
render_: function() {
if (!this.ctor)
this.templatize(this);
var parentNode = this.parentNode;
if (parentNode && !this._instance) {
this._instance = /** @type {TemplateInstance} */(this.stamp({}));
var root = this._instance.root;
parentNode.insertBefore(root, this);
if (parentNode && !this.child_) {
var instance = this.stamp({});
this.child_ = instance.root.firstElementChild;
parentNode.insertBefore(instance.root, this);
}
},
......@@ -104,8 +73,8 @@ Polymer({
* @param {Object} value
*/
_forwardParentProp: function(prop, value) {
if (this._instance)
this._instance.__setProperty(prop, value, true);
if (this.child_)
this.child_._templateInstance[prop] = value;
},
/**
......@@ -113,7 +82,7 @@ Polymer({
* @param {Object} value
*/
_forwardParentPath: function(path, value) {
if (this._instance)
this._instance._notifyPath(path, value, true);
if (this.child_)
this.child_._templateInstance.notifyPath(path, value, true);
}
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment