PanelChrome.tsx 3.8 KB

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