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

158 lines
4.3 KiB
TypeScript
Raw Normal View History

2017-10-09 10:24:10 -05:00
import React from 'react';
import coreModule from 'app/core/core_module';
import ReactGridLayout from 'react-grid-layout';
import {GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT} from 'app/core/constants';
import {DashboardPanel} from './DashboardPanel';
import {DashboardModel} from '../dashboard_model';
2017-10-10 02:34:14 -05:00
import {PanelContainer} from './PanelContainer';
import {PanelModel} from '../panel_model';
import classNames from 'classnames';
2017-10-09 10:24:10 -05:00
import sizeMe from 'react-sizeme';
2017-10-11 14:36:03 -05:00
let lastGridWidth = 1200;
2017-10-09 10:24:10 -05:00
2017-10-11 14:36:03 -05:00
function GridWrapper({size, layout, onLayoutChange, children, onResize, onResizeStop, onWidthChange}) {
2017-10-09 10:24:10 -05:00
if (size.width === 0) {
console.log('size is zero!');
}
2017-10-11 14:36:03 -05:00
const width = size.width > 0 ? size.width : lastGridWidth;
if (width !== lastGridWidth) {
onWidthChange();
lastGridWidth = width;
}
2017-10-09 10:24:10 -05:00
return (
<ReactGridLayout
2017-10-11 14:36:03 -05:00
width={lastGridWidth}
2017-10-09 10:24:10 -05:00
className="layout"
isDraggable={true}
isResizable={true}
measureBeforeMount={false}
containerPadding={[0, 0]}
2017-10-12 12:01:02 -05:00
useCSSTransforms={false}
margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
cols={GRID_COLUMN_COUNT}
rowHeight={GRID_CELL_HEIGHT}
2017-10-09 10:24:10 -05:00
draggableHandle=".grid-drag-handle"
layout={layout}
2017-10-10 10:57:53 -05:00
onResize={onResize}
2017-10-11 14:36:03 -05:00
onResizeStop={onResizeStop}
2017-10-09 10:24:10 -05:00
onLayoutChange={onLayoutChange}>
{children}
</ReactGridLayout>
);
}
const SizedReactLayoutGrid = sizeMe({monitorWidth: true})(GridWrapper);
export interface DashboardGridProps {
2017-10-10 02:34:14 -05:00
getPanelContainer: () => PanelContainer;
2017-10-09 10:24:10 -05:00
}
export class DashboardGrid extends React.Component<DashboardGridProps, any> {
gridToPanelMap: any;
2017-10-10 02:34:14 -05:00
panelContainer: PanelContainer;
2017-10-10 10:57:53 -05:00
dashboard: DashboardModel;
panelMap: {[id: string]: PanelModel};
2017-10-09 10:24:10 -05:00
constructor(props) {
super(props);
2017-10-10 02:34:14 -05:00
this.panelContainer = this.props.getPanelContainer();
2017-10-09 10:24:10 -05:00
this.onLayoutChange = this.onLayoutChange.bind(this);
2017-10-10 10:57:53 -05:00
this.onResize = this.onResize.bind(this);
2017-10-11 14:36:03 -05:00
this.onResizeStop = this.onResizeStop.bind(this);
this.onWidthChange = this.onWidthChange.bind(this);
2017-10-10 10:57:53 -05:00
// subscribe to dashboard events
this.dashboard = this.panelContainer.getDashboard();
2017-10-11 09:32:05 -05:00
this.dashboard.on('panel-added', this.triggerForceUpdate.bind(this));
2017-10-12 12:01:02 -05:00
this.dashboard.on('panel-removed', this.triggerForceUpdate.bind(this));
2017-10-12 14:37:27 -05:00
this.dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this));
2017-10-11 09:32:05 -05:00
this.dashboard.on('view-mode-changed', this.triggerForceUpdate.bind(this));
2017-10-09 10:24:10 -05:00
}
buildLayout() {
const layout = [];
this.panelMap = {};
2017-10-10 02:34:14 -05:00
2017-10-10 10:57:53 -05:00
for (let panel of this.dashboard.panels) {
let stringId = panel.id.toString();
this.panelMap[stringId] = panel;
if (!panel.gridPos) {
console.log('panel without gridpos');
continue;
}
2017-10-09 10:24:10 -05:00
layout.push({
i: stringId,
x: panel.gridPos.x,
y: panel.gridPos.y,
w: panel.gridPos.w,
h: panel.gridPos.h,
2017-10-16 02:55:55 -05:00
static: panel.gridPos.static,
2017-10-09 10:24:10 -05:00
});
}
2017-10-10 02:34:14 -05:00
2017-10-09 10:24:10 -05:00
return layout;
}
onLayoutChange(newLayout) {
for (const newPos of newLayout) {
this.panelMap[newPos.i].updateGridPos(newPos);
}
}
2017-10-09 10:24:10 -05:00
2017-10-11 09:32:05 -05:00
triggerForceUpdate() {
2017-10-10 10:57:53 -05:00
this.forceUpdate();
}
2017-10-11 14:36:03 -05:00
onWidthChange() {
console.log('width change');
for (const panel of this.dashboard.panels) {
panel.resizeDone();
}
}
2017-10-10 10:57:53 -05:00
onResize(layout, oldItem, newItem) {
this.panelMap[newItem.i].updateGridPos(newItem);
}
2017-10-11 14:36:03 -05:00
onResizeStop(layout, oldItem, newItem) {
this.panelMap[newItem.i].resizeDone();
}
2017-10-09 10:24:10 -05:00
renderPanels() {
const panelElements = [];
2017-10-10 02:34:14 -05:00
2017-10-10 10:57:53 -05:00
for (let panel of this.dashboard.panels) {
const panelClasses = classNames({panel: true, 'panel--fullscreen': panel.fullscreen});
2017-10-09 10:24:10 -05:00
panelElements.push(
<div key={panel.id.toString()} className={panelClasses}>
<DashboardPanel panel={panel} getPanelContainer={this.props.getPanelContainer} />
2017-10-09 10:24:10 -05:00
</div>,
);
}
2017-10-10 02:34:14 -05:00
2017-10-09 10:24:10 -05:00
return panelElements;
}
render() {
return (
2017-10-11 14:36:03 -05:00
<SizedReactLayoutGrid
layout={this.buildLayout()}
onLayoutChange={this.onLayoutChange}
onWidthChange={this.onWidthChange}
onResize={this.onResize}
onResizeStop={this.onResizeStop}>
2017-10-09 10:24:10 -05:00
{this.renderPanels()}
</SizedReactLayoutGrid>
);
}
}
coreModule.directive('dashboardGrid', function(reactDirective) {
2017-10-10 02:34:14 -05:00
return reactDirective(DashboardGrid, [['getPanelContainer', {watchDepth: 'reference', wrapApply: false}]]);
2017-10-09 10:24:10 -05:00
});