VisualizationTab.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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
  100. ? `<div class="panel-options-group__header">
  101. <span class="panel-options-group__title">{{ctrl.editorTabs[${i}].title}}
  102. </span>
  103. </div>`
  104. : '') +
  105. `<div class="panel-options-group__body">
  106. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  107. </div>
  108. </div>
  109. `;
  110. }
  111. const loader = getAngularLoader();
  112. const scopeProps = { ctrl: panelCtrl };
  113. this.angularOptions = loader.load(this.element, scopeProps, template);
  114. }
  115. componentWillUnmount() {
  116. this.cleanUpAngularOptions();
  117. }
  118. cleanUpAngularOptions() {
  119. if (this.angularOptions) {
  120. this.angularOptions.destroy();
  121. this.angularOptions = null;
  122. }
  123. }
  124. clearQuery = () => {
  125. this.setState({ searchQuery: '' });
  126. };
  127. onPanelOptionsChanged = (options: any) => {
  128. this.props.panel.updateOptions(options);
  129. this.forceUpdate();
  130. };
  131. onOpenVizPicker = () => {
  132. this.setState({ isVizPickerOpen: true, scrollTop: 0 });
  133. };
  134. onCloseVizPicker = () => {
  135. if (this.props.urlOpenVizPicker) {
  136. this.props.updateLocation({ query: { openVizPicker: null }, partial: true });
  137. }
  138. this.setState({ isVizPickerOpen: false });
  139. };
  140. onSearchQueryChange = evt => {
  141. const value = evt.target.value;
  142. this.setState({
  143. searchQuery: value,
  144. });
  145. };
  146. renderToolbar = (): JSX.Element => {
  147. const { plugin } = this.props;
  148. const { searchQuery } = this.state;
  149. if (this.state.isVizPickerOpen) {
  150. return (
  151. <>
  152. <label className="gf-form--has-input-icon">
  153. <input
  154. type="text"
  155. className="gf-form-input width-13"
  156. placeholder=""
  157. onChange={this.onSearchQueryChange}
  158. value={searchQuery}
  159. ref={elem => elem && elem.focus()}
  160. />
  161. <i className="gf-form-input-icon fa fa-search" />
  162. </label>
  163. <button className="btn btn-link toolbar__close" onClick={this.onCloseVizPicker}>
  164. <i className="fa fa-chevron-up" />
  165. </button>
  166. </>
  167. );
  168. } else {
  169. return (
  170. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  171. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  172. <div className="toolbar__main-name">{plugin.name}</div>
  173. <i className="fa fa-caret-down" />
  174. </div>
  175. );
  176. }
  177. };
  178. onTypeChanged = (plugin: PanelPlugin) => {
  179. if (plugin.id === this.props.plugin.id) {
  180. this.setState({ isVizPickerOpen: false });
  181. } else {
  182. this.props.onTypeChanged(plugin);
  183. }
  184. };
  185. renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
  186. setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
  187. const target = event.target as HTMLElement;
  188. this.setState({ scrollTop: target.scrollTop });
  189. };
  190. render() {
  191. const { plugin } = this.props;
  192. const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
  193. const pluginHelp: EditorToolbarView = {
  194. heading: 'Help',
  195. icon: 'fa fa-question',
  196. render: this.renderHelp,
  197. };
  198. return (
  199. <EditorTabBody
  200. heading="Visualization"
  201. renderToolbar={this.renderToolbar}
  202. toolbarItems={[pluginHelp]}
  203. scrollTop={scrollTop}
  204. setScrollTop={this.setScrollTop}
  205. >
  206. <>
  207. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true} onExited={this.clearQuery}>
  208. <VizTypePicker
  209. current={plugin}
  210. onTypeChanged={this.onTypeChanged}
  211. searchQuery={searchQuery}
  212. onClose={this.onCloseVizPicker}
  213. />
  214. </FadeIn>
  215. {this.renderPanelOptions()}
  216. </>
  217. </EditorTabBody>
  218. );
  219. }
  220. }
  221. const mapStateToProps = (state: StoreState) => ({
  222. urlOpenVizPicker: !!state.location.query.openVizPicker,
  223. });
  224. const mapDispatchToProps = {
  225. updateLocation,
  226. };
  227. export default connectWithStore(VisualizationTab, mapStateToProps, mapDispatchToProps);