DashboardGrid.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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={false}
  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> {
  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. // subscribe to dashboard events
  74. const dashboard = this.props.dashboard;
  75. dashboard.on('panel-added', this.triggerForceUpdate.bind(this));
  76. dashboard.on('panel-removed', this.triggerForceUpdate.bind(this));
  77. dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this));
  78. dashboard.on('view-mode-changed', this.onViewModeChanged.bind(this));
  79. dashboard.on('row-collapsed', this.triggerForceUpdate.bind(this));
  80. dashboard.on('row-expanded', this.triggerForceUpdate.bind(this));
  81. }
  82. buildLayout() {
  83. const layout = [];
  84. this.panelMap = {};
  85. for (const panel of this.props.dashboard.panels) {
  86. const stringId = panel.id.toString();
  87. this.panelMap[stringId] = panel;
  88. if (!panel.gridPos) {
  89. console.log('panel without gridpos');
  90. continue;
  91. }
  92. const panelPos: any = {
  93. i: stringId,
  94. x: panel.gridPos.x,
  95. y: panel.gridPos.y,
  96. w: panel.gridPos.w,
  97. h: panel.gridPos.h,
  98. };
  99. if (panel.type === 'row') {
  100. panelPos.w = GRID_COLUMN_COUNT;
  101. panelPos.h = 1;
  102. panelPos.isResizable = false;
  103. panelPos.isDraggable = panel.collapsed;
  104. }
  105. layout.push(panelPos);
  106. }
  107. return layout;
  108. }
  109. onLayoutChange(newLayout) {
  110. for (const newPos of newLayout) {
  111. this.panelMap[newPos.i].updateGridPos(newPos);
  112. }
  113. this.props.dashboard.sortPanelsByGridPos();
  114. }
  115. triggerForceUpdate() {
  116. this.forceUpdate();
  117. }
  118. onWidthChange() {
  119. for (const panel of this.props.dashboard.panels) {
  120. panel.resizeDone();
  121. }
  122. }
  123. onViewModeChanged(payload) {
  124. ignoreNextWidthChange = true;
  125. this.forceUpdate();
  126. }
  127. updateGridPos(item, layout) {
  128. this.panelMap[item.i].updateGridPos(item);
  129. // react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
  130. // So it's required to call it explicitly when panel resized or moved to save layout changes.
  131. this.onLayoutChange(layout);
  132. }
  133. onResize(layout, oldItem, newItem) {
  134. this.panelMap[newItem.i].updateGridPos(newItem);
  135. }
  136. onResizeStop(layout, oldItem, newItem) {
  137. this.updateGridPos(newItem, layout);
  138. this.panelMap[newItem.i].resizeDone();
  139. }
  140. onDragStop(layout, oldItem, newItem) {
  141. this.updateGridPos(newItem, layout);
  142. }
  143. renderPanels() {
  144. const panelElements = [];
  145. for (const panel of this.props.dashboard.panels) {
  146. const panelClasses = classNames({ 'react-grid-item--fullscreen': panel.fullscreen });
  147. panelElements.push(
  148. <div key={panel.id.toString()} className={panelClasses} id={`panel-${panel.id}`}>
  149. <DashboardPanel
  150. panel={panel}
  151. dashboard={this.props.dashboard}
  152. isEditing={panel.isEditing}
  153. isFullscreen={panel.fullscreen}
  154. />
  155. </div>
  156. );
  157. }
  158. return panelElements;
  159. }
  160. render() {
  161. return (
  162. <SizedReactLayoutGrid
  163. className={classNames({ layout: true })}
  164. layout={this.buildLayout()}
  165. isResizable={this.props.dashboard.meta.canEdit}
  166. isDraggable={this.props.dashboard.meta.canEdit}
  167. onLayoutChange={this.onLayoutChange}
  168. onWidthChange={this.onWidthChange}
  169. onDragStop={this.onDragStop}
  170. onResize={this.onResize}
  171. onResizeStop={this.onResizeStop}
  172. isFullscreen={this.props.dashboard.meta.fullscreen}
  173. >
  174. {this.renderPanels()}
  175. </SizedReactLayoutGrid>
  176. );
  177. }
  178. }
  179. export default hot(module)(DashboardGrid);