DashboardGrid.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import React from 'react';
  2. import { hot } from 'react-hot-loader';
  3. import ReactGridLayout from 'react-grid-layout';
  4. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants';
  5. import { DashboardPanel } from './DashboardPanel';
  6. import { DashboardModel } from '../dashboard_model';
  7. import { PanelModel } from '../panel_model';
  8. import classNames from 'classnames';
  9. import sizeMe from 'react-sizeme';
  10. let lastGridWidth = 1200;
  11. let ignoreNextWidthChange = false;
  12. function GridWrapper({
  13. size,
  14. layout,
  15. onLayoutChange,
  16. children,
  17. onDragStop,
  18. onResize,
  19. onResizeStop,
  20. onWidthChange,
  21. className,
  22. isResizable,
  23. isDraggable,
  24. isFullscreen,
  25. }) {
  26. const width = size.width > 0 ? size.width : lastGridWidth;
  27. // logic to ignore width changes (optimization)
  28. if (width !== lastGridWidth) {
  29. if (ignoreNextWidthChange) {
  30. ignoreNextWidthChange = false;
  31. } else if (!isFullscreen && Math.abs(width - lastGridWidth) > 8) {
  32. onWidthChange();
  33. lastGridWidth = width;
  34. }
  35. }
  36. return (
  37. <ReactGridLayout
  38. width={lastGridWidth}
  39. className={className}
  40. isDraggable={isDraggable}
  41. isResizable={isResizable}
  42. measureBeforeMount={false}
  43. containerPadding={[0, 0]}
  44. useCSSTransforms={true}
  45. margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
  46. cols={GRID_COLUMN_COUNT}
  47. rowHeight={GRID_CELL_HEIGHT}
  48. draggableHandle=".grid-drag-handle"
  49. layout={layout}
  50. onResize={onResize}
  51. onResizeStop={onResizeStop}
  52. onDragStop={onDragStop}
  53. onLayoutChange={onLayoutChange}
  54. >
  55. {children}
  56. </ReactGridLayout>
  57. );
  58. }
  59. const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
  60. export interface DashboardGridProps {
  61. dashboard: DashboardModel;
  62. }
  63. export class DashboardGrid extends React.Component<DashboardGridProps, any> {
  64. gridToPanelMap: any;
  65. panelMap: { [id: string]: PanelModel };
  66. constructor(props) {
  67. super(props);
  68. this.onLayoutChange = this.onLayoutChange.bind(this);
  69. this.onResize = this.onResize.bind(this);
  70. this.onResizeStop = this.onResizeStop.bind(this);
  71. this.onDragStop = this.onDragStop.bind(this);
  72. this.onWidthChange = this.onWidthChange.bind(this);
  73. this.state = { animated: false };
  74. // subscribe to dashboard events
  75. const dashboard = this.props.dashboard;
  76. dashboard.on('panel-added', this.triggerForceUpdate.bind(this));
  77. dashboard.on('panel-removed', this.triggerForceUpdate.bind(this));
  78. dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this));
  79. dashboard.on('view-mode-changed', this.onViewModeChanged.bind(this));
  80. dashboard.on('row-collapsed', this.triggerForceUpdate.bind(this));
  81. dashboard.on('row-expanded', this.triggerForceUpdate.bind(this));
  82. }
  83. buildLayout() {
  84. const layout = [];
  85. this.panelMap = {};
  86. for (const panel of this.props.dashboard.panels) {
  87. const stringId = panel.id.toString();
  88. this.panelMap[stringId] = panel;
  89. if (!panel.gridPos) {
  90. console.log('panel without gridpos');
  91. continue;
  92. }
  93. const panelPos: any = {
  94. i: stringId,
  95. x: panel.gridPos.x,
  96. y: panel.gridPos.y,
  97. w: panel.gridPos.w,
  98. h: panel.gridPos.h,
  99. };
  100. if (panel.type === 'row') {
  101. panelPos.w = GRID_COLUMN_COUNT;
  102. panelPos.h = 1;
  103. panelPos.isResizable = false;
  104. panelPos.isDraggable = panel.collapsed;
  105. }
  106. layout.push(panelPos);
  107. }
  108. return layout;
  109. }
  110. onLayoutChange(newLayout) {
  111. for (const newPos of newLayout) {
  112. this.panelMap[newPos.i].updateGridPos(newPos);
  113. }
  114. this.props.dashboard.sortPanelsByGridPos();
  115. }
  116. triggerForceUpdate() {
  117. this.forceUpdate();
  118. }
  119. onWidthChange() {
  120. for (const panel of this.props.dashboard.panels) {
  121. panel.resizeDone();
  122. }
  123. }
  124. onViewModeChanged(payload) {
  125. ignoreNextWidthChange = true;
  126. this.setState({ animated: !payload.fullscreen });
  127. }
  128. updateGridPos(item, layout) {
  129. this.panelMap[item.i].updateGridPos(item);
  130. // react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
  131. // So it's required to call it explicitly when panel resized or moved to save layout changes.
  132. this.onLayoutChange(layout);
  133. }
  134. onResize(layout, oldItem, newItem) {
  135. this.panelMap[newItem.i].updateGridPos(newItem);
  136. }
  137. onResizeStop(layout, oldItem, newItem) {
  138. this.updateGridPos(newItem, layout);
  139. this.panelMap[newItem.i].resizeDone();
  140. }
  141. onDragStop(layout, oldItem, newItem) {
  142. this.updateGridPos(newItem, layout);
  143. }
  144. componentDidMount() {
  145. setTimeout(() => {
  146. this.setState({ animated: true });
  147. });
  148. }
  149. renderPanels() {
  150. const panelElements = [];
  151. for (const panel of this.props.dashboard.panels) {
  152. const panelClasses = classNames({ 'react-grid-item--fullscreen': panel.fullscreen });
  153. panelElements.push(
  154. <div key={panel.id.toString()} className={panelClasses} id={`panel-${panel.id}`}>
  155. <DashboardPanel
  156. panel={panel}
  157. dashboard={this.props.dashboard}
  158. isEditing={panel.isEditing}
  159. isFullscreen={panel.fullscreen}
  160. />
  161. </div>
  162. );
  163. }
  164. return panelElements;
  165. }
  166. render() {
  167. return (
  168. <SizedReactLayoutGrid
  169. className={classNames({ layout: true, animated: this.state.animated })}
  170. layout={this.buildLayout()}
  171. isResizable={this.props.dashboard.meta.canEdit}
  172. isDraggable={this.props.dashboard.meta.canEdit}
  173. onLayoutChange={this.onLayoutChange}
  174. onWidthChange={this.onWidthChange}
  175. onDragStop={this.onDragStop}
  176. onResize={this.onResize}
  177. onResizeStop={this.onResizeStop}
  178. isFullscreen={this.props.dashboard.meta.fullscreen}
  179. >
  180. {this.renderPanels()}
  181. </SizedReactLayoutGrid>
  182. );
  183. }
  184. }
  185. export default hot(module)(DashboardGrid);