2022-10-24 08:39:10 +08:00
|
|
|
import { AppEvents, PluginState, SelectableValue } from '@grafana/data';
|
|
|
|
|
import { hasAlphaPanels } from 'app/core/config';
|
2022-07-12 21:31:02 +08:00
|
|
|
|
|
|
|
|
import appEvents from '../../../core/app_events';
|
2022-10-24 08:39:10 +08:00
|
|
|
import { advancedElementItems, CanvasElementItem, defaultElementItems } from '../../../features/canvas';
|
2022-07-12 21:31:02 +08:00
|
|
|
import { ElementState } from '../../../features/canvas/runtime/element';
|
|
|
|
|
import { FrameState } from '../../../features/canvas/runtime/frame';
|
|
|
|
|
import { Scene, SelectionParams } from '../../../features/canvas/runtime/scene';
|
|
|
|
|
|
|
|
|
|
export function doSelect(scene: Scene, element: ElementState | FrameState) {
|
|
|
|
|
try {
|
|
|
|
|
let selection: SelectionParams = { targets: [] };
|
|
|
|
|
if (element instanceof FrameState) {
|
|
|
|
|
const targetElements: HTMLDivElement[] = [];
|
|
|
|
|
targetElements.push(element?.div!);
|
|
|
|
|
selection.targets = targetElements;
|
|
|
|
|
selection.frame = element;
|
|
|
|
|
scene.select(selection);
|
|
|
|
|
} else {
|
|
|
|
|
scene.currentLayer = element.parent;
|
|
|
|
|
selection.targets = [element?.div!];
|
|
|
|
|
scene.select(selection);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
appEvents.emit(AppEvents.alertError, ['Unable to select element, try selecting element in panel instead']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-08-02 04:34:05 +08:00
|
|
|
|
2022-10-24 08:39:10 +08:00
|
|
|
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;
|
2022-08-02 04:34:05 +08:00
|
|
|
}
|