Browse Source

remove panel plugin setters

ryan 6 years ago
parent
commit
82be27a42a

+ 4 - 26
packages/grafana-ui/src/types/panel.ts

@@ -24,7 +24,7 @@ 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>;
+export type PanelMigrationHook<TOptions = any> = (options: any) => Partial<TOptions>;
 
 /**
  * Called before a panel is initalized
@@ -40,35 +40,13 @@ export class ReactPanelPlugin<TOptions = any> {
   editor?: ComponentClass<PanelEditorProps<TOptions>>;
   defaults?: TOptions;
 
-  panelMigrationHook?: PanelMigrationHook<TOptions>;
-  panelTypeChangedHook?: PanelTypeChangedHook<TOptions>;
+  onPanelMigration?: PanelMigrationHook<TOptions>;
+  onPanelTypeChanged?: PanelTypeChangedHook<TOptions>;
 
-  constructor(panel: ComponentClass<PanelProps<TOptions>>) {
+  constructor(panel: ComponentClass<PanelProps<TOptions>>, defaults?: 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
-   */
-  setPanelMigrationHook(v: PanelMigrationHook<TOptions>) {
-    this.panelMigrationHook = v;
-  }
-
-  /**
-   * Called when the visualization changes.
-   * Lets you keep whatever settings made sense in the previous panel
-   */
-  setPanelTypeChangedHook(v: PanelTypeChangedHook<TOptions>) {
-    this.panelTypeChangedHook = v;
-  }
 }
 
 export interface PanelSize {

+ 2 - 2
public/app/features/dashboard/dashgrid/DashboardPanel.tsx

@@ -94,12 +94,12 @@ export class DashboardPanel extends PureComponent<Props, State> {
         } else {
           let hook: PanelTypeChangedHook | null = null;
           if (plugin.exports.reactPanel) {
-            hook = plugin.exports.reactPanel.panelTypeChangedHook;
+            hook = plugin.exports.reactPanel.onPanelTypeChanged;
           }
           panel.changeType(pluginId, hook);
         }
       } else if (plugin.exports && plugin.exports.reactPanel && panel.options) {
-        const hook = plugin.exports.reactPanel.panelMigrationHook;
+        const hook = plugin.exports.reactPanel.onPanelMigration;
         if (hook) {
           panel.options = hook(panel.options);
         }

+ 3 - 4
public/app/plugins/panel/bargauge/module.tsx

@@ -5,8 +5,7 @@ import { BarGaugePanelEditor } from './BarGaugePanelEditor';
 import { BarGaugeOptions, defaults } from './types';
 import { singleStatBaseOptionsCheck } from '../singlestat2/module';
 
-export const reactPanel = new ReactPanelPlugin<BarGaugeOptions>(BarGaugePanel);
+export const reactPanel = new ReactPanelPlugin<BarGaugeOptions>(BarGaugePanel, defaults);
 
-reactPanel.setEditor(BarGaugePanelEditor);
-reactPanel.setDefaults(defaults);
-reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck);
+reactPanel.editor = BarGaugePanelEditor;
+reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;

+ 5 - 5
public/app/plugins/panel/gauge/module.tsx

@@ -3,10 +3,10 @@ import { ReactPanelPlugin } from '@grafana/ui';
 import { GaugePanelEditor } from './GaugePanelEditor';
 import { GaugePanel } from './GaugePanel';
 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.setDefaults(defaults);
-reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck);
+reactPanel.editor = GaugePanelEditor;
+reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
+reactPanel.onPanelMigration = singleStatMigrationCheck;

+ 4 - 3
public/app/plugins/panel/graph2/module.tsx

@@ -2,7 +2,8 @@ import { ReactPanelPlugin } from '@grafana/ui';
 
 import { GraphPanelEditor } from './GraphPanelEditor';
 import { GraphPanel } from './GraphPanel';
-import { Options } from './types';
+import { Options, defaults } from './types';
 
