From dede40d459e722e1de1de9e0164e4ff5478fea22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Sun, 17 Feb 2019 07:03:42 +0100 Subject: [PATCH] Began work on handling panel type switching and keep setting --- packages/grafana-ui/src/types/panel.ts | 7 +++++++ .../dashboard/dashgrid/DashboardPanel.tsx | 16 +++++++--------- public/app/plugins/panel/bargauge/types.ts | 2 +- public/app/plugins/panel/gauge/module.tsx | 9 +++++++++ public/app/plugins/panel/gauge/types.ts | 2 +- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/packages/grafana-ui/src/types/panel.ts b/packages/grafana-ui/src/types/panel.ts index cc3c6fdb086..051798a3792 100644 --- a/packages/grafana-ui/src/types/panel.ts +++ b/packages/grafana-ui/src/types/panel.ts @@ -25,10 +25,13 @@ export interface PanelEditorProps { onChange: (options: T) => void; } +export type PreservePanelOptionsHandler = (pluginId: string, prevOptions: any) => Partial; + export class ReactPanelPlugin { panel: ComponentClass>; editor?: ComponentClass>; defaults?: TOptions; + preserveOptions?: PreservePanelOptionsHandler; constructor(panel: ComponentClass>) { this.panel = panel; @@ -41,6 +44,10 @@ export class ReactPanelPlugin { setDefaults(defaults: TOptions) { this.defaults = defaults; } + + setPreserveOptionsHandler(handler: PreservePanelOptionsHandler) { + this.preserveOptions = handler; + } } export interface PanelSize { diff --git a/public/app/features/dashboard/dashgrid/DashboardPanel.tsx b/public/app/features/dashboard/dashgrid/DashboardPanel.tsx index 8f52a9d18db..962947fa2a6 100644 --- a/public/app/features/dashboard/dashgrid/DashboardPanel.tsx +++ b/public/app/features/dashboard/dashgrid/DashboardPanel.tsx @@ -76,21 +76,19 @@ export class DashboardPanel extends PureComponent { // unmount angular panel this.cleanUpAngularPanel(); - if (panel.type !== pluginId) { - this.props.panel.changeType(pluginId, fromAngularPanel); - } - - if (plugin.exports) { - this.setState({ plugin, angularPanel: null }); - } else { + if (!plugin.exports) { try { plugin.exports = await importPluginModule(plugin.module); } catch (e) { plugin = getPanelPluginNotFound(pluginId); } - - this.setState({ plugin, angularPanel: null }); } + + if (panel.type !== pluginId) { + this.props.panel.changeType(pluginId, fromAngularPanel); + } + + this.setState({ plugin, angularPanel: null }); } } diff --git a/public/app/plugins/panel/bargauge/types.ts b/public/app/plugins/panel/bargauge/types.ts index 1f20050d141..8c7a24d9d62 100644 --- a/public/app/plugins/panel/bargauge/types.ts +++ b/public/app/plugins/panel/bargauge/types.ts @@ -19,6 +19,6 @@ export const defaults: BarGaugeOptions = { suffix: '', decimals: null, }, - thresholds: [{ index: 2, value: 80, color: 'red' }, { index: 0, value: -Infinity, color: 'green' }], + thresholds: [{ index: 1, value: 80, color: 'red' }, { index: 0, value: -Infinity, color: 'green' }], valueMappings: [], }; diff --git a/public/app/plugins/panel/gauge/module.tsx b/public/app/plugins/panel/gauge/module.tsx index a32cb7cd538..c11d397b425 100644 --- a/public/app/plugins/panel/gauge/module.tsx +++ b/public/app/plugins/panel/gauge/module.tsx @@ -8,3 +8,12 @@ export const reactPanel = new ReactPanelPlugin(GaugePanel); reactPanel.setEditor(GaugePanelEditor); reactPanel.setDefaults(defaults); +reactPanel.setPreserveOptionsHandler((pluginId: string, prevOptions: any) => { + const options: Partial = {}; + + if (prevOptions.valueOptions.unit) { + options.valueOptions = prevOptions.valueOptions; + } + + return options; +}); diff --git a/public/app/plugins/panel/gauge/types.ts b/public/app/plugins/panel/gauge/types.ts index 10dd475eff5..d6f2b8ab251 100644 --- a/public/app/plugins/panel/gauge/types.ts +++ b/public/app/plugins/panel/gauge/types.ts @@ -31,5 +31,5 @@ export const defaults: GaugeOptions = { unit: 'none', }, valueMappings: [], - thresholds: [], + thresholds: [{ index: 1, value: 80, color: 'red' }, { index: 0, value: -Infinity, color: 'green' }], };