Commit 1230f3e4 by Johannes Schill

chore: Fix typings and remove bindings for arrow functions in DashboardGrid

parent a21f6777
import React from 'react'; import React from 'react';
import { hot } from 'react-hot-loader'; import { hot } from 'react-hot-loader';
import ReactGridLayout from 'react-grid-layout'; import ReactGridLayout, { ItemCallback } from 'react-grid-layout';
import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants'; import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants';
import { DashboardPanel } from './DashboardPanel'; import { DashboardPanel } from './DashboardPanel';
import { DashboardModel } from '../dashboard_model'; import { DashboardModel } from '../dashboard_model';
...@@ -11,6 +11,21 @@ import sizeMe from 'react-sizeme'; ...@@ -11,6 +11,21 @@ import sizeMe from 'react-sizeme';
let lastGridWidth = 1200; let lastGridWidth = 1200;
let ignoreNextWidthChange = false; let ignoreNextWidthChange = false;
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;
}
function GridWrapper({ function GridWrapper({
size, size,
layout, layout,
...@@ -24,7 +39,7 @@ function GridWrapper({ ...@@ -24,7 +39,7 @@ function GridWrapper({
isResizable, isResizable,
isDraggable, isDraggable,
isFullscreen, isFullscreen,
}) { }: GridWrapperProps) {
const width = size.width > 0 ? size.width : lastGridWidth; const width = size.width > 0 ? size.width : lastGridWidth;
// logic to ignore width changes (optimization) // logic to ignore width changes (optimization)
...@@ -43,7 +58,6 @@ function GridWrapper({ ...@@ -43,7 +58,6 @@ function GridWrapper({
className={className} className={className}
isDraggable={isDraggable} isDraggable={isDraggable}
isResizable={isResizable} isResizable={isResizable}
measureBeforeMount={false}
containerPadding={[0, 0]} containerPadding={[0, 0]}
useCSSTransforms={false} useCSSTransforms={false}
margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]} margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
...@@ -71,22 +85,17 @@ export class DashboardGrid extends React.Component<DashboardGridProps> { ...@@ -71,22 +85,17 @@ export class DashboardGrid extends React.Component<DashboardGridProps> {
gridToPanelMap: any; gridToPanelMap: any;
panelMap: { [id: string]: PanelModel }; panelMap: { [id: string]: PanelModel };
constructor(props) { constructor(props: DashboardGridProps) {
super(props); super(props);
this.onLayoutChange = this.onLayoutChange.bind(this);
this.onResize = this.onResize.bind(this);
this.onResizeStop = this.onResizeStop.bind(this);
this.onDragStop = this.onDragStop.bind(this);
this.onWidthChange = this.onWidthChange.bind(this);
// subscribe to dashboard events // subscribe to dashboard events
const dashboard = this.props.dashboard; const dashboard = this.props.dashboard;
dashboard.on('panel-added', this.triggerForceUpdate.bind(this)); dashboard.on('panel-added', this.triggerForceUpdate);
dashboard.on('panel-removed', this.triggerForceUpdate.bind(this)); dashboard.on('panel-removed', this.triggerForceUpdate);
dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this)); dashboard.on('repeats-processed', this.triggerForceUpdate);
dashboard.on('view-mode-changed', this.onViewModeChanged.bind(this)); dashboard.on('view-mode-changed', this.onViewModeChanged);
dashboard.on('row-collapsed', this.triggerForceUpdate.bind(this)); dashboard.on('row-collapsed', this.triggerForceUpdate);
dashboard.on('row-expanded', this.triggerForceUpdate.bind(this)); dashboard.on('row-expanded', this.triggerForceUpdate);
} }
buildLayout() { buildLayout() {
...@@ -123,7 +132,7 @@ export class DashboardGrid extends React.Component<DashboardGridProps> { ...@@ -123,7 +132,7 @@ export class DashboardGrid extends React.Component<DashboardGridProps> {
return layout; return layout;
} }
onLayoutChange(newLayout) { onLayoutChange = (newLayout: ReactGridLayout.Layout[]) => {
for (const newPos of newLayout) { for (const newPos of newLayout) {
this.panelMap[newPos.i].updateGridPos(newPos); this.panelMap[newPos.i].updateGridPos(newPos);
} }
...@@ -131,22 +140,22 @@ export class DashboardGrid extends React.Component<DashboardGridProps> { ...@@ -131,22 +140,22 @@ export class DashboardGrid extends React.Component<DashboardGridProps> {
this.props.dashboard.sortPanelsByGridPos(); this.props.dashboard.sortPanelsByGridPos();
} }
triggerForceUpdate() { triggerForceUpdate = () => {
this.forceUpdate(); this.forceUpdate();
} }
onWidthChange() { onWidthChange = () => {
for (const panel of this.props.dashboard.panels) { for (const panel of this.props.dashboard.panels) {
panel.resizeDone(); panel.resizeDone();
} }
} }
onViewModeChanged(payload) { onViewModeChanged = () => {
ignoreNextWidthChange = true; ignoreNextWidthChange = true;
this.forceUpdate(); this.forceUpdate();
} }
updateGridPos(item, layout) { updateGridPos = (item: ReactGridLayout.Layout, layout: ReactGridLayout.Layout[]) => {
this.panelMap[item.i].updateGridPos(item); this.panelMap[item.i].updateGridPos(item);
// react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted. // react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
...@@ -154,16 +163,17 @@ export class DashboardGrid extends React.Component<DashboardGridProps> { ...@@ -154,16 +163,17 @@ export class DashboardGrid extends React.Component<DashboardGridProps> {
this.onLayoutChange(layout); this.onLayoutChange(layout);
} }
onResize(layout, oldItem, newItem) { onResize: ItemCallback = (layout, oldItem, newItem) => {
console.log();
this.panelMap[newItem.i].updateGridPos(newItem); this.panelMap[newItem.i].updateGridPos(newItem);
} }
onResizeStop(layout, oldItem, newItem) { onResizeStop: ItemCallback = (layout, oldItem, newItem) => {
this.updateGridPos(newItem, layout); this.updateGridPos(newItem, layout);
this.panelMap[newItem.i].resizeDone(); this.panelMap[newItem.i].resizeDone();
} }
onDragStop(layout, oldItem, newItem) { onDragStop: ItemCallback = (layout, oldItem, newItem) => {
this.updateGridPos(newItem, layout); this.updateGridPos(newItem, layout);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment