Commit e17b76ad by Dominik Prokop Committed by GitHub

New Panel Edit: works for panels with and without queries (#22024)

* Make new edit work again

* add cx

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
parent a51ac787
import React, { PureComponent } from 'react';
import { css, cx } from 'emotion';
import { GrafanaTheme } from '@grafana/data';
import { GrafanaTheme, PanelData, LoadingState, DefaultTimeRange, PanelEvents } from '@grafana/data';
import { stylesFactory, Forms } from '@grafana/ui';
import config from 'app/core/config';
......@@ -12,6 +12,7 @@ import SplitPane from 'react-split-pane';
import { StoreState } from '../../../../types/store';
import { connect } from 'react-redux';
import { updateLocation } from '../../../../core/reducers/location';
import { Unsubscribable } from 'rxjs';
const getStyles = stylesFactory((theme: GrafanaTheme) => {
const resizer = css`
......@@ -61,15 +62,63 @@ interface Props {
}
interface State {
pluginLoadedCounter: number;
dirtyPanel?: PanelModel;
data: PanelData;
}
export class PanelEditor extends PureComponent<Props, State> {
querySubscription: Unsubscribable;
state: State = {
pluginLoadedCounter: 0,
data: {
state: LoadingState.NotStarted,
series: [],
timeRange: DefaultTimeRange,
},
};
constructor(props: Props) {
super(props);
const { panel } = props;
// To ensure visualisation settings are re-rendered when plugin has loaded
// panelInitialised event is emmited from PanelChrome
props.panel.events.on(PanelEvents.panelInitialized, () => {
this.setState(state => ({
pluginLoadedCounter: state.pluginLoadedCounter + 1,
}));
});
}
componentDidMount() {
const { panel } = this.props;
const dirtyPanel = panel.getEditClone();
this.state = { dirtyPanel };
this.setState({ dirtyPanel });
// Get data from any pending
panel
.getQueryRunner()
.getData()
.subscribe({
next: (data: PanelData) => {
this.setState({ data });
// TODO, cancel????
},
});
// Listen for queries on the new panel
const queryRunner = dirtyPanel.getQueryRunner();
this.querySubscription = queryRunner.getData().subscribe({
next: (data: PanelData) => this.setState({ data }),
});
}
componentWillUnmount() {
if (this.querySubscription) {
this.querySubscription.unsubscribe();
}
//this.cleanUpAngularOptions();
}
onPanelUpdate = () => {
......
......@@ -85,14 +85,19 @@ export class PanelChrome extends PureComponent<Props, State> {
},
isFirstLoad: false,
});
} else if (isInEditMode && this.panelHasLastResult()) {
console.log('Reusing results!');
const lastResult = panel.getQueryRunner().getLastResult();
if (lastResult) {
this.onDataUpdate(lastResult);
} else {
if (isInEditMode) {
this.querySubscription = panel
.getQueryRunner()
.getData()
.subscribe({
next: data => this.onDataUpdate(data),
});
}
if (!this.wantsQueryExecution) {
this.setState({ isFirstLoad: false });
}
} else if (!this.wantsQueryExecution) {
this.setState({ isFirstLoad: false });
}
}
......@@ -244,11 +249,7 @@ export class PanelChrome extends PureComponent<Props, State> {
};
get wantsQueryExecution() {
return !(
this.props.plugin.meta.skipDataQuery ||
this.hasPanelSnapshot ||
(this.props.isInEditMode && !this.panelHasLastResult())
);
return !(this.props.plugin.meta.skipDataQuery || this.hasPanelSnapshot);
}
onChangeTimeRange = (timeRange: AbsoluteTimeRange) => {
......
......@@ -18,6 +18,7 @@ import config from 'app/core/config';
import { PanelQueryRunner } from './PanelQueryRunner';
import { eventFactory } from '@grafana/data';
import { take } from 'rxjs/operators';
export const panelAdded = eventFactory<PanelModel | undefined>('panel-added');
export const panelRemoved = eventFactory<PanelModel | undefined>('panel-removed');
......@@ -349,7 +350,14 @@ export class PanelModel {
getEditClone() {
const clone = new PanelModel(this.getSaveModel());
clone.queryRunner = new PanelQueryRunner(this.queryRunner.getLastResult());
clone.queryRunner = new PanelQueryRunner();
// This will send the last result to the new runner
this.getQueryRunner()
.getData()
.pipe(take(1))
.subscribe(val => clone.queryRunner.pipeDataToSubject(val));
clone.isNewEdit = true;
return clone;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment