-
Notifications
You must be signed in to change notification settings - Fork 8k
/
panel_management.ts
139 lines (128 loc) · 4.19 KB
/
panel_management.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
132
133
134
135
136
137
138
139
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import {
EmbeddableInput,
EmbeddableOutput,
IEmbeddable,
PanelState,
} from '@kbn/embeddable-plugin/public';
import { v4 as uuidv4 } from 'uuid';
import {
IPanelPlacementArgs,
PanelPlacementMethod,
} from '../../component/panel/dashboard_panel_placement';
import { DashboardPanelState } from '../../../../common';
import { createPanelState } from '../../component/panel';
import { DashboardContainer } from '../dashboard_container';
import { PLACEHOLDER_EMBEDDABLE } from '../../../placeholder_embeddable';
export async function addOrUpdateEmbeddable<
EEI extends EmbeddableInput = EmbeddableInput,
EEO extends EmbeddableOutput = EmbeddableOutput,
E extends IEmbeddable<EEI, EEO> = IEmbeddable<EEI, EEO>
>(this: DashboardContainer, type: string, explicitInput: Partial<EEI>, embeddableId?: string) {
const idToReplace = embeddableId || explicitInput.id;
if (idToReplace && this.input.panels[idToReplace]) {
return this.replacePanel(this.input.panels[idToReplace], {
type,
explicitInput: {
...explicitInput,
id: idToReplace,
},
});
}
return this.addNewEmbeddable<EEI, EEO, E>(type, explicitInput);
}
export async function replacePanel(
this: DashboardContainer,
previousPanelState: DashboardPanelState<EmbeddableInput>,
newPanelState: Partial<PanelState>,
generateNewId?: boolean
): Promise<string> {
let panels;
let panelId;
if (generateNewId) {
// replace panel can be called with generateNewId in order to totally destroy and recreate the embeddable
panelId = uuidv4();
panels = { ...this.input.panels };
delete panels[previousPanelState.explicitInput.id];
panels[panelId] = {
...previousPanelState,
...newPanelState,
gridData: {
...previousPanelState.gridData,
i: panelId,
},
explicitInput: {
...newPanelState.explicitInput,
id: panelId,
},
};
} else {
// Because the embeddable type can change, we have to operate at the container level here
panelId = previousPanelState.explicitInput.id;
panels = {
...this.input.panels,
[panelId]: {
...previousPanelState,
...newPanelState,
gridData: {
...previousPanelState.gridData,
},
explicitInput: {
...newPanelState.explicitInput,
id: panelId,
},
},
};
}
await this.updateInput({ panels });
return panelId;
}
export function showPlaceholderUntil<TPlacementMethodArgs extends IPanelPlacementArgs>(
this: DashboardContainer,
newStateComplete: Promise<Partial<PanelState>>,
placementMethod?: PanelPlacementMethod<TPlacementMethodArgs>,
placementArgs?: TPlacementMethodArgs
): void {
const originalPanelState = {
type: PLACEHOLDER_EMBEDDABLE,
explicitInput: {
id: uuidv4(),
disabledActions: [
'ACTION_CUSTOMIZE_PANEL',
'CUSTOM_TIME_RANGE',
'clonePanel',
'replacePanel',
'togglePanel',
],
},
} as PanelState<EmbeddableInput>;
const { otherPanels, newPanel: placeholderPanelState } = createPanelState(
originalPanelState,
this.input.panels,
placementMethod,
placementArgs
);
this.updateInput({
panels: {
...otherPanels,
[placeholderPanelState.explicitInput.id]: placeholderPanelState,
},
});
// wait until the placeholder is ready, then replace it with new panel
// this is useful as sometimes panels can load faster than the placeholder one (i.e. by value embeddables)
this.untilEmbeddableLoaded(originalPanelState.explicitInput.id)
.then(() => newStateComplete)
.then(async (newPanelState: Partial<PanelState>) => {
const panelId = await this.replacePanel(placeholderPanelState, newPanelState);
if (placementArgs?.scrollToPanel) {
this.setScrollToPanelId(panelId);
this.setHighlightPanelId(panelId);
}
});
}