From e19330a9dbfed9695ed7d4fea6206ab0a8715cc4 Mon Sep 17 00:00:00 2001 From: Viktar Donich Date: Mon, 13 Aug 2018 15:30:27 -0700 Subject: [PATCH] Install style modules in case plugins were installed early Applies style modules ahead of loading all of the plugins. Also, apply style modules before full rendering of the element. This allows use of plugin styling in plugin bundles and applies plugin styles before first render, applying site theme before the default one. Change-Id: Ibfab5879ba49c0fa2c5eef0cdaa875fce1d35d1e --- .../gr-external-style/gr-external-style.js | 27 +++++- .../gr-external-style_test.html | 84 +++++++++++++++---- 2 files changed, 91 insertions(+), 20 deletions(-) diff --git a/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style.js b/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style.js index 51ad0b7eab..0e8bb459e0 100644 --- a/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style.js +++ b/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style.js @@ -22,24 +22,35 @@ properties: { name: String, + _urlsImported: { + type: Array, + value() { return []; }, + }, + _stylesApplied: { + type: Array, + value() { return []; }, + }, }, _import(url) { + if (this._urlsImported.includes(url)) { return Promise.resolve(); } + this._urlsImported.push(url); return new Promise((resolve, reject) => { this.importHref(url, resolve, reject); }); }, _applyStyle(name) { + if (this._stylesApplied.includes(name)) { return; } + this._stylesApplied.push(name); const s = document.createElement('style', 'custom-style'); s.setAttribute('include', name); Polymer.dom(this.root).appendChild(s); }, - ready() { - Gerrit.awaitPluginsLoaded().then(() => Promise.all( - Gerrit._endpoints.getPlugins(this.name).map( - pluginUrl => this._import(pluginUrl))) + _importAndApply() { + Promise.all(Gerrit._endpoints.getPlugins(this.name).map( + pluginUrl => this._import(pluginUrl)) ).then(() => { const moduleNames = Gerrit._endpoints.getModules(this.name); for (const name of moduleNames) { @@ -47,5 +58,13 @@ } }); }, + + attached() { + this._importAndApply(); + }, + + ready() { + Gerrit.awaitPluginsLoaded().then(() => this._importAndApply()); + }, }); })(); diff --git a/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style_test.html b/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style_test.html index d1893baf46..ec2888d13f 100644 --- a/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style_test.html +++ b/polygerrit-ui/app/elements/plugins/gr-external-style/gr-external-style_test.html @@ -32,38 +32,90 @@ limitations under the License.