PanelChrome.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, transparent } = panel;
  69. const PanelComponent = plugin.exports.Panel;
  70. const containerClassNames = `panel-container panel-container--absolute ${transparent ? 'panel-transparent' : ''}`;
  71. return (
  72. <AutoSizer>
  73. {({ width, height }) => {
  74. if (width === 0) {
  75. return null;
  76. }
  77. return (
  78. <div className={containerClassNames}>
  79. <PanelHeader
  80. panel={panel}
  81. dashboard={dashboard}
  82. timeInfo={timeInfo}
  83. title={panel.title}
  84. description={panel.description}
  85. scopedVars={panel.scopedVars}
  86. links={panel.links}
  87. />
  88. <DataPanel
  89. datasource={datasource}
  90. queries={targets}
  91. timeRange={timeRange}
  92. isVisible={this.isVisible}
  93. widthPixels={width}
  94. refreshCounter={refreshCounter}
  95. >
  96. {({ loading, timeSeries }) => {
  97. return (
  98. <div className="panel-content">
  99. <PanelComponent
  100. loading={loading}
  101. timeSeries={timeSeries}
  102. timeRange={timeRange}
  103. options={panel.getOptions(plugin.exports.PanelDefaults)}
  104. width={width}
  105. height={height - PANEL_HEADER_HEIGHT}
  106. renderCounter={renderCounter}
  107. />
  108. </div>
  109. );
  110. }}
  111. </DataPanel>
  112. </div>
  113. );
  114. }}
  115. </AutoSizer>
  116. );
  117. }
  118. }