2022-02-10 08:45:39 -06:00
|
|
|
import { PanelPluginMeta, PluginState, unEscapeStringFromRegex } from '@grafana/data';
|
2021-10-25 06:55:06 -05:00
|
|
|
import { config } from 'app/core/config';
|
|
|
|
|
|
|
|
export function getAllPanelPluginMeta(): PanelPluginMeta[] {
|
|
|
|
const allPanels = config.panels;
|
|
|
|
|
|
|
|
return Object.keys(allPanels)
|
|
|
|
.filter((key) => allPanels[key]['hideFromList'] === false)
|
|
|
|
.map((key) => allPanels[key])
|
|
|
|
.sort((a: PanelPluginMeta, b: PanelPluginMeta) => a.sort - b.sort);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function filterPluginList(
|
|
|
|
pluginsList: PanelPluginMeta[],
|
2022-02-10 08:45:39 -06:00
|
|
|
searchQuery: string, // Note: this will be an escaped regex string as it comes from `FilterInput`
|
2021-10-25 06:55:06 -05:00
|
|
|
current: PanelPluginMeta
|
|
|
|
): PanelPluginMeta[] {
|
|
|
|
if (!searchQuery.length) {
|
|
|
|
return pluginsList.filter((p) => {
|
|
|
|
if (p.state === PluginState.deprecated) {
|
|
|
|
return current.id === p.id;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-10 08:45:39 -06:00
|
|
|
const query = unEscapeStringFromRegex(searchQuery).toLowerCase();
|
2021-10-25 06:55:06 -05:00
|
|
|
const first: PanelPluginMeta[] = [];
|
|
|
|
const match: PanelPluginMeta[] = [];
|
2021-11-15 12:31:03 -06:00
|
|
|
const isGraphQuery = 'graph'.startsWith(query);
|
2021-10-25 06:55:06 -05:00
|
|
|
|
|
|
|
for (const item of pluginsList) {
|
|
|
|
if (item.state === PluginState.deprecated && current.id !== item.id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = item.name.toLowerCase();
|
|
|
|
const idx = name.indexOf(query);
|
|
|
|
|
|
|
|
if (idx === 0) {
|
|
|
|
first.push(item);
|
|
|
|
} else if (idx > 0) {
|
|
|
|
match.push(item);
|
2021-11-15 12:31:03 -06:00
|
|
|
} else if (isGraphQuery && item.id === 'timeseries') {
|
|
|
|
first.push(item);
|
2021-10-25 06:55:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return first.concat(match);
|
|
|
|
}
|