-export const reactPanel = new ReactPanelPlugin<Options>(GraphPanel);
-reactPanel.setEditor(GraphPanelEditor);
+export const reactPanel = new ReactPanelPlugin<Options>(GraphPanel, defaults);
+
+reactPanel.editor = GraphPanelEditor;

+ 6 - 0
public/app/plugins/panel/graph2/types.ts

@@ -3,3 +3,9 @@ export interface Options {
   showLines: boolean;
   showPoints: boolean;
 }
+
+export const defaults: Options = {
+  showBars: false,
+  showLines: true,
+  showPoints: false,
+};

+ 2 - 3
public/app/plugins/panel/piechart/module.tsx

@@ -4,7 +4,6 @@ import PieChartPanelEditor from './PieChartPanelEditor';
 import { PieChartPanel } from './PieChartPanel';
 import { PieChartOptions, defaults } from './types';
 
-export const reactPanel = new ReactPanelPlugin<PieChartOptions>(PieChartPanel);
+export const reactPanel = new ReactPanelPlugin<PieChartOptions>(PieChartPanel, defaults);
 
-reactPanel.setEditor(PieChartPanelEditor);
-reactPanel.setDefaults(defaults);
+reactPanel.editor = PieChartPanelEditor;

+ 5 - 6
public/app/plugins/panel/singlestat2/module.tsx

@@ -4,8 +4,6 @@ import { SingleStatPanel } from './SingleStatPanel';
 import cloneDeep from 'lodash/cloneDeep';
 import { SingleStatEditor } from './SingleStatEditor';
 
-export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel);
-
 const optionsToKeep = ['valueOptions', 'stat', 'maxValue', 'maxValue', 'thresholds', 'valueMappings'];
 
 export const singleStatBaseOptionsCheck = (
@@ -33,7 +31,8 @@ export const singleStatMigrationCheck = (options: Partial<SingleStatBaseOptions>
   return options;
 };
 
-reactPanel.setEditor(SingleStatEditor);
-reactPanel.setDefaults(defaults);
-reactPanel.setPanelTypeChangedHook(singleStatBaseOptionsCheck);
-reactPanel.setPanelMigrationHook(singleStatMigrationCheck);
+export const reactPanel = new ReactPanelPlugin<SingleStatOptions>(SingleStatPanel, defaults);
+
+reactPanel.editor = SingleStatEditor;
+reactPanel.onPanelTypeChanged = singleStatBaseOptionsCheck;
+reactPanel.onPanelMigration = singleStatMigrationCheck;

+ 2 - 3
public/app/plugins/panel/table2/module.tsx

@@ -4,6 +4,5 @@ import { TablePanelEditor } from './TablePanelEditor';
 import { TablePanel } from './TablePanel';
 import { Options, defaults } from './types';
 
-export const reactPanel = new ReactPanelPlugin<Options>(TablePanel);
-reactPanel.setEditor(TablePanelEditor);
-reactPanel.setDefaults(defaults);
+export const reactPanel = new ReactPanelPlugin<Options>(TablePanel, defaults);
+reactPanel.editor = TablePanelEditor;

+ 4 - 6
public/app/plugins/panel/text2/module.tsx

@@ -4,14 +4,12 @@ import { TextPanelEditor } from './TextPanelEditor';
 import { TextPanel } from './TextPanel';
 import { TextOptions, defaults } from './types';
 
-export const reactPanel = new ReactPanelPlugin<TextOptions>(TextPanel);
+export const reactPanel = new ReactPanelPlugin<TextOptions>(TextPanel, defaults);
 
-reactPanel.setEditor(TextPanelEditor);
-reactPanel.setDefaults(defaults);
-reactPanel.setPanelTypeChangedHook((options: TextOptions, prevPluginId: string, prevOptions: any) => {
+reactPanel.editor = TextPanelEditor;
+reactPanel.onPanelTypeChanged = (options: TextOptions, prevPluginId: string, prevOptions: any) => {
   if (prevPluginId === 'text') {
     return prevOptions as TextOptions;
   }
-
   return options;
-});
+};