OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 * settings-idle-render is a simple variant of dom-if designed for lazy | 7 * settings-idle-render is a simple variant of dom-if designed for lazy |
8 * rendering of elements that are accessed imperatively. | 8 * rendering of elements that are accessed imperatively. |
9 * If a use for idle time expansion is found outside of settings, this code | 9 * If a use for idle time expansion is found outside of settings, this code |
10 * should be replaced by cr-lazy-render after that feature is merged into | 10 * should be replaced by cr-lazy-render after that feature is merged into |
11 * ui/webui/resources/cr_elements/cr_lazy_render/cr_lazy_render.js | 11 * ui/webui/resources/cr_elements/cr_lazy_render/cr_lazy_render.js |
12 */ | 12 */ |
13 | 13 |
14 Polymer({ | 14 Polymer({ |
15 is: 'settings-idle-render', | 15 is: 'settings-idle-load', |
dpapad
2017/03/16 20:47:16
I'll rename this file to settings_idle_load.js. Ju
| |
16 extends: 'template', | 16 extends: 'template', |
17 | 17 |
18 behaviors: [Polymer.Templatizer], | 18 behaviors: [Polymer.Templatizer], |
19 | 19 |
20 properties: { | |
21 /** | |
22 * If specified, it will be loaded via an HTML import before stamping the | |
23 * template. | |
24 */ | |
25 url: String, | |
26 }, | |
27 | |
20 /** @private {TemplatizerNode} */ | 28 /** @private {TemplatizerNode} */ |
21 child_: null, | 29 child_: null, |
22 | 30 |
23 /** @private {number} */ | 31 /** @private {number} */ |
24 idleCallback_: 0, | 32 idleCallback_: 0, |
25 | 33 |
26 /** @override */ | 34 /** @override */ |
27 attached: function() { | 35 attached: function() { |
28 this.idleCallback_ = requestIdleCallback(this.render_.bind(this)); | 36 this.idleCallback_ = requestIdleCallback(this.get.bind(this)); |
29 }, | 37 }, |
30 | 38 |
31 /** @override */ | 39 /** @override */ |
32 detached: function() { | 40 detached: function() { |
33 // No-op if callback already fired. | 41 // No-op if callback already fired. |
34 cancelIdleCallback(this.idleCallback_); | 42 cancelIdleCallback(this.idleCallback_); |
35 }, | 43 }, |
36 | 44 |
37 /** | 45 /** |
38 * Stamp the template into the DOM tree synchronously | 46 * @return {!Promise<Element>} Child element which has been stamped into the |
39 * @return {Element} Child element which has been stamped into the DOM tree. | 47 * DOM tree. |
40 */ | 48 */ |
41 get: function() { | 49 get: function() { |
42 if (!this.child_) | 50 if (!this.loading_) { |
43 this.render_(); | 51 if (this.url) { |
44 return this.child_; | 52 this.loading_ = new Promise(function(resolve, reject) { |
45 }, | 53 this.importHref(this.url, resolve, reject, true); |
46 | 54 }.bind(this)); |
47 /** @private */ | 55 } else { |
48 render_: function() { | 56 this.loading_ = Promise.resolve(); |
49 if (!this.ctor) | 57 } |
50 this.templatize(this); | |
51 var parentNode = this.parentNode; | |
52 if (parentNode && !this.child_) { | |
53 var instance = this.stamp({}); | |
54 this.child_ = instance.root.firstElementChild; | |
55 parentNode.insertBefore(instance.root, this); | |
56 } | 58 } |
59 return this.loading_.then(function() { | |
60 if (!this.child_) { | |
61 if (!this.ctor) | |
62 this.templatize(this); | |
63 var parentNode = this.parentNode; | |
64 if (parentNode && !this.child_) { | |
65 var instance = this.stamp({}); | |
66 this.child_ = instance.root.firstElementChild; | |
67 parentNode.insertBefore(instance.root, this); | |
68 } | |
69 } | |
70 return this.child_; | |
71 }.bind(this)); | |
57 }, | 72 }, |
58 | 73 |
59 /** | 74 /** |
60 * @param {string} prop | 75 * @param {string} prop |
61 * @param {Object} value | 76 * @param {Object} value |
62 */ | 77 */ |
63 _forwardParentProp: function(prop, value) { | 78 _forwardParentProp: function(prop, value) { |
64 if (this.child_) | 79 if (this.child_) |
65 this.child_._templateInstance[prop] = value; | 80 this.child_._templateInstance[prop] = value; |
66 }, | 81 }, |
67 | 82 |
68 /** | 83 /** |
69 * @param {string} path | 84 * @param {string} path |
70 * @param {Object} value | 85 * @param {Object} value |
71 */ | 86 */ |
72 _forwardParentPath: function(path, value) { | 87 _forwardParentPath: function(path, value) { |
73 if (this.child_) | 88 if (this.child_) |
74 this.child_._templateInstance.notifyPath(path, value, true); | 89 this.child_._templateInstance.notifyPath(path, value, true); |
75 } | 90 } |
76 }); | 91 }); |
OLD | NEW |