Canvas: Button element (alpha) (#57491)

This commit is contained in:
Adela Almasan
2022-10-23 19:39:10 -05:00
committed by GitHub
parent 6e3de8d016
commit 73c215ae41
6 changed files with 52 additions and 17 deletions

View File

@@ -1,7 +1,8 @@
import { AppEvents } from '@grafana/data/src';
import { AppEvents, PluginState, SelectableValue } from '@grafana/data';
import { hasAlphaPanels } from 'app/core/config';
import appEvents from '../../../core/app_events';
import { CanvasElementItem, canvasElementRegistry, defaultElementItems } from '../../../features/canvas';
import { advancedElementItems, CanvasElementItem, defaultElementItems } from '../../../features/canvas';
import { ElementState } from '../../../features/canvas/runtime/element';
import { FrameState } from '../../../features/canvas/runtime/frame';
import { Scene, SelectionParams } from '../../../features/canvas/runtime/scene';
@@ -25,13 +26,46 @@ export function doSelect(scene: Scene, element: ElementState | FrameState) {
}
}
export function getElementTypes(
shouldShowAdvancedTypes: boolean | undefined,
current: string[] | undefined = undefined
) {
return shouldShowAdvancedTypes
? canvasElementRegistry.selectOptions(current, (elementItem) => elementItem.id !== 'button').options
: canvasElementRegistry.selectOptions(current, (elementItem: CanvasElementItem<any, any>) => {
return !!defaultElementItems.filter((item) => item.id === elementItem.id).length;
}).options;
export function getElementTypes(shouldShowAdvancedTypes: boolean | undefined, current?: string): RegistrySelectInfo {
if (shouldShowAdvancedTypes) {
return getElementTypesOptions([...defaultElementItems, ...advancedElementItems], current);
}
return getElementTypesOptions([...defaultElementItems], current);
}
interface RegistrySelectInfo {
options: Array<SelectableValue<string>>;
current: Array<SelectableValue<string>>;
}
export function getElementTypesOptions(
items: Array<CanvasElementItem<any>>,
current: string | undefined
): RegistrySelectInfo {
const selectables: RegistrySelectInfo = { options: [], current: [] };
const alpha: Array<SelectableValue<string>> = [];
for (const item of items) {
const option: SelectableValue<string> = { label: item.name, value: item.id, description: item.description };
if (item.state === PluginState.alpha) {
if (!hasAlphaPanels) {
continue;
}
option.label = `${item.name} (Alpha)`;
alpha.push(option);
} else {
selectables.options.push(option);
}
if (item.id === current) {
selectables.current.push(option);
}
}
for (const a of alpha) {
selectables.options.push(a);
}
return selectables;
}