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-load is a simple variant of dom-if designed for lazy |
| 8 * loading and rendering of elements that are accessed imperatively. A URL is |
| 9 * given that holds the elements to be loaded lazily. |
| 10 */ |
| 11 Polymer({ |
| 12 is: 'settings-idle-load', |
| 13 extends: 'template', |
| 14 |
| 15 behaviors: [Polymer.Templatizer], |
| 16 |
| 17 properties: { |
| 18 /** |
| 19 * If specified, it will be loaded via an HTML import before stamping the |
| 20 * template. |
| 21 */ |
| 22 url: String, |
| 23 }, |
| 24 |
| 25 /** @private {TemplatizerNode} */ |
| 26 child_: null, |
| 27 |
| 28 /** @private {number} */ |
| 29 idleCallback_: 0, |
| 30 |
| 31 /** @override */ |
| 32 attached: function() { |
| 33 this.idleCallback_ = requestIdleCallback(this.get.bind(this)); |
| 34 }, |
| 35 |
| 36 /** @override */ |
| 37 detached: function() { |
| 38 // No-op if callback already fired. |
| 39 cancelIdleCallback(this.idleCallback_); |
| 40 }, |
| 41 |
| 42 /** |
| 43 * @return {!Promise<Element>} Child element which has been stamped into the |
| 44 * DOM tree. |
| 45 */ |
| 46 get: function() { |
| 47 if (this.loading_) |
| 48 return this.loading_; |
| 49 |
| 50 this.loading_ = new Promise(function(resolve, reject) { |
| 51 this.importHref(this.url, function() { |
| 52 assert(!this.ctor); |
| 53 this.templatize(this); |
| 54 assert(this.ctor); |
| 55 |
| 56 var instance = this.stamp({}); |
| 57 |
| 58 assert(!this.child_); |
| 59 this.child_ = instance.root.firstElementChild; |
| 60 |
| 61 this.parentNode.insertBefore(instance.root, this); |
| 62 resolve(this.child_); |
| 63 |
| 64 this.fire('lazy-loaded'); |
| 65 }.bind(this), reject, true); |
| 66 }.bind(this)); |
| 67 |
| 68 return this.loading_; |
| 69 }, |
| 70 |
| 71 /** |
| 72 * @param {string} prop |
| 73 * @param {Object} value |
| 74 */ |
| 75 _forwardParentProp: function(prop, value) { |
| 76 if (this.child_) |
| 77 this.child_._templateInstance[prop] = value; |
| 78 }, |
| 79 |
| 80 /** |
| 81 * @param {string} path |
| 82 * @param {Object} value |
| 83 */ |
| 84 _forwardParentPath: function(path, value) { |
| 85 if (this.child_) |
| 86 this.child_._templateInstance.notifyPath(path, value, true); |
| 87 } |
| 88 }); |
OLD | NEW |