VisualizationTab.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // Types
  9. import { PanelModel } from '../panel_model';
  10. import { DashboardModel } from '../dashboard_model';
  11. import { PanelPlugin } from 'app/types/plugins';
  12. interface Props {
  13. panel: PanelModel;
  14. dashboard: DashboardModel;
  15. plugin: PanelPlugin;
  16. angularPanel?: AngularComponent;
  17. onTypeChanged: (newType: PanelPlugin) => void;
  18. }
  19. interface State {
  20. isVizPickerOpen: boolean;
  21. searchQuery: string;
  22. }
  23. export class VisualizationTab extends PureComponent<Props, State> {
  24. element: HTMLElement;
  25. angularOptions: AngularComponent;
  26. searchInput: HTMLElement;
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. isVizPickerOpen: false,
  31. };
  32. }
  33. getPanelDefaultOptions = () => {
  34. const { panel, plugin } = this.props;
  35. if (plugin.exports.PanelDefaults) {
  36. return panel.getOptions(plugin.exports.PanelDefaults.options);
  37. }
  38. return panel.getOptions(plugin.exports.PanelDefaults);
  39. };
  40. renderPanelOptions() {
  41. const { plugin, angularPanel } = this.props;
  42. const { PanelOptions } = plugin.exports;
  43. if (angularPanel) {
  44. return <div ref={element => (this.element = element)} />;
  45. }
  46. if (PanelOptions) {
  47. return <PanelOptions options={this.getPanelDefaultOptions()} onChange={this.onPanelOptionsChanged} />;
  48. } else {
  49. return <p>Visualization has no options</p>;
  50. }
  51. }
  52. componentDidMount() {
  53. if (this.shouldLoadAngularOptions()) {
  54. this.loadAngularOptions();
  55. }
  56. }
  57. componentDidUpdate(prevProps: Props) {
  58. if (this.props.plugin !== prevProps.plugin) {
  59. this.cleanUpAngularOptions();
  60. }
  61. if (this.shouldLoadAngularOptions()) {
  62. this.loadAngularOptions();
  63. }
  64. }
  65. shouldLoadAngularOptions() {
  66. return this.props.angularPanel && this.element && !this.angularOptions;
  67. }
  68. loadAngularOptions() {
  69. const { angularPanel } = this.props;
  70. const scope = angularPanel.getScope();
  71. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  72. if (!scope.$$childHead) {
  73. setTimeout(() => {
  74. this.forceUpdate();
  75. });
  76. return;
  77. }
  78. const panelCtrl = scope.$$childHead.ctrl;
  79. let template = '';
  80. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  81. template +=
  82. `
  83. <div class="form-section" ng-cloak>` +
  84. (i > 0 ? `<div class="form-section__header">{{ctrl.editorTabs[${i}].title}}</div>` : '') +
  85. `<div class="form-section__body">
  86. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  87. </div>
  88. </div>
  89. `;
  90. }
  91. const loader = getAngularLoader();
  92. const scopeProps = { ctrl: panelCtrl };
  93. this.angularOptions = loader.load(this.element, scopeProps, template);
  94. }
  95. componentWillUnmount() {
  96. this.cleanUpAngularOptions();
  97. }
  98. cleanUpAngularOptions() {
  99. if (this.angularOptions) {
  100. this.angularOptions.destroy();
  101. this.angularOptions = null;
  102. }
  103. }
  104. onPanelOptionsChanged = (options: any) => {
  105. this.props.panel.updateOptions(options);
  106. this.forceUpdate();
  107. };
  108. onOpenVizPicker = () => {
  109. this.setState({ isVizPickerOpen: true });
  110. };
  111. renderToolbar = () => {
  112. const { plugin } = this.props;
  113. if (this.state.isVizPickerOpen) {
  114. return (
  115. <label className="gf-form--has-input-icon">
  116. <input
  117. type="text"
  118. className="gf-form-input width-13"
  119. placeholder=""
  120. ref={elem => (this.searchInput = elem)}
  121. />
  122. <i className="gf-form-input-icon fa fa-search" />
  123. </label>
  124. );
  125. } else {
  126. return (
  127. <div className="toolbar__main" onClick={this.onOpenVizPicker}>
  128. <img className="toolbar__main-image" src={plugin.info.logos.small} />
  129. <div className="toolbar__main-name">{plugin.name}</div>
  130. <i className="fa fa-caret-down" />
  131. </div>
  132. );
  133. }
  134. };
  135. render() {
  136. const { plugin, onTypeChanged } = this.props;
  137. const { isVizPickerOpen } = this.state;
  138. const panelHelp = {
  139. title: '',
  140. icon: 'fa fa-question',
  141. render: () => <h2>Help</h2>,
  142. };
  143. return (
  144. <EditorTabBody heading="Visualization" renderToolbar={this.renderToolbar} toolbarItems={[panelHelp]}>
  145. {isVizPickerOpen && <VizTypePicker current={plugin} onTypeChanged={onTypeChanged} />}
  146. {this.renderPanelOptions()}
  147. </EditorTabBody>
  148. );
  149. }
  150. }