VisualizationTab.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // 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. <>
  51. {PanelOptions ? (
  52. <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />
  53. ) : (
  54. <p>Visualization has no options</p>
  55. )}
  56. </>
  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. panelCtrl.initEditMode();
  87. let template = '';
  88. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  89. template +=
  90. `
  91. <div class="panel-options-group" ng-cloak>` +
  92. (i > 0 ? `<div class="panel-options-group__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
  93. `<div class="panel-options-group__body">
  94. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  95. </div>
  96. </div>
  97. `;
  98. }
  99. const loader = getAngularLoader();
  100. const scopeProps = { ctrl: panelCtrl };
  101. this.angularOptions = loader.load(this.element, scopeProps, template);
  102. }
  103. componentWillUnmount() {
  104. this.cleanUpAngularOptions();
  105. }
  106. cleanUpAngularOptions() {
  107. if (this.angularOptions) {
  108. this.angularOptions.destroy();
  109. this.angularOptions = null;
  110. }
  111. }
  112. onPanelOptionsChanged = (options: any) => {
  113. this.props.panel.updateOptions(options);
  114. this.forceUpdate();
  115. };
  116. onOpenVizPicker = () => {
  117. this.setState({ isVizPickerOpen: true });
  118. };
  119. onCloseVizPicker = () => {
  120. this.setState({ isVizPickerOpen: false });
  121. };
  122. onSearchQueryChange = evt => {
  123. const value = evt.target.value;
  124. this.setState({
  125. searchQuery: value,
  126. });
  127. };
  128. renderToolbar = (): JSX.Element => {
  129. const { plugin } = this.props;
  130. const { searchQuery } = this.state;
  131. if (this.state.isVizPickerOpen) {
  132. return (
  133. <>
  134. <label className="gf-form--has-input-icon">
  135. <input
  136. type="text"
  137. className="gf-form-input width-13"
  138. placeholder=""
  139. onChange={this.onSearchQueryChange}
  140. value={searchQuery}
  141. ref={elem => elem && elem.focus()}
  142. />
  143. <i className="gf-form-input-icon fa fa-search" />
  144. </label>
  145. <button className="btn btn-link toolbar__close" 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. renderHelp = () => <PluginHelp plugin={this.props.plugin} type="help" />;
  168. render() {
  169. const { plugin } = this.props;
  170. const { isVizPickerOpen, searchQuery } = this.state;
  171. const pluginHelp: EditorToolbarView = {
  172. heading: 'Help',
  173. icon: 'fa fa-question',
  174. render: this.renderHelp,
  175. };
  176. return (
  177. <EditorTabBody heading="Visualization" renderToolbar={this.renderToolbar} toolbarItems={[pluginHelp]}>
  178. <>
  179. <FadeIn in={isVizPickerOpen} duration={200} unmountOnExit={true}>
  180. <VizTypePicker
  181. current={plugin}
  182. onTypeChanged={this.onTypeChanged}
  183. searchQuery={searchQuery}
  184. onClose={this.onCloseVizPicker}
  185. />
  186. </FadeIn>
  187. {this.renderPanelOptions()}
  188. </>
  189. </EditorTabBody>
  190. );
  191. }
  192. }