VisualizationTab.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. export class VisualizationTab extends PureComponent<Props> {
  20. element: HTMLElement;
  21. angularOptions: AngularComponent;
  22. constructor(props) {
  23. super(props);
  24. }
  25. renderPanelOptions() {
  26. const { plugin, panel, angularPanel } = this.props;
  27. const { PanelOptions } = plugin.exports;
  28. if (angularPanel) {
  29. return <div ref={element => (this.element = element)} />;
  30. }
  31. if (PanelOptions) {
  32. return <PanelOptions options={panel.getOptions()} onChange={this.onPanelOptionsChanged} />;
  33. } else {
  34. return <p>Visualization has no options</p>;
  35. }
  36. }
  37. componentDidMount() {
  38. if (this.shouldLoadAngularOptions()) {
  39. this.loadAngularOptions();
  40. }
  41. }
  42. componentDidUpdate(prevProps: Props) {
  43. if (this.props.plugin !== prevProps.plugin) {
  44. this.cleanUpAngularOptions();
  45. }
  46. if (this.shouldLoadAngularOptions()) {
  47. this.loadAngularOptions();
  48. }
  49. }
  50. shouldLoadAngularOptions() {
  51. return this.props.angularPanel && this.element && !this.angularOptions;
  52. }
  53. loadAngularOptions() {
  54. const { angularPanel } = this.props;
  55. const scope = angularPanel.getScope();
  56. // When full page reloading in edit mode the angular panel has on fully compiled & instantiated yet
  57. if (!scope.$$childHead) {
  58. setTimeout(() => {
  59. this.forceUpdate();
  60. });
  61. return;
  62. }
  63. const panelCtrl = scope.$$childHead.ctrl;
  64. let template = '';
  65. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  66. template += `
  67. <div class="form-section" ng-cloak>
  68. <div class="form-section__header">{{ctrl.editorTabs[${i}].title}}</div>
  69. <div class="form-section__body">
  70. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  71. </div>
  72. </div>
  73. `;
  74. }
  75. const loader = getAngularLoader();
  76. const scopeProps = { ctrl: panelCtrl };
  77. this.angularOptions = loader.load(this.element, scopeProps, template);
  78. }
  79. componentWillUnmount() {
  80. this.cleanUpAngularOptions();
  81. }
  82. cleanUpAngularOptions() {
  83. if (this.angularOptions) {
  84. this.angularOptions.destroy();
  85. this.angularOptions = null;
  86. }
  87. }
  88. onPanelOptionsChanged = (options: any) => {
  89. this.props.panel.updateOptions(options);
  90. this.forceUpdate();
  91. };
  92. render() {
  93. const { plugin } = this.props;
  94. const panelSelection = {
  95. title: plugin.name,
  96. imgSrc: plugin.info.logos.small,
  97. render: () => {
  98. // the needs to be scoped inside this closure
  99. const { plugin, onTypeChanged } = this.props;
  100. return <VizTypePicker current={plugin} onTypeChanged={onTypeChanged} />;
  101. },
  102. };
  103. const panelHelp = {
  104. title: '',
  105. icon: 'fa fa-question',
  106. render: () => <h2>Help</h2>,
  107. };
  108. return (
  109. <EditorTabBody heading="Visualization" main={panelSelection} toolbarItems={[panelHelp]}>
  110. {this.renderPanelOptions()}
  111. </EditorTabBody>
  112. );
  113. }
  114. }