-
Notifications
You must be signed in to change notification settings - Fork 11.6k
/
utils.ts
131 lines (110 loc) · 4.47 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { AppEvents, Field, LinkModel, PluginState, SelectableValue } from '@grafana/data';
import { hasAlphaPanels } from 'app/core/config';
import appEvents from '../../../core/app_events';
import {
advancedElementItems,
CanvasElementItem,
CanvasElementOptions,
canvasElementRegistry,
defaultElementItems,
TextConfig,
} from '../../../features/canvas';
import { notFoundItem } from '../../../features/canvas/elements/notFound';
import { ElementState } from '../../../features/canvas/runtime/element';
import { FrameState } from '../../../features/canvas/runtime/frame';
import { Scene, SelectionParams } from '../../../features/canvas/runtime/scene';
import { DimensionContext } from '../../../features/dimensions';
import { AnchorPoint } from './types';
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']);
}
}
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: CanvasElementItem[], 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;
}
export function onAddItem(sel: SelectableValue<string>, rootLayer: FrameState | undefined, anchorPoint?: AnchorPoint) {
const newItem = canvasElementRegistry.getIfExists(sel.value) ?? notFoundItem;
const newElementOptions = newItem.getNewOptions() as CanvasElementOptions;
newElementOptions.type = newItem.id;
if (anchorPoint) {
newElementOptions.placement = { ...newElementOptions.placement, top: anchorPoint.y, left: anchorPoint.x };
}
if (newItem.defaultSize) {
newElementOptions.placement = { ...newElementOptions.placement, ...newItem.defaultSize };
}
if (rootLayer) {
const newElement = new ElementState(newItem, newElementOptions, rootLayer);
newElement.updateData(rootLayer.scene.context);
rootLayer.elements.push(newElement);
rootLayer.scene.save();
rootLayer.reinitializeMoveable();
setTimeout(() => doSelect(rootLayer.scene, newElement));
}
}
export function getDataLinks(ctx: DimensionContext, cfg: TextConfig, textData: string | undefined): LinkModel[] {
const panelData = ctx.getPanelData();
const frames = panelData?.series;
const links: Array<LinkModel<Field>> = [];
const linkLookup = new Set<string>();
frames?.forEach((frame) => {
const visibleFields = frame.fields.filter((field) => !Boolean(field.config.custom?.hideFrom?.tooltip));
if (cfg.text?.field && visibleFields.some((f) => f.name === cfg.text?.field)) {
const field = visibleFields.filter((field) => field.name === cfg.text?.field)[0];
if (field?.getLinks) {
const disp = field.display ? field.display(textData) : { text: `${textData}`, numeric: +textData! };
field.getLinks({ calculatedValue: disp }).forEach((link) => {
const key = `${link.title}/${link.href}`;
if (!linkLookup.has(key)) {
links.push(link);
linkLookup.add(key);
}
});
}
}
});
return links;
}