VisualizationTab.tsx 6.7 KB

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