2017-10-09 10:24:10 -05:00
|
|
|
import React from 'react';
|
2018-11-23 08:01:36 -06:00
|
|
|
import { hot } from 'react-hot-loader';
|
2019-01-30 08:28:41 -06:00
|
|
|
import ReactGridLayout, { ItemCallback } from 'react-grid-layout';
|
2017-12-15 09:23:26 -06:00
|
|
|
import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants';
|
|
|
|
import { DashboardPanel } from './DashboardPanel';
|
|
|
|
import { DashboardModel } from '../dashboard_model';
|
|
|
|
import { PanelModel } from '../panel_model';
|
2017-10-11 04:42:49 -05:00
|
|
|
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;
|
2018-11-10 09:34:09 -06:00
|
|
|
let ignoreNextWidthChange = false;
|
2017-10-09 10:24:10 -05:00
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
interface GridWrapperProps {
|
|
|
|
size: { width: number; };
|
|
|
|
layout: ReactGridLayout.Layout[];
|
|
|
|
onLayoutChange: (layout: ReactGridLayout.Layout[]) => void;
|
|
|
|
children: JSX.Element | JSX.Element[];
|
|
|
|
onDragStop: ItemCallback;
|
|
|
|
onResize: ItemCallback;
|
|
|
|
onResizeStop: ItemCallback;
|
|
|
|
onWidthChange: () => void;
|
|
|
|
className: string;
|
|
|
|
isResizable?: boolean;
|
|
|
|
isDraggable?: boolean;
|
|
|
|
isFullscreen?: boolean;
|
|
|
|
}
|
|
|
|
|
2017-12-15 09:23:26 -06:00
|
|
|
function GridWrapper({
|
|
|
|
size,
|
|
|
|
layout,
|
|
|
|
onLayoutChange,
|
|
|
|
children,
|
|
|
|
onDragStop,
|
|
|
|
onResize,
|
|
|
|
onResizeStop,
|
|
|
|
onWidthChange,
|
|
|
|
className,
|
|
|
|
isResizable,
|
|
|
|
isDraggable,
|
2018-10-27 09:54:04 -05:00
|
|
|
isFullscreen,
|
2019-01-30 08:28:41 -06:00
|
|
|
}: GridWrapperProps) {
|
2017-12-08 04:53:51 -06:00
|
|
|
const width = size.width > 0 ? size.width : lastGridWidth;
|
2018-11-10 09:34:09 -06:00
|
|
|
|
|
|
|
// logic to ignore width changes (optimization)
|
2017-12-08 04:53:51 -06:00
|
|
|
if (width !== lastGridWidth) {
|
2018-11-10 09:34:09 -06:00
|
|
|
if (ignoreNextWidthChange) {
|
|
|
|
ignoreNextWidthChange = false;
|
|
|
|
} else if (!isFullscreen && Math.abs(width - lastGridWidth) > 8) {
|
2018-10-27 09:54:04 -05:00
|
|
|
onWidthChange();
|
|
|
|
lastGridWidth = width;
|
|
|
|
}
|
2017-10-11 14:36:03 -05:00
|
|
|
}
|
2017-10-09 10:24:10 -05:00
|
|
|
|
2017-12-08 04:53:51 -06:00
|
|
|
return (
|
|
|
|
<ReactGridLayout
|
|
|
|
width={lastGridWidth}
|
2017-12-08 05:17:09 -06:00
|
|
|
className={className}
|
2017-12-15 09:23:26 -06:00
|
|
|
isDraggable={isDraggable}
|
|
|
|
isResizable={isResizable}
|
2017-12-08 04:53:51 -06:00
|
|
|
containerPadding={[0, 0]}
|
2018-12-14 13:53:42 -06:00
|
|
|
useCSSTransforms={false}
|
2017-12-08 04:53:51 -06:00
|
|
|
margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
|
|
|
|
cols={GRID_COLUMN_COUNT}
|
|
|
|
rowHeight={GRID_CELL_HEIGHT}
|
|
|
|
draggableHandle=".grid-drag-handle"
|
|
|
|
layout={layout}
|
|
|
|
onResize={onResize}
|
|
|
|
onResizeStop={onResizeStop}
|
2017-12-13 10:53:57 -06:00
|
|
|
onDragStop={onDragStop}
|
2018-01-03 06:33:54 -06:00
|
|
|
onLayoutChange={onLayoutChange}
|
|
|
|
>
|
2017-12-08 04:53:51 -06:00
|
|
|
{children}
|
|
|
|
</ReactGridLayout>
|
|
|
|
);
|
2017-10-09 10:24:10 -05:00
|
|
|
}
|
|
|
|
|
2017-12-15 09:23:26 -06:00
|
|
|
const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
|
2017-10-09 10:24:10 -05:00
|
|
|
|
|
|
|
export interface DashboardGridProps {
|
2018-06-19 14:25:57 -05:00
|
|
|
dashboard: DashboardModel;
|
2017-10-09 10:24:10 -05:00
|
|
|
}
|
|
|
|
|
2018-12-14 13:53:42 -06:00
|
|
|
export class DashboardGrid extends React.Component<DashboardGridProps> {
|
2017-10-09 10:24:10 -05:00
|
|
|
gridToPanelMap: any;
|
2017-12-15 09:23:26 -06:00
|
|
|
panelMap: { [id: string]: PanelModel };
|
2017-10-09 10:24:10 -05:00
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
constructor(props: DashboardGridProps) {
|
2017-10-09 10:24:10 -05:00
|
|
|
super(props);
|
2017-10-10 10:57:53 -05:00
|
|
|
|
|
|
|
// subscribe to dashboard events
|
2018-09-10 08:42:36 -05:00
|
|
|
const dashboard = this.props.dashboard;
|
2019-01-30 08:28:41 -06:00
|
|
|
dashboard.on('panel-added', this.triggerForceUpdate);
|
|
|
|
dashboard.on('panel-removed', this.triggerForceUpdate);
|
|
|
|
dashboard.on('repeats-processed', this.triggerForceUpdate);
|
|
|
|
dashboard.on('view-mode-changed', this.onViewModeChanged);
|
|
|
|
dashboard.on('row-collapsed', this.triggerForceUpdate);
|
|
|
|
dashboard.on('row-expanded', this.triggerForceUpdate);
|
2017-10-09 10:24:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
buildLayout() {
|
|
|
|
const layout = [];
|
2017-10-10 07:20:53 -05:00
|
|
|
this.panelMap = {};
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2018-09-10 08:42:36 -05:00
|
|
|
for (const panel of this.props.dashboard.panels) {
|
2018-08-26 10:14:40 -05:00
|
|
|
const stringId = panel.id.toString();
|
2017-10-10 07:20:53 -05:00
|
|
|
this.panelMap[stringId] = panel;
|
|
|
|
|
|
|
|
if (!panel.gridPos) {
|
|
|
|
console.log('panel without gridpos');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-26 10:14:40 -05:00
|
|
|
const panelPos: any = {
|
2017-10-10 07:20:53 -05:00
|
|
|
i: stringId,
|
|
|
|
x: panel.gridPos.x,
|
|
|
|
y: panel.gridPos.y,
|
|
|
|
w: panel.gridPos.w,
|
|
|
|
h: panel.gridPos.h,
|
2017-10-16 09:09:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (panel.type === 'row') {
|
|
|
|
panelPos.w = GRID_COLUMN_COUNT;
|
|
|
|
panelPos.h = 1;
|
|
|
|
panelPos.isResizable = false;
|
2017-10-17 07:53:52 -05:00
|
|
|
panelPos.isDraggable = panel.collapsed;
|
2017-10-16 09:09:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
layout.push(panelPos);
|
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;
|
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
onLayoutChange = (newLayout: ReactGridLayout.Layout[]) => {
|
2017-10-10 07:20:53 -05:00
|
|
|
for (const newPos of newLayout) {
|
|
|
|
this.panelMap[newPos.i].updateGridPos(newPos);
|
|
|
|
}
|
2017-10-24 05:33:14 -05:00
|
|
|
|
2018-06-19 14:25:57 -05:00
|
|
|
this.props.dashboard.sortPanelsByGridPos();
|
2017-10-10 07:20:53 -05:00
|
|
|
}
|
2017-10-09 10:24:10 -05:00
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
triggerForceUpdate = () => {
|
2017-10-10 10:57:53 -05:00
|
|
|
this.forceUpdate();
|
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
onWidthChange = () => {
|
2018-06-19 14:25:57 -05:00
|
|
|
for (const panel of this.props.dashboard.panels) {
|
2017-10-11 14:36:03 -05:00
|
|
|
panel.resizeDone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
onViewModeChanged = () => {
|
2018-11-10 09:34:09 -06:00
|
|
|
ignoreNextWidthChange = true;
|
2018-12-14 13:53:42 -06:00
|
|
|
this.forceUpdate();
|
2018-06-19 09:57:55 -05:00
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
updateGridPos = (item: ReactGridLayout.Layout, layout: ReactGridLayout.Layout[]) => {
|
2017-12-13 10:53:57 -06:00
|
|
|
this.panelMap[item.i].updateGridPos(item);
|
|
|
|
|
|
|
|
// react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
|
|
|
|
// So it's required to call it explicitly when panel resized or moved to save layout changes.
|
|
|
|
this.onLayoutChange(layout);
|
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
onResize: ItemCallback = (layout, oldItem, newItem) => {
|
|
|
|
console.log();
|
2017-10-10 10:57:53 -05:00
|
|
|
this.panelMap[newItem.i].updateGridPos(newItem);
|
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
onResizeStop: ItemCallback = (layout, oldItem, newItem) => {
|
2017-12-13 10:53:57 -06:00
|
|
|
this.updateGridPos(newItem, layout);
|
2017-10-11 14:36:03 -05:00
|
|
|
this.panelMap[newItem.i].resizeDone();
|
|
|
|
}
|
|
|
|
|
2019-01-30 08:28:41 -06:00
|
|
|
onDragStop: ItemCallback = (layout, oldItem, newItem) => {
|
2017-12-13 10:53:57 -06:00
|
|
|
this.updateGridPos(newItem, layout);
|
|
|
|
}
|
|
|
|
|
2017-10-09 10:24:10 -05:00
|
|
|
renderPanels() {
|
|
|
|
const panelElements = [];
|
2017-10-10 02:34:14 -05:00
|
|
|
|
2018-09-10 08:42:36 -05:00
|
|
|
for (const panel of this.props.dashboard.panels) {
|
2018-11-20 09:00:19 -06:00
|
|
|
const panelClasses = classNames({ 'react-grid-item--fullscreen': panel.fullscreen });
|
2017-10-09 10:24:10 -05:00
|
|
|
panelElements.push(
|
2018-09-10 08:42:36 -05:00
|
|
|
<div key={panel.id.toString()} className={panelClasses} id={`panel-${panel.id}`}>
|
2018-11-07 09:03:33 -06:00
|
|
|
<DashboardPanel
|
|
|
|
panel={panel}
|
|
|
|
dashboard={this.props.dashboard}
|
|
|
|
isEditing={panel.isEditing}
|
|
|
|
isFullscreen={panel.fullscreen}
|
|
|
|
/>
|
2018-01-03 06:33:54 -06:00
|
|
|
</div>
|
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 panelElements;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2017-10-11 14:36:03 -05:00
|
|
|
<SizedReactLayoutGrid
|
2018-12-14 13:53:42 -06:00
|
|
|
className={classNames({ layout: true })}
|
2017-10-11 14:36:03 -05:00
|
|
|
layout={this.buildLayout()}
|
2018-06-19 14:25:57 -05:00
|
|
|
isResizable={this.props.dashboard.meta.canEdit}
|
|
|
|
isDraggable={this.props.dashboard.meta.canEdit}
|
2017-10-11 14:36:03 -05:00
|
|
|
onLayoutChange={this.onLayoutChange}
|
|
|
|
onWidthChange={this.onWidthChange}
|
2017-12-13 10:53:57 -06:00
|
|
|
onDragStop={this.onDragStop}
|
2017-10-11 14:36:03 -05:00
|
|
|
onResize={this.onResize}
|
2018-01-03 06:33:54 -06:00
|
|
|
onResizeStop={this.onResizeStop}
|
2018-10-27 09:54:04 -05:00
|
|
|
isFullscreen={this.props.dashboard.meta.fullscreen}
|
2018-01-03 06:33:54 -06:00
|
|
|
>
|
2017-10-09 10:24:10 -05:00
|
|
|
{this.renderPanels()}
|
|
|
|
</SizedReactLayoutGrid>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-11-23 08:01:36 -06:00
|
|
|
|
|
|
|
export default hot(module)(DashboardGrid);
|