VisualizationTab.tsx 3.7 KB

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