DashboardPanel.tsx 5.1 KB

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