VisualizationTab.tsx 6.0 KB

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