VisualizationTab.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Utils & Services
  4. import { AngularComponent, getAngularLoader } from '@grafana/runtime';
  5. import { connectWithStore } from 'app/core/utils/connectWithReduxStore';
  6. import { StoreState } from 'app/types';
  7. import { updateLocation } from 'app/core/actions';
  8. // Components
  9. import { EditorTabBody, EditorToolbarView } from './EditorTabBody';
  10. import { VizTypePicker } from './VizTypePicker';
  11. import { PluginHelp } from 'app/core/components/PluginHelp/PluginHelp';
  12. import { FadeIn } from 'app/core/components/Animations/FadeIn';
  13. // Types
  14. import { PanelModel } from '../state';
  15. import { DashboardModel } from '../state';
  16. import { VizPickerSearch } from './VizPickerSearch';
  17. import PluginStateinfo from 'app/features/plugins/PluginStateInfo';
  18. import { PanelPlugin, PanelPluginMeta, PanelData } from '@grafana/ui';
  19. import { PanelCtrl } from 'app/plugins/sdk';
  20. import { Unsubscribable } from 'rxjs';
  21. import { LoadingState } from '@grafana/data';
  22. interface Props {
  23. panel: PanelModel;
  24. dashboard: DashboardModel;
  25. plugin: PanelPlugin;
  26. angularPanel?: AngularComponent;
  27. onPluginTypeChange: (newType: PanelPluginMeta) => void;
  28. updateLocation: typeof updateLocation;
  29. urlOpenVizPicker: boolean;
  30. }
  31. interface State {
  32. isVizPickerOpen: boolean;
  33. searchQuery: string;
  34. scrollTop: number;
  35. hasBeenFocused: boolean;
  36. data: PanelData;
  37. }
  38. export class VisualizationTab extends PureComponent<Props, State> {
  39. element: HTMLElement;
  40. angularOptions: AngularComponent;
  41. querySubscription: Unsubscribable;
  42. constructor(props: Props) {
  43. super(props);
  44. this.state = {
  45. isVizPickerOpen: this.props.urlOpenVizPicker,
  46. hasBeenFocused: false,
  47. searchQuery: '',
  48. scrollTop: 0,
  49. data: {
  50. state: LoadingState.NotStarted,
  51. series: [],
  52. },
  53. };
  54. }
  55. getReactPanelOptions = () => {
  56. const { panel } = this.props;
  57. return panel.getOptions();
  58. };
  59. renderPanelOptions() {
  60. const { plugin, angularPanel } = this.props;
  61. if (angularPanel) {
  62. return <div ref={element => (this.element = element)} />;
  63. }
  64. if (plugin.editor) {
  65. return (
  66. <plugin.editor
  67. data={this.state.data}
  68. options={this.getReactPanelOptions()}
  69. onOptionsChange={this.onPanelOptionsChanged}
  70. />
  71. );
  72. }
  73. return <p>Visualization has no options</p>;
  74. }
  75. componentDidMount() {
  76. const { panel } = this.props;
  77. const queryRunner = panel.getQueryRunner();
  78. if (this.shouldLoadAngularOptions()) {
  79. this.loadAngularOptions();
  80. }
  81. this.querySubscription = queryRunner.getData().subscribe({
  82. next: (data: PanelData) => this.setState({ data }),
  83. });
  84. }
  85. componentDidUpdate(prevProps: Props) {
  86. if (this.props.plugin !== prevProps.plugin) {
  87. this.cleanUpAngularOptions();
  88. }
  89. if (this.shouldLoadAngularOptions()) {
  90. this.loadAngularOptions();
  91. }
  92. }
  93. shouldLoadAngularOptions() {
  94. return this.props.angularPanel && this.element && !this.angularOptions;
  95. }
  96. loadAngularOptions() {
  97. const { angularPanel } = this.props;
  98. const scope = angularPanel.getScope();
  99. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  100. if (!scope.$$childHead) {
  101. setTimeout(() => {
  102. this.forceUpdate();
  103. });
  104. return;
  105. }
  106. const panelCtrl: PanelCtrl = scope.$$childHead.ctrl;
  107. panelCtrl.initEditMode();
  108. panelCtrl.onPluginTypeChange = this.onPluginTypeChange;
  109. let template = '';
  110. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  111. template +=
  112. `
  113. <div class="panel-options-group" ng-cloak>` +
  114. (i > 0
  115. ? `<div class="panel-options-group__header">
  116. <span class="panel-options-group__title">{{ctrl.editorTabs[${i}].title}}
  117. </span>
  118. </div>`
  119. : '') +
  120. `<div class="panel-options-group__body">
  121. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  122. </div>
  123. </div>
  124. `;
  125. }
  126. const loader = getAngularLoader();
  127. const scopeProps = { ctrl: panelCtrl };
  128. this.angularOptions = loader.load(this.element, scopeProps, template);
  129. }
  130. componentWillUnmount() {
  131. this.cleanUpAngularOptions();
  132. }
  133. cleanUpAngularOptions() {
  134. if (this.angularOptions) {
  135. this.angularOptions.destroy();
  136. this.angularOptions = null;
  137. }
  138. }
  139. clearQuery = () => {
  140. this.setState({ searchQuery: '' });
  141. };
  142. onPanelOptionsChanged = (options: any) => {
  143. this.props.panel.updateOptions(options);
  144. this.forceUpdate();
  145. };
  146. onOpenVizPicker = () => {
  147. this.setState({ isVizPickerOpen: true, scrollTop: 0 });
  148. };
  149. onCloseVizPicker = () => {
  150. if (this.props.urlOpenVizPicker) {
  151. this.props.updateLocation({ query: { openVizPicker: null }, partial: true });
  152. }
  153. this.setState({ isVizPickerOpen: false, hasBeenFocused: false });
  154. };
  155. onSearchQueryChange = (value: string) => {
  156. this.setState({
  157. searchQuery: value,
  158. });
  159. };
  160. renderToolbar = (): JSX.Element => {
  161. const { plugin } = this.props;
  162. const { isVizPickerOpen, searchQuery } = this.state;
  163. const { meta } = plugin;
  164. if (isVizPickerOpen) {
  165. return (
  166. <VizPickerSearch
  167. plugin={meta}
  168. searchQuery={searchQuery}
  169. onChange={this.onSearchQueryChange}
  170. onClose={this.onCloseVizPicker}
  171. />
  172. );
  173. } else {
  174. return (
  175. <>
  176. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  177. <img className="toolbar__main-image" src={meta.info.logos.small} />
  178. <div className="toolbar__main-name">{meta.name}</div>
  179. <i className="fa fa-caret-down" />
  180. </div>
  181. <PluginStateinfo state={meta.state} />
  182. </>
  183. );
  184. }
  185. };
  186. onPluginTypeChange = (plugin: PanelPluginMeta) => {
  187. if (plugin.id === this.props.plugin.meta.id) {
  188. this.setState({ isVizPickerOpen: false });
  189. } else {
  190. this.props.onPluginTypeChange(plugin);
  191. }
  192. };
  193. renderHelp = () => <PluginHelp plugin={this.props.plugin.meta} type="help" />;
  194. setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
  195. const target = event.target as HTMLElement;
  196. this.setState({ scrollTop: target.scrollTop });
  197. };
  198. render() {
  199. const { plugin } = this.props;
  200. const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
  201. const { meta } = plugin;
  202. const pluginHelp: EditorToolbarView = {
  203. heading: 'Help',
  204. icon: 'fa fa-question',
  205. render: this.renderHelp,
  206. };
  207. return (
  208. <EditorTabBody
  209. heading="Visualization"
  210. renderToolbar={this.renderToolbar}
  211. toolbarItems={[pluginHelp]}
  212. scrollTop={scrollTop}
  213. setScrollTop={this.setScrollTop}
  214. >
  215. <>
  216. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true} onExited={this.clearQuery}>
  217. <VizTypePicker
  218. current={meta}
  219. onTypeChange={this.onPluginTypeChange}
  220. searchQuery={searchQuery}
  221. onClose={this.onCloseVizPicker}
  222. />
  223. </FadeIn>
  224. {this.renderPanelOptions()}
  225. </>
  226. </EditorTabBody>
  227. );
  228. }
  229. }
  230. const mapStateToProps = (state: StoreState) => ({
  231. urlOpenVizPicker: !!state.location.query.openVizPicker,
  232. });
  233. const mapDispatchToProps = {
  234. updateLocation,
  235. };
  236. export default connectWithStore(VisualizationTab, mapStateToProps, mapDispatchToProps);