DashboardGrid.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React from 'react';
  2. import ReactGridLayout from 'react-grid-layout';
  3. import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN, GRID_COLUMN_COUNT } from 'app/core/constants';
  4. import { DashboardPanel } from './DashboardPanel';
  5. import { DashboardModel } from '../dashboard_model';
  6. import { PanelModel } from '../panel_model';
  7. import classNames from 'classnames';
  8. import sizeMe from 'react-sizeme';
  9. let lastGridWidth = 1200;
  10. let ignoreNextWidthChange = false;
  11. function GridWrapper({
  12. size,
  13. layout,
  14. onLayoutChange,
  15. children,
  16. onDragStop,
  17. onResize,
  18. onResizeStop,
  19. onWidthChange,
  20. className,
  21. isResizable,
  22. isDraggable,
  23. isFullscreen,
  24. }) {
  25. const width = size.width > 0 ? size.width : lastGridWidth;
  26. // logic to ignore width changes (optimization)
  27. if (width !== lastGridWidth) {
  28. if (ignoreNextWidthChange) {
  29. ignoreNextWidthChange = false;
  30. } else if (!isFullscreen && Math.abs(width - lastGridWidth) > 8) {
  31. onWidthChange();
  32. lastGridWidth = width;
  33. }
  34. }
  35. return (
  36. <ReactGridLayout
  37. width={lastGridWidth}
  38. className={className}
  39. isDraggable={isDraggable}
  40. isResizable={isResizable}
  41. measureBeforeMount={false}
  42. containerPadding={[0, 0]}
  43. useCSSTransforms={true}
  44. margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
  45. cols={GRID_COLUMN_COUNT}
  46. rowHeight={GRID_CELL_HEIGHT}
  47. draggableHandle=".grid-drag-handle"
  48. layout={layout}
  49. onResize={onResize}
  50. onResizeStop={onResizeStop}
  51. onDragStop={onDragStop}
  52. onLayoutChange={onLayoutChange}
  53. >
  54. {children}
  55. </ReactGridLayout>
  56. );
  57. }
  58. const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
  59. export interface DashboardGridProps {
  60. dashboard: DashboardModel;
  61. }
  62. export class DashboardGrid extends React.Component<DashboardGridProps, any> {
  63. gridToPanelMap: any;
  64. panelMap: { [id: string]: PanelModel };
  65. constructor(props) {
  66. super(props);
  67. this.onLayoutChange = this.onLayoutChange.bind(this);
  68. this.onResize = this.onResize.bind(this);
  69. this.onResizeStop = this.onResizeStop.bind(this);
  70. this.onDragStop = this.onDragStop.bind(this);
  71. this.onWidthChange = this.onWidthChange.bind(this);
  72. this.state = { animated: false };
  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.setState({ animated: !payload.fullscreen });
  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. componentDidMount() {
  144. setTimeout(() => {
  145. this.setState({ animated: true });
  146. });
  147. }
  148. renderPanels() {
  149. const panelElements = [];
  150. for (const panel of this.props.dashboard.panels) {
  151. const panelClasses = classNames({ 'react-grid-item--fullscreen': panel.fullscreen });
  152. panelElements.push(
  153. <div key={panel.id.toString()} className={panelClasses} id={`panel-${panel.id}`}>
  154. <DashboardPanel
  155. panel={panel}
  156. dashboard={this.props.dashboard}
  157. isEditing={panel.isEditing}
  158. isFullscreen={panel.fullscreen}
  159. />
  160. </div>
  161. );
  162. }
  163. return panelElements;
  164. }
  165. render() {
  166. return (
  167. <SizedReactLayoutGrid
  168. className={classNames({ layout: true, animated: this.state.animated })}
  169. layout={this.buildLayout()}
  170. isResizable={this.props.dashboard.meta.canEdit}
  171. isDraggable={this.props.dashboard.meta.canEdit}
  172. onLayoutChange={this.onLayoutChange}
  173. onWidthChange={this.onWidthChange}
  174. onDragStop={this.onDragStop}
  175. onResize={this.onResize}
  176. onResizeStop={this.onResizeStop}
  177. isFullscreen={this.props.dashboard.meta.fullscreen}
  178. >
  179. {this.renderPanels()}
  180. </SizedReactLayoutGrid>
  181. );
  182. }
  183. }