DashboardPanel.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React, { PureComponent } from 'react';
  2. import config from 'app/core/config';
  3. import { PanelModel } from '../panel_model';
  4. import { DashboardModel } from '../dashboard_model';
  5. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  6. import { DashboardRow } from './DashboardRow';
  7. import { AddPanelPanel } from './AddPanelPanel';
  8. import { importPluginModule } from 'app/features/plugins/plugin_loader';
  9. import { PluginExports, PanelPlugin } from 'app/types/plugins';
  10. import { PanelChrome } from './PanelChrome';
  11. import { PanelEditor } from './PanelEditor';
  12. export interface Props {
  13. panel: PanelModel;
  14. dashboard: DashboardModel;
  15. isEditing: boolean;
  16. isFullscreen: boolean;
  17. }
  18. export interface State {
  19. pluginExports: PluginExports;
  20. }
  21. export class DashboardPanel extends PureComponent<Props, State> {
  22. element: any;
  23. angularPanel: AngularComponent;
  24. pluginInfo: any;
  25. specialPanels = {};
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. pluginExports: null,
  30. };
  31. this.specialPanels['row'] = this.renderRow.bind(this);
  32. this.specialPanels['add-panel'] = this.renderAddPanel.bind(this);
  33. }
  34. isSpecial() {
  35. return this.specialPanels[this.props.panel.type];
  36. }
  37. renderRow() {
  38. return <DashboardRow panel={this.props.panel} dashboard={this.props.dashboard} />;
  39. }
  40. renderAddPanel() {
  41. return <AddPanelPanel panel={this.props.panel} dashboard={this.props.dashboard} />;
  42. }
  43. onPluginTypeChanged = (plugin: PanelPlugin) => {
  44. this.props.panel.changeType(plugin.id);
  45. this.loadPlugin();
  46. };
  47. onAngularPluginTypeChanged = () => {
  48. this.loadPlugin();
  49. };
  50. loadPlugin() {
  51. if (this.isSpecial()) {
  52. return;
  53. }
  54. // handle plugin loading & changing of plugin type
  55. if (!this.pluginInfo || this.pluginInfo.id !== this.props.panel.type) {
  56. this.pluginInfo = config.panels[this.props.panel.type];
  57. if (this.pluginInfo.exports) {
  58. this.cleanUpAngularPanel();
  59. this.setState({ pluginExports: this.pluginInfo.exports });
  60. } else {
  61. importPluginModule(this.pluginInfo.module).then(pluginExports => {
  62. this.cleanUpAngularPanel();
  63. // cache plugin exports (saves a promise async cycle next time)
  64. this.pluginInfo.exports = pluginExports;
  65. // update panel state
  66. this.setState({ pluginExports: pluginExports });
  67. });
  68. }
  69. }
  70. }
  71. componentDidMount() {
  72. this.loadPlugin();
  73. }
  74. componentDidUpdate() {
  75. this.loadPlugin();
  76. // handle angular plugin loading
  77. if (!this.element || this.angularPanel) {
  78. return;
  79. }
  80. const loader = getAngularLoader();
  81. const template = '<plugin-component type="panel" class="panel-height-helper"></plugin-component>';
  82. const scopeProps = { panel: this.props.panel, dashboard: this.props.dashboard };
  83. this.angularPanel = loader.load(this.element, scopeProps, template);
  84. }
  85. cleanUpAngularPanel() {
  86. if (this.angularPanel) {
  87. this.angularPanel.destroy();
  88. this.angularPanel = null;
  89. }
  90. }
  91. componentWillUnmount() {
  92. this.cleanUpAngularPanel();
  93. }
  94. renderReactPanel() {
  95. const { pluginExports } = this.state;
  96. const containerClass = this.props.isEditing ? 'panel-editor-container' : 'panel-height-helper';
  97. const panelWrapperClass = this.props.isEditing ? 'panel-editor-container__panel' : 'panel-height-helper';
  98. // this might look strange with these classes that change when edit, but
  99. // I want to try to keep markup (parents) for panel the same in edit mode to avoide unmount / new mount of panel
  100. return (
  101. <div className={containerClass}>
  102. <div className={panelWrapperClass}>
  103. <PanelChrome
  104. component={pluginExports.PanelComponent}
  105. panel={this.props.panel}
  106. dashboard={this.props.dashboard}
  107. />
  108. </div>
  109. {this.props.panel.isEditing && (
  110. <div className="panel-editor-container__editor">
  111. <PanelEditor
  112. panel={this.props.panel}
  113. panelType={this.props.panel.type}
  114. dashboard={this.props.dashboard}
  115. onTypeChanged={this.onPluginTypeChanged}
  116. pluginExports={pluginExports}
  117. />
  118. </div>
  119. )}
  120. </div>
  121. );
  122. }
  123. render() {
  124. if (this.isSpecial()) {
  125. return this.specialPanels[this.props.panel.type]();
  126. }
  127. if (!this.state.pluginExports) {
  128. return null;
  129. }
  130. if (this.state.pluginExports.PanelComponent) {
  131. return this.renderReactPanel();
  132. }
  133. // legacy angular rendering
  134. return <div ref={element => (this.element = element)} className="panel-height-helper" />;
  135. }
  136. }