VisualizationTab.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. const { angularPanel } = this.props;
  39. if (angularPanel) {
  40. const scope = angularPanel.getScope();
  41. const panelCtrl = scope.$$childHead.ctrl;
  42. const loader = getAngularLoader();
  43. const template = '<panel-editor-tab editor-tab="tab" ctrl="ctrl"></panel-editor-tab>';
  44. const scopeProps = { ctrl: panelCtrl, tab: panelCtrl.editorTabs[2] };
  45. this.angularOptions = loader.load(this.element, scopeProps, template);
  46. }
  47. }
  48. componentWillUnmount() {
  49. if (this.angularOptions) {
  50. this.angularOptions.destroy();
  51. }
  52. }
  53. onPanelOptionsChanged = (options: any) => {
  54. this.props.panel.updateOptions(options);
  55. this.forceUpdate();
  56. };
  57. render() {
  58. const { plugin } = this.props;
  59. const panelSelection = {
  60. title: plugin.name,
  61. imgSrc: plugin.info.logos.small,
  62. render: () => {
  63. // the needs to be scoped inside this closure
  64. const { plugin, onTypeChanged } = this.props;
  65. return <VizTypePicker current={plugin} onTypeChanged={onTypeChanged} />;
  66. },
  67. };
  68. return (
  69. <EditorTabBody main={panelSelection} toolbarItems={[]}>
  70. {this.renderPanelOptions()}
  71. </EditorTabBody>
  72. );
  73. }
  74. }