Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple API for adding a settings page #13467

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 48 additions & 4 deletions shell/core/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
TabLocation,
ModelExtensionConstructor,
PluginRouteRecordRaw, RegisterStore, UnregisterStore, CoreStoreSpecifics, CoreStoreConfig, OnNavToPackage, OnNavAwayFromPackage, OnLogOut,
ExtensionEnvironment
ExtensionEnvironment,
SettingsPage,
} from './types';
import coreStore, { coreStoreModule, coreStoreState } from '@shell/plugins/dashboard-store';
import { defineAsyncComponent, markRaw, Component } from 'vue';
Expand All @@ -29,14 +30,16 @@ export const EXT_IDS = {

export type ProductFunction = (plugin: IPlugin, store: any) => void;

export type ProductModule = { init: ProductFunction };

export class Plugin implements IPlugin {
public id: string;
public name: string;
public types: any = {};
public l10n: { [key: string]: Function[] } = {};
public modelExtensions: { [key: string]: Function[] } = {};
public locales: { locale: string, label: string}[] = [];
public products: ProductFunction[] = [];
public products: Promise<ProductModule>[] = [];
public productNames: string[] = [];
public routes: { parent?: string, route: RouteRecordRaw }[] = [];
public stores: { storeName: string, register: RegisterStore, unregister: UnregisterStore }[] = [];
Expand Down Expand Up @@ -108,8 +111,14 @@ export class Plugin implements IPlugin {
return storeDSL;
}

addProduct(product: ProductFunction): void {
this.products.push(product);
addProduct(product: ProductFunction | Promise<ProductModule>): void {
if (typeof product === 'function') {
this.products.push(new Promise<ProductModule>((resolve) => {
resolve({ init: product as ProductFunction });
}));
} else {
this.products.push(product as Promise<ProductModule>);
}
}

addLocale(locale: string, label: string): void {
Expand Down Expand Up @@ -259,6 +268,41 @@ export class Plugin implements IPlugin {
});
}

/**
* Add a settings page to Global Settings
*/
addSettingsPage(settings: SettingsPage) {
if (settings.name && settings.labelKey && settings.component) {
const name = settings.name.replaceAll('-', '_');
const routeName = `c-cluster-settings-${ name }`;

this.addRoute({
name: routeName,
path: `/c/:cluster/settings/${ name }`,
component: settings.component,
});

const updateSettingProduct = function init($plugin: IPlugin, store: any) {
const {
virtualType,
basicType
} = $plugin.DSL(store, 'settings');

virtualType({
name: settings.name,
labelKey: settings.labelKey,
route: { name: routeName },
});

basicType([settings.name]);
};

this.addProduct(updateSettingProduct);
} else {
console.error('Incomplete settings page configuration', settings); // eslint-disable-line no-console
}
}

addUninstallHook(hook: Function) {
this.uninstallHooks.push(hook);
}
Expand Down
16 changes: 15 additions & 1 deletion shell/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ProductFunction } from './plugin';
import { RouteRecordRaw } from 'vue-router';
import { RouteRecordRaw, RouteComponent } from 'vue-router';

// Cluster Provisioning types
export * from './types-provisioning';
Expand Down Expand Up @@ -166,6 +166,14 @@ export type ExtensionEnvironment = {
docsVersion: string; /** e.g. 'v2.10' */
};

/**
* Settings Page metadata to add a new page into Global Settings
*/
export type SettingsPage = {
name: string;
labelKey: string;
component: RouteComponent;
};
export interface ProductOptions {
/**
* The category this product belongs under. i.e. 'config'
Expand Down Expand Up @@ -588,6 +596,12 @@ export interface IPlugin {
*/
setHomePage(component: any): void;

/**
* Add a settings page to Global Settings
* @param settings Settings configuration
*/
addSettingsPage(settings: SettingsPage): void;

/**
* Add routes to the Vue Router
*/
Expand Down
Loading