moved migration hook to its own function

This commit is contained in:
ryan 2019-03-22 10:22:25 -07:00
parent a6c2a8c3cc
commit d8595e9f4e
3 changed files with 28 additions and 14 deletions

View File

@ -21,13 +21,18 @@ export interface PanelEditorProps<T = any> {
onOptionsChange: (options: T) => void;
}
/**
* 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>>) {
@ -49,6 +55,13 @@ export class ReactPanelPlugin<TOptions = any> {
this.defaults = defaults;
}
/**
* 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

View File

@ -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);
}
}

View File

@ -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);