DashboardPanel.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React, { PureComponent } from 'react';
  2. import config from 'app/core/config';
  3. import { getAngularLoader, AngularComponent } from 'app/core/services/AngularLoader';
  4. import { importPluginModule } from 'app/features/plugins/plugin_loader';
  5. import { AddPanelPanel } from './AddPanelPanel';
  6. import { getPanelPluginNotFound } from './PanelPluginNotFound';
  7. import { DashboardRow } from './DashboardRow';
  8. import { PanelChrome } from './PanelChrome';
  9. import { PanelEditor } from './PanelEditor';
  10. import { PanelModel } from '../panel_model';
  11. import { DashboardModel } from '../dashboard_model';
  12. import { PanelPlugin } from 'app/types';
  13. export interface Props {
  14. panel: PanelModel;
  15. dashboard: DashboardModel;
  16. isEditing: boolean;
  17. isFullscreen: boolean;
  18. }
  19. export interface State {
  20. plugin: PanelPlugin;
  21. angularPanel: AngularComponent;
  22. }
  23. export class DashboardPanel extends PureComponent<Props, State> {
  24. element: HTMLElement;
  25. specialPanels = {};
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. plugin: null,
  30. angularPanel: null,
  31. };
  32. this.specialPanels['row'] = this.renderRow.bind(this);
  33. this.specialPanels['add-panel'] = this.renderAddPanel.bind(this);
  34. }
  35. isSpecial() {
  36. return this.specialPanels[this.props.panel.type];
  37. }
  38. renderRow() {
  39. return <DashboardRow panel={this.props.panel} dashboard={this.props.dashboard} />;
  40. }
  41. renderAddPanel() {
  42. return <AddPanelPanel panel={this.props.panel} dashboard={this.props.dashboard} />;
  43. }
  44. onPluginTypeChanged = (plugin: PanelPlugin) => {
  45. this.props.panel.changeType(plugin.id, this.state.angularPanel !== null);
  46. this.loadPlugin();
  47. };
  48. onAngularPluginTypeChanged = () => {
  49. this.loadPlugin();
  50. };
  51. loadPlugin() {
  52. if (this.isSpecial()) {
  53. return;
  54. }
  55. const { panel } = this.props;
  56. // handle plugin loading & changing of plugin type
  57. if (!this.state.plugin || this.state.plugin.id !== panel.type) {
  58. const plugin = config.panels[panel.type] || getPanelPluginNotFound(panel.type);
  59. if (plugin.exports) {
  60. this.cleanUpAngularPanel();
  61. this.setState({ plugin: plugin });
  62. } else {
  63. importPluginModule(plugin.module).then(pluginExports => {
  64. this.cleanUpAngularPanel();
  65. // cache plugin exports (saves a promise async cycle next time)
  66. plugin.exports = pluginExports;
  67. // update panel state
  68. this.setState({ plugin: plugin });
  69. });
  70. }
  71. }
  72. }
  73. componentDidMount() {
  74. this.loadPlugin();
  75. }
  76. componentDidUpdate() {
  77. this.loadPlugin();
  78. // handle angular plugin loading
  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(unmounted?: boolean) {
  89. if (this.state.angularPanel) {
  90. this.state.angularPanel.destroy();
  91. if (!unmounted) {
  92. this.setState({ angularPanel: null });
  93. }
  94. }
  95. }
  96. componentWillUnmount() {
  97. this.cleanUpAngularPanel(true);
  98. }
  99. onMouseEnter = () => {
  100. this.props.dashboard.setPanelFocus(this.props.panel.id);
  101. };
  102. onMouseLeave = () => {
  103. this.props.dashboard.setPanelFocus(0);
  104. };
  105. renderReactPanel() {
  106. const { dashboard, panel } = this.props;
  107. const { plugin } = this.state;
  108. return <PanelChrome component={plugin.exports.Panel} panel={panel} dashboard={dashboard} />;
  109. }
  110. renderAngularPanel() {
  111. return <div ref={element => (this.element = element)} className="panel-height-helper" />;
  112. }
  113. render() {
  114. const { panel, dashboard } = this.props;
  115. const { plugin, angularPanel } = this.state;
  116. if (this.isSpecial()) {
  117. return this.specialPanels[panel.type]();
  118. }
  119. // if we have not loaded plugin exports yet, wait
  120. if (!plugin || !plugin.exports) {
  121. return null;
  122. }
  123. console.log('DashboardPanel.render()');
  124. const containerClass = this.props.isEditing ? 'panel-editor-container' : 'panel-height-helper';
  125. const panelWrapperClass = this.props.isEditing ? 'panel-editor-container__panel' : 'panel-height-helper';
  126. return (
  127. <div className={containerClass}>
  128. <div className={panelWrapperClass} onMouseEnter={this.onMouseEnter} onMouseLeave={this.onMouseLeave}>
  129. {plugin.exports.Panel && this.renderReactPanel()}
  130. {plugin.exports.PanelCtrl && this.renderAngularPanel()}
  131. </div>
  132. {panel.isEditing && (
  133. <PanelEditor
  134. panel={panel}
  135. plugin={plugin}
  136. dashboard={dashboard}
  137. angularPanel={angularPanel}
  138. onTypeChanged={this.onPluginTypeChanged}
  139. />
  140. )}
  141. </div>
  142. );
  143. }
  144. }