VisualizationTab.tsx 6.9 KB

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