mirror of https://github.com/grafana/grafana.git
copying options between visualizations
This commit is contained in:
parent
58f194b483
commit
89883c2cf6
|
|
@ -25,7 +25,7 @@ export interface PanelEditorProps<T = any> {
|
|||
onChange: (options: T) => void;
|
||||
}
|
||||
|
||||
export type PreservePanelOptionsHandler<TOptions> = (pluginId: string, prevOptions: any) => Partial<TOptions>;
|
||||
export type PreservePanelOptionsHandler<TOptions = any> = (pluginId: string, prevOptions: any) => Partial<TOptions>;
|
||||
|
||||
export class ReactPanelPlugin<TOptions = any> {
|
||||
panel: ComponentClass<PanelProps<TOptions>>;
|
||||
|
|
|
|||
|
|
@ -85,7 +85,14 @@ export class DashboardPanel extends PureComponent<Props, State> {
|
|||
}
|
||||
|
||||
if (panel.type !== pluginId) {
|
||||
this.props.panel.changeType(pluginId, fromAngularPanel);
|
||||
if (fromAngularPanel) {
|
||||
// for angular panels only we need to remove all events and let angular panels do some cleanup
|
||||
panel.destroy();
|
||||
|
||||
this.props.panel.changeType(pluginId);
|
||||
} else {
|
||||
panel.changeType(pluginId, plugin.exports.reactPanel.preserveOptions);
|
||||
}
|
||||
}
|
||||
|
||||
this.setState({ plugin, angularPanel: null });
|
||||
|
|
|
|||
|
|
@ -228,10 +228,6 @@ export class PanelModel {
|
|||
}, {});
|
||||
}
|
||||
|
||||
private saveCurrentPanelOptions() {
|
||||
this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
|
||||
}
|
||||
|
||||
private restorePanelOptions(pluginId: string) {
|
||||
const prevOptions = this.cachedPluginOptions[pluginId] || {};
|
||||
|
||||
|
|
@ -240,14 +236,11 @@ export class PanelModel {
|
|||
});
|
||||
}
|
||||
|
||||
changeType(pluginId: string, fromAngularPanel: boolean) {
|
||||
this.saveCurrentPanelOptions();
|
||||
this.type = pluginId;
|
||||
changeType(pluginId: string, preserveOptions?: any) {
|
||||
const oldOptions: any = this.getOptionsToRemember();
|
||||
const oldPluginId = this.type;
|
||||
|
||||
// for angular panels only we need to remove all events and let angular panels do some cleanup
|
||||
if (fromAngularPanel) {
|
||||
this.destroy();
|
||||
}
|
||||
this.type = pluginId;
|
||||
|
||||
// remove panel type specific options
|
||||
for (const key of _.keys(this)) {
|
||||
|
|
@ -258,7 +251,13 @@ export class PanelModel {
|
|||
delete this[key];
|
||||
}
|
||||
|
||||
this.cachedPluginOptions[oldPluginId] = oldOptions;
|
||||
this.restorePanelOptions(pluginId);
|
||||
|
||||
if (preserveOptions && oldOptions) {
|
||||
this.options = this.options || {};
|
||||
Object.assign(this.options, preserveOptions(oldPluginId, oldOptions.options));
|
||||
}
|
||||
}
|
||||
|
||||
addQuery(query?: Partial<DataQuery>) {
|
||||
|
|
|
|||
|
|
@ -8,3 +8,15 @@ export const reactPanel = new ReactPanelPlugin<BarGaugeOptions>(BarGaugePanel);
|
|||
|
||||
reactPanel.setEditor(BarGaugePanelEditor);
|
||||
reactPanel.setDefaults(defaults);
|
||||
reactPanel.setPreserveOptionsHandler((pluginId: string, prevOptions: any) => {
|
||||
const options: Partial<BarGaugeOptions> = {};
|
||||
|
||||
if (prevOptions.valueOptions) {
|
||||
options.valueOptions = prevOptions.valueOptions;
|
||||
options.thresholds = prevOptions.thresholds;
|
||||
options.maxValue = prevOptions.maxValue;
|
||||
options.minValue = prevOptions.minValue;
|
||||
}
|
||||
|
||||
return options;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -11,8 +11,11 @@ reactPanel.setDefaults(defaults);
|
|||
reactPanel.setPreserveOptionsHandler((pluginId: string, prevOptions: any) => {
|
||||
const options: Partial<GaugeOptions> = {};
|
||||
|
||||
if (prevOptions.valueOptions.unit) {
|
||||
if (prevOptions.valueOptions) {
|
||||
options.valueOptions = prevOptions.valueOptions;
|
||||
options.thresholds = prevOptions.thresholds;
|
||||
options.maxValue = prevOptions.maxValue;
|
||||
options.minValue = prevOptions.minValue;
|
||||
}
|
||||
|
||||
return options;
|
||||
|
|
|
|||
Loading…
Reference in New Issue