OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * cr-lazy-render is a simple variant of dom-if designed for lazy rendering | 7 * cr-lazy-render is a simple variant of dom-if designed for lazy rendering |
8 * of elements that are accessed imperatively. | 8 * of elements that are accessed imperatively. |
9 * Usage: | 9 * Usage: |
10 * <template is="cr-lazy-render" id="menu"> | 10 * <template is="cr-lazy-render" id="menu"> |
11 * <heavy-menu></heavy-menu> | 11 * <heavy-menu></heavy-menu> |
12 * </template> | 12 * </template> |
13 * | 13 * |
14 * this.$.menu.get().then(function(menu) { | 14 * this.$.menu.get().then(function(menu) { |
15 * menu.show(); | 15 * menu.show(); |
16 * }); | 16 * }); |
17 */ | 17 */ |
18 | 18 |
19 /** | |
20 * A template instance created by Polymer.Templatizer. | |
21 * @constructor | |
22 * @extends {PolymerElement} | |
23 */ | |
24 var TemplateInstance = function() {}; | |
25 | |
26 /** @type {Array<Element>} */ | |
27 TemplateInstance.prototype._children; | |
28 | |
29 /** | |
30 * @param {string} prop | |
31 * @param {Object} value | |
32 * @param {boolean} quiet | |
33 */ | |
34 TemplateInstance.prototype.__setProperty = function(prop, value, quiet) {}; | |
35 | |
36 /** | |
37 * @param {string} path | |
38 * @param {Object} value | |
39 * @param {boolean} quiet | |
40 */ | |
41 TemplateInstance.prototype._notifyPath = function(path, value, quiet) {}; | |
42 | |
43 Polymer({ | 19 Polymer({ |
44 is: 'cr-lazy-render', | 20 is: 'cr-lazy-render', |
45 extends: 'template', | 21 extends: 'template', |
46 | 22 |
47 behaviors: [ | 23 behaviors: [ |
48 Polymer.Templatizer | 24 Polymer.Templatizer |
49 ], | 25 ], |
50 | 26 |
51 /** @private {Promise<Element>} */ | 27 /** @private {Promise<Element>} */ |
52 _renderPromise: null, | 28 _renderPromise: null, |
53 | 29 |
54 /** @private {TemplateInstance} */ | 30 /** @private {TemplatizerNode} */ |
55 _instance: null, | 31 _child: null, |
Dan Beam
2016/09/15 17:19:00
use trailing underscore for Chrome JS unless it ha
tsergeant
2016/09/15 23:26:49
I was originally trying to avoid things like `inst
| |
56 | 32 |
57 /** | 33 /** |
58 * Stamp the template into the DOM tree asynchronously | 34 * Stamp the template into the DOM tree asynchronously |
59 * @return {Promise<Element>} Promise which resolves when the template has | 35 * @return {Promise<Element>} Promise which resolves when the template has |
60 * been stamped. | 36 * been stamped. |
61 */ | 37 */ |
62 get: function() { | 38 get: function() { |
63 if (!this._renderPromise) { | 39 if (!this._renderPromise) { |
64 this._renderPromise = new Promise(function(resolve) { | 40 this._renderPromise = new Promise(function(resolve) { |
65 this._debounceTemplate(function() { | 41 this._debounceTemplate(function() { |
66 this._render(); | 42 this._render(); |
67 this._renderPromise = null; | 43 this._renderPromise = null; |
68 resolve(this.getIfExists()); | 44 resolve(this.getIfExists()); |
69 }.bind(this)); | 45 }.bind(this)); |
70 }.bind(this)); | 46 }.bind(this)); |
71 } | 47 } |
72 return this._renderPromise; | 48 return this._renderPromise; |
73 }, | 49 }, |
74 | 50 |
75 /** | 51 /** |
76 * @return {?Element} The element contained in the template, if it has | 52 * @return {?Element} The element contained in the template, if it has |
77 * already been stamped. | 53 * already been stamped. |
78 */ | 54 */ |
79 getIfExists: function() { | 55 getIfExists: function() { |
80 if (this._instance) { | 56 return this._child; |
81 var children = this._instance._children; | |
82 | |
83 for (var i = 0; i < children.length; i++) { | |
84 if (children[i].nodeType == Node.ELEMENT_NODE) | |
85 return children[i]; | |
86 } | |
87 } | |
88 return null; | |
89 }, | 57 }, |
90 | 58 |
59 /** @private */ | |
Dan Beam
2016/09/15 17:19:00
is this actually private or @protected in polymer
tsergeant
2016/09/15 23:26:49
It's private, yeah. Nothing else should be using i
| |
91 _render: function() { | 60 _render: function() { |
92 if (!this.ctor) | 61 if (!this.ctor) |
93 this.templatize(this); | 62 this.templatize(this); |
94 var parentNode = this.parentNode; | 63 var parentNode = this.parentNode; |
95 if (parentNode && !this._instance) { | 64 if (parentNode && !this._child) { |
96 this._instance = /** @type {TemplateInstance} */(this.stamp({})); | 65 var instance = this.stamp({}); |
97 var root = this._instance.root; | 66 this._child = instance.root.querySelector('*'); |
michaelpg
2016/09/15 08:44:41
instance.root.firstElementChild
tsergeant
2016/09/15 23:26:49
Done.
| |
98 parentNode.insertBefore(root, this); | 67 parentNode.insertBefore(instance.root, this); |
99 } | 68 } |
100 }, | 69 }, |
101 | 70 |
102 /** | 71 /** |
103 * @param {string} prop | 72 * @param {string} prop |
104 * @param {Object} value | 73 * @param {Object} value |
105 */ | 74 */ |
106 _forwardParentProp: function(prop, value) { | 75 _forwardParentProp: function(prop, value) { |
107 if (this._instance) | 76 if (this._child) |
108 this._instance.__setProperty(prop, value, true); | 77 this._child._templateInstance[prop] = value; |
109 }, | 78 }, |
110 | 79 |
111 /** | 80 /** |
112 * @param {string} path | 81 * @param {string} path |
113 * @param {Object} value | 82 * @param {Object} value |
114 */ | 83 */ |
115 _forwardParentPath: function(path, value) { | 84 _forwardParentPath: function(path, value) { |
116 if (this._instance) | 85 if (this._child) |
117 this._instance._notifyPath(path, value, true); | 86 this._child._templateInstance.notifyPath(path, value, true); |
118 } | 87 } |
119 }); | 88 }); |
OLD | NEW |