2018-11-15 09:46:21 +01:00
|
|
|
// Libraries
|
2018-11-09 13:17:41 +01:00
|
|
|
import React, { PureComponent } from 'react';
|
2019-01-04 12:38:50 +01:00
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
// Utils & Services
|
2018-12-21 11:57:21 +01:00
|
|
|
import { AngularComponent, getAngularLoader } from 'app/core/services/AngularLoader';
|
2019-02-05 17:15:21 +01:00
|
|
|
import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
|
|
|
|
|
import { StoreState } from 'app/types';
|
|
|
|
|
import { updateLocation } from 'app/core/actions';
|
2019-01-04 12:38:50 +01:00
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
// Components
|
2019-01-04 12:38:50 +01:00
|
|
|
import { EditorTabBody, EditorToolbarView } from './EditorTabBody';
|
2018-11-09 13:17:41 +01:00
|
|
|
import { VizTypePicker } from './VizTypePicker';
|
2018-12-19 10:53:14 +01:00
|
|
|
import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
|
2018-12-12 13:59:19 +01:00
|
|
|
import { FadeIn } from 'app/core/components/Animations/FadeIn';
|
2019-01-04 12:38:50 +01:00
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
// Types
|
2019-03-06 13:07:08 +01:00
|
|
|
import { PanelModel } from '../state';
|
|
|
|
|
import { DashboardModel } from '../state';
|
2018-11-10 17:27:25 +01:00
|
|
|
import { PanelPlugin } from 'app/types/plugins';
|
2019-03-06 13:07:08 +01:00
|
|
|
import { VizPickerSearch } from './VizPickerSearch';
|
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;
|
2019-02-05 17:15:21 +01:00
|
|
|
updateLocation: typeof updateLocation;
|
|
|
|
|
urlOpenVizPicker: boolean;
|
2018-11-09 13:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
2018-12-12 09:51:17 +01:00
|
|
|
interface State {
|
|
|
|
|
isVizPickerOpen: boolean;
|
|
|
|
|
searchQuery: string;
|
2019-01-17 11:27:32 +01:00
|
|
|
scrollTop: number;
|
2019-03-06 13:07:08 +01:00
|
|
|
hasBeenFocused: boolean;
|
2018-12-12 09:51:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class VisualizationTab extends PureComponent<Props, State> {
|
2018-11-15 09:46:21 +01:00
|
|
|
element: HTMLElement;
|
|
|
|
|
angularOptions: AngularComponent;
|
2018-12-12 09:51:17 +01:00
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
|
|
this.state = {
|
2019-02-05 17:15:21 +01:00
|
|
|
isVizPickerOpen: this.props.urlOpenVizPicker,
|
2019-03-06 13:07:08 +01:00
|
|
|
hasBeenFocused: false,
|
2018-12-12 11:24:18 +01:00
|
|
|
searchQuery: '',
|
2019-01-17 11:27:32 +01:00
|
|
|
scrollTop: 0,
|
2018-12-12 09:51:17 +01:00
|
|
|
};
|
|
|
|
|
}
|
2018-11-15 09:46:21 +01:00
|
|
|
|
2019-02-18 11:41:14 +01:00
|
|
|
getReactPanelOptions = () => {
|
2018-12-03 13:33:17 +01:00
|
|
|
const { panel, plugin } = this.props;
|
2019-02-18 11:41:14 +01:00
|
|
|
return panel.getOptions(plugin.exports.reactPanel.defaults);
|
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-09 13:17:41 +01:00
|
|
|
|
2018-11-15 09:46:21 +01:00
|
|
|
if (angularPanel) {
|
|
|
|
|
return <div ref={element => (this.element = element)} />;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-18 11:41:14 +01:00
|
|
|
if (plugin.exports.reactPanel) {
|
|
|
|
|
const PanelEditor = plugin.exports.reactPanel.editor;
|
|
|
|
|
|
|
|
|
|
if (PanelEditor) {
|
2019-03-04 12:35:24 -08:00
|
|
|
return <PanelEditor options={this.getReactPanelOptions()} onOptionsChange={this.onPanelOptionsChanged} />;
|
2019-02-18 11:41:14 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <p>Visualization has no options</p>;
|
2018-11-09 13:17:41 +01:00
|
|
|
}
|
|
|
|
|
|
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-12-19 17:00:26 +01:00
|
|
|
panelCtrl.initEditMode();
|
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 +=
|
|
|
|
|
`
|
2019-01-13 12:42:21 +01:00
|
|
|
<div class="panel-options-group" ng-cloak>` +
|
2019-02-08 21:53:51 +01:00
|
|
|
(i > 0
|
|
|
|
|
? `<div class="panel-options-group__header">
|
|
|
|
|
<span class="panel-options-group__title">{{ctrl.editorTabs[${i}].title}}
|
|
|
|
|
</span>
|
|
|
|
|
</div>`
|
|
|
|
|
: '') +
|
2019-01-13 12:42:21 +01:00
|
|
|
`<div class="panel-options-group__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
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 11:03:24 +01:00
|
|
|
clearQuery = () => {
|
|
|
|
|
this.setState({ searchQuery: '' });
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-09 13:17:41 +01:00
|
|
|
onPanelOptionsChanged = (options: any) => {
|
|
|
|
|
this.props.panel.updateOptions(options);
|
|
|
|
|
this.forceUpdate();
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-12 09:51:17 +01:00
|
|
|
onOpenVizPicker = () => {
|
2019-01-17 11:27:32 +01:00
|
|
|
this.setState({ isVizPickerOpen: true, scrollTop: 0 });
|
2018-12-12 09:51:17 +01:00
|
|
|
};
|
|
|
|
|
|
2018-12-12 13:59:19 +01:00
|
|
|
onCloseVizPicker = () => {
|
2019-02-05 17:15:21 +01:00
|
|
|
if (this.props.urlOpenVizPicker) {
|
|
|
|
|
this.props.updateLocation({ query: { openVizPicker: null }, partial: true });
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 13:07:08 +01:00
|
|
|
this.setState({ isVizPickerOpen: false, hasBeenFocused: false });
|
2018-12-12 13:59:19 +01:00
|
|
|
};
|
|
|
|
|
|
2019-02-12 08:03:43 +01:00
|
|
|
onSearchQueryChange = (value: string) => {
|
2018-12-12 11:24:18 +01:00
|
|
|
this.setState({
|
|
|
|
|
searchQuery: value,
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
renderToolbar = (): JSX.Element => {
|
2018-11-09 11:33:33 -08:00
|
|
|
const { plugin } = this.props;
|
2019-03-06 13:07:08 +01:00
|
|
|
const { isVizPickerOpen, searchQuery } = this.state;
|
2018-11-09 13:17:41 +01:00
|
|
|
|
2019-03-06 13:07:08 +01:00
|
|
|
if (isVizPickerOpen) {
|
2018-12-12 14:44:40 +01:00
|
|
|
return (
|
2019-03-06 13:07:08 +01:00
|
|
|
<VizPickerSearch
|
|
|
|
|
plugin={plugin}
|
|
|
|
|
searchQuery={searchQuery}
|
|
|
|
|
onChange={this.onSearchQueryChange}
|
|
|
|
|
onClose={this.onCloseVizPicker}
|
|
|
|
|
/>
|
2018-12-12 14:44:40 +01:00
|
|
|
);
|
|
|
|
|
} else {
|
2018-12-12 09:51:17 +01:00
|
|
|
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>
|
|
|
|
|
);
|
2018-12-12 14:44:40 +01:00
|
|
|
}
|
2018-12-12 09:51:17 +01:00
|
|
|
};
|
|
|
|
|
|
2018-12-12 11:24:18 +01:00
|
|
|
onTypeChanged = (plugin: PanelPlugin) => {
|
2018-12-13 07:44:58 +01:00
|
|
|
if (plugin.id === this.props.plugin.id) {
|
|
|
|
|
this.setState({ isVizPickerOpen: false });
|
|
|
|
|
} else {
|
|
|
|
|
this.props.onTypeChanged(plugin);
|
|
|
|
|
}
|
2018-12-12 11:24:18 +01:00
|
|
|
};
|
|
|
|
|
|
2018-12-19 10:53:14 +01:00
|
|
|
renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
|
2018-12-18 14:40:54 +01:00
|
|
|
|
2019-01-17 11:27:32 +01:00
|
|
|
setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
|
|
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
this.setState({ scrollTop: target.scrollTop });
|
|
|
|
|
};
|
|
|
|
|
|
2018-12-12 09:51:17 +01:00
|
|
|
render() {
|
2018-12-12 11:24:18 +01:00
|
|
|
const { plugin } = this.props;
|
2019-01-17 11:27:32 +01:00
|
|
|
const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
|
2018-11-22 11:41:33 +01:00
|
|
|
|
2018-12-21 11:57:21 +01:00
|
|
|
const pluginHelp: EditorToolbarView = {
|
2018-12-18 14:40:54 +01:00
|
|
|
heading: 'Help',
|
|
|
|
|
icon: 'fa fa-question',
|
|
|
|
|
render: this.renderHelp,
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-09 13:17:41 +01:00
|
|
|
return (
|
2019-02-08 21:53:51 +01:00
|
|
|
<EditorTabBody
|
|
|
|
|
heading="Visualization"
|
|
|
|
|
renderToolbar={this.renderToolbar}
|
|
|
|
|
toolbarItems={[pluginHelp]}
|
|
|
|
|
scrollTop={scrollTop}
|
|
|
|
|
setScrollTop={this.setScrollTop}
|
|
|
|
|
>
|
2018-12-12 12:51:39 +01:00
|
|
|
<>
|
2019-02-11 11:03:24 +01:00
|
|
|
<FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true} onExited={this.clearQuery}>
|
2018-12-12 14:44:40 +01:00
|
|
|
<VizTypePicker
|
|
|
|
|
current={plugin}
|
|
|
|
|
onTypeChanged={this.onTypeChanged}
|
|
|
|
|
searchQuery={searchQuery}
|
|
|
|
|
onClose={this.onCloseVizPicker}
|
|
|
|
|
/>
|
2018-12-12 13:59:19 +01:00
|
|
|
</FadeIn>
|
2018-12-12 12:51:39 +01:00
|
|
|
{this.renderPanelOptions()}
|
|
|
|
|
</>
|
2018-11-09 13:17:41 +01:00
|
|
|
</EditorTabBody>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-05 17:15:21 +01:00
|
|
|
|
|
|
|
|
const mapStateToProps = (state: StoreState) => ({
|
2019-02-08 21:53:51 +01:00
|
|
|
urlOpenVizPicker: !!state.location.query.openVizPicker,
|
2019-02-05 17:15:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mapDispatchToProps = {
|
2019-02-08 21:53:51 +01:00
|
|
|
updateLocation,
|
2019-02-05 17:15:21 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connectWithStore(VisualizationTab, mapStateToProps, mapDispatchToProps);
|