 Chromium Code Reviews
 Chromium Code Reviews Issue 2754563002:
  MD Settings: Lazy load the contents of the "advanced" settings.  (Closed)
    
  
    Issue 2754563002:
  MD Settings: Lazy load the contents of the "advanced" settings.  (Closed) 
  | 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', | 
| 
Dan Beam
2017/03/18 00:36:40
can you rename this file now?
 | |
| 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 return this.loading_; | 
| 44 return this.child_; | |
| 45 }, | |
| 46 | 52 | 
| 47 /** @private */ | 53 this.loading_ = new Promise(function(resolve, reject) { | 
| 48 render_: function() { | 54 this.importHref( | 
| 49 if (!this.ctor) | 55 this.url, | 
| 50 this.templatize(this); | 56 function() { | 
| 
Dan Beam
2017/03/18 00:15:51
i think this code could use some new lines.  sugge
 | |
| 51 var parentNode = this.parentNode; | 57 assert(!this.ctor); | 
| 52 if (parentNode && !this.child_) { | 58 this.templatize(this); | 
| 53 var instance = this.stamp({}); | 59 assert(this.ctor); | 
| 
Dan Beam
2017/03/18 00:15:51
\n
 
dpapad
2017/03/20 20:34:54
Done.
 | |
| 54 this.child_ = instance.root.firstElementChild; | 60 var instance = this.stamp({}); | 
| 
Dan Beam
2017/03/18 00:15:51
\n
 
dpapad
2017/03/20 20:34:55
Done.
 | |
| 55 parentNode.insertBefore(instance.root, this); | 61 assert(!this.child_); | 
| 56 } | 62 this.child_ = instance.root.firstElementChild; | 
| 
Dan Beam
2017/03/18 00:15:51
\n
 
dpapad
2017/03/20 20:34:54
Done.
 | |
| 63 this.parentNode.insertBefore(instance.root, this); | |
| 64 resolve(this.child_); | |
| 
Dan Beam
2017/03/18 00:15:51
\n
 
dpapad
2017/03/20 20:34:55
Done.
 | |
| 65 this.fire('lazy-loaded'); | |
| 66 }.bind(this), | |
| 67 reject, true); | |
| 68 }.bind(this)); | |
| 
Dan Beam
2017/03/18 00:15:51
please use `git cl format --js` on this code
 
dpapad
2017/03/20 20:34:55
Done.
 | |
| 69 | |
| 70 return this.loading_; | |
| 57 }, | 71 }, | 
| 58 | 72 | 
| 59 /** | 73 /** | 
| 60 * @param {string} prop | 74 * @param {string} prop | 
| 61 * @param {Object} value | 75 * @param {Object} value | 
| 62 */ | 76 */ | 
| 63 _forwardParentProp: function(prop, value) { | 77 _forwardParentProp: function(prop, value) { | 
| 64 if (this.child_) | 78 if (this.child_) | 
| 65 this.child_._templateInstance[prop] = value; | 79 this.child_._templateInstance[prop] = value; | 
| 66 }, | 80 }, | 
| 67 | 81 | 
| 68 /** | 82 /** | 
| 69 * @param {string} path | 83 * @param {string} path | 
| 70 * @param {Object} value | 84 * @param {Object} value | 
| 71 */ | 85 */ | 
| 72 _forwardParentPath: function(path, value) { | 86 _forwardParentPath: function(path, value) { | 
| 73 if (this.child_) | 87 if (this.child_) | 
| 74 this.child_._templateInstance.notifyPath(path, value, true); | 88 this.child_._templateInstance.notifyPath(path, value, true); | 
| 75 } | 89 } | 
| 76 }); | 90 }); | 
| OLD | NEW |