| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- // 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<Props, State> {
- element: HTMLElement;
- angularOptions: AngularComponent;
- searchInput: HTMLElement;
- constructor(props) {
- super(props);
- this.state = {
- isVizPickerOpen: false,
- };
- }
- 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 <div ref={element => (this.element = element)} />;
- }
- if (PanelOptions) {
- return <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />;
- } else {
- return <p>Visualization has no options</p>;
- }
- }
- 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 +=
- `
- <div class="form-section" ng-cloak>` +
- (i > 0 ? `<div class="form-section__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
- `<div class="form-section__body">
- <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
- </div>
- </div>
- `;
- }
- 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 });
- };
- renderToolbar = () => {
- const { plugin } = this.props;
- if (this.state.isVizPickerOpen) {
- return (
- <label className="gf-form--has-input-icon">
- <input
- type="text"
- className="gf-form-input width-13"
- placeholder=""
- ref={elem => (this.searchInput = elem)}
- />
- <i className="gf-form-input-icon fa fa-search" />
- </label>
- );
- } else {
- return (
- <div className="toolbar__main" onClick={this.onOpenVizPicker}>
- <img className="toolbar__main-image" src={plugin.info.logos.small} />
- <div className="toolbar__main-name">{plugin.name}</div>
- <i className="fa fa-caret-down" />
- </div>
- );
- }
- };
- render() {
- const { plugin, onTypeChanged } = this.props;
- const { isVizPickerOpen } = this.state;
- const panelHelp = {
- title: '',
- icon: 'fa fa-question',
- render: () => <h2>Help</h2>,
- };
- return (
- <EditorTabBody heading="Visualization" renderToolbar={this.renderToolbar} toolbarItems={[panelHelp]}>
- {isVizPickerOpen && <VizTypePicker current={plugin} onTypeChanged={onTypeChanged} />}
- {this.renderPanelOptions()}
- </EditorTabBody>
- );
- }
- }
|