2022-09-05 20:56:08 +08:00
|
|
|
import { Location as HistoryLocation } from 'history';
|
|
|
|
|
|
|
|
|
|
import { GrafanaPlugin, NavIndex, NavModel, NavModelItem, PanelPluginMeta, PluginType } from '@grafana/data';
|
|
|
|
|
import { config } from '@grafana/runtime';
|
2022-04-22 21:33:13 +08:00
|
|
|
|
|
|
|
|
import { importPanelPluginFromMeta } from './importPanelPlugin';
|
2021-11-19 20:42:26 +08: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 20:02:32 +08:00
|
|
|
result = panelPlugin as unknown as GrafanaPlugin;
|
2021-11-19 20:42:26 +08: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 20:56:08 +08:00
|
|
|
|
2022-09-28 14:29:35 +08:00
|
|
|
export function buildPluginSectionNav(
|
|
|
|
|
location: HistoryLocation,
|
|
|
|
|
pluginNav: NavModel | null,
|
|
|
|
|
navIndex: NavIndex,
|
|
|
|
|
pluginId: string
|
|
|
|
|
) {
|
2022-09-05 20:56:08 +08:00
|
|
|
// When topnav is disabled we only just show pluginNav like before
|
|
|
|
|
if (!config.featureToggles.topnav) {
|
|
|
|
|
return pluginNav;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 14:29:35 +08:00
|
|
|
const section = { ...getPluginSection(location, navIndex, pluginId) };
|
2022-09-05 20:56:08 +08:00
|
|
|
|
|
|
|
|
// If we have plugin nav don't set active page in section as it will cause double breadcrumbs
|
|
|
|
|
const currentUrl = config.appSubUrl + location.pathname + location.search;
|
|
|
|
|
let activePage: NavModelItem | undefined;
|
|
|
|
|
|
2022-09-28 14:29:35 +08:00
|
|
|
// Find and set active page
|
2022-09-05 20:56:08 +08:00
|
|
|
section.children = (section?.children ?? []).map((child) => {
|
|
|
|
|
if (child.children) {
|
|
|
|
|
return {
|
|
|
|
|
...child,
|
|
|
|
|
children: child.children.map((pluginPage) => {
|
|
|
|
|
if (currentUrl.startsWith(pluginPage.url ?? '')) {
|
|
|
|
|
activePage = {
|
|
|
|
|
...pluginPage,
|
|
|
|
|
active: true,
|
|
|
|
|
};
|
|
|
|
|
return activePage;
|
|
|
|
|
}
|
|
|
|
|
return pluginPage;
|
|
|
|
|
}),
|
|
|
|
|
};
|
2022-09-28 14:29:35 +08:00
|
|
|
} else {
|
|
|
|
|
if (currentUrl.startsWith(child.url ?? '')) {
|
|
|
|
|
activePage = {
|
|
|
|
|
...child,
|
|
|
|
|
active: true,
|
|
|
|
|
};
|
|
|
|
|
return activePage;
|
|
|
|
|
}
|
2022-09-05 20:56:08 +08:00
|
|
|
}
|
|
|
|
|
return child;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { main: section, node: activePage ?? section };
|
|
|
|
|
}
|
2022-09-28 14:29:35 +08:00
|
|
|
|
|
|
|
|
// TODO make work for sub pages
|
|
|
|
|
export function getPluginSection(location: HistoryLocation, navIndex: NavIndex, pluginId: string): NavModelItem {
|
|
|
|
|
// First check if this page exist in navIndex using path, some plugin pages are not under their own section
|
|
|
|
|
const byPath = navIndex[`standalone-plugin-page-${location.pathname}`];
|
|
|
|
|
if (byPath) {
|
|
|
|
|
const parent = byPath.parentItem!;
|
|
|
|
|
// in case the standalone page is in nested section
|
|
|
|
|
return parent.parentItem ?? parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const navTreeNodeForPlugin = navIndex[`plugin-page-${pluginId}`];
|
|
|
|
|
if (!navTreeNodeForPlugin) {
|
|
|
|
|
throw new Error('Plugin not found in navigation tree');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!navTreeNodeForPlugin.parentItem) {
|
|
|
|
|
throw new Error('Could not find plugin section');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return navTreeNodeForPlugin.parentItem;
|
|
|
|
|
}
|