VisualizationTab.tsx 6.0 KB

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