// Libraries import React, { PureComponent } from 'react'; // Utils & Services import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader'; // Components import { EditorTabBody } from './EditorTabBody'; import { VizTypePicker } from './VizTypePicker'; // Types import { PanelModel } from '../panel_model'; import { DashboardModel } from '../dashboard_model'; import { PanelPlugin } from 'app/types/plugins'; interface Props { panel: PanelModel; dashboard: DashboardModel; plugin: PanelPlugin; angularPanel?: AngularComponent; onTypeChanged: (newType: PanelPlugin) => void; } interface State { isVizPickerOpen: boolean; searchQuery: string; } export class VisualizationTab extends PureComponent { element: HTMLElement; angularOptions: AngularComponent; searchInput: HTMLElement; constructor(props) { super(props); this.state = { isVizPickerOpen: false, searchQuery: '', }; } getPanelDefaultOptions = () => { const { panel, plugin } = this.props; if (plugin.exports.PanelDefaults) { return panel.getOptions(plugin.exports.PanelDefaults.options); } return panel.getOptions(plugin.exports.PanelDefaults); }; renderPanelOptions() { const { plugin, angularPanel } = this.props; const { PanelOptions } = plugin.exports; if (angularPanel) { return
(this.element = element)} />; } if (PanelOptions) { return ; } else { return

Visualization has no options

; } } componentDidMount() { if (this.shouldLoadAngularOptions()) { this.loadAngularOptions(); } } componentDidUpdate(prevProps: Props) { if (this.props.plugin !== prevProps.plugin) { this.cleanUpAngularOptions(); } if (this.shouldLoadAngularOptions()) { this.loadAngularOptions(); } } shouldLoadAngularOptions() { return this.props.angularPanel && this.element && !this.angularOptions; } loadAngularOptions() { const { angularPanel } = this.props; const scope = angularPanel.getScope(); // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet if (!scope.$$childHead) { setTimeout(() => { this.forceUpdate(); }); return; } const panelCtrl = scope.$$childHead.ctrl; let template = ''; for (let i = 0; i < panelCtrl.editorTabs.length; i++) { template += `
` + (i > 0 ? `
{{ctrl.editorTabs[${i}].title}}
` : '') + `
`; } const loader = getAngularLoader(); const scopeProps = { ctrl: panelCtrl }; this.angularOptions = loader.load(this.element, scopeProps, template); } componentWillUnmount() { this.cleanUpAngularOptions(); } cleanUpAngularOptions() { if (this.angularOptions) { this.angularOptions.destroy(); this.angularOptions = null; } } onPanelOptionsChanged = (options: any) => { this.props.panel.updateOptions(options); this.forceUpdate(); }; onOpenVizPicker = () => { this.setState({ isVizPickerOpen: true }); }; onSearchQueryChange = evt => { const value = evt.target.value; this.setState({ searchQuery: value, }); }; renderToolbar = (): JSX.Element => { const { plugin } = this.props; const { searchQuery } = this.state; if (this.state.isVizPickerOpen) { return ( ); } else { return (
{plugin.name}
); } }; onTypeChanged = (plugin: PanelPlugin) => { // this.setState({ isVizPickerOpen: false }); this.props.onTypeChanged(plugin); }; render() { const { plugin } = this.props; const { isVizPickerOpen, searchQuery } = this.state; const panelHelp = { title: '', icon: 'fa fa-question', render: () =>

Help

, }; return ( {isVizPickerOpen && } {this.renderPanelOptions()} ); } }