Convert gr-plugin-host to typescript
The change converts the following files to typescript:
* elements/plugins/gr-plugin-host/gr-plugin-host.ts
Change-Id: I348a4ad6afed898c28e307ef31f6af65df8bd6fb
diff --git a/polygerrit-ui/app/elements/plugins/gr-plugin-host/gr-plugin-host.ts b/polygerrit-ui/app/elements/plugins/gr-plugin-host/gr-plugin-host.ts
index 6c10c7c..ed84406 100644
--- a/polygerrit-ui/app/elements/plugins/gr-plugin-host/gr-plugin-host.ts
+++ b/polygerrit-ui/app/elements/plugins/gr-plugin-host/gr-plugin-host.ts
@@ -14,47 +14,45 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-import '../../shared/gr-js-api-interface/gr-js-api-interface.js';
-import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
-import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin.js';
-import {PolymerElement} from '@polymer/polymer/polymer-element.js';
-import {getPluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader.js';
+import '../../shared/gr-js-api-interface/gr-js-api-interface';
+import {GestureEventListeners} from '@polymer/polymer/lib/mixins/gesture-event-listeners';
+import {LegacyElementMixin} from '@polymer/polymer/lib/legacy/legacy-element-mixin';
+import {PolymerElement} from '@polymer/polymer/polymer-element';
+import {getPluginLoader} from '../../shared/gr-js-api-interface/gr-plugin-loader';
+import {customElement, property} from '@polymer/decorators';
+import {ServerInfo} from '../../../types/common';
-/** @extends PolymerElement */
+@customElement('gr-plugin-host')
class GrPluginHost extends GestureEventListeners(
- LegacyElementMixin(
- PolymerElement)) {
- static get is() { return 'gr-plugin-host'; }
+ LegacyElementMixin(PolymerElement)
+) {
+ @property({type: Object, observer: '_configChanged'})
+ config?: ServerInfo;
- static get properties() {
- return {
- config: {
- type: Object,
- observer: '_configChanged',
- },
- };
- }
-
- _configChanged(config) {
+ _configChanged(config: ServerInfo) {
const plugins = config.plugin;
- const htmlPlugins = (plugins && plugins.html_resource_paths || []);
+ const htmlPlugins = (plugins && plugins.html_resource_paths) || [];
const jsPlugins = this._handleMigrations(
- plugins && plugins.js_resource_paths || [], htmlPlugins
+ (plugins && plugins.js_resource_paths) || [],
+ htmlPlugins
);
- const shouldLoadTheme = config.default_theme &&
- !getPluginLoader().isPluginPreloaded('preloaded:gerrit-theme');
- const themeToLoad =
- shouldLoadTheme ? [config.default_theme] : [];
+ const shouldLoadTheme =
+ !!config.default_theme &&
+ !getPluginLoader().isPluginPreloaded('preloaded:gerrit-theme');
+ // config.default_theme is defined when shouldLoadTheme is true
+ const themeToLoad: string[] = shouldLoadTheme
+ ? [config.default_theme!]
+ : [];
// Theme should be loaded first if has one to have better UX
- const pluginsPending =
- themeToLoad.concat(jsPlugins, htmlPlugins);
+ const pluginsPending = themeToLoad.concat(jsPlugins, htmlPlugins);
- const pluginOpts = {};
+ const pluginOpts: {[key: string]: {sync: boolean}} = {};
if (shouldLoadTheme) {
+ // config.default_theme is defined when shouldLoadTheme is true
// Theme needs to be loaded synchronous.
- pluginOpts[config.default_theme] = {sync: true};
+ pluginOpts[config.default_theme!] = {sync: true};
}
getPluginLoader().loadPlugins(pluginsPending, pluginOpts);
@@ -64,7 +62,7 @@
* Omit .js plugins that have .html counterparts.
* For example, if plugin provides foo.js and foo.html, skip foo.js.
*/
- _handleMigrations(jsPlugins, htmlPlugins) {
+ _handleMigrations(jsPlugins: string[], htmlPlugins: string[]) {
return jsPlugins.filter(url => {
const counterpart = url.replace(/\.js$/, '.html');
return !htmlPlugins.includes(counterpart);
@@ -72,4 +70,8 @@
}
}
-customElements.define(GrPluginHost.is, GrPluginHost);
+declare global {
+ interface HTMLElementTagNameMap {
+ 'gr-plugin-host': GrPluginHost;
+ }
+}
diff --git a/polygerrit-ui/app/types/common.ts b/polygerrit-ui/app/types/common.ts
index f453c4a..364a1e8 100644
--- a/polygerrit-ui/app/types/common.ts
+++ b/polygerrit-ui/app/types/common.ts
@@ -908,6 +908,8 @@
*/
export interface PluginConfigInfo {
has_avatars: boolean;
+ js_resource_paths: string[];
+ html_resource_paths: string[];
}
/**