grafana/public/app/features/dashboard/dashgrid/PanelChrome.tsx

167 lines
4.6 KiB
TypeScript
Raw Normal View History

// Libraries
2018-11-20 10:01:58 -06:00
import React, { PureComponent } from 'react';
2018-11-13 11:50:12 -06:00
import { AutoSizer } from 'react-virtualized';
// Services
import { getTimeSrv, TimeSrv } from '../time_srv';
// Components
import { PanelHeader } from './PanelHeader/PanelHeader';
import { DataPanel } from './DataPanel';
// Utils
import { applyPanelTimeOverrides } from 'app/features/dashboard/utils/panel';
2018-11-14 06:20:19 -06:00
import { PANEL_HEADER_HEIGHT } from 'app/core/constants';
// Types
2018-06-19 07:51:57 -05:00
import { PanelModel } from '../panel_model';
import { DashboardModel } from '../dashboard_model';
import { PanelPlugin } from 'app/types';
import { TimeRange } from '@grafana/ui';
2018-06-19 07:51:57 -05:00
import variables from 'sass/_variables.scss';
2019-01-15 10:15:46 -06:00
import templateSrv from 'app/features/templating/template_srv';
2019-01-30 03:39:42 -06:00
import { DataQueryResponse } from '@grafana/ui/src';
export interface Props {
2018-06-19 07:51:57 -05:00
panel: PanelModel;
dashboard: DashboardModel;
2018-11-20 10:01:58 -06:00
plugin: PanelPlugin;
2018-06-19 07:51:57 -05:00
}
export interface State {
refreshCounter: number;
2018-11-05 10:46:09 -06:00
renderCounter: number;
timeInfo?: string;
timeRange?: TimeRange;
}
export class PanelChrome extends PureComponent<Props, State> {
timeSrv: TimeSrv = getTimeSrv();
2018-06-19 07:51:57 -05:00
constructor(props) {
super(props);
this.state = {
refreshCounter: 0,
2018-11-05 10:46:09 -06:00
renderCounter: 0,
};
}
componentDidMount() {
this.props.panel.events.on('refresh', this.onRefresh);
2018-11-05 10:46:09 -06:00
this.props.panel.events.on('render', this.onRender);
2018-10-14 09:31:20 -05:00
this.props.dashboard.panelInitialized(this.props.panel);
}
componentWillUnmount() {
this.props.panel.events.off('refresh', this.onRefresh);
}
onRefresh = () => {
console.log('onRefresh');
if (!this.isVisible) {
return;
}
const { panel } = this.props;
const timeData = applyPanelTimeOverrides(panel, this.timeSrv.timeRange());
this.setState({
refreshCounter: this.state.refreshCounter + 1,
timeRange: timeData.timeRange,
timeInfo: timeData.timeInfo,
});
};
2018-11-05 10:46:09 -06:00
onRender = () => {
this.setState({
renderCounter: this.state.renderCounter + 1,
});
};
2019-01-15 10:15:46 -06:00
onInterpolate = (value: string, format?: string) => {
return templateSrv.replace(value, this.props.panel.scopedVars, format);
};
2019-01-30 03:39:42 -06:00
onDataResponse = (dataQueryResponse: DataQueryResponse) => {
if (this.props.dashboard.isSnapshot()) {
this.props.panel.snapshotData = dataQueryResponse;
}
};
get isVisible() {
2018-10-14 09:31:20 -05:00
return !this.props.dashboard.otherPanelInFullscreen(this.props.panel);
}
2019-01-30 03:39:42 -06:00
renderPanel(loading, timeSeries, width, height): JSX.Element {
const { panel, plugin } = this.props;
const { timeRange, renderCounter } = this.state;
const PanelComponent = plugin.exports.Panel;
return (
<div className="panel-content">
<PanelComponent
loading={loading}
timeSeries={timeSeries}
timeRange={timeRange}
options={panel.getOptions(plugin.exports.PanelDefaults)}
width={width - 2 * variables.panelHorizontalPadding}
height={height - PANEL_HEADER_HEIGHT - variables.panelVerticalPadding}
renderCounter={renderCounter}
onInterpolate={this.onInterpolate}
/>
</div>
);
}
render() {
2019-01-30 03:39:42 -06:00
const { panel, dashboard } = this.props;
const { refreshCounter, timeRange, timeInfo } = this.state;
2018-12-06 03:34:27 -06:00
const { datasource, targets, transparent } = panel;
const containerClassNames = `panel-container panel-container--absolute ${transparent ? 'panel-transparent' : ''}`;
2018-06-19 07:51:57 -05:00
return (
<AutoSizer>
{({ width, height }) => {
if (width === 0) {
return null;
}
return (
2018-12-06 03:34:27 -06:00
<div className={containerClassNames}>
<PanelHeader
panel={panel}
dashboard={dashboard}
timeInfo={timeInfo}
title={panel.title}
description={panel.description}
scopedVars={panel.scopedVars}
links={panel.links}
/>
2019-01-30 03:39:42 -06:00
{panel.snapshotData ? (
this.renderPanel(false, panel.snapshotData, width, height)
) : (
<DataPanel
datasource={datasource}
queries={targets}
timeRange={timeRange}
isVisible={this.isVisible}
widthPixels={width}
refreshCounter={refreshCounter}
onDataResponse={this.onDataResponse}
>
{({ loading, timeSeries }) => {
return this.renderPanel(loading, timeSeries, width, height);
}}
</DataPanel>
)}
</div>
);
}}
</AutoSizer>
2018-06-19 07:51:57 -05:00
);
}
}