DashboardPanel.tsx 6.1 KB

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