AngularPanelOptions.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { PanelPlugin, PanelPluginMeta } from '@grafana/data';
  4. import { AngularComponent, getAngularLoader } from '@grafana/runtime';
  5. import { PanelCtrl } from 'app/angular/panel/panel_ctrl';
  6. import { changePanelPlugin } from 'app/features/panel/state/actions';
  7. import { getPanelStateForModel } from 'app/features/panel/state/selectors';
  8. import { StoreState } from 'app/types';
  9. import { PanelModel, DashboardModel } from '../../state';
  10. import { getSectionOpenState, saveSectionOpenState } from './state/utils';
  11. interface OwnProps {
  12. panel: PanelModel;
  13. dashboard: DashboardModel;
  14. plugin: PanelPlugin;
  15. }
  16. const mapStateToProps = (state: StoreState, props: OwnProps) => ({
  17. angularPanelComponent: getPanelStateForModel(state, props.panel)?.angularComponent,
  18. });
  19. const mapDispatchToProps = { changePanelPlugin };
  20. const connector = connect(mapStateToProps, mapDispatchToProps);
  21. type Props = ConnectedProps<typeof connector> & OwnProps;
  22. export class AngularPanelOptionsUnconnected extends PureComponent<Props> {
  23. element?: HTMLElement | null;
  24. angularOptions?: AngularComponent | null;
  25. constructor(props: Props) {
  26. super(props);
  27. }
  28. componentDidMount() {
  29. this.loadAngularOptions();
  30. }
  31. componentDidUpdate(prevProps: Props) {
  32. if (
  33. this.props.plugin !== prevProps.plugin ||
  34. this.props.angularPanelComponent !== prevProps.angularPanelComponent
  35. ) {
  36. this.cleanUpAngularOptions();
  37. }
  38. this.loadAngularOptions();
  39. }
  40. componentWillUnmount() {
  41. this.cleanUpAngularOptions();
  42. }
  43. cleanUpAngularOptions() {
  44. if (this.angularOptions) {
  45. this.angularOptions.destroy();
  46. this.angularOptions = null;
  47. }
  48. }
  49. loadAngularOptions() {
  50. const { panel, angularPanelComponent, changePanelPlugin } = this.props;
  51. if (!this.element || !angularPanelComponent || this.angularOptions) {
  52. return;
  53. }
  54. const scope = angularPanelComponent.getScope();
  55. // When full page reloading in edit mode the angular panel has on fully compiled and instantiated yet
  56. if (!scope.$$childHead) {
  57. setTimeout(() => {
  58. this.forceUpdate();
  59. });
  60. return;
  61. }
  62. const panelCtrl: PanelCtrl = scope.$$childHead.ctrl;
  63. panelCtrl.initEditMode();
  64. panelCtrl.onPluginTypeChange = (plugin: PanelPluginMeta) => {
  65. changePanelPlugin({ panel, pluginId: plugin.id });
  66. };
  67. let template = '';
  68. for (let i = 0; i < panelCtrl.editorTabs.length; i++) {
  69. const tab = panelCtrl.editorTabs[i];
  70. tab.isOpen = getSectionOpenState(tab.title, i === 0);
  71. template += `
  72. <div class="panel-options-group" ng-cloak>
  73. <div class="panel-options-group__header" ng-click="toggleOptionGroup(${i})" aria-label="${tab.title} section">
  74. <div class="panel-options-group__icon">
  75. <icon name="ctrl.editorTabs[${i}].isOpen ? 'angle-down' : 'angle-right'"></icon>
  76. </div>
  77. <div class="panel-options-group__title">${tab.title}</div>
  78. </div>
  79. <div class="panel-options-group__body" ng-if="ctrl.editorTabs[${i}].isOpen">
  80. <panel-editor-tab editor-tab="ctrl.editorTabs[${i}]" ctrl="ctrl"></panel-editor-tab>
  81. </div>
  82. </div>
  83. `;
  84. }
  85. const loader = getAngularLoader();
  86. const scopeProps = {
  87. ctrl: panelCtrl,
  88. toggleOptionGroup: (index: number) => {
  89. const tab = panelCtrl.editorTabs[index];
  90. tab.isOpen = !tab.isOpen;
  91. saveSectionOpenState(tab.title, tab.isOpen as boolean);
  92. },
  93. };
  94. this.angularOptions = loader.load(this.element, scopeProps, template);
  95. this.angularOptions.digest();
  96. }
  97. render() {
  98. return <div ref={(elem) => (this.element = elem)} />;
  99. }
  100. }
  101. export const AngularPanelOptions = connect(mapStateToProps, mapDispatchToProps)(AngularPanelOptionsUnconnected);