2022-11-07 08:19:31 -06:00
|
|
|
import { GrafanaPlugin, NavModel, NavModelItem, PanelPluginMeta, PluginType } from '@grafana/data';
|
2022-09-05 07:56:08 -05:00
|
|
|
import { config } from '@grafana/runtime';
|
2022-04-22 08:33:13 -05:00
|
|
|
|
|
|
|
import { importPanelPluginFromMeta } from './importPanelPlugin';
|
2021-11-19 06:42:26 -06:00
|
|
|
import { getPluginSettings } from './pluginSettings';
|
|
|
|
import { importAppPlugin, importDataSourcePlugin } from './plugin_loader';
|
|
|
|
|
|
|
|
export async function loadPlugin(pluginId: string): Promise<GrafanaPlugin> {
|
|
|
|
const info = await getPluginSettings(pluginId);
|
|
|
|
let result: GrafanaPlugin | undefined;
|
|
|
|
|
|
|
|
if (info.type === PluginType.app) {
|
|
|
|
result = await importAppPlugin(info);
|
|
|
|
}
|
|
|
|
if (info.type === PluginType.datasource) {
|
|
|
|
result = await importDataSourcePlugin(info);
|
|
|
|
}
|
|
|
|
if (info.type === PluginType.panel) {
|
|
|
|
const panelPlugin = await importPanelPluginFromMeta(info as PanelPluginMeta);
|
2022-02-02 06:02:32 -06:00
|
|
|
result = panelPlugin as unknown as GrafanaPlugin;
|
2021-11-19 06:42:26 -06:00
|
|
|
}
|
|
|
|
if (info.type === PluginType.renderer) {
|
|
|
|
result = { meta: info } as GrafanaPlugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
throw new Error('Unknown Plugin type: ' + info.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2022-09-05 07:56:08 -05:00
|
|
|
|
2022-09-28 01:29:35 -05:00
|
|
|
export function buildPluginSectionNav(
|
2022-11-07 08:19:31 -06:00
|
|
|
pluginNavSection: NavModelItem,
|
2022-09-28 01:29:35 -05:00
|
|
|
pluginNav: NavModel | null,
|
2022-11-07 08:19:31 -06:00
|
|
|
currentUrl: string
|
2022-09-29 06:27:51 -05:00
|
|
|
): NavModel | undefined {
|
2022-09-05 07:56:08 -05:00
|
|
|
// When topnav is disabled we only just show pluginNav like before
|
|
|
|
if (!config.featureToggles.topnav) {
|
2022-09-29 06:27:51 -05:00
|
|
|
return pluginNav ?? undefined;
|
2022-09-05 07:56:08 -05:00
|
|
|
}
|
|
|
|
|
2022-09-29 06:27:51 -05:00
|
|
|
// shallow clone as we set active flag
|
2022-11-07 08:19:31 -06:00
|
|
|
let copiedPluginNavSection = { ...pluginNavSection };
|
2022-09-05 07:56:08 -05:00
|
|
|
let activePage: NavModelItem | undefined;
|
|
|
|
|
2022-09-29 06:27:51 -05:00
|
|
|
function setPageToActive(page: NavModelItem, currentUrl: string): NavModelItem {
|
|
|
|
if (!currentUrl.startsWith(page.url ?? '')) {
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
2023-01-26 08:10:09 -06:00
|
|
|
// Check if there is already an active page found with with a more specific url (possibly a child of the current page)
|
|
|
|
// (In this case we bail out early and don't mark the parent as active)
|
2022-09-29 06:27:51 -05:00
|
|
|
if (activePage && (activePage.url?.length ?? 0) > (page.url?.length ?? 0)) {
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (activePage) {
|
|
|
|
activePage.active = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
activePage = { ...page, active: true };
|
2023-01-26 08:10:09 -06:00
|
|
|
|
2022-09-29 06:27:51 -05:00
|
|
|
return activePage;
|
|
|
|
}
|
|
|
|
|
2022-09-28 01:29:35 -05:00
|
|
|
// Find and set active page
|
2022-11-07 08:19:31 -06:00
|
|
|
copiedPluginNavSection.children = (copiedPluginNavSection?.children ?? []).map((child) => {
|
2022-09-05 07:56:08 -05:00
|
|
|
if (child.children) {
|
2023-01-26 08:10:09 -06:00
|
|
|
// Doing this here to make sure that first we check if any of the children is active
|
|
|
|
// (In case yes, then the check for the parent will not mark it as active)
|
|
|
|
const children = child.children.map((pluginPage) => setPageToActive(pluginPage, currentUrl));
|
|
|
|
|
2022-09-05 07:56:08 -05:00
|
|
|
return {
|
2022-10-05 04:46:27 -05:00
|
|
|
...setPageToActive(child, currentUrl),
|
2023-01-26 08:10:09 -06:00
|
|
|
children,
|
2022-09-05 07:56:08 -05:00
|
|
|
};
|
|
|
|
}
|
2022-09-29 06:27:51 -05:00
|
|
|
|
|
|
|
return setPageToActive(child, currentUrl);
|
2022-09-05 07:56:08 -05:00
|
|
|
});
|
|
|
|
|
2022-11-07 08:19:31 -06:00
|
|
|
return { main: copiedPluginNavSection, node: activePage ?? copiedPluginNavSection };
|
2022-09-28 01:29:35 -05:00
|
|
|
}
|