VisualizationTab.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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/PanelModel';
  15. import { DashboardModel } from '../state/DashboardModel';
  16. import { PanelPlugin } from 'app/types/plugins';
  17. import { FilterInput } from 'app/core/components/FilterInput/FilterInput';
  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. }
  32. export class VisualizationTab extends PureComponent<Props, State> {
  33. element: HTMLElement;
  34. angularOptions: AngularComponent;
  35. searchInput: HTMLElement;
  36. constructor(props) {
  37. super(props);
  38. this.state = {
  39. isVizPickerOpen: this.props.urlOpenVizPicker,
  40. searchQuery: '',
  41. scrollTop: 0,
  42. };
  43. }
  44. getReactPanelOptions = () => {
  45. const { panel, plugin } = this.props;
  46. return panel.getOptions(plugin.exports.reactPanel.defaults);
  47. };
  48. renderPanelOptions() {
  49. const { plugin, angularPanel } = this.props;
  50. if (angularPanel) {
  51. return <div ref={element => (this.element = element)} />;
  52. }
  53. if (plugin.exports.reactPanel) {
  54. const PanelEditor = plugin.exports.reactPanel.editor;
  55. if (PanelEditor) {
  56. return <PanelEditor options={this.getReactPanelOptions()} onChange={this.onPanelOptionsChanged} />;
  57. }
  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 = scope.$$childHead.ctrl;
  88. panelCtrl.initEditMode();
  89. let template = '';
  90. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  91. template +=
  92. `
  93. <div class="panel-options-group" ng-cloak>` +
  94. (i > 0
  95. ? `<div class="panel-options-group__header">
  96. <span class="panel-options-group__title">{{ctrl.editorTabs[${i}].title}}
  97. </span>
  98. </div>`
  99. : '') +
  100. `<div class="panel-options-group__body">
  101. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  102. </div>
  103. </div>
  104. `;
  105. }
  106. const loader = getAngularLoader();
  107. const scopeProps = { ctrl: panelCtrl };
  108. this.angularOptions = loader.load(this.element, scopeProps, template);
  109. }
  110. componentWillUnmount() {
  111. this.cleanUpAngularOptions();
  112. }
  113. cleanUpAngularOptions() {
  114. if (this.angularOptions) {
  115. this.angularOptions.destroy();
  116. this.angularOptions = null;
  117. }
  118. }
  119. clearQuery = () => {
  120. this.setState({ searchQuery: '' });
  121. };
  122. onPanelOptionsChanged = (options: any) => {
  123. this.props.panel.updateOptions(options);
  124. this.forceUpdate();
  125. };
  126. onOpenVizPicker = () => {
  127. this.setState({ isVizPickerOpen: true, scrollTop: 0 });
  128. };
  129. onCloseVizPicker = () => {
  130. if (this.props.urlOpenVizPicker) {
  131. this.props.updateLocation({ query: { openVizPicker: null }, partial: true });
  132. }
  133. this.setState({ isVizPickerOpen: false });
  134. };
  135. onSearchQueryChange = (value: string) => {
  136. this.setState({
  137. searchQuery: value,
  138. });
  139. };
  140. renderToolbar = (): JSX.Element => {
  141. const { plugin } = this.props;
  142. const { searchQuery } = this.state;
  143. if (this.state.isVizPickerOpen) {
  144. return (
  145. <>
  146. <FilterInput
  147. labelClassName="gf-form--has-input-icon"
  148. inputClassName="gf-form-input width-13"
  149. placeholder=""
  150. onChange={this.onSearchQueryChange}
  151. value={searchQuery}
  152. ref={elem => elem && elem.focus()}
  153. />
  154. <button className="btn btn-link toolbar__close" onClick={this.onCloseVizPicker}>
  155. <i className="fa fa-chevron-up" />
  156. </button>
  157. </>
  158. );
  159. } else {
  160. return (
  161. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  162. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  163. <div className="toolbar__main-name">{plugin.name}</div>
  164. <i className="fa fa-caret-down" />
  165. </div>
  166. );
  167. }
  168. };
  169. onTypeChanged = (plugin: PanelPlugin) => {
  170. if (plugin.id === this.props.plugin.id) {
  171. this.setState({ isVizPickerOpen: false });
  172. } else {
  173. this.props.onTypeChanged(plugin);
  174. }
  175. };
  176. renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
  177. setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
  178. const target = event.target as HTMLElement;
  179. this.setState({ scrollTop: target.scrollTop });
  180. };
  181. render() {
  182. const { plugin } = this.props;
  183. const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
  184. const pluginHelp: EditorToolbarView = {
  185. heading: 'Help',
  186. icon: 'fa fa-question',
  187. render: this.renderHelp,
  188. };
  189. return (
  190. <EditorTabBody
  191. heading="Visualization"
  192. renderToolbar={this.renderToolbar}
  193. toolbarItems={[pluginHelp]}
  194. scrollTop={scrollTop}
  195. setScrollTop={this.setScrollTop}
  196. >
  197. <>
  198. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true} onExited={this.clearQuery}>
  199. <VizTypePicker
  200. current={plugin}
  201. onTypeChanged={this.onTypeChanged}
  202. searchQuery={searchQuery}
  203. onClose={this.onCloseVizPicker}
  204. />
  205. </FadeIn>
  206. {this.renderPanelOptions()}
  207. </>
  208. </EditorTabBody>
  209. );
  210. }
  211. }
  212. const mapStateToProps = (state: StoreState) => ({
  213. urlOpenVizPicker: !!state.location.query.openVizPicker,
  214. });
  215. const mapDispatchToProps = {
  216. updateLocation,
  217. };
  218. export default connectWithStore(VisualizationTab, mapStateToProps, mapDispatchToProps);