VisualizationTab.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Utils & Services
  4. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  5. // Components
  6. import { EditorTabBody } from './EditorTabBody';
  7. import { VizTypePicker } from './VizTypePicker';
  8. import { FadeIn } from 'app/core/components/Animations/FadeIn';
  9. import { PanelOptionSection } from './PanelOptionSection';
  10. // Types
  11. import { PanelModel } from '../panel_model';
  12. import { DashboardModel } from '../dashboard_model';
  13. import { PanelPlugin } from 'app/types/plugins';
  14. interface Props {
  15. panel: PanelModel;
  16. dashboard: DashboardModel;
  17. plugin: PanelPlugin;
  18. angularPanel?: AngularComponent;
  19. onTypeChanged: (newType: PanelPlugin) => void;
  20. }
  21. interface State {
  22. isVizPickerOpen: boolean;
  23. searchQuery: string;
  24. }
  25. export class VisualizationTab extends PureComponent<Props, State> {
  26. element: HTMLElement;
  27. angularOptions: AngularComponent;
  28. searchInput: HTMLElement;
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. isVizPickerOpen: false,
  33. searchQuery: '',
  34. };
  35. }
  36. getPanelDefaultOptions = () => {
  37. const { panel, plugin } = this.props;
  38. if (plugin.exports.PanelDefaults) {
  39. return panel.getOptions(plugin.exports.PanelDefaults.options);
  40. }
  41. return panel.getOptions(plugin.exports.PanelDefaults);
  42. };
  43. renderPanelOptions() {
  44. const { plugin, angularPanel } = this.props;
  45. const { PanelOptions } = plugin.exports;
  46. if (angularPanel) {
  47. return <div ref={element => (this.element = element)} />;
  48. }
  49. return (
  50. <PanelOptionSection>
  51. {PanelOptions ? (
  52. <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />
  53. ) : (
  54. <p>Visualization has no options</p>
  55. )}
  56. </PanelOptionSection>
  57. );
  58. }
  59. componentDidMount() {
  60. if (this.shouldLoadAngularOptions()) {
  61. this.loadAngularOptions();
  62. }
  63. }
  64. componentDidUpdate(prevProps: Props) {
  65. if (this.props.plugin !== prevProps.plugin) {
  66. this.cleanUpAngularOptions();
  67. }
  68. if (this.shouldLoadAngularOptions()) {
  69. this.loadAngularOptions();
  70. }
  71. }
  72. shouldLoadAngularOptions() {
  73. return this.props.angularPanel && this.element && !this.angularOptions;
  74. }
  75. loadAngularOptions() {
  76. const { angularPanel } = this.props;
  77. const scope = angularPanel.getScope();
  78. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  79. if (!scope.$$childHead) {
  80. setTimeout(() => {
  81. this.forceUpdate();
  82. });
  83. return;
  84. }
  85. const panelCtrl = scope.$$childHead.ctrl;
  86. let template = '';
  87. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  88. template +=
  89. `
  90. <div class="panel-option-section" ng-cloak>` +
  91. (i > 0 ? `<div class="panel-option-section__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
  92. `<div class="panel-option-section__body">
  93. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  94. </div>
  95. </div>
  96. `;
  97. }
  98. const loader = getAngularLoader();
  99. const scopeProps = { ctrl: panelCtrl };
  100. this.angularOptions = loader.load(this.element, scopeProps, template);
  101. }
  102. componentWillUnmount() {
  103. this.cleanUpAngularOptions();
  104. }
  105. cleanUpAngularOptions() {
  106. if (this.angularOptions) {
  107. this.angularOptions.destroy();
  108. this.angularOptions = null;
  109. }
  110. }
  111. onPanelOptionsChanged = (options: any) => {
  112. this.props.panel.updateOptions(options);
  113. this.forceUpdate();
  114. };
  115. onOpenVizPicker = () => {
  116. this.setState({ isVizPickerOpen: true });
  117. };
  118. onCloseVizPicker = () => {
  119. this.setState({ isVizPickerOpen: false });
  120. };
  121. onSearchQueryChange = evt => {
  122. const value = evt.target.value;
  123. this.setState({
  124. searchQuery: value,
  125. });
  126. };
  127. renderToolbar = (): JSX.Element => {
  128. const { plugin } = this.props;
  129. const { searchQuery } = this.state;
  130. if (this.state.isVizPickerOpen) {
  131. return (
  132. <>
  133. <label className="gf-form--has-input-icon">
  134. <input
  135. type="text"
  136. className="gf-form-input width-13"
  137. placeholder=""
  138. onChange={this.onSearchQueryChange}
  139. value={searchQuery}
  140. ref={elem => elem && elem.focus()}
  141. />
  142. <i className="gf-form-input-icon fa fa-search" />
  143. </label>
  144. <div className="flex-grow" />
  145. <button className="btn btn-link" onClick={this.onCloseVizPicker}>
  146. <i className="fa fa-chevron-up" />
  147. </button>
  148. </>
  149. );
  150. } else {
  151. return (
  152. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  153. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  154. <div className="toolbar__main-name">{plugin.name}</div>
  155. <i className="fa fa-caret-down" />
  156. </div>
  157. );
  158. }
  159. };
  160. onTypeChanged = (plugin: PanelPlugin) => {
  161. if (plugin.id === this.props.plugin.id) {
  162. this.setState({ isVizPickerOpen: false });
  163. } else {
  164. this.props.onTypeChanged(plugin);
  165. }
  166. };
  167. render() {
  168. const { plugin } = this.props;
  169. const { isVizPickerOpen, searchQuery } = this.state;
  170. return (
  171. <EditorTabBody heading="Visualization" renderToolbar={this.renderToolbar}>
  172. <>
  173. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true}>
  174. <VizTypePicker
  175. current={plugin}
  176. onTypeChanged={this.onTypeChanged}
  177. searchQuery={searchQuery}
  178. onClose={this.onCloseVizPicker}
  179. />
  180. </FadeIn>
  181. {this.renderPanelOptions()}
  182. </>
  183. </EditorTabBody>
  184. );
  185. }
  186. }