mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
PanelEditor: add data provider when switching from non data panel (#94220)
* add data provider when switching from non data panel * handle adding and cleaning up data provider in panel editor on panel switch * add data provider check sin panel editor tests
This commit is contained in:
parent
b2d930e079
commit
f32fe9a846
@ -277,13 +277,17 @@ describe('PanelEditor', () => {
|
||||
|
||||
describe('PanelDataPane', () => {
|
||||
it('should not exist if panel is skipDataQuery', async () => {
|
||||
const { panelEditor } = await setup({ pluginSkipDataQuery: true });
|
||||
const { panelEditor, panel } = await setup({ pluginSkipDataQuery: true });
|
||||
expect(panelEditor.state.dataPane).toBeUndefined();
|
||||
|
||||
expect(panel.state.$data).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should exist if panel is supporting querying', async () => {
|
||||
const { panelEditor } = await setup({ pluginSkipDataQuery: false });
|
||||
const { panelEditor, panel } = await setup({ pluginSkipDataQuery: false });
|
||||
expect(panelEditor.state.dataPane).toBeDefined();
|
||||
|
||||
expect(panel.state.$data).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -2,18 +2,21 @@ import * as H from 'history';
|
||||
import { debounce } from 'lodash';
|
||||
|
||||
import { NavIndex, PanelPlugin } from '@grafana/data';
|
||||
import { locationService } from '@grafana/runtime';
|
||||
import { config, locationService } from '@grafana/runtime';
|
||||
import {
|
||||
PanelBuilders,
|
||||
SceneDataTransformer,
|
||||
SceneObjectBase,
|
||||
SceneObjectRef,
|
||||
SceneObjectState,
|
||||
SceneObjectStateChangedEvent,
|
||||
SceneQueryRunner,
|
||||
sceneUtils,
|
||||
VizPanel,
|
||||
} from '@grafana/scenes';
|
||||
import { Panel } from '@grafana/schema/dist/esm/index.gen';
|
||||
import { OptionFilter } from 'app/features/dashboard/components/PanelEditor/OptionsPaneOptions';
|
||||
import { getLastUsedDatasourceFromStorage } from 'app/features/dashboard/utils/dashboard';
|
||||
import { saveLibPanel } from 'app/features/library-panels/state/api';
|
||||
|
||||
import { DashboardSceneChangeTracker } from '../saving/DashboardSceneChangeTracker';
|
||||
@ -183,13 +186,46 @@ export class PanelEditor extends SceneObjectBase<PanelEditorState> {
|
||||
private _updateDataPane(plugin: PanelPlugin) {
|
||||
const skipDataQuery = plugin.meta.skipDataQuery;
|
||||
|
||||
if (skipDataQuery && this.state.dataPane) {
|
||||
locationService.partial({ tab: null }, true);
|
||||
this.setState({ dataPane: undefined });
|
||||
const panel = this.state.panelRef.resolve();
|
||||
|
||||
if (skipDataQuery) {
|
||||
if (this.state.dataPane) {
|
||||
locationService.partial({ tab: null }, true);
|
||||
this.setState({ dataPane: undefined });
|
||||
}
|
||||
|
||||
// clean up data provider when switching from data to non data panel
|
||||
if (panel.state.$data) {
|
||||
panel.setState({
|
||||
$data: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (!skipDataQuery && !this.state.dataPane) {
|
||||
this.setState({ dataPane: PanelDataPane.createFor(this.getPanel()) });
|
||||
if (!skipDataQuery) {
|
||||
if (!this.state.dataPane) {
|
||||
this.setState({ dataPane: PanelDataPane.createFor(this.getPanel()) });
|
||||
}
|
||||
|
||||
// add data provider when switching from non data to data panel
|
||||
if (!panel.state.$data) {
|
||||
let ds = getLastUsedDatasourceFromStorage(getDashboardSceneFor(this).state.uid!)?.datasourceUid;
|
||||
if (!ds) {
|
||||
ds = config.defaultDatasource;
|
||||
}
|
||||
|
||||
panel.setState({
|
||||
$data: new SceneDataTransformer({
|
||||
$data: new SceneQueryRunner({
|
||||
datasource: {
|
||||
uid: ds,
|
||||
},
|
||||
queries: [{ refId: 'A' }],
|
||||
}),
|
||||
transformations: [],
|
||||
}),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user