DashboardGrid.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. function GridWrapper({
  11. size,
  12. layout,
  13. onLayoutChange,
  14. children,
  15. onDragStop,
  16. onResize,
  17. onResizeStop,
  18. onWidthChange,
  19. className,
  20. isResizable,
  21. isDraggable,
  22. isFullscreen,
  23. }) {
  24. const width = size.width > 0 ? size.width : lastGridWidth;
  25. if (width !== lastGridWidth) {
  26. if (!isFullscreen && Math.abs(width - lastGridWidth) > 8) {
  27. onWidthChange();
  28. lastGridWidth = width;
  29. }
  30. }
  31. return (
  32. <ReactGridLayout
  33. width={lastGridWidth}
  34. className={className}
  35. isDraggable={isDraggable}
  36. isResizable={isResizable}
  37. measureBeforeMount={false}
  38. containerPadding={[0, 0]}
  39. useCSSTransforms={true}
  40. margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
  41. cols={GRID_COLUMN_COUNT}
  42. rowHeight={GRID_CELL_HEIGHT}
  43. draggableHandle=".grid-drag-handle"
  44. layout={layout}
  45. onResize={onResize}
  46. onResizeStop={onResizeStop}
  47. onDragStop={onDragStop}
  48. onLayoutChange={onLayoutChange}
  49. >
  50. {children}
  51. </ReactGridLayout>
  52. );
  53. }
  54. const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
  55. export interface DashboardGridProps {
  56. dashboard: DashboardModel;
  57. }
  58. export class DashboardGrid extends React.Component<DashboardGridProps, any> {
  59. gridToPanelMap: any;
  60. panelMap: { [id: string]: PanelModel };
  61. constructor(props) {
  62. super(props);
  63. this.onLayoutChange = this.onLayoutChange.bind(this);
  64. this.onResize = this.onResize.bind(this);
  65. this.onResizeStop = this.onResizeStop.bind(this);
  66. this.onDragStop = this.onDragStop.bind(this);
  67. this.onWidthChange = this.onWidthChange.bind(this);
  68. this.state = { animated: false };
  69. // subscribe to dashboard events
  70. const dashboard = this.props.dashboard;
  71. dashboard.on('panel-added', this.triggerForceUpdate.bind(this));
  72. dashboard.on('panel-removed', this.triggerForceUpdate.bind(this));
  73. dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this));
  74. dashboard.on('view-mode-changed', this.onViewModeChanged.bind(this));
  75. dashboard.on('row-collapsed', this.triggerForceUpdate.bind(this));
  76. dashboard.on('row-expanded', this.triggerForceUpdate.bind(this));
  77. }
  78. buildLayout() {
  79. const layout = [];
  80. this.panelMap = {};
  81. for (const panel of this.props.dashboard.panels) {
  82. const stringId = panel.id.toString();
  83. this.panelMap[stringId] = panel;
  84. if (!panel.gridPos) {
  85. console.log('panel without gridpos');
  86. continue;
  87. }
  88. const panelPos: any = {
  89. i: stringId,
  90. x: panel.gridPos.x,
  91. y: panel.gridPos.y,
  92. w: panel.gridPos.w,
  93. h: panel.gridPos.h,
  94. };
  95. if (panel.type === 'row') {
  96. panelPos.w = GRID_COLUMN_COUNT;
  97. panelPos.h = 1;
  98. panelPos.isResizable = false;
  99. panelPos.isDraggable = panel.collapsed;
  100. }
  101. layout.push(panelPos);
  102. }
  103. return layout;
  104. }
  105. onLayoutChange(newLayout) {
  106. for (const newPos of newLayout) {
  107. this.panelMap[newPos.i].updateGridPos(newPos);
  108. }
  109. this.props.dashboard.sortPanelsByGridPos();
  110. }
  111. triggerForceUpdate() {
  112. this.forceUpdate();
  113. }
  114. onWidthChange() {
  115. for (const panel of this.props.dashboard.panels) {
  116. panel.resizeDone();
  117. }
  118. }
  119. onViewModeChanged(payload) {
  120. this.setState({ animated: !payload.fullscreen });
  121. }
  122. updateGridPos(item, layout) {
  123. this.panelMap[item.i].updateGridPos(item);
  124. // react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
  125. // So it's required to call it explicitly when panel resized or moved to save layout changes.
  126. this.onLayoutChange(layout);
  127. }
  128. onResize(layout, oldItem, newItem) {
  129. this.panelMap[newItem.i].updateGridPos(newItem);
  130. }
  131. onResizeStop(layout, oldItem, newItem) {
  132. this.updateGridPos(newItem, layout);
  133. this.panelMap[newItem.i].resizeDone();
  134. }
  135. onDragStop(layout, oldItem, newItem) {
  136. this.updateGridPos(newItem, layout);
  137. }
  138. componentDidMount() {
  139. setTimeout(() => {
  140. this.setState({ animated: true });
  141. });
  142. }
  143. renderPanels() {
  144. const panelElements = [];
  145. for (const panel of this.props.dashboard.panels) {
  146. const panelClasses = classNames({ panel: true, 'panel--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, animated: this.state.animated })}
  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. }