Chromium Code Reviews| Index: ui/login/resource_loader.js |
| diff --git a/ui/login/resource_loader.js b/ui/login/resource_loader.js |
| index e44a088c47acbf04655289295960b76d751b89db..e358cfb8d777278d8019320b5d0b90ccdb6c6684 100644 |
| --- a/ui/login/resource_loader.js |
| +++ b/ui/login/resource_loader.js |
| @@ -24,7 +24,8 @@ cr.define('cr.ui.login.ResourceLoader', function() { |
| * @param {Array=} desc.css URLs containing CSS rules. |
| * @param {Array<Object>=} desc.html Descriptors for HTML fragments, |
| * each of which has a 'url' property and a 'targetID' property that |
| - * specifies the node under which the HTML should be appended. |
| + * specifies the node under which the HTML should be appended. If 'targetID' |
| + * is null, then the fetched body will be appended to document.body. |
| * |
| * Example: |
| * ResourceLoader.registerAssets({ |
| @@ -118,15 +119,18 @@ cr.define('cr.ui.login.ResourceLoader', function() { |
| * @param {string} id Identifier of the page's asset bundle. |
| * @param {Object} html Descriptor of the HTML to fetch. |
| * @param {string} html.url The URL resolving to some HTML. |
| - * @param {string} html.targetID The element ID to which the retrieved |
| - * HTML nodes should be appended. |
| + * @param {string?} html.targetID The element ID to which the retrieved |
| + * HTML nodes should be appended. If null, then the elements will be appended |
| + * to document.body instead. |
| */ |
| function loadHTML(id, html) { |
| var xhr = new XMLHttpRequest(); |
| xhr.open('GET', html.url); |
| xhr.onreadystatechange = function() { |
| if (isSuccessful(html.url, xhr)) { |
| - moveNodes(this.responseXML.body, $(html.targetID)); |
| + moveNodes(this.responseXML.head, document.head); |
| + moveNodes(this.responseXML.body, $(html.targetID) || document.body); |
| + |
| resourceLoaded(id); |
| } |
| }; |
| @@ -153,7 +157,7 @@ cr.define('cr.ui.login.ResourceLoader', function() { |
| */ |
| function finishedLoading(id) { |
| var assets = ASSETS[id]; |
| - console.log('Finished loading asset bundle', id); |
| + console.log('Finished loading asset bundle ' + id); |
| assets.loaded = true; |
| window.setTimeout(function() { |
| assets.callback(); |
| @@ -169,7 +173,7 @@ cr.define('cr.ui.login.ResourceLoader', function() { |
| function loadAssets(id, callback) { |
| var assets = ASSETS[id]; |
| assets.callback = callback || function() {}; |
| - console.log('Loading asset bundle', id); |
| + console.log('Loading asset bundle ' + id); |
| if (alreadyLoadedAssets(id)) |
| console.warn('asset bundle', id, 'already loaded!'); |
| if (assets.count == 0) { |
| @@ -181,10 +185,33 @@ cr.define('cr.ui.login.ResourceLoader', function() { |
| } |
| } |
| + /** |
| + * Load an asset bundle after the document has been loaded and Chrome is idle. |
| + * @param {string} id Identifier for the asset bundle to load. |
| + * @param {function()=} callback Function to invoke when done loading. |
| + * @param {number=} idleTimeoutMs The maximum amount of time to wait for an |
| + * idle notification. |
| + */ |
| + function loadAssetsOnIdle(id, callback, idleTimeoutMs = 250) { |
|
xiyuan
2016/05/09 18:27:38
idleTimeoutMs -> opt_IdleTimeoutMs since it is an
stevenjb
2016/05/09 19:10:26
We have been using ES6 is settings UI since it wil
jdufault
2016/05/10 18:45:23
Done.
|
| + let loadOnIdle = function() { |
| + window.requestIdleCallback(function() { |
| + loadAssets(id, callback); |
| + }, { timeout: idleTimeoutMs }); |
| + }; |
| + |
| + if (document.readyState == 'complete') { |
| + loadOnIdle(); |
| + } |
| + else { |
|
xiyuan
2016/05/09 18:27:38
nit: move this to line 204, i.e. } else {
jdufault
2016/05/10 18:45:23
Done.
|
| + window.addEventListener('DOMContentLoaded', loadOnIdle); |
| + } |
| + } |
| + |
| return { |
| alreadyLoadedAssets: alreadyLoadedAssets, |
| hasDeferredAssets: hasDeferredAssets, |
| loadAssets: loadAssets, |
| + loadAssetsOnIdle: loadAssetsOnIdle, |
| registerAssets: registerAssets |
| }; |
| }); |