DashboardGrid.tsx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Libaries
  2. import React, { PureComponent } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import ReactGridLayout, { ItemCallback } from 'react-grid-layout';
  5. import classNames from 'classnames';
  6. // @ts-ignore
  7. import sizeMe from 'react-sizeme';
  8. // Types
  9. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants';
  10. import { DashboardPanel } from './DashboardPanel';
  11. import { DashboardModel, PanelModel } from '../state';
  12. let lastGridWidth = 1200;
  13. let ignoreNextWidthChange = false;
  14. interface GridWrapperProps {
  15. size: { width: number };
  16. layout: ReactGridLayout.Layout[];
  17. onLayoutChange: (layout: ReactGridLayout.Layout[]) => void;
  18. children: JSX.Element | JSX.Element[];
  19. onDragStop: ItemCallback;
  20. onResize: ItemCallback;
  21. onResizeStop: ItemCallback;
  22. onWidthChange: () => void;
  23. className: string;
  24. isResizable?: boolean;
  25. isDraggable?: boolean;
  26. isFullscreen?: boolean;
  27. }
  28. function GridWrapper({
  29. size,
  30. layout,
  31. onLayoutChange,
  32. children,
  33. onDragStop,
  34. onResize,
  35. onResizeStop,
  36. onWidthChange,
  37. className,
  38. isResizable,
  39. isDraggable,
  40. isFullscreen,
  41. }: GridWrapperProps) {
  42. const width = size.width > 0 ? size.width : lastGridWidth;
  43. // logic to ignore width changes (optimization)
  44. if (width !== lastGridWidth) {
  45. if (ignoreNextWidthChange) {
  46. ignoreNextWidthChange = false;
  47. } else if (!isFullscreen && Math.abs(width - lastGridWidth) > 8) {
  48. onWidthChange();
  49. lastGridWidth = width;
  50. }
  51. }
  52. return (
  53. <ReactGridLayout
  54. width={lastGridWidth}
  55. className={className}
  56. isDraggable={isDraggable}
  57. isResizable={isResizable}
  58. containerPadding={[0, 0]}
  59. useCSSTransforms={false}
  60. margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
  61. cols={GRID_COLUMN_COUNT}
  62. rowHeight={GRID_CELL_HEIGHT}
  63. draggableHandle=".grid-drag-handle"
  64. layout={layout}
  65. onResize={onResize}
  66. onResizeStop={onResizeStop}
  67. onDragStop={onDragStop}
  68. onLayoutChange={onLayoutChange}
  69. >
  70. {children}
  71. </ReactGridLayout>
  72. );
  73. }
  74. const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
  75. export interface Props {
  76. dashboard: DashboardModel;
  77. isEditing: boolean;
  78. isFullscreen: boolean;
  79. scrollTop: number;
  80. }
  81. export class DashboardGrid extends PureComponent<Props> {
  82. panelMap: { [id: string]: PanelModel };
  83. panelRef: { [id: string]: HTMLElement } = {};
  84. componentDidMount() {
  85. const { dashboard } = this.props;
  86. dashboard.on('panel-added', this.triggerForceUpdate);
  87. dashboard.on('panel-removed', this.triggerForceUpdate);
  88. dashboard.on('repeats-processed', this.triggerForceUpdate);
  89. dashboard.on('view-mode-changed', this.onViewModeChanged);
  90. dashboard.on('row-collapsed', this.triggerForceUpdate);
  91. dashboard.on('row-expanded', this.triggerForceUpdate);
  92. }
  93. componentWillUnmount() {
  94. const { dashboard } = this.props;
  95. dashboard.off('panel-added', this.triggerForceUpdate);
  96. dashboard.off('panel-removed', this.triggerForceUpdate);
  97. dashboard.off('repeats-processed', this.triggerForceUpdate);
  98. dashboard.off('view-mode-changed', this.onViewModeChanged);
  99. dashboard.off('row-collapsed', this.triggerForceUpdate);
  100. dashboard.off('row-expanded', this.triggerForceUpdate);
  101. }
  102. buildLayout() {
  103. const layout = [];
  104. this.panelMap = {};
  105. for (const panel of this.props.dashboard.panels) {
  106. const stringId = panel.id.toString();
  107. this.panelMap[stringId] = panel;
  108. if (!panel.gridPos) {
  109. console.log('panel without gridpos');
  110. continue;
  111. }
  112. const panelPos: any = {
  113. i: stringId,
  114. x: panel.gridPos.x,
  115. y: panel.gridPos.y,
  116. w: panel.gridPos.w,
  117. h: panel.gridPos.h,
  118. };
  119. if (panel.type === 'row') {
  120. panelPos.w = GRID_COLUMN_COUNT;
  121. panelPos.h = 1;
  122. panelPos.isResizable = false;
  123. panelPos.isDraggable = panel.collapsed;
  124. }
  125. layout.push(panelPos);
  126. }
  127. return layout;
  128. }
  129. onLayoutChange = (newLayout: ReactGridLayout.Layout[]) => {
  130. for (const newPos of newLayout) {
  131. this.panelMap[newPos.i].updateGridPos(newPos);
  132. }
  133. this.props.dashboard.sortPanelsByGridPos();
  134. // Call render() after any changes. This is called when the layour loads
  135. this.forceUpdate();
  136. };
  137. triggerForceUpdate = () => {
  138. this.forceUpdate();
  139. };
  140. onWidthChange = () => {
  141. for (const panel of this.props.dashboard.panels) {
  142. panel.resizeDone();
  143. }
  144. this.forceUpdate();
  145. };
  146. onViewModeChanged = () => {
  147. ignoreNextWidthChange = true;
  148. };
  149. updateGridPos = (item: ReactGridLayout.Layout, layout: ReactGridLayout.Layout[]) => {
  150. this.panelMap[item.i].updateGridPos(item);
  151. // react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
  152. // So it's required to call it explicitly when panel resized or moved to save layout changes.
  153. this.onLayoutChange(layout);
  154. };
  155. onResize: ItemCallback = (layout, oldItem, newItem) => {
  156. this.panelMap[newItem.i].updateGridPos(newItem);
  157. };
  158. onResizeStop: ItemCallback = (layout, oldItem, newItem) => {
  159. this.updateGridPos(newItem, layout);
  160. this.panelMap[newItem.i].resizeDone();
  161. };
  162. onDragStop: ItemCallback = (layout, oldItem, newItem) => {
  163. this.updateGridPos(newItem, layout);
  164. };
  165. isInView = (panel: PanelModel): boolean => {
  166. if (panel.fullscreen || panel.isEditing) {
  167. return true;
  168. }
  169. // elem is set *after* the first render
  170. const elem = this.panelRef[panel.id.toString()];
  171. if (!elem) {
  172. // NOTE the gridPos is also not valid until after the first render
  173. // since it is passed to the layout engine and made to be valid
  174. // for example, you can have Y=0 for everything and it will stack them
  175. // down vertically in the second call
  176. return false;
  177. }
  178. const top = elem.offsetTop;
  179. const height = panel.gridPos.h * GRID_CELL_HEIGHT + 40;
  180. const bottom = top + height;
  181. // Show things that are almost in the view
  182. const buffer = 250;
  183. const viewTop = this.props.scrollTop;
  184. if (viewTop > bottom + buffer) {
  185. return false; // The panel is above the viewport
  186. }
  187. // Use the whole browser height (larger than real value)
  188. // TODO? is there a better way
  189. const viewHeight = isNaN(window.innerHeight) ? (window as any).clientHeight : window.innerHeight;
  190. const viewBot = viewTop + viewHeight;
  191. if (top > viewBot + buffer) {
  192. return false;
  193. }
  194. return !this.props.dashboard.otherPanelInFullscreen(panel);
  195. };
  196. renderPanels() {
  197. const panelElements = [];
  198. for (const panel of this.props.dashboard.panels) {
  199. const panelClasses = classNames({ 'react-grid-item--fullscreen': panel.fullscreen });
  200. const id = panel.id.toString();
  201. panel.isInView = this.isInView(panel);
  202. panelElements.push(
  203. <div
  204. key={id}
  205. className={panelClasses}
  206. id={'panel-' + id}
  207. ref={elem => {
  208. this.panelRef[id] = elem;
  209. }}
  210. >
  211. <DashboardPanel
  212. panel={panel}
  213. dashboard={this.props.dashboard}
  214. isEditing={panel.isEditing}
  215. isFullscreen={panel.fullscreen}
  216. isInView={panel.isInView}
  217. />
  218. </div>
  219. );
  220. }
  221. return panelElements;
  222. }
  223. render() {
  224. const { dashboard, isFullscreen } = this.props;
  225. return (
  226. <SizedReactLayoutGrid
  227. className={classNames({ layout: true })}
  228. layout={this.buildLayout()}
  229. isResizable={dashboard.meta.canEdit}
  230. isDraggable={dashboard.meta.canEdit}
  231. onLayoutChange={this.onLayoutChange}
  232. onWidthChange={this.onWidthChange}
  233. onDragStop={this.onDragStop}
  234. onResize={this.onResize}
  235. onResizeStop={this.onResizeStop}
  236. isFullscreen={isFullscreen}
  237. >
  238. {this.renderPanels()}
  239. </SizedReactLayoutGrid>
  240. );
  241. }
  242. }
  243. export default hot(module)(DashboardGrid);