Commit d8595e9f by ryan

moved migration hook to its own function

parent a6c2a8c3
......@@ -22,12 +22,17 @@ export interface PanelEditorProps<T = any> {
}
/**
* Called when a panel is first loaded with existing options
*/
export type PanelMigrationHook<TOptions = any> = (options: Partial<TOptions>) => Partial<TOptions>;
/**
* Called before a panel is initalized
*/
export type PanelTypeChangedHook<TOptions = any> = (
options: Partial<TOptions>,
prevPluginId?: string,
prevOptions?: any
prevPluginId: string,
prevOptions: any
) => Partial<TOptions>;
export class ReactPanelPlugin<TOptions = any> {
......@@ -35,6 +40,7 @@ export class ReactPanelPlugin<TOptions = any> {
editor?: ComponentClass<PanelEditorProps<TOptions>>;
defaults?: TOptions;
panelMigrationHook?: PanelMigrationHook<TOptions>;
panelTypeChangedHook?: PanelTypeChangedHook<TOptions>;
constructor(panel: ComponentClass<PanelProps<TOptions>>) {
......@@ -50,6 +56,13 @@ export class ReactPanelPlugin<TOptions = any> {
}
/**
* Called when the panel first loaded with
*/
setPanelMigrationHook(v: PanelMigrationHook<TOptions>) {
this.panelMigrationHook = v;
}
/**
* Called when the visualization changes.
* Lets you keep whatever settings made sense in the previous panel
*/
......
......@@ -98,10 +98,10 @@ export class DashboardPanel extends PureComponent<Props, State> {
}
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);
}
}
......
......@@ -10,23 +10,23 @@ const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'threshol
export const singleStatBaseOptionsCheck = (
options: Partial<SingleStatBaseOptions>,
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<SingleStatBaseOptions>) => {
// 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);
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