PanelChromeAngular.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import classNames from 'classnames';
  2. import React, { PureComponent } from 'react';
  3. import { connect, MapDispatchToProps, MapStateToProps } from 'react-redux';
  4. import { Subscription } from 'rxjs';
  5. import { getDefaultTimeRange, LoadingState, PanelData, PanelPlugin } from '@grafana/data';
  6. import { selectors } from '@grafana/e2e-selectors';
  7. import { AngularComponent, getAngularLoader, locationService } from '@grafana/runtime';
  8. import config from 'app/core/config';
  9. import { PANEL_BORDER } from 'app/core/constants';
  10. import { setPanelAngularComponent } from 'app/features/panel/state/reducers';
  11. import { getPanelStateForModel } from 'app/features/panel/state/selectors';
  12. import { StoreState } from 'app/types';
  13. import { isSoloRoute } from '../../../routes/utils';
  14. import { getTimeSrv, TimeSrv } from '../services/TimeSrv';
  15. import { DashboardModel, PanelModel } from '../state';
  16. import { PanelHeader } from './PanelHeader/PanelHeader';
  17. interface OwnProps {
  18. panel: PanelModel;
  19. dashboard: DashboardModel;
  20. plugin: PanelPlugin;
  21. isViewing: boolean;
  22. isEditing: boolean;
  23. isInView: boolean;
  24. width: number;
  25. height: number;
  26. }
  27. interface ConnectedProps {
  28. angularComponent?: AngularComponent;
  29. }
  30. interface DispatchProps {
  31. setPanelAngularComponent: typeof setPanelAngularComponent;
  32. }
  33. export type Props = OwnProps & ConnectedProps & DispatchProps;
  34. export interface State {
  35. data: PanelData;
  36. errorMessage?: string;
  37. }
  38. interface AngularScopeProps {
  39. panel: PanelModel;
  40. dashboard: DashboardModel;
  41. size: {
  42. height: number;
  43. width: number;
  44. };
  45. }
  46. export class PanelChromeAngularUnconnected extends PureComponent<Props, State> {
  47. element: HTMLElement | null = null;
  48. timeSrv: TimeSrv = getTimeSrv();
  49. scopeProps?: AngularScopeProps;
  50. subs = new Subscription();
  51. constructor(props: Props) {
  52. super(props);
  53. this.state = {
  54. data: {
  55. state: LoadingState.NotStarted,
  56. series: [],
  57. timeRange: getDefaultTimeRange(),
  58. },
  59. };
  60. }
  61. componentDidMount() {
  62. const { panel } = this.props;
  63. this.loadAngularPanel();
  64. // subscribe to data events
  65. const queryRunner = panel.getQueryRunner();
  66. // we are not displaying any of this data so no need for transforms or field config
  67. this.subs.add(
  68. queryRunner.getData({ withTransforms: false, withFieldConfig: false }).subscribe({
  69. next: (data: PanelData) => this.onPanelDataUpdate(data),
  70. })
  71. );
  72. }
  73. onPanelDataUpdate(data: PanelData) {
  74. let errorMessage: string | undefined;
  75. if (data.state === LoadingState.Error) {
  76. const { error } = data;
  77. if (error) {
  78. if (errorMessage !== error.message) {
  79. errorMessage = error.message;
  80. }
  81. }
  82. }
  83. this.setState({ data, errorMessage });
  84. }
  85. componentWillUnmount() {
  86. this.subs.unsubscribe();
  87. }
  88. componentDidUpdate(prevProps: Props, prevState: State) {
  89. const { plugin, height, width, panel } = this.props;
  90. if (prevProps.plugin !== plugin) {
  91. this.loadAngularPanel();
  92. }
  93. if (prevProps.width !== width || prevProps.height !== height) {
  94. if (this.scopeProps) {
  95. this.scopeProps.size.height = this.getInnerPanelHeight();
  96. this.scopeProps.size.width = this.getInnerPanelWidth();
  97. panel.render();
  98. }
  99. }
  100. }
  101. getInnerPanelHeight() {
  102. const { plugin, height } = this.props;
  103. const { theme } = config;
  104. const headerHeight = this.hasOverlayHeader() ? 0 : theme.panelHeaderHeight;
  105. const chromePadding = plugin.noPadding ? 0 : theme.panelPadding;
  106. return height - headerHeight - chromePadding * 2 - PANEL_BORDER;
  107. }
  108. getInnerPanelWidth() {
  109. const { plugin, width } = this.props;
  110. const { theme } = config;
  111. const chromePadding = plugin.noPadding ? 0 : theme.panelPadding;
  112. return width - chromePadding * 2 - PANEL_BORDER;
  113. }
  114. loadAngularPanel() {
  115. const { panel, dashboard, setPanelAngularComponent } = this.props;
  116. // if we have no element or already have loaded the panel return
  117. if (!this.element) {
  118. return;
  119. }
  120. const loader = getAngularLoader();
  121. const template = '<plugin-component type="panel" class="panel-height-helper"></plugin-component>';
  122. this.scopeProps = {
  123. panel: panel,
  124. dashboard: dashboard,
  125. size: { width: this.getInnerPanelWidth(), height: this.getInnerPanelHeight() },
  126. };
  127. setPanelAngularComponent({
  128. key: panel.key,
  129. angularComponent: loader.load(this.element, this.scopeProps, template),
  130. });
  131. }
  132. hasOverlayHeader() {
  133. const { panel } = this.props;
  134. const { data } = this.state;
  135. // always show normal header if we have time override
  136. if (data.request && data.request.timeInfo) {
  137. return false;
  138. }
  139. return !panel.hasTitle();
  140. }
  141. render() {
  142. const { dashboard, panel, isViewing, isEditing, plugin } = this.props;
  143. const { errorMessage, data } = this.state;
  144. const { transparent } = panel;
  145. const alertState = data.alertState?.state;
  146. const containerClassNames = classNames({
  147. 'panel-container': true,
  148. 'panel-container--absolute': isSoloRoute(locationService.getLocation().pathname),
  149. 'panel-container--transparent': transparent,
  150. 'panel-container--no-title': this.hasOverlayHeader(),
  151. 'panel-has-alert': panel.alert !== undefined,
  152. [`panel-alert-state--${alertState}`]: alertState !== undefined,
  153. });
  154. const panelContentClassNames = classNames({
  155. 'panel-content': true,
  156. 'panel-content--no-padding': plugin.noPadding,
  157. });
  158. return (
  159. <div className={containerClassNames} aria-label={selectors.components.Panels.Panel.containerByTitle(panel.title)}>
  160. <PanelHeader
  161. panel={panel}
  162. dashboard={dashboard}
  163. title={panel.title}
  164. description={panel.description}
  165. links={panel.links}
  166. error={errorMessage}
  167. isViewing={isViewing}
  168. isEditing={isEditing}
  169. data={data}
  170. alertState={alertState}
  171. />
  172. <div className={panelContentClassNames}>
  173. <div ref={(element) => (this.element = element)} className="panel-height-helper" />
  174. </div>
  175. </div>
  176. );
  177. }
  178. }
  179. const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = (state, props) => {
  180. return {
  181. angularComponent: getPanelStateForModel(state, props.panel)?.angularComponent,
  182. };
  183. };
  184. const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = { setPanelAngularComponent };
  185. export const PanelChromeAngular = connect(mapStateToProps, mapDispatchToProps)(PanelChromeAngularUnconnected);