util.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { PanelPluginMeta, PluginState, unEscapeStringFromRegex } from '@grafana/data';
  2. import { config } from 'app/core/config';
  3. export function getAllPanelPluginMeta(): PanelPluginMeta[] {
  4. const allPanels = config.panels;
  5. return Object.keys(allPanels)
  6. .filter((key) => allPanels[key]['hideFromList'] === false)
  7. .map((key) => allPanels[key])
  8. .sort((a: PanelPluginMeta, b: PanelPluginMeta) => a.sort - b.sort);
  9. }
  10. export function filterPluginList(
  11. pluginsList: PanelPluginMeta[],
  12. searchQuery: string, // Note: this will be an escaped regex string as it comes from `FilterInput`
  13. current: PanelPluginMeta
  14. ): PanelPluginMeta[] {
  15. if (!searchQuery.length) {
  16. return pluginsList.filter((p) => {
  17. if (p.state === PluginState.deprecated) {
  18. return current.id === p.id;
  19. }
  20. return true;
  21. });
  22. }
  23. const query = unEscapeStringFromRegex(searchQuery).toLowerCase();
  24. const first: PanelPluginMeta[] = [];
  25. const match: PanelPluginMeta[] = [];
  26. const isGraphQuery = 'graph'.startsWith(query);
  27. for (const item of pluginsList) {
  28. if (item.state === PluginState.deprecated && current.id !== item.id) {
  29. continue;
  30. }
  31. const name = item.name.toLowerCase();
  32. const idx = name.indexOf(query);
  33. if (idx === 0) {
  34. first.push(item);
  35. } else if (idx > 0) {
  36. match.push(item);
  37. } else if (isGraphQuery && item.id === 'timeseries') {
  38. first.push(item);
  39. }
  40. }
  41. return first.concat(match);
  42. }