PanelChrome.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { AutoSizer } from 'react-virtualized';
  4. // Services
  5. import { getTimeSrv, TimeSrv } from '../time_srv';
  6. // Components
  7. import { PanelHeader } from './PanelHeader/PanelHeader';
  8. import { DataPanel } from './DataPanel';
  9. // Utils
  10. import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel';
  11. import { PANEL_HEADER_HEIGHT } from 'app/core/constants';
  12. // Types
  13. import { PanelModel } from '../panel_model';
  14. import { DashboardModel } from '../dashboard_model';
  15. import { PanelPlugin } from 'app/types';
  16. import { TimeRange } from '@grafana/ui';
  17. import variables from 'sass/_variables.scss';
  18. export interface Props {
  19. panel: PanelModel;
  20. dashboard: DashboardModel;
  21. plugin: PanelPlugin;
  22. }
  23. export interface State {
  24. refreshCounter: number;
  25. renderCounter: number;
  26. timeInfo?: string;
  27. timeRange?: TimeRange;
  28. }
  29. export class PanelChrome extends PureComponent<Props, State> {
  30. timeSrv: TimeSrv = getTimeSrv();
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. refreshCounter: 0,
  35. renderCounter: 0,
  36. };
  37. }
  38. componentDidMount() {
  39. this.props.panel.events.on('refresh', this.onRefresh);
  40. this.props.panel.events.on('render', this.onRender);
  41. this.props.dashboard.panelInitialized(this.props.panel);
  42. }
  43. componentWillUnmount() {
  44. this.props.panel.events.off('refresh', this.onRefresh);
  45. }
  46. onRefresh = () => {
  47. console.log('onRefresh');
  48. if (!this.isVisible) {
  49. return;
  50. }
  51. const { panel } = this.props;
  52. const timeData = applyPanelTimeOverrides(panel, this.timeSrv.timeRange());
  53. this.setState({
  54. refreshCounter: this.state.refreshCounter + 1,
  55. timeRange: timeData.timeRange,
  56. timeInfo: timeData.timeInfo,
  57. });
  58. };
  59. onRender = () => {
  60. this.setState({
  61. renderCounter: this.state.renderCounter + 1,
  62. });
  63. };
  64. get isVisible() {
  65. return !this.props.dashboard.otherPanelInFullscreen(this.props.panel);
  66. }
  67. render() {
  68. const { panel, dashboard, plugin } = this.props;
  69. const { refreshCounter, timeRange, timeInfo, renderCounter } = this.state;
  70. const { datasource, targets, transparent } = panel;
  71. const PanelComponent = plugin.exports.Panel;
  72. const containerClassNames = `panel-container panel-container--absolute ${transparent ? 'panel-transparent' : ''}`;
  73. return (
  74. <AutoSizer>
  75. {({ width, height }) => {
  76. if (width === 0) {
  77. return null;
  78. }
  79. return (
  80. <div className={containerClassNames}>
  81. <PanelHeader
  82. panel={panel}
  83. dashboard={dashboard}
  84. timeInfo={timeInfo}
  85. title={panel.title}
  86. description={panel.description}
  87. scopedVars={panel.scopedVars}
  88. links={panel.links}
  89. />
  90. <DataPanel
  91. datasource={datasource}
  92. queries={targets}
  93. timeRange={timeRange}
  94. isVisible={this.isVisible}
  95. widthPixels={width}
  96. refreshCounter={refreshCounter}
  97. >
  98. {({ loading, timeSeries }) => {
  99. return (
  100. <div className="panel-content">
  101. <PanelComponent
  102. loading={loading}
  103. timeSeries={timeSeries}
  104. timeRange={timeRange}
  105. options={panel.getOptions(plugin.exports.PanelDefaults)}
  106. width={width - 2 * variables.panelHorizontalPadding }
  107. height={height - PANEL_HEADER_HEIGHT - variables.panelVerticalPadding}
  108. renderCounter={renderCounter}
  109. />
  110. </div>
  111. );
  112. }}
  113. </DataPanel>
  114. </div>
  115. );
  116. }}
  117. </AutoSizer>
  118. );
  119. }
  120. }