VisualizationTab.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. getPanelDefaultOptions = () => {
  45. const { panel, plugin } = this.props;
  46. if (plugin.exports.PanelDefaults) {
  47. return panel.getOptions(plugin.exports.PanelDefaults.options);
  48. }
  49. return panel.getOptions(plugin.exports.PanelDefaults);
  50. };
  51. renderPanelOptions() {
  52. const { plugin, angularPanel } = this.props;
  53. const { PanelOptions } = plugin.exports;
  54. if (angularPanel) {
  55. return <div ref={element => (this.element = element)} />;
  56. }
  57. return (
  58. <>
  59. {PanelOptions ? (
  60. <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />
  61. ) : (
  62. <p>Visualization has no options</p>
  63. )}
  64. </>
  65. );
  66. }
  67. componentDidMount() {
  68. if (this.shouldLoadAngularOptions()) {
  69. this.loadAngularOptions();
  70. }
  71. }
  72. componentDidUpdate(prevProps: Props) {
  73. if (this.props.plugin !== prevProps.plugin) {
  74. this.cleanUpAngularOptions();
  75. }
  76. if (this.shouldLoadAngularOptions()) {
  77. this.loadAngularOptions();
  78. }
  79. }
  80. shouldLoadAngularOptions() {
  81. return this.props.angularPanel && this.element && !this.angularOptions;
  82. }
  83. loadAngularOptions() {
  84. const { angularPanel } = this.props;
  85. const scope = angularPanel.getScope();
  86. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  87. if (!scope.$$childHead) {
  88. setTimeout(() => {
  89. this.forceUpdate();
  90. });
  91. return;
  92. }
  93. const panelCtrl = scope.$$childHead.ctrl;
  94. panelCtrl.initEditMode();
  95. let template = '';
  96. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  97. template +=
  98. `
  99. <div class="panel-options-group" ng-cloak>` +
  100. (i > 0
  101. ? `<div class="panel-options-group__header">
  102. <span class="panel-options-group__title">{{ctrl.editorTabs[${i}].title}}
  103. </span>
  104. </div>`
  105. : '') +
  106. `<div class="panel-options-group__body">
  107. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  108. </div>
  109. </div>
  110. `;
  111. }
  112. const loader = getAngularLoader();
  113. const scopeProps = { ctrl: panelCtrl };
  114. this.angularOptions = loader.load(this.element, scopeProps, template);
  115. }
  116. componentWillUnmount() {
  117. this.cleanUpAngularOptions();
  118. }
  119. cleanUpAngularOptions() {
  120. if (this.angularOptions) {
  121. this.angularOptions.destroy();
  122. this.angularOptions = null;
  123. }
  124. }
  125. clearQuery = () => {
  126. this.setState({ searchQuery: '' });
  127. };
  128. onPanelOptionsChanged = (options: any) => {
  129. this.props.panel.updateOptions(options);
  130. this.forceUpdate();
  131. };
  132. onOpenVizPicker = () => {
  133. this.setState({ isVizPickerOpen: true, scrollTop: 0 });
  134. };
  135. onCloseVizPicker = () => {
  136. if (this.props.urlOpenVizPicker) {
  137. this.props.updateLocation({ query: { openVizPicker: null }, partial: true });
  138. }
  139. this.setState({ isVizPickerOpen: false });
  140. };
  141. onSearchQueryChange = (value: string) => {
  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. <FilterInput
  153. labelClassName="gf-form--has-input-icon"
  154. inputClassName="gf-form-input width-13"
  155. placeholder=""
  156. onChange={this.onSearchQueryChange}
  157. value={searchQuery}
  158. ref={elem => elem && elem.focus()}
  159. />
  160. <button className="btn btn-link toolbar__close" onClick={this.onCloseVizPicker}>
  161. <i className="fa fa-chevron-up" />
  162. </button>
  163. </>
  164. );
  165. } else {
  166. return (
  167. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  168. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  169. <div className="toolbar__main-name">{plugin.name}</div>
  170. <i className="fa fa-caret-down" />
  171. </div>
  172. );
  173. }
  174. };
  175. onTypeChanged = (plugin: PanelPlugin) => {
  176. if (plugin.id === this.props.plugin.id) {
  177. this.setState({ isVizPickerOpen: false });
  178. } else {
  179. this.props.onTypeChanged(plugin);
  180. }
  181. };
  182. renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
  183. setScrollTop = (event: React.MouseEvent<HTMLElement>) => {
  184. const target = event.target as HTMLElement;
  185. this.setState({ scrollTop: target.scrollTop });
  186. };
  187. render() {
  188. const { plugin } = this.props;
  189. const { isVizPickerOpen, searchQuery, scrollTop } = this.state;
  190. const pluginHelp: EditorToolbarView = {
  191. heading: 'Help',
  192. icon: 'fa fa-question',
  193. render: this.renderHelp,
  194. };
  195. return (
  196. <EditorTabBody
  197. heading="Visualization"
  198. renderToolbar={this.renderToolbar}
  199. toolbarItems={[pluginHelp]}
  200. scrollTop={scrollTop}
  201. setScrollTop={this.setScrollTop}
  202. >
  203. <>
  204. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true} onExited={this.clearQuery}>
  205. <VizTypePicker
  206. current={plugin}
  207. onTypeChanged={this.onTypeChanged}
  208. searchQuery={searchQuery}
  209. onClose={this.onCloseVizPicker}
  210. />
  211. </FadeIn>
  212. {this.renderPanelOptions()}
  213. </>
  214. </EditorTabBody>
  215. );
  216. }
  217. }
  218. const mapStateToProps = (state: StoreState) => ({
  219. urlOpenVizPicker: !!state.location.query.openVizPicker,
  220. });
  221. const mapDispatchToProps = {
  222. updateLocation,
  223. };
  224. export default connectWithStore(VisualizationTab, mapStateToProps, mapDispatchToProps);