VisualizationTab.tsx 6.8 KB

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