DashboardPanel.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. // Utils & Services
  5. import { getAngularLoader, AngularComponent } from '@grafana/runtime';
  6. import { importPanelPlugin } from 'app/features/plugins/plugin_loader';
  7. // Components
  8. import { AddPanelWidget } from '../components/AddPanelWidget';
  9. import { DashboardRow } from '../components/DashboardRow';
  10. import { PanelChrome } from './PanelChrome';
  11. import { PanelEditor } from '../panel_editor/PanelEditor';
  12. import { PanelResizer } from './PanelResizer';
  13. // Types
  14. import { PanelModel, DashboardModel } from '../state';
  15. import { PanelPluginMeta, PanelPlugin } from '@grafana/ui/src/types/panel';
  16. import { AutoSizer } from 'react-virtualized';
  17. export interface Props {
  18. panel: PanelModel;
  19. dashboard: DashboardModel;
  20. isEditing: boolean;
  21. isFullscreen: boolean;
  22. isInView: boolean;
  23. }
  24. export interface State {
  25. plugin: PanelPlugin;
  26. angularPanel: AngularComponent;
  27. isLazy: boolean;
  28. }
  29. export class DashboardPanel extends PureComponent<Props, State> {
  30. element: HTMLElement;
  31. specialPanels: { [key: string]: Function } = {};
  32. constructor(props: Props) {
  33. super(props);
  34. this.state = {
  35. plugin: null,
  36. angularPanel: null,
  37. isLazy: !props.isInView,
  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. onPluginTypeChange = (plugin: PanelPluginMeta) => {
  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.meta.id !== pluginId) {
  61. const plugin = await importPanelPlugin(pluginId);
  62. // unmount angular panel
  63. this.cleanUpAngularPanel();
  64. if (panel.type !== pluginId) {
  65. panel.changePlugin(plugin);
  66. } else {
  67. panel.pluginLoaded(plugin);
  68. }
  69. this.setState({ plugin, angularPanel: null });
  70. }
  71. }
  72. componentDidMount() {
  73. this.loadPlugin(this.props.panel.type);
  74. }
  75. componentDidUpdate(prevProps: Props, prevState: State) {
  76. if (this.state.isLazy && this.props.isInView) {
  77. this.setState({ isLazy: false });
  78. }
  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. renderPanel() {
  104. const { dashboard, panel, isFullscreen, isInView } = this.props;
  105. const { plugin } = this.state;
  106. if (plugin.angularPanelCtrl) {
  107. return <div ref={element => (this.element = element)} className="panel-height-helper" />;
  108. }
  109. return (
  110. <AutoSizer>
  111. {({ width, height }) => {
  112. if (width === 0) {
  113. return null;
  114. }
  115. return (
  116. <PanelChrome
  117. plugin={plugin}
  118. panel={panel}
  119. dashboard={dashboard}
  120. isFullscreen={isFullscreen}
  121. isInView={isInView}
  122. width={width}
  123. height={height}
  124. />
  125. );
  126. }}
  127. </AutoSizer>
  128. );
  129. }
  130. render() {
  131. const { panel, dashboard, isFullscreen, isEditing } = this.props;
  132. const { plugin, angularPanel, isLazy } = this.state;
  133. if (this.isSpecial(panel.type)) {
  134. return this.specialPanels[panel.type]();
  135. }
  136. // if we have not loaded plugin exports yet, wait
  137. if (!plugin) {
  138. return null;
  139. }
  140. // If we are lazy state don't render anything
  141. if (isLazy) {
  142. return null;
  143. }
  144. const editorContainerClasses = classNames({
  145. 'panel-editor-container': isEditing,
  146. 'panel-height-helper': !isEditing,
  147. });
  148. const panelWrapperClass = classNames({
  149. 'panel-wrapper': true,
  150. 'panel-wrapper--edit': isEditing,
  151. 'panel-wrapper--view': isFullscreen && !isEditing,
  152. });
  153. return (
  154. <div className={editorContainerClasses}>
  155. <PanelResizer
  156. isEditing={isEditing}
  157. panel={panel}
  158. render={styles => (
  159. <div
  160. className={panelWrapperClass}
  161. onMouseEnter={this.onMouseEnter}
  162. onMouseLeave={this.onMouseLeave}
  163. style={styles}
  164. >
  165. {this.renderPanel()}
  166. </div>
  167. )}
  168. />
  169. {panel.isEditing && (
  170. <PanelEditor
  171. panel={panel}
  172. plugin={plugin}
  173. dashboard={dashboard}
  174. angularPanel={angularPanel}
  175. onPluginTypeChange={this.onPluginTypeChange}
  176. />
  177. )}
  178. </div>
  179. );
  180. }
  181. }