Commit 292f54ba by Torkel Ödegaard

Merge branch 'drop-panel-plugin-setters' of https://github.com/ryantxu/grafana…

Merge branch 'drop-panel-plugin-setters' of https://github.com/ryantxu/grafana into ryantxu-drop-panel-plugin-setters
parents 55723235 009cd086
...@@ -24,7 +24,7 @@ export interface PanelEditorProps<T = any> { ...@@ -24,7 +24,7 @@ export interface PanelEditorProps<T = any> {
/** /**
* Called when a panel is first loaded with existing options * Called when a panel is first loaded with existing options
*/ */
export type PanelMigrationHook<TOptions = any> = (options: Partial<TOptions>) => Partial<TOptions>; export type PanelMigrationHook<TOptions = any> = (exiting: any, oldVersion?: string) => Partial<TOptions>;
/** /**
* Called before a panel is initalized * Called before a panel is initalized
...@@ -40,34 +40,23 @@ export class ReactPanelPlugin<TOptions = any> { ...@@ -40,34 +40,23 @@ export class ReactPanelPlugin<TOptions = any> {
editor?: ComponentClass<PanelEditorProps<TOptions>>; editor?: ComponentClass<PanelEditorProps<TOptions>>;
defaults?: TOptions; defaults?: TOptions;
panelMigrationHook?: PanelMigrationHook<TOptions>;
panelTypeChangedHook?: PanelTypeChangedHook<TOptions>;
constructor(panel: ComponentClass<PanelProps<TOptions>>) {
this.panel = panel;
}
setEditor(editor: ComponentClass<PanelEditorProps<TOptions>>) {
this.editor = editor;
}
setDefaults(defaults: TOptions) {
this.defaults = defaults;
}
/** /**
* Called when the panel first loaded with * This function is called before the panel first loads if
* the current version is different than the version that was saved.
*
* This is a good place to support any changes to the options model
*/ */
setPanelMigrationHook(v: PanelMigrationHook<TOptions>) { onPanelMigration?: PanelMigrationHook<TOptions>;
this.panelMigrationHook = v;
}
/** /**
* Called when the visualization changes. * This function is called when the visualization was changed. This
* Lets you keep whatever settings made sense in the previous panel * passes in the options that were used in the previous visualization
*/ */
setPanelTypeChangedHook(v: PanelTypeChangedHook<TOptions>) { onPanelTypeChanged?: PanelTypeChangedHook<TOptions>;
this.panelTypeChangedHook = v;
constructor(panel: ComponentClass<PanelProps<TOptions>>, defaults?: TOptions) {
this.panel = panel;
this.defaults = defaults;
} }
} }
......
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import config from 'app/core/config'; import config from 'app/core/config';
import classNames from 'classnames'; import classNames from 'classnames';
import get from 'lodash/get';
import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader'; import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
import { importPluginModule } from 'app/features/plugins/plugin_loader'; import { importPluginModule } from 'app/features/plugins/plugin_loader';
...@@ -94,17 +95,18 @@ export class DashboardPanel extends PureComponent<Props, State> { ...@@ -94,17 +95,18 @@ export class DashboardPanel extends PureComponent<Props, State> {
} else { } else {
let hook: PanelTypeChangedHook | null = null; let hook: PanelTypeChangedHook | null = null;
if (plugin.exports.reactPanel) { if (plugin.exports.reactPanel) {
hook = plugin.exports.reactPanel.panelTypeChangedHook; hook = plugin.exports.reactPanel.onPanelTypeChanged;
} }
panel.changeType(pluginId, hook); panel.changeType(pluginId, hook);
} }
} else if (plugin.exports && plugin.exports.reactPanel && panel.options) { } else if (plugin.exports && plugin.exports.reactPanel && panel.options) {
const hook = plugin.exports.reactPanel.panelMigrationHook; const pluginVersion = get(plugin, 'info.version') || config.buildInfo.version;
if (hook) { const hook = plugin.exports.reactPanel.onPanelMigration;
panel.options = hook(panel.options); if (hook && panel.pluginVersion !== pluginVersion) {
panel.options = hook(panel.options, panel.pluginVersion);
panel.pluginVersion = pluginVersion;
} }
} }
this.setState({ plugin, angularPanel: null }); this.setState({ plugin, angularPanel: null });
} }
} }
......
...@@ -58,6 +58,7 @@ const mustKeepProps: { [str: string]: boolean } = { ...@@ -58,6 +58,7 @@ const mustKeepProps: { [str: string]: boolean } = {
cacheTimeout: true, cacheTimeout: true,
cachedPluginOptions: true, cachedPluginOptions: true,
transparent: true, transparent: true,
pluginVersion: true,
}; };
const defaults: any = { const defaults: any = {
...@@ -87,6 +88,7 @@ export class PanelModel { ...@@ -87,6 +88,7 @@ export class PanelModel {
targets: DataQuery[]; targets: DataQuery[];
datasource: string; datasource: string;
thresholds?: any; thresholds?: any;
pluginVersion?: string;
snapshotData?: TimeSeries[] | [TableData]; snapshotData?: TimeSeries[] | [TableData];
timeFrom?: any; timeFrom?: any;
......
...@@ -5,8 +5,7 @@ import { BarGaugePanelEditor } from './BarGaugePanelEditor'; ...@@ -5,8 +5,7 @@ import { BarGaugePanelEditor } from './BarGaugePanelEditor';
import { BarGaugeOptions, defaults } from './types'; import { BarGaugeOptions, defaults } from './types';
import { singleStatBaseOptionsCheck } from '../singlestat2/module'; import { singleStatBaseOptionsCheck } from '../singlestat2/module';
export const reactPanel = new ReactPanelPlugin<BarGaugeOptions>(BarGaugePanel); export const reactPanel = new ReactPanelPlugin<BarGaugeOptions>(BarGaugePanel, defaults);
reactPanel.setEditor(BarGaugePanelEditor); reactPanel.editor = BarGaugePanelEditor;
reactPanel.setDefaults(defaults); reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck);
...@@ -3,10 +3,10 @@ import { ReactPanelPlugin } from '@grafana/ui'; ...@@ -3,10 +3,10 @@ import { ReactPanelPlugin } from '@grafana/ui';
import { GaugePanelEditor } from './GaugePanelEditor'; import { GaugePanelEditor } from './GaugePanelEditor';
import { GaugePanel } from './GaugePanel'; import { GaugePanel } from './GaugePanel';
import { GaugeOptions, defaults } from './types'; import { GaugeOptions, defaults } from './types';
import { singleStatBaseOptionsCheck } from '../singlestat2/module'; import { singleStatBaseOptionsCheck, singleStatMigrationCheck } from '../singlestat2/module';
export const reactPanel = new ReactPanelPlugin<GaugeOptions>(GaugePanel); export const reactPanel = new ReactPanelPlugin<GaugeOptions>(GaugePanel, defaults);
reactPanel.setEditor(GaugePanelEditor); reactPanel.editor = GaugePanelEditor;
reactPanel.setDefaults(defaults); reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck); reactPanel.onPanelMigration = singleStatMigrationCheck;
...@@ -2,7 +2,8 @@ import { ReactPanelPlugin } from '@grafana/ui'; ...@@ -2,7 +2,8 @@ import { ReactPanelPlugin } from '@grafana/ui';
import { GraphPanelEditor } from './GraphPanelEditor'; import { GraphPanelEditor } from './GraphPanelEditor';
import { GraphPanel } from './GraphPanel'; import { GraphPanel } from './GraphPanel';
import { Options } from './types'; import { Options, defaults } from './types';
export const reactPanel = new ReactPanelPlugin<Options>(GraphPanel); export const reactPanel = new ReactPanelPlugin<Options>(GraphPanel, defaults);
reactPanel.setEditor(GraphPanelEditor);
reactPanel.editor = GraphPanelEditor;
...@@ -3,3 +3,9 @@ export interface Options { ...@@ -3,3 +3,9 @@ export interface Options {
showLines: boolean; showLines: boolean;
showPoints: boolean; showPoints: boolean;
} }
export const defaults: Options = {
showBars: false,
showLines: true,
showPoints: false,
};
...@@ -5,8 +5,7 @@ import { PieChartPanel } from './PieChartPanel'; ...@@ -5,8 +5,7 @@ import { PieChartPanel } from './PieChartPanel';
import { PieChartOptions, defaults } from './types'; import { PieChartOptions, defaults } from './types';
import { singleStatBaseOptionsCheck } from '../singlestat2/module'; import { singleStatBaseOptionsCheck } from '../singlestat2/module';
export const reactPanel = new ReactPanelPlugin<PieChartOptions>(PieChartPanel); export const reactPanel = new ReactPanelPlugin<PieChartOptions>(PieChartPanel, defaults);
reactPanel.setEditor(PieChartPanelEditor); reactPanel.editor = PieChartPanelEditor;
reactPanel.setDefaults(defaults); reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck);
...@@ -4,8 +4,6 @@ import { SingleStatPanel } from './SingleStatPanel'; ...@@ -4,8 +4,6 @@ import { SingleStatPanel } from './SingleStatPanel';
import cloneDeep from 'lodash/cloneDeep'; import cloneDeep from 'lodash/cloneDeep';
import { SingleStatEditor } from './SingleStatEditor'; import { SingleStatEditor } from './SingleStatEditor';
export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel);
const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'thresholds', 'valueMappings']; const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'thresholds', 'valueMappings'];
export const singleStatBaseOptionsCheck = ( export const singleStatBaseOptionsCheck = (
...@@ -23,17 +21,22 @@ export const singleStatBaseOptionsCheck = ( ...@@ -23,17 +21,22 @@ export const singleStatBaseOptionsCheck = (
return options; return options;
}; };
export const singleStatMigrationCheck = (options: Partial<SingleStatBaseOptions>) => { export const singleStatMigrationCheck = (exiting: any, oldVersion?: string) => {
// 6.1 renamed some stats, This makes sure they are up to date const options = exiting as Partial<SingleStatOptions>;
// avg -> mean, current -> last, total -> sum if (options.valueOptions) {
const { valueOptions } = options; // 6.1 renamed some stats, This makes sure they are up to date
if (valueOptions && valueOptions.stat) { // avg -> mean, current -> last, total -> sum
valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0];
const { valueOptions } = options;
if (valueOptions && valueOptions.stat) {
valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0];
}
} }
return options; return options;
}; };
reactPanel.setEditor(SingleStatEditor); export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel, defaults);
reactPanel.setDefaults(defaults);
reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck); reactPanel.editor = SingleStatEditor;
reactPanel.setPanelMigrationHook(singleStatMigrationCheck); reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
reactPanel.onPanelMigration = singleStatMigrationCheck;
...@@ -4,6 +4,5 @@ import { TablePanelEditor } from './TablePanelEditor'; ...@@ -4,6 +4,5 @@ import { TablePanelEditor } from './TablePanelEditor';
import { TablePanel } from './TablePanel'; import { TablePanel } from './TablePanel';
import { Options, defaults } from './types'; import { Options, defaults } from './types';
export const reactPanel = new ReactPanelPlugin<Options>(TablePanel); export const reactPanel = new ReactPanelPlugin<Options>(TablePanel, defaults);
reactPanel.setEditor(TablePanelEditor); reactPanel.editor = TablePanelEditor;
reactPanel.setDefaults(defaults);
...@@ -4,14 +4,12 @@ import { TextPanelEditor } from './TextPanelEditor'; ...@@ -4,14 +4,12 @@ import { TextPanelEditor } from './TextPanelEditor';
import { TextPanel } from './TextPanel'; import { TextPanel } from './TextPanel';
import { TextOptions, defaults } from './types'; import { TextOptions, defaults } from './types';
export const reactPanel = new ReactPanelPlugin<TextOptions>(TextPanel); export const reactPanel = new ReactPanelPlugin<TextOptions>(TextPanel, defaults);
reactPanel.setEditor(TextPanelEditor); reactPanel.editor = TextPanelEditor;
reactPanel.setDefaults(defaults); reactPanel.onPanelTypeChanged = (options: TextOptions, prevPluginId: string, prevOptions: any) => {
reactPanel.setPanelTypeChangedHook((options: TextOptions, prevPluginId: string, prevOptions: any) => {
if (prevPluginId === 'text') { if (prevPluginId === 'text') {
return prevOptions as TextOptions; return prevOptions as TextOptions;
} }
return options; return options;
}); };
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