2023-09-11 13:51:05 +02:00
|
|
|
import * as H from 'history';
|
|
|
|
|
|
2023-11-02 20:02:25 +01:00
|
|
|
import { NavIndex } from '@grafana/data';
|
2024-02-07 00:51:25 -08:00
|
|
|
import { config, locationService } from '@grafana/runtime';
|
2024-02-14 13:37:52 +01:00
|
|
|
import { SceneGridItem, SceneObject, SceneObjectBase, SceneObjectState, VizPanel } from '@grafana/scenes';
|
2023-09-11 13:51:05 +02:00
|
|
|
|
2024-01-17 05:53:53 -08:00
|
|
|
import {
|
|
|
|
|
findVizPanelByKey,
|
|
|
|
|
getDashboardSceneFor,
|
|
|
|
|
getPanelIdForVizPanel,
|
|
|
|
|
getVizPanelKeyForPanelId,
|
|
|
|
|
} from '../utils/utils';
|
2023-09-11 13:51:05 +02:00
|
|
|
|
2023-12-07 10:25:06 +01:00
|
|
|
import { PanelDataPane } from './PanelDataPane/PanelDataPane';
|
2023-09-11 13:51:05 +02:00
|
|
|
import { PanelEditorRenderer } from './PanelEditorRenderer';
|
|
|
|
|
import { PanelOptionsPane } from './PanelOptionsPane';
|
2023-12-06 16:14:54 +00:00
|
|
|
import { VizPanelManager } from './VizPanelManager';
|
2023-09-11 13:51:05 +02:00
|
|
|
|
|
|
|
|
export interface PanelEditorState extends SceneObjectState {
|
|
|
|
|
controls?: SceneObject[];
|
|
|
|
|
isDirty?: boolean;
|
2024-01-17 05:53:53 -08:00
|
|
|
panelId: number;
|
2024-02-16 13:04:45 +01:00
|
|
|
optionsPane: PanelOptionsPane;
|
2024-02-14 13:37:52 +01:00
|
|
|
dataPane?: PanelDataPane;
|
|
|
|
|
vizManager: VizPanelManager;
|
2023-09-11 13:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class PanelEditor extends SceneObjectBase<PanelEditorState> {
|
|
|
|
|
static Component = PanelEditorRenderer;
|
|
|
|
|
|
2024-02-13 14:23:47 +01:00
|
|
|
private _discardChanges = false;
|
|
|
|
|
|
2023-09-11 13:51:05 +02:00
|
|
|
public constructor(state: PanelEditorState) {
|
|
|
|
|
super(state);
|
2024-02-13 14:23:47 +01:00
|
|
|
|
2024-02-14 13:37:52 +01:00
|
|
|
this.addActivationHandler(this._activationHandler.bind(this));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _activationHandler() {
|
|
|
|
|
const panelManager = this.state.vizManager;
|
|
|
|
|
const panel = panelManager.state.panel;
|
|
|
|
|
|
|
|
|
|
this._subs.add(
|
|
|
|
|
panelManager.subscribeToState((n, p) => {
|
|
|
|
|
if (n.panel.state.pluginId !== p.panel.state.pluginId) {
|
|
|
|
|
this._initDataPane(n.panel.state.pluginId);
|
2024-02-13 14:23:47 +01:00
|
|
|
}
|
2024-02-14 13:37:52 +01:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
this._initDataPane(panel.state.pluginId);
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
if (!this._discardChanges) {
|
|
|
|
|
this.commitChanges();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _initDataPane(pluginId: string) {
|
|
|
|
|
const skipDataQuery = config.panels[pluginId].skipDataQuery;
|
|
|
|
|
|
2024-02-16 13:04:45 +01:00
|
|
|
if (skipDataQuery && this.state.dataPane) {
|
2024-02-14 13:37:52 +01:00
|
|
|
locationService.partial({ tab: null }, true);
|
|
|
|
|
this.setState({ dataPane: undefined });
|
|
|
|
|
}
|
2024-02-16 13:04:45 +01:00
|
|
|
|
|
|
|
|
if (!skipDataQuery && !this.state.dataPane) {
|
|
|
|
|
this.setState({ dataPane: new PanelDataPane(this.state.vizManager) });
|
|
|
|
|
}
|
2023-09-11 13:51:05 +02:00
|
|
|
}
|
2024-02-14 13:37:52 +01:00
|
|
|
|
2024-01-17 05:53:53 -08:00
|
|
|
public getUrlKey() {
|
|
|
|
|
return this.state.panelId.toString();
|
2023-09-11 13:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-02 20:02:25 +01:00
|
|
|
public getPageNav(location: H.Location, navIndex: NavIndex) {
|
2024-01-17 05:53:53 -08:00
|
|
|
const dashboard = getDashboardSceneFor(this);
|
|
|
|
|
|
2023-09-11 13:51:05 +02:00
|
|
|
return {
|
|
|
|
|
text: 'Edit panel',
|
2024-01-17 05:53:53 -08:00
|
|
|
parentItem: dashboard.getPageNav(location, navIndex),
|
2023-09-11 13:51:05 +02:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onDiscard = () => {
|
2024-02-13 14:23:47 +01:00
|
|
|
this._discardChanges = true;
|
|
|
|
|
locationService.partial({ editPanel: null });
|
2023-09-11 13:51:05 +02:00
|
|
|
};
|
|
|
|
|
|
2024-02-13 14:23:47 +01:00
|
|
|
public commitChanges() {
|
2024-01-17 05:53:53 -08:00
|
|
|
const dashboard = getDashboardSceneFor(this);
|
|
|
|
|
const sourcePanel = findVizPanelByKey(dashboard.state.body, getVizPanelKeyForPanelId(this.state.panelId));
|
2023-12-19 14:51:19 +01:00
|
|
|
|
2023-09-11 13:51:05 +02:00
|
|
|
if (!dashboard.state.isEditing) {
|
|
|
|
|
dashboard.onEnterEditMode();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-17 05:53:53 -08:00
|
|
|
if (sourcePanel!.parent instanceof SceneGridItem) {
|
2024-02-14 13:37:52 +01:00
|
|
|
sourcePanel!.parent.setState({ body: this.state.vizManager.state.panel.clone() });
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-11 13:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
2024-01-17 05:53:53 -08:00
|
|
|
export function buildPanelEditScene(panel: VizPanel): PanelEditor {
|
2023-09-11 13:51:05 +02:00
|
|
|
const panelClone = panel.clone();
|
2024-01-12 01:21:32 -08:00
|
|
|
const vizPanelMgr = new VizPanelManager(panelClone);
|
2023-09-11 13:51:05 +02:00
|
|
|
|
|
|
|
|
return new PanelEditor({
|
2024-01-17 05:53:53 -08:00
|
|
|
panelId: getPanelIdForVizPanel(panel),
|
2024-02-14 13:37:52 +01:00
|
|
|
optionsPane: new PanelOptionsPane({}),
|
|
|
|
|
vizManager: vizPanelMgr,
|
2023-09-11 13:51:05 +02:00
|
|
|
});
|
|
|
|
|
}
|