copying options between visualizations

This commit is contained in:
Peter Holmberg
2019-02-21 13:43:36 +01:00
parent 58f194b483
commit 89883c2cf6
5 changed files with 35 additions and 14 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ export interface PanelEditorProps<T = any> {
onChange: (options: T) => void; 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> { export class ReactPanelPlugin<TOptions = any> {
panel: ComponentClass<PanelProps<TOptions>>; panel: ComponentClass<PanelProps<TOptions>>;
@@ -85,7 +85,14 @@ export class DashboardPanel extends PureComponent<Props, State> {
} }
if (panel.type !== pluginId) { 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 }); this.setState({ plugin, angularPanel: null });
@@ -228,10 +228,6 @@ export class PanelModel {
}, {}); }, {});
} }
private saveCurrentPanelOptions() {
this.cachedPluginOptions[this.type] = this.getOptionsToRemember();
}
private restorePanelOptions(pluginId: string) { private restorePanelOptions(pluginId: string) {
const prevOptions = this.cachedPluginOptions[pluginId] || {}; const prevOptions = this.cachedPluginOptions[pluginId] || {};
@@ -240,14 +236,11 @@ export class PanelModel {
}); });
} }
changeType(pluginId: string, fromAngularPanel: boolean) { changeType(pluginId: string, preserveOptions?: any) {
this.saveCurrentPanelOptions(); const oldOptions: any = this.getOptionsToRemember();
this.type = pluginId; const oldPluginId = this.type;
// for angular panels only we need to remove all events and let angular panels do some cleanup this.type = pluginId;
if (fromAngularPanel) {
this.destroy();
}
// remove panel type specific options // remove panel type specific options
for (const key of _.keys(this)) { for (const key of _.keys(this)) {
@@ -258,7 +251,13 @@ export class PanelModel {
delete this[key]; delete this[key];
} }
this.cachedPluginOptions[oldPluginId] = oldOptions;
this.restorePanelOptions(pluginId); this.restorePanelOptions(pluginId);
if (preserveOptions && oldOptions) {
this.options = this.options || {};
Object.assign(this.options, preserveOptions(oldPluginId, oldOptions.options));
}
} }
addQuery(query?: Partial<DataQuery>) { addQuery(query?: Partial<DataQuery>) {
@@ -8,3 +8,15 @@ export const reactPanel = new ReactPanelPlugin<BarGaugeOptions>(BarGaugePanel);
reactPanel.setEditor(BarGaugePanelEditor); reactPanel.setEditor(BarGaugePanelEditor);
reactPanel.setDefaults(defaults); 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;
});
+4 -1
View File
@@ -11,8 +11,11 @@ reactPanel.setDefaults(defaults);
reactPanel.setPreserveOptionsHandler((pluginId: string, prevOptions: any) => { reactPanel.setPreserveOptionsHandler((pluginId: string, prevOptions: any) => {
const options: Partial<GaugeOptions> = {}; const options: Partial<GaugeOptions> = {};
if (prevOptions.valueOptions.unit) { if (prevOptions.valueOptions) {
options.valueOptions = prevOptions.valueOptions; options.valueOptions = prevOptions.valueOptions;
options.thresholds = prevOptions.thresholds;
options.maxValue = prevOptions.maxValue;
options.minValue = prevOptions.minValue;
} }
return options; return options;