Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * @fileoverview | |
| 7 * settings-idle-render is a simple variant of dom-if designed for lazy | |
| 8 * rendering of elements that are accessed imperatively. | |
| 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 | |
| 11 * ui/webui/resources/cr_elements/cr_lazy_render/cr_lazy_render.js | |
| 12 */ | |
| 13 | |
| 14 Polymer({ | |
| 15 is: 'settings-idle-render', | |
| 16 extends: 'template', | |
| 17 | |
| 18 behaviors: [Polymer.Templatizer], | |
| 19 | |
| 20 properties: { | |
| 21 /** | |
| 22 * Expand the contents of the template when the JavaScript thread is idle. | |
| 23 * This Boolean takes action when it is set to true from the false state. | |
| 24 * Setting the value to false doesn't undo the call to |render_|. | |
| 25 */ | |
| 26 renderWhenIdle: { | |
|
dpapad
2017/01/31 23:10:13
Is this attribute necessary? In other words, does
dschuyler
2017/01/31 23:53:03
Done.
| |
| 27 observer: 'renderWhenIdleChanged_', | |
| 28 type: Boolean, | |
| 29 }, | |
| 30 }, | |
| 31 | |
| 32 /** @private {TemplatizerNode} */ | |
| 33 child_: null, | |
| 34 | |
| 35 /** @private {number} */ | |
| 36 idleCallback_: 0, | |
| 37 | |
| 38 /** @override */ | |
| 39 attached: function() { | |
| 40 this.renderWhenIdleChanged_(); | |
| 41 }, | |
| 42 | |
| 43 /** @override */ | |
| 44 detached: function() { | |
| 45 if (this.idleCallback_) | |
|
dpapad
2017/01/31 23:10:13
Can we avoid implicit boolean conversions? It is O
dschuyler
2017/01/31 23:53:03
Done.
| |
| 46 cancelIdleCallback(this.idleCallback_); | |
| 47 }, | |
| 48 | |
| 49 /** @private */ | |
| 50 renderWhenIdleChanged_: function() { | |
| 51 if (this.renderWhenIdle && !this.child_) | |
| 52 this.idleCallback_ = requestIdleCallback(this.render_.bind(this)); | |
| 53 }, | |
| 54 | |
| 55 /** | |
| 56 * Stamp the template into the DOM tree synchronously | |
| 57 * @return {Element} Child element which has been stamped into the DOM tree. | |
| 58 */ | |
| 59 get: function() { | |
| 60 if (!this.child_) | |
| 61 this.render_(); | |
| 62 return this.child_; | |
| 63 }, | |
| 64 | |
| 65 /** @private */ | |
| 66 render_: function() { | |
| 67 if (!this.ctor) | |
| 68 this.templatize(this); | |
| 69 var parentNode = this.parentNode; | |
| 70 if (parentNode && !this.child_) { | |
| 71 var instance = this.stamp({}); | |
| 72 this.child_ = instance.root.firstElementChild; | |
| 73 parentNode.insertBefore(instance.root, this); | |
| 74 } | |
| 75 }, | |
| 76 | |
| 77 /** | |
| 78 * @param {string} prop | |
| 79 * @param {Object} value | |
| 80 */ | |
| 81 _forwardParentProp: function(prop, value) { | |
| 82 if (this.child_) | |
| 83 this.child_._templateInstance[prop] = value; | |
| 84 }, | |
| 85 | |
| 86 /** | |
| 87 * @param {string} path | |
| 88 * @param {Object} value | |
| 89 */ | |
| 90 _forwardParentPath: function(path, value) { | |
| 91 if (this.child_) | |
| 92 this.child_._templateInstance.notifyPath(path, value, true); | |
| 93 } | |
| 94 }); | |
| OLD | NEW |