PanelChrome.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, TimeRange } from 'app/types';
  16. export interface Props {
  17. panel: PanelModel;
  18. dashboard: DashboardModel;
  19. plugin: PanelPlugin;
  20. }
  21. export interface State {
  22. refreshCounter: number;
  23. renderCounter: number;
  24. timeInfo?: string;
  25. timeRange?: TimeRange;
  26. }
  27. export class PanelChrome extends PureComponent<Props, State> {
  28. timeSrv: TimeSrv = getTimeSrv();
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. refreshCounter: 0,
  33. renderCounter: 0,
  34. };
  35. }
  36. componentDidMount() {
  37. this.props.panel.events.on('refresh', this.onRefresh);
  38. this.props.panel.events.on('render', this.onRender);
  39. this.props.dashboard.panelInitialized(this.props.panel);
  40. }
  41. componentWillUnmount() {
  42. this.props.panel.events.off('refresh', this.onRefresh);
  43. }
  44. onRefresh = () => {
  45. console.log('onRefresh');
  46. if (!this.isVisible) {
  47. return;
  48. }
  49. const { panel } = this.props;
  50. const timeData = applyPanelTimeOverrides(panel, this.timeSrv.timeRange());
  51. this.setState({
  52. refreshCounter: this.state.refreshCounter + 1,
  53. timeRange: timeData.timeRange,
  54. timeInfo: timeData.timeInfo,
  55. });
  56. };
  57. onRender = () => {
  58. this.setState({
  59. renderCounter: this.state.renderCounter + 1,
  60. });
  61. };
  62. get isVisible() {
  63. return !this.props.dashboard.otherPanelInFullscreen(this.props.panel);
  64. }
  65. render() {
  66. const { panel, dashboard, plugin } = this.props;
  67. const { refreshCounter, timeRange, timeInfo, renderCounter } = this.state;
  68. const { datasource, targets } = panel;
  69. const PanelComponent = plugin.exports.Panel;
  70. return (
  71. <AutoSizer>
  72. {({ width, height }) => {
  73. if (width === 0) {
  74. return null;
  75. }
  76. return (
  77. <div className="panel-container panel-container--absolute">
  78. <PanelHeader panel={panel} dashboard={dashboard} timeInfo={timeInfo} />
  79. <DataPanel
  80. datasource={datasource}
  81. queries={targets}
  82. timeRange={timeRange}
  83. isVisible={this.isVisible}
  84. widthPixels={width}
  85. refreshCounter={refreshCounter}
  86. >
  87. {({ loading, timeSeries }) => {
  88. return (
  89. <div className="panel-content">
  90. <PanelComponent
  91. loading={loading}
  92. timeSeries={timeSeries}
  93. timeRange={timeRange}
  94. options={panel.getOptions(plugin.exports.PanelDefaults)}
  95. width={width}
  96. height={height - PANEL_HEADER_HEIGHT}
  97. renderCounter={renderCounter}
  98. />
  99. </div>
  100. );
  101. }}
  102. </DataPanel>
  103. </div>
  104. );
  105. }}
  106. </AutoSizer>
  107. );
  108. }
  109. }