VisualizationTab.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. interface Props {
  18. panel: PanelModel;
  19. dashboard: DashboardModel;
  20. plugin: PanelPlugin;
  21. angularPanel?: AngularComponent;
  22. onTypeChanged: (newType: PanelPlugin) => void;
  23. updateLocation: typeof updateLocation;
  24. urlOpenVizPicker: boolean;
  25. }
  26. interface State {
  27. isVizPickerOpen: boolean;
  28. searchQuery: string;
  29. scrollTop: number;
  30. }
  31. export class VisualizationTab extends PureComponent<Props, State> {
  32. element: HTMLElement;
  33. angularOptions: AngularComponent;
  34. searchInput: HTMLElement;
  35. constructor(props) {
  36. super(props);
  37. this.state = {
  38. isVizPickerOpen: this.props.urlOpenVizPicker,
  39. searchQuery: '',
  40. scrollTop: 0,
  41. };
  42. }
  43. getPanelDefaultOptions = () => {
  44. const { panel, plugin } = this.props;
  45. if (plugin.exports.PanelDefaults) {
  46. return panel.getOptions(plugin.exports.PanelDefaults.options);
  47. }
  48. return panel.getOptions(plugin.exports.PanelDefaults);
  49. };
  50. renderPanelOptions() {
  51. const { plugin, angularPanel } = this.props;
  52. const { PanelOptions } = plugin.exports;
  53. if (angularPanel) {
  54. return <div ref={element => (this.element = element)} />;
  55. }
  56. return (
  57. <>
  58. {PanelOptions ? (
  59. <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />
  60. ) : (
  61. <p>Visualization has no options</p>
  62. )}
  63. </>
  64. );
  65. }
  66. componentDidMount() {
  67. if (this.shouldLoadAngularOptions()) {
  68. this.loadAngularOptions();
  69. }
  70. }
  71. componentDidUpdate(prevProps: Props) {
  72. if (this.props.plugin !== prevProps.plugin) {
  73. this.cleanUpAngularOptions();
  74. }
  75. if (this.shouldLoadAngularOptions()) {
  76. this.loadAngularOptions();
  77. }
  78. }
  79. shouldLoadAngularOptions() {
  80. return this.props.angularPanel && this.element && !this.angularOptions;
  81. }
  82. loadAngularOptions() {
  83. const { angularPanel } = this.props;
  84. const scope = angularPanel.getScope();
  85. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  86. if (!scope.$$childHead) {
  87. setTimeout(() => {
  88. this.forceUpdate();
  89. });
  90. return;
  91. }
  92. const panelCtrl = scope.$$childHead.ctrl;
  93. panelCtrl.initEditMode();
  94. let template = '';
  95. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  96. template +=
  97. `
  98. <div class="panel-options-group" ng-cloak>` +
  99. (i > 0 ? `<div class="panel-options-group__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
  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. onPanelOptionsChanged = (options: any) => {
  120. this.props.panel.updateOptions(options);
  121. this.forceUpdate();
  122. };
  123. onOpenVizPicker = () => {
  124. this.setState({ isVizPickerOpen: true, scrollTop: 0 });
  125. };
  126. onCloseVizPicker = () => {
  127. if (this.props.urlOpenVizPicker) {
  128. this.props.updateLocation({ query: { openVizPicker: null }, partial: true });
  129. }
  130. this.setState({ isVizPickerOpen: false });
  131. };
  132. onSearchQueryChange = evt => {
  133. const value = evt.target.value;
  134. this.setState({
  135. searchQuery: value,
  136. });
  137. };
  138. renderToolbar = (): JSX.Element => {
  139. const { plugin } = this.props;
  140. const { searchQuery } = this.state;
  141. if (this.state.isVizPickerOpen) {
  142. return (
  143. <>
  144. <label className="gf-form--has-input-icon">
  145. <input
  146. type="text"
  147. className="gf-form-input width-13"
  148. placeholder=""
  149. onChange={this.onSearchQueryChange}
  150. value={searchQuery}
  151. ref={elem => elem && elem.focus()}
  152. />
  153. <i className="gf-form-input-icon fa fa-search" />
  154. </label>
  155. <button className="btn btn-link toolbar__close" onClick={this.onCloseVizPicker}>
  156. <i className="fa fa-chevron-up" />
  157. </button>
  158. </>
  159. );
  160. } else {
  161. return (
  162. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  163. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  164. <div className="toolbar__main-name">{plugin.name}</div>
  165. <i className="fa fa-caret-down" />
  166. </div>
  167. );
  168. }
  169. };
  170. onTypeChanged = (plugin: PanelPlugin) => {
  171. if (plugin.id === this.props.plugin.id) {
  172. this.setState({ isVizPickerOpen: false });
  173. } else {
  174. this.props.onTypeChanged(plugin);
  175. }
  176. };
  177. renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
  178. setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
  179. const target = event.target as HTMLElement;
  180. this.setState({ scrollTop: target.scrollTop });
  181. };
  182. render() {
  183. const { plugin } = this.props;
  184. const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
  185. const pluginHelp: EditorToolbarView = {
  186. heading: 'Help',
  187. icon: 'fa fa-question',
  188. render: this.renderHelp,
  189. };
  190. return (
  191. <EditorTabBody heading="Visualization" renderToolbar={this.renderToolbar} toolbarItems={[pluginHelp]}
  192. scrollTop={scrollTop} setScrollTop={this.setScrollTop}>
  193. <>
  194. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true}>
  195. <VizTypePicker
  196. current={plugin}
  197. onTypeChanged={this.onTypeChanged}
  198. searchQuery={searchQuery}
  199. onClose={this.onCloseVizPicker}
  200. />
  201. </FadeIn>
  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);