DashboardPanel.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import classNames from 'classnames';
  4. // Utils & Services
  5. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  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. }
  23. export interface State {
  24. plugin: PanelPlugin;
  25. angularPanel: AngularComponent;
  26. }
  27. export class DashboardPanel extends PureComponent<Props, State> {
  28. element: HTMLElement;
  29. specialPanels = {};
  30. constructor(props: Props) {
  31. super(props);
  32. this.state = {
  33. plugin: null,
  34. angularPanel: null,
  35. };
  36. this.specialPanels['row'] = this.renderRow.bind(this);
  37. this.specialPanels['add-panel'] = this.renderAddPanel.bind(this);
  38. }
  39. isSpecial(pluginId: string) {
  40. return this.specialPanels[pluginId];
  41. }
  42. renderRow() {
  43. return <DashboardRow panel={this.props.panel} dashboard={this.props.dashboard} />;
  44. }
  45. renderAddPanel() {
  46. return <AddPanelWidget panel={this.props.panel} dashboard={this.props.dashboard} />;
  47. }
  48. onPluginTypeChanged = (plugin: PanelPluginMeta) => {
  49. this.loadPlugin(plugin.id);
  50. };
  51. async loadPlugin(pluginId: string) {
  52. if (this.isSpecial(pluginId)) {
  53. return;
  54. }
  55. const { panel } = this.props;
  56. // handle plugin loading & changing of plugin type
  57. if (!this.state.plugin || this.state.plugin.meta.id !== pluginId) {
  58. const plugin = await importPanelPlugin(pluginId);
  59. // unmount angular panel
  60. this.cleanUpAngularPanel();
  61. if (panel.type !== pluginId) {
  62. panel.changePlugin(plugin);
  63. } else {
  64. panel.pluginLoaded(plugin);
  65. }
  66. this.setState({ plugin, angularPanel: null });
  67. }
  68. }
  69. componentDidMount() {
  70. this.loadPlugin(this.props.panel.type);
  71. }
  72. componentDidUpdate() {
  73. if (!this.element || this.state.angularPanel) {
  74. return;
  75. }
  76. const loader = getAngularLoader();
  77. const template = '<plugin-component type="panel" class="panel-height-helper"></plugin-component>';
  78. const scopeProps = { panel: this.props.panel, dashboard: this.props.dashboard };
  79. const angularPanel = loader.load(this.element, scopeProps, template);
  80. this.setState({ angularPanel });
  81. }
  82. cleanUpAngularPanel() {
  83. if (this.state.angularPanel) {
  84. this.state.angularPanel.destroy();
  85. this.element = null;
  86. }
  87. }
  88. componentWillUnmount() {
  89. this.cleanUpAngularPanel();
  90. }
  91. onMouseEnter = () => {
  92. this.props.dashboard.setPanelFocus(this.props.panel.id);
  93. };
  94. onMouseLeave = () => {
  95. this.props.dashboard.setPanelFocus(0);
  96. };
  97. renderReactPanel() {
  98. const { dashboard, panel, isFullscreen } = this.props;
  99. const { plugin } = this.state;
  100. return (
  101. <AutoSizer>
  102. {({ width, height }) => {
  103. if (width === 0) {
  104. return null;
  105. }
  106. return (
  107. <PanelChrome
  108. plugin={plugin}
  109. panel={panel}
  110. dashboard={dashboard}
  111. isFullscreen={isFullscreen}
  112. width={width}
  113. height={height}
  114. />
  115. );
  116. }}
  117. </AutoSizer>
  118. );
  119. }
  120. renderAngularPanel() {
  121. return <div ref={element => (this.element = element)} className="panel-height-helper" />;
  122. }
  123. render() {
  124. const { panel, dashboard, isFullscreen, isEditing } = this.props;
  125. const { plugin, angularPanel } = this.state;
  126. if (this.isSpecial(panel.type)) {
  127. return this.specialPanels[panel.type]();
  128. }
  129. // if we have not loaded plugin exports yet, wait
  130. if (!plugin) {
  131. return null;
  132. }
  133. const containerClass = classNames({ 'panel-editor-container': isEditing, 'panel-height-helper': !isEditing });
  134. const panelWrapperClass = classNames({
  135. 'panel-wrapper': true,
  136. 'panel-wrapper--edit': isEditing,
  137. 'panel-wrapper--view': isFullscreen && !isEditing,
  138. });
  139. return (
  140. <div className={containerClass}>
  141. <PanelResizer
  142. isEditing={isEditing}
  143. panel={panel}
  144. render={styles => (
  145. <div
  146. className={panelWrapperClass}
  147. onMouseEnter={this.onMouseEnter}
  148. onMouseLeave={this.onMouseLeave}
  149. style={styles}
  150. >
  151. {plugin.angularPanelCtrl ? this.renderAngularPanel() : this.renderReactPanel()}
  152. </div>
  153. )}
  154. />
  155. {panel.isEditing && (
  156. <PanelEditor
  157. panel={panel}
  158. plugin={plugin}
  159. dashboard={dashboard}
  160. angularPanel={angularPanel}
  161. onTypeChanged={this.onPluginTypeChanged}
  162. />
  163. )}
  164. </div>
  165. );
  166. }
  167. }