diff --git a/packages/grafana-ui/src/types/panel.ts b/packages/grafana-ui/src/types/panel.ts index 6c42ecfceb9..1878c858167 100644 --- a/packages/grafana-ui/src/types/panel.ts +++ b/packages/grafana-ui/src/types/panel.ts @@ -21,13 +21,18 @@ export interface PanelEditorProps { onOptionsChange: (options: T) => void; } +/** + * Called when a panel is first loaded with existing options + */ +export type PanelMigrationHook = (options: Partial) => Partial; + /** * Called before a panel is initalized */ export type PanelTypeChangedHook = ( options: Partial, - prevPluginId?: string, - prevOptions?: any + prevPluginId: string, + prevOptions: any ) => Partial; export class ReactPanelPlugin { @@ -35,6 +40,7 @@ export class ReactPanelPlugin { editor?: ComponentClass>; defaults?: TOptions; + panelMigrationHook?: PanelMigrationHook; panelTypeChangedHook?: PanelTypeChangedHook; constructor(panel: ComponentClass>) { @@ -49,6 +55,13 @@ export class ReactPanelPlugin { this.defaults = defaults; } + /** + * Called when the panel first loaded with + */ + setPanelMigrationHook(v: PanelMigrationHook) { + this.panelMigrationHook = v; + } + /** * Called when the visualization changes. * Lets you keep whatever settings made sense in the previous panel diff --git a/public/app/features/dashboard/dashgrid/DashboardPanel.tsx b/public/app/features/dashboard/dashgrid/DashboardPanel.tsx index e554e081239..243380c617e 100644 --- a/public/app/features/dashboard/dashgrid/DashboardPanel.tsx +++ b/public/app/features/dashboard/dashgrid/DashboardPanel.tsx @@ -98,10 +98,10 @@ export class DashboardPanel extends PureComponent { } panel.changeType(pluginId, hook); } - } else if (plugin.exports && plugin.exports.reactPanel) { - const hook = plugin.exports.reactPanel.panelTypeChangedHook; + } else if (plugin.exports && plugin.exports.reactPanel && panel.options) { + const hook = plugin.exports.reactPanel.panelMigrationHook; if (hook) { - panel.options = hook(panel.options || {}, null, null); + panel.options = hook(panel.options); } } diff --git a/public/app/plugins/panel/singlestat2/module.tsx b/public/app/plugins/panel/singlestat2/module.tsx index 89ee712470d..6ce0de69dbc 100644 --- a/public/app/plugins/panel/singlestat2/module.tsx +++ b/public/app/plugins/panel/singlestat2/module.tsx @@ -10,23 +10,23 @@ const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'threshol export const singleStatBaseOptionsCheck = ( options: Partial, - prevPluginId?: string, - prevOptions?: any + prevPluginId: string, + prevOptions: any ) => { - if (prevOptions) { - for (const otk of optionsToKeep) { - if (prevOptions.hasOwnProperty(otk)) { - options[otk] = cloneDeep(prevOptions[otk]); - } + optionsToKeep.forEach(v => { + if (prevOptions.hasOwnProperty(v)) { + options[v] = cloneDeep(prevOptions.display); } - } + }); + return options; +}; +export const singleStatMigrationCheck = (options: Partial) => { // 6.1 renamed some stats, This makes sure they are up to date // avg -> mean, current -> last, total -> sum const { valueOptions } = options; if (valueOptions && valueOptions.stat) { valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0]; - console.log('CHANGED', valueOptions); } return options; }; @@ -34,3 +34,4 @@ export const singleStatBaseOptionsCheck = ( reactPanel.setEditor(SingleStatEditor); reactPanel.setDefaults(defaults); reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck); +reactPanel.setPanelMigrationHook(singleStatMigrationCheck);