Chromium Code Reviews| Index: chrome/browser/resources/settings/controls/settings_idle_render.js |
| diff --git a/chrome/browser/resources/settings/controls/settings_idle_render.js b/chrome/browser/resources/settings/controls/settings_idle_render.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8e224c9e5414b54437e08f556a7411fdbb2a9868 |
| --- /dev/null |
| +++ b/chrome/browser/resources/settings/controls/settings_idle_render.js |
| @@ -0,0 +1,76 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * @fileoverview |
| + * settings-idle-render is a simple variant of dom-if designed for lazy |
| + * rendering of elements that are accessed imperatively. |
| + * If a use for idle time expansion is found outside of settings, this code |
| + * should be replaced by cr-lazy-render after that feature is merged into |
| + * ui/webui/resources/cr_elements/cr_lazy_render/cr_lazy_render.js |
| + */ |
| + |
| +Polymer({ |
| + is: 'settings-idle-render', |
| + extends: 'template', |
| + |
| + behaviors: [Polymer.Templatizer], |
| + |
| + /** @private {TemplatizerNode} */ |
| + child_: null, |
| + |
| + /** @private {number} */ |
| + idleCallback_: 0, |
| + |
| + /** @override */ |
| + attached: function() { |
| + this.idleCallback_ = requestIdleCallback(this.render_.bind(this)); |
| + }, |
| + |
| + /** @override */ |
| + detached: function() { |
| + // No-op if callback already fired. |
| + cancelIdleCallback(this.idleCallback_); |
| + }, |
| + |
| + /** |
| + * Stamp the template into the DOM tree synchronously |
| + * @return {Element} Child element which has been stamped into the DOM tree. |
| + */ |
| + get: function() { |
| + if (!this.child_) |
| + this.render_(); |
| + return this.child_; |
| + }, |
| + |
| + /** @private */ |
| + render_: function() { |
| + if (!this.ctor) |
| + this.templatize(this); |
| + var parentNode = this.parentNode; |
|
michaelpg
2017/02/01 07:53:50
is it really worth creating a variable for this.pa
dschuyler
2017/02/01 21:52:30
I'm curious about the concern. Most reviewers so f
michaelpg
2017/02/02 00:00:35
That may be true for some variables, but we're tal
Dan Beam
2017/02/02 00:19:36
i don't think it matters either way
Dan Beam
2017/02/02 00:19:36
waiiiiit, why do you have to check parentNode? do
dschuyler
2017/02/02 02:28:15
I agree that it's unlikely to matter either way an
dschuyler
2017/02/02 02:28:15
I asked Tim about this (he's the original author),
Dan Beam
2017/02/02 03:56:17
I guess I mean: the idle callback is created on at
dschuyler
2017/02/02 22:13:54
Ah. I've made a follow-up CL to explore that quest
|
| + if (parentNode && !this.child_) { |
| + var instance = this.stamp({}); |
| + this.child_ = instance.root.firstElementChild; |
| + parentNode.insertBefore(instance.root, this); |
| + } |
| + }, |
| + |
| + /** |
| + * @param {string} prop |
| + * @param {Object} value |
| + */ |
| + _forwardParentProp: function(prop, value) { |
| + if (this.child_) |
| + this.child_._templateInstance[prop] = value; |
|
michaelpg
2017/02/01 07:53:50
this bypasses __setProperty which does some stuff
dschuyler
2017/02/01 21:52:31
It's done this way in cr-lazy-render (which settin
tsergeant
2017/02/01 22:14:59
FWIW, cr-lazy-render used to use _setProperty here
michaelpg
2017/02/02 00:00:35
SGTM, thanks for the info.
|
| + }, |
| + |
| + /** |
| + * @param {string} path |
| + * @param {Object} value |
| + */ |
| + _forwardParentPath: function(path, value) { |
| + if (this.child_) |
| + this.child_._templateInstance.notifyPath(path, value, true); |
| + } |
| +}); |