2018-11-15 09:46:21 +01:00
|
|
|
// Libraries
|
2018-11-09 13:17:41 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
// Utils & Services
|
|
|
|
|
import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
|
|
|
|
|
|
|
|
|
|
// Components
|
2018-11-09 13:17:41 +01:00
|
|
|
import { EditorTabBody } from './EditorTabBody';
|
|
|
|
|
import { VizTypePicker } from './VizTypePicker';
|
|
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
// Types
|
2018-11-09 13:17:41 +01:00
|
|
|
import { PanelModel } from '../panel_model';
|
|
|
|
|
import { DashboardModel } from '../dashboard_model';
|
2018-11-10 17:27:25 +01:00
|
|
|
import { PanelPlugin } from 'app/types/plugins';
|
2018-11-09 13:17:41 +01:00
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
|
panel: PanelModel;
|
|
|
|
|
dashboard: DashboardModel;
|
2018-11-10 17:27:25 +01:00
|
|
|
plugin: PanelPlugin;
|
2018-11-15 09:46:21 +01:00
|
|
|
angularPanel?: AngularComponent;
|
2018-11-09 13:17:41 +01:00
|
|
|
onTypeChanged: (newType: PanelPlugin) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class VisualizationTab extends PureComponent<Props> {
|
2018-11-15 09:46:21 +01:00
|
|
|
element: HTMLElement;
|
|
|
|
|
angularOptions: AngularComponent;
|
|
|
|
|
|
2018-12-03 15:02:41 +01:00
|
|
|
getPanelDefaultOptions = () => {
|
2018-12-03 13:33:17 +01:00
|
|
|
const { panel, plugin } = this.props;
|
|
|
|
|
|
|
|
|
|
if (plugin.exports.PanelDefaults) {
|
|
|
|
|
return panel.getOptions(plugin.exports.PanelDefaults.options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return panel.getOptions(plugin.exports.PanelDefaults);
|
2018-12-03 15:02:41 +01:00
|
|
|
};
|
2018-12-03 13:33:17 +01:00
|
|
|
|
2018-11-09 13:17:41 +01:00
|
|
|
renderPanelOptions() {
|
2018-12-04 12:56:54 +01:00
|
|
|
const { plugin, angularPanel } = this.props;
|
2018-11-13 08:40:42 +01:00
|
|
|
const { PanelOptions } = plugin.exports;
|
2018-11-09 13:17:41 +01:00
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
if (angularPanel) {
|
|
|
|
|
return <div ref={element => (this.element = element)} />;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-13 08:40:42 +01:00
|
|
|
if (PanelOptions) {
|
2018-12-03 13:33:17 +01:00
|
|
|
return <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />;
|
2018-11-09 13:17:41 +01:00
|
|
|
} else {
|
|
|
|
|
return <p>Visualization has no options</p>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
componentDidMount() {
|
2018-11-16 10:44:39 +01:00
|
|
|
if (this.shouldLoadAngularOptions()) {
|
|
|
|
|
this.loadAngularOptions();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
|
|
|
if (this.props.plugin !== prevProps.plugin) {
|
|
|
|
|
this.cleanUpAngularOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.shouldLoadAngularOptions()) {
|
|
|
|
|
this.loadAngularOptions();
|
|
|
|
|
}
|
2018-11-16 08:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-16 10:44:39 +01:00
|
|
|
shouldLoadAngularOptions() {
|
|
|
|
|
return this.props.angularPanel && this.element && !this.angularOptions;
|
2018-11-16 08:31:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
loadAngularOptions() {
|
2018-11-15 09:46:21 +01:00
|
|
|
const { angularPanel } = this.props;
|
2018-11-16 10:44:39 +01:00
|
|
|
|
|
|
|
|
const scope = angularPanel.getScope();
|
2018-11-15 09:46:21 +01:00
|
|
|
|
2018-11-16 10:44:39 +01:00
|
|
|
// When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
|
|
|
|
|
if (!scope.$$childHead) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.forceUpdate();
|
|
|
|
|
});
|
2018-11-16 08:31:29 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-16 10:44:39 +01:00
|
|
|
const panelCtrl = scope.$$childHead.ctrl;
|
2018-11-16 08:31:29 +01:00
|
|
|
|
2018-11-16 10:44:39 +01:00
|
|
|
let template = '';
|
|
|
|
|
for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
|
2018-12-11 10:33:09 +01:00
|
|
|
template +=
|
|
|
|
|
`
|
|
|
|
|
<div class="form-section" ng-cloak>` +
|
|
|
|
|
(i > 0 ? `<div class="form-section__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
|
|
|
|
|
`<div class="form-section__body">
|
2018-11-20 11:06:36 +01:00
|
|
|
<panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
`;
|
2018-11-16 10:44:39 +01:00
|
|
|
}
|
2018-11-16 08:31:29 +01:00
|
|
|
|
2018-11-16 10:44:39 +01:00
|
|
|
const loader = getAngularLoader();
|
|
|
|
|
const scopeProps = { ctrl: panelCtrl };
|
2018-11-15 09:46:21 +01:00
|
|
|
|
2018-11-16 10:44:39 +01:00
|
|
|
this.angularOptions = loader.load(this.element, scopeProps, template);
|
2018-11-15 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-11-16 10:44:39 +01:00
|
|
|
this.cleanUpAngularOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cleanUpAngularOptions() {
|
2018-11-15 09:46:21 +01:00
|
|
|
if (this.angularOptions) {
|
|
|
|
|
this.angularOptions.destroy();
|
2018-11-16 10:44:39 +01:00
|
|
|
this.angularOptions = null;
|
2018-11-15 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-09 13:17:41 +01:00
|
|
|
onPanelOptionsChanged = (options: any) => {
|
|
|
|
|
this.props.panel.updateOptions(options);
|
|
|
|
|
this.forceUpdate();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-11-09 11:33:33 -08:00
|
|
|
const { plugin } = this.props;
|
2018-11-09 13:17:41 +01:00
|
|
|
|
|
|
|
|
const panelSelection = {
|
|
|
|
|
title: plugin.name,
|
|
|
|
|
imgSrc: plugin.info.logos.small,
|
|
|
|
|
render: () => {
|
2018-11-09 11:33:33 -08:00
|
|
|
// the needs to be scoped inside this closure
|
|
|
|
|
const { plugin, onTypeChanged } = this.props;
|
2018-11-09 13:17:41 +01:00
|
|
|
return <VizTypePicker current={plugin} onTypeChanged={onTypeChanged} />;
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-22 11:41:33 +01:00
|
|
|
const panelHelp = {
|
|
|
|
|
title: '',
|
|
|
|
|
icon: 'fa fa-question',
|
|
|
|
|
render: () => <h2>Help</h2>,
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-09 13:17:41 +01:00
|
|
|
return (
|
2018-11-22 11:41:33 +01:00
|
|
|
<EditorTabBody heading="Visualization" main={panelSelection} toolbarItems={[panelHelp]}>
|
2018-11-09 13:17:41 +01:00
|
|
|
{this.renderPanelOptions()}
|
|
|
|
|
</EditorTabBody>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|