DashboardPanel.tsx 4.8 KB

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