DashboardPanel.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import React, { PureComponent } from 'react';
  2. import config from 'app/core/config';
  3. import classNames from 'classnames';
  4. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  5. import { importPluginModule } from 'app/features/plugins/plugin_loader';
  6. import { AddPanelWidget } from '../components/AddPanelWidget';
  7. import { getPanelPluginNotFound } from './PanelPluginNotFound';
  8. import { DashboardRow } from '../components/DashboardRow';
  9. import { PanelChrome } from './PanelChrome';
  10. import { PanelEditor } from '../panel_editor/PanelEditor';
  11. import { PanelModel, DashboardModel } from '../state';
  12. import { PanelPlugin } from 'app/types';
  13. import { PanelResizer } from './PanelResizer';
  14. export interface Props {
  15. panel: PanelModel;
  16. dashboard: DashboardModel;
  17. isEditing: boolean;
  18. isFullscreen: boolean;
  19. }
  20. export interface State {
  21. plugin: PanelPlugin;
  22. angularPanel: AngularComponent;
  23. }
  24. export class DashboardPanel extends PureComponent<Props, State> {
  25. element: HTMLElement;
  26. specialPanels = {};
  27. constructor(props) {
  28. super(props);
  29. this.state = {
  30. plugin: null,
  31. angularPanel: null,
  32. };
  33. this.specialPanels['row'] = this.renderRow.bind(this);
  34. this.specialPanels['add-panel'] = this.renderAddPanel.bind(this);
  35. }
  36. isSpecial(pluginId: string) {
  37. return this.specialPanels[pluginId];
  38. }
  39. renderRow() {
  40. return <DashboardRow panel={this.props.panel} dashboard={this.props.dashboard} />;
  41. }
  42. renderAddPanel() {
  43. return <AddPanelWidget panel={this.props.panel} dashboard={this.props.dashboard} />;
  44. }
  45. onPluginTypeChanged = (plugin: PanelPlugin) => {
  46. this.loadPlugin(plugin.id);
  47. };
  48. async loadPlugin(pluginId: string) {
  49. if (this.isSpecial(pluginId)) {
  50. return;
  51. }
  52. const { panel } = this.props;
  53. // handle plugin loading & changing of plugin type
  54. if (!this.state.plugin || this.state.plugin.id !== pluginId) {
  55. let plugin = config.panels[pluginId] || getPanelPluginNotFound(pluginId);
  56. // unmount angular panel
  57. this.cleanUpAngularPanel();
  58. if (!plugin.exports) {
  59. try {
  60. plugin.exports = await importPluginModule(plugin.module);
  61. } catch (e) {
  62. plugin = getPanelPluginNotFound(pluginId);
  63. }
  64. }
  65. if (panel.type !== pluginId) {
  66. panel.changePlugin(plugin);
  67. } else {
  68. panel.pluginLoaded(plugin);
  69. }
  70. this.setState({ plugin, angularPanel: null });
  71. }
  72. }
  73. componentDidMount() {
  74. this.loadPlugin(this.props.panel.type);
  75. }
  76. componentDidUpdate() {
  77. if (!this.element || this.state.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. const angularPanel = loader.load(this.element, scopeProps, template);
  84. this.setState({ angularPanel });
  85. }
  86. cleanUpAngularPanel() {
  87. if (this.state.angularPanel) {
  88. this.state.angularPanel.destroy();
  89. this.element = null;
  90. }
  91. }
  92. componentWillUnmount() {
  93. this.cleanUpAngularPanel();
  94. }
  95. onMouseEnter = () => {
  96. this.props.dashboard.setPanelFocus(this.props.panel.id);
  97. };
  98. onMouseLeave = () => {
  99. this.props.dashboard.setPanelFocus(0);
  100. };
  101. renderReactPanel() {
  102. const { dashboard, panel, isFullscreen, isEditing } = this.props;
  103. const { plugin } = this.state;
  104. return (
  105. <PanelChrome
  106. plugin={plugin}
  107. panel={panel}
  108. dashboard={dashboard}
  109. isFullscreen={isFullscreen}
  110. isEditing={isEditing}
  111. />
  112. );
  113. }
  114. renderAngularPanel() {
  115. return <div ref={element => (this.element = element)} className="panel-height-helper" />;
  116. }
  117. render() {
  118. const { panel, dashboard, isFullscreen, isEditing } = this.props;
  119. const { plugin, angularPanel } = this.state;
  120. if (this.isSpecial(panel.type)) {
  121. return this.specialPanels[panel.type]();
  122. }
  123. // if we have not loaded plugin exports yet, wait
  124. if (!plugin || !plugin.exports) {
  125. return null;
  126. }
  127. const containerClass = classNames({ 'panel-editor-container': isEditing, 'panel-height-helper': !isEditing });
  128. const panelWrapperClass = classNames({
  129. 'panel-wrapper': true,
  130. 'panel-wrapper--edit': isEditing,
  131. 'panel-wrapper--view': isFullscreen && !isEditing,
  132. });
  133. return (
  134. <div className={containerClass}>
  135. <PanelResizer
  136. isEditing={isEditing}
  137. panel={panel}
  138. render={styles => (
  139. <div
  140. className={panelWrapperClass}
  141. onMouseEnter={this.onMouseEnter}
  142. onMouseLeave={this.onMouseLeave}
  143. style={styles}
  144. >
  145. {plugin.exports.reactPanel && this.renderReactPanel()}
  146. {plugin.exports.PanelCtrl && this.renderAngularPanel()}
  147. </div>
  148. )}
  149. />
  150. {panel.isEditing && (
  151. <PanelEditor
  152. panel={panel}
  153. plugin={plugin}
  154. dashboard={dashboard}
  155. angularPanel={angularPanel}
  156. onTypeChanged={this.onPluginTypeChanged}
  157. />
  158. )}
  159. </div>
  160. );
  161. }
  162. }