VisualizationTab.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. // Utils & Services
  4. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  5. import { getDatasourceSrv } from '../../plugins/datasource_srv';
  6. // Components
  7. import { EditorTabBody } from './EditorTabBody';
  8. import { VizTypePicker } from './VizTypePicker';
  9. import PluginHelp from 'app/core/components/PanelHelp/PluginHelp';
  10. import { FadeIn } from 'app/core/components/Animations/FadeIn';
  11. import { PanelOptionSection } from './PanelOptionSection';
  12. // Types
  13. import { PanelModel } from '../panel_model';
  14. import { DashboardModel } from '../dashboard_model';
  15. import { PanelPlugin } from 'app/types/plugins';
  16. import { DataSourceSelectItem } from 'app/types';
  17. interface Props {
  18. panel: PanelModel;
  19. dashboard: DashboardModel;
  20. plugin: PanelPlugin;
  21. angularPanel?: AngularComponent;
  22. onTypeChanged: (newType: PanelPlugin) => void;
  23. }
  24. interface State {
  25. currentDataSource: DataSourceSelectItem;
  26. isVizPickerOpen: boolean;
  27. searchQuery: string;
  28. }
  29. export class VisualizationTab extends PureComponent<Props, State> {
  30. element: HTMLElement;
  31. angularOptions: AngularComponent;
  32. searchInput: HTMLElement;
  33. dataSources: DataSourceSelectItem[] = getDatasourceSrv().getMetricSources();
  34. constructor(props) {
  35. super(props);
  36. const { panel } = props;
  37. this.state = {
  38. isVizPickerOpen: false,
  39. searchQuery: '',
  40. currentDataSource: this.dataSources.find(datasource => datasource.value === panel.datasource),
  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. <PanelOptionSection>
  58. {PanelOptions ? (
  59. <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />
  60. ) : (
  61. <p>Visualization has no options</p>
  62. )}
  63. </PanelOptionSection>
  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. let template = '';
  94. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  95. template +=
  96. `
  97. <div class="panel-option-section" ng-cloak>` +
  98. (i > 0 ? `<div class="panel-option-section__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
  99. `<div class="panel-option-section__body">
  100. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  101. </div>
  102. </div>
  103. `;
  104. }
  105. const loader = getAngularLoader();
  106. const scopeProps = { ctrl: panelCtrl };
  107. this.angularOptions = loader.load(this.element, scopeProps, template);
  108. }
  109. componentWillUnmount() {
  110. this.cleanUpAngularOptions();
  111. }
  112. cleanUpAngularOptions() {
  113. if (this.angularOptions) {
  114. this.angularOptions.destroy();
  115. this.angularOptions = null;
  116. }
  117. }
  118. onPanelOptionsChanged = (options: any) => {
  119. this.props.panel.updateOptions(options);
  120. this.forceUpdate();
  121. };
  122. onOpenVizPicker = () => {
  123. this.setState({ isVizPickerOpen: true });
  124. };
  125. onCloseVizPicker = () => {
  126. this.setState({ isVizPickerOpen: false });
  127. };
  128. onSearchQueryChange = evt => {
  129. const value = evt.target.value;
  130. this.setState({
  131. searchQuery: value,
  132. });
  133. };
  134. renderToolbar = (): JSX.Element => {
  135. const { plugin } = this.props;
  136. const { searchQuery } = this.state;
  137. if (this.state.isVizPickerOpen) {
  138. return (
  139. <>
  140. <label className="gf-form--has-input-icon">
  141. <input
  142. type="text"
  143. className="gf-form-input width-13"
  144. placeholder=""
  145. onChange={this.onSearchQueryChange}
  146. value={searchQuery}
  147. ref={elem => elem && elem.focus()}
  148. />
  149. <i className="gf-form-input-icon fa fa-search" />
  150. </label>
  151. <button className="btn btn-link toolbar__close" onClick={this.onCloseVizPicker}>
  152. <i className="fa fa-chevron-up" />
  153. </button>
  154. </>
  155. );
  156. } else {
  157. return (
  158. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  159. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  160. <div className="toolbar__main-name">{plugin.name}</div>
  161. <i className="fa fa-caret-down" />
  162. </div>
  163. );
  164. }
  165. };
  166. onTypeChanged = (plugin: PanelPlugin) => {
  167. if (plugin.id === this.props.plugin.id) {
  168. this.setState({ isVizPickerOpen: false });
  169. } else {
  170. this.props.onTypeChanged(plugin);
  171. }
  172. };
  173. renderHelp = () => <PluginHelp plugin={this.state.currentDataSource.meta} type="panel_help" />;
  174. render() {
  175. const { plugin } = this.props;
  176. const { isVizPickerOpen, searchQuery } = this.state;
  177. const pluginHelp = {
  178. heading: 'Help',
  179. icon: 'fa fa-question',
  180. render: this.renderHelp,
  181. };
  182. return (
  183. <EditorTabBody heading="Visualization" renderToolbar={this.renderToolbar} toolbarItems={[pluginHelp]}>
  184. <>
  185. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true}>
  186. <VizTypePicker
  187. current={plugin}
  188. onTypeChanged={this.onTypeChanged}
  189. searchQuery={searchQuery}
  190. onClose={this.onCloseVizPicker}
  191. />
  192. </FadeIn>
  193. {this.renderPanelOptions()}
  194. </>
  195. </EditorTabBody>
  196. );
  197. }
  198. }