DashboardPanel.tsx 5.8 KB

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