DashboardGrid.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { PanelContainer } from './PanelContainer';
  7. import { PanelModel } from '../panel_model';
  8. import classNames from 'classnames';
  9. import sizeMe from 'react-sizeme';
  10. let lastGridWidth = 1200;
  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. }) {
  24. if (size.width === 0) {
  25. console.log('size is zero!');
  26. }
  27. const width = size.width > 0 ? size.width : lastGridWidth;
  28. if (width !== lastGridWidth) {
  29. onWidthChange();
  30. lastGridWidth = width;
  31. }
  32. return (
  33. <ReactGridLayout
  34. width={lastGridWidth}
  35. className={className}
  36. isDraggable={isDraggable}
  37. isResizable={isResizable}
  38. measureBeforeMount={false}
  39. containerPadding={[0, 0]}
  40. useCSSTransforms={true}
  41. margin={[GRID_CELL_VMARGIN, GRID_CELL_VMARGIN]}
  42. cols={GRID_COLUMN_COUNT}
  43. rowHeight={GRID_CELL_HEIGHT}
  44. draggableHandle=".grid-drag-handle"
  45. layout={layout}
  46. onResize={onResize}
  47. onResizeStop={onResizeStop}
  48. onDragStop={onDragStop}
  49. onLayoutChange={onLayoutChange}>
  50. {children}
  51. </ReactGridLayout>
  52. );
  53. }
  54. const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
  55. export interface DashboardGridProps {
  56. getPanelContainer: () => PanelContainer;
  57. }
  58. export class DashboardGrid extends React.Component<DashboardGridProps, any> {
  59. gridToPanelMap: any;
  60. panelContainer: PanelContainer;
  61. dashboard: DashboardModel;
  62. panelMap: { [id: string]: PanelModel };
  63. constructor(props) {
  64. super(props);
  65. this.panelContainer = this.props.getPanelContainer();
  66. this.onLayoutChange = this.onLayoutChange.bind(this);
  67. this.onResize = this.onResize.bind(this);
  68. this.onResizeStop = this.onResizeStop.bind(this);
  69. this.onDragStop = this.onDragStop.bind(this);
  70. this.onWidthChange = this.onWidthChange.bind(this);
  71. this.state = { animated: false };
  72. // subscribe to dashboard events
  73. this.dashboard = this.panelContainer.getDashboard();
  74. this.dashboard.on('panel-added', this.triggerForceUpdate.bind(this));
  75. this.dashboard.on('panel-removed', this.triggerForceUpdate.bind(this));
  76. this.dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this));
  77. this.dashboard.on('view-mode-changed', this.triggerForceUpdate.bind(this));
  78. this.dashboard.on('row-collapsed', this.triggerForceUpdate.bind(this));
  79. this.dashboard.on('row-expanded', this.triggerForceUpdate.bind(this));
  80. }
  81. buildLayout() {
  82. const layout = [];
  83. this.panelMap = {};
  84. for (let panel of this.dashboard.panels) {
  85. let stringId = panel.id.toString();
  86. this.panelMap[stringId] = panel;
  87. if (!panel.gridPos) {
  88. console.log('panel without gridpos');
  89. continue;
  90. }
  91. let panelPos: any = {
  92. i: stringId,
  93. x: panel.gridPos.x,
  94. y: panel.gridPos.y,
  95. w: panel.gridPos.w,
  96. h: panel.gridPos.h,
  97. };
  98. if (panel.type === 'row') {
  99. panelPos.w = GRID_COLUMN_COUNT;
  100. panelPos.h = 1;
  101. panelPos.isResizable = false;
  102. panelPos.isDraggable = panel.collapsed;
  103. }
  104. layout.push(panelPos);
  105. }
  106. return layout;
  107. }
  108. onLayoutChange(newLayout) {
  109. for (const newPos of newLayout) {
  110. this.panelMap[newPos.i].updateGridPos(newPos);
  111. }
  112. this.dashboard.sortPanelsByGridPos();
  113. }
  114. triggerForceUpdate() {
  115. this.forceUpdate();
  116. }
  117. onWidthChange() {
  118. for (const panel of this.dashboard.panels) {
  119. panel.resizeDone();
  120. }
  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(() => {
  141. return { animated: true };
  142. });
  143. });
  144. }
  145. renderPanels() {
  146. const panelElements = [];
  147. for (let panel of this.dashboard.panels) {
  148. const panelClasses = classNames({ panel: true, 'panel--fullscreen': panel.fullscreen });
  149. panelElements.push(
  150. <div key={panel.id.toString()} className={panelClasses}>
  151. <DashboardPanel panel={panel} getPanelContainer={this.props.getPanelContainer} />
  152. </div>,
  153. );
  154. }
  155. return panelElements;
  156. }
  157. render() {
  158. return (
  159. <SizedReactLayoutGrid
  160. className={classNames({ layout: true, animated: this.state.animated })}
  161. layout={this.buildLayout()}
  162. isResizable={this.dashboard.meta.canEdit}
  163. isDraggable={this.dashboard.meta.canEdit}
  164. onLayoutChange={this.onLayoutChange}
  165. onWidthChange={this.onWidthChange}
  166. onDragStop={this.onDragStop}
  167. onResize={this.onResize}
  168. onResizeStop={this.onResizeStop}>
  169. {this.renderPanels()}
  170. </SizedReactLayoutGrid>
  171. );
  172. }
  173. }