DashboardPanel.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. // remember if this is from an angular panel
  57. const fromAngularPanel = this.state.angularPanel != null;
  58. // unmount angular panel
  59. this.cleanUpAngularPanel();
  60. if (panel.type !== pluginId) {
  61. this.props.panel.changeType(pluginId, fromAngularPanel);
  62. }
  63. if (plugin.exports) {
  64. this.setState({ plugin, angularPanel: null });
  65. } else {
  66. try {
  67. plugin.exports = await importPluginModule(plugin.module);
  68. } catch (e) {
  69. plugin = getPanelPluginNotFound(pluginId);
  70. }
  71. this.setState({ plugin, angularPanel: null });
  72. }
  73. }
  74. }
  75. componentDidMount() {
  76. this.loadPlugin(this.props.panel.type);
  77. }
  78. componentDidUpdate() {
  79. if (!this.element || this.state.angularPanel) {
  80. return;
  81. }
  82. const loader = getAngularLoader();
  83. const template = '<plugin-component type="panel" class="panel-height-helper"></plugin-component>';
  84. const scopeProps = { panel: this.props.panel, dashboard: this.props.dashboard };
  85. const angularPanel = loader.load(this.element, scopeProps, template);
  86. this.setState({ angularPanel });
  87. }
  88. cleanUpAngularPanel() {
  89. if (this.state.angularPanel) {
  90. this.state.angularPanel.destroy();
  91. this.element = null;
  92. }
  93. }
  94. componentWillUnmount() {
  95. this.cleanUpAngularPanel();
  96. }
  97. onMouseEnter = () => {
  98. this.props.dashboard.setPanelFocus(this.props.panel.id);
  99. };
  100. onMouseLeave = () => {
  101. this.props.dashboard.setPanelFocus(0);
  102. };
  103. renderReactPanel() {
  104. const { dashboard, panel } = this.props;
  105. const { plugin } = this.state;
  106. return <PanelChrome plugin={plugin} panel={panel} dashboard={dashboard} />;
  107. }
  108. renderAngularPanel() {
  109. return <div ref={element => (this.element = element)} className="panel-height-helper" />;
  110. }
  111. render() {
  112. const { panel, dashboard, isFullscreen, isEditing } = this.props;
  113. const { plugin, angularPanel } = this.state;
  114. if (this.isSpecial(panel.type)) {
  115. return this.specialPanels[panel.type]();
  116. }
  117. // if we have not loaded plugin exports yet, wait
  118. if (!plugin || !plugin.exports) {
  119. return null;
  120. }
  121. const containerClass = classNames({ 'panel-editor-container': isEditing, 'panel-height-helper': !isEditing });
  122. const panelWrapperClass = classNames({
  123. 'panel-wrapper': true,
  124. 'panel-wrapper--edit': isEditing,
  125. 'panel-wrapper--view': isFullscreen && !isEditing,
  126. });
  127. return (
  128. <div className={containerClass}>
  129. <PanelResizer
  130. isEditing={isEditing}
  131. panel={panel}
  132. render={styles => (
  133. <div
  134. className={panelWrapperClass}
  135. onMouseEnter={this.onMouseEnter}
  136. onMouseLeave={this.onMouseLeave}
  137. style={styles}
  138. >
  139. {plugin.exports.Panel && this.renderReactPanel()}
  140. {plugin.exports.PanelCtrl && this.renderAngularPanel()}
  141. </div>
  142. )}
  143. />
  144. {panel.isEditing && (
  145. <PanelEditor
  146. panel={panel}
  147. plugin={plugin}
  148. dashboard={dashboard}
  149. angularPanel={angularPanel}
  150. onTypeChanged={this.onPluginTypeChanged}
  151. />
  152. )}
  153. </div>
  154. );
  155. }
  156. }