DashboardPanel.tsx 5.1 KB

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