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 { PanelPlugin } from 'app/types/plugins';
  17. import { VizPickerSearch } from './VizPickerSearch';
  18. interface Props {
  19. panel: PanelModel;
  20. dashboard: DashboardModel;
  21. plugin: PanelPlugin;
  22. angularPanel?: AngularComponent;
  23. onTypeChanged: (newType: PanelPlugin) => void;
  24. updateLocation: typeof updateLocation;
  25. urlOpenVizPicker: boolean;
  26. }
  27. interface State {
  28. isVizPickerOpen: boolean;
  29. searchQuery: string;
  30. scrollTop: number;
  31. hasBeenFocused: boolean;
  32. }
  33. export class VisualizationTab extends PureComponent<Props, State> {
  34. element: HTMLElement;
  35. angularOptions: AngularComponent;
  36. constructor(props) {
  37. super(props);
  38. this.state = {
  39. isVizPickerOpen: this.props.urlOpenVizPicker,
  40. hasBeenFocused: false,
  41. searchQuery: '',
  42. scrollTop: 0,
  43. };
  44. }
  45. getReactPanelOptions = () => {
  46. const { panel, plugin } = this.props;
  47. return panel.getOptions(plugin.exports.reactPanel.defaults);
  48. };
  49. renderPanelOptions() {
  50. const { plugin, angularPanel } = this.props;
  51. if (angularPanel) {
  52. return <div ref={element => (this.element = element)} />;
  53. }
  54. if (plugin.exports.reactPanel) {
  55. const PanelEditor = plugin.exports.reactPanel.editor;
  56. if (PanelEditor) {
  57. return <PanelEditor options={this.getReactPanelOptions()} onOptionsChange={this.onPanelOptionsChanged} />;
  58. }
  59. }
  60. return <p>Visualization has no options</p>;
  61. }
  62. componentDidMount() {
  63. if (this.shouldLoadAngularOptions()) {
  64. this.loadAngularOptions();
  65. }
  66. }
  67. componentDidUpdate(prevProps: Props) {
  68. if (this.props.plugin !== prevProps.plugin) {
  69. this.cleanUpAngularOptions();
  70. }
  71. if (this.shouldLoadAngularOptions()) {
  72. this.loadAngularOptions();
  73. }
  74. }
  75. shouldLoadAngularOptions() {
  76. return this.props.angularPanel && this.element && !this.angularOptions;
  77. }
  78. loadAngularOptions() {
  79. const { angularPanel } = this.props;
  80. const scope = angularPanel.getScope();
  81. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  82. if (!scope.$$childHead) {
  83. setTimeout(() => {
  84. this.forceUpdate();
  85. });
  86. return;
  87. }
  88. const panelCtrl = scope.$$childHead.ctrl;
  89. panelCtrl.initEditMode();
  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. if (isVizPickerOpen) {
  145. return (
  146. <VizPickerSearch
  147. plugin={plugin}
  148. searchQuery={searchQuery}
  149. onChange={this.onSearchQueryChange}
  150. onClose={this.onCloseVizPicker}
  151. />
  152. );
  153. } else {
  154. return (
  155. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  156. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  157. <div className="toolbar__main-name">{plugin.name}</div>
  158. <i className="fa fa-caret-down" />
  159. </div>
  160. );
  161. }
  162. };
  163. onTypeChanged = (plugin: PanelPlugin) => {
  164. if (plugin.id === this.props.plugin.id) {
  165. this.setState({ isVizPickerOpen: false });
  166. } else {
  167. this.props.onTypeChanged(plugin);
  168. }
  169. };
  170. renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
  171. setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
  172. const target = event.target as HTMLElement;
  173. this.setState({ scrollTop: target.scrollTop });
  174. };
  175. render() {
  176. const { plugin } = this.props;
  177. const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
  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={plugin}
  195. onTypeChanged={this.onTypeChanged}
  196. searchQuery={searchQuery}
  197. onClose={this.onCloseVizPicker}
  198. />
  199. </FadeIn>
  200. {this.renderPanelOptions()}
  201. </>
  202. </EditorTabBody>
  203. );
  204. }
  205. }
  206. const mapStateToProps = (state: StoreState) => ({
  207. urlOpenVizPicker: !!state.location.query.openVizPicker,
  208. });
  209. const mapDispatchToProps = {
  210. updateLocation,
  211. };
  212. export default connectWithStore(VisualizationTab, mapStateToProps, mapDispatchToProps);