DashboardGrid.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import React from 'react';
  2. import ReactGridLayout from 'react-grid-layout-grafana';
  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. >
  51. {children}
  52. </ReactGridLayout>
  53. );
  54. }
  55. const SizedReactLayoutGrid = sizeMe({ monitorWidth: true })(GridWrapper);
  56. export interface DashboardGridProps {
  57. getPanelContainer: () => PanelContainer;
  58. }
  59. export class DashboardGrid extends React.Component<DashboardGridProps, any> {
  60. gridToPanelMap: any;
  61. panelContainer: PanelContainer;
  62. dashboard: DashboardModel;
  63. panelMap: { [id: string]: PanelModel };
  64. constructor(props) {
  65. super(props);
  66. this.panelContainer = this.props.getPanelContainer();
  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. this.dashboard = this.panelContainer.getDashboard();
  75. this.dashboard.on('panel-added', this.triggerForceUpdate.bind(this));
  76. this.dashboard.on('panel-removed', this.triggerForceUpdate.bind(this));
  77. this.dashboard.on('repeats-processed', this.triggerForceUpdate.bind(this));
  78. this.dashboard.on('view-mode-changed', this.triggerForceUpdate.bind(this));
  79. this.dashboard.on('row-collapsed', this.triggerForceUpdate.bind(this));
  80. this.dashboard.on('row-expanded', this.triggerForceUpdate.bind(this));
  81. }
  82. buildLayout() {
  83. const layout = [];
  84. this.panelMap = {};
  85. for (let panel of this.dashboard.panels) {
  86. let stringId = panel.id.toString();
  87. this.panelMap[stringId] = panel;
  88. if (!panel.gridPos) {
  89. console.log('panel without gridpos');
  90. continue;
  91. }
  92. let 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.dashboard.sortPanelsByGridPos();
  114. }
  115. triggerForceUpdate() {
  116. this.forceUpdate();
  117. }
  118. onWidthChange() {
  119. for (const panel of this.dashboard.panels) {
  120. panel.resizeDone();
  121. }
  122. }
  123. updateGridPos(item, layout) {
  124. this.panelMap[item.i].updateGridPos(item);
  125. // react-grid-layout has a bug (#670), and onLayoutChange() is only called when the component is mounted.
  126. // So it's required to call it explicitly when panel resized or moved to save layout changes.
  127. this.onLayoutChange(layout);
  128. }
  129. onResize(layout, oldItem, newItem) {
  130. this.panelMap[newItem.i].updateGridPos(newItem);
  131. }
  132. onResizeStop(layout, oldItem, newItem) {
  133. this.updateGridPos(newItem, layout);
  134. this.panelMap[newItem.i].resizeDone();
  135. }
  136. onDragStop(layout, oldItem, newItem) {
  137. this.updateGridPos(newItem, layout);
  138. }
  139. componentDidMount() {
  140. setTimeout(() => {
  141. this.setState(() => {
  142. return { animated: true };
  143. });
  144. });
  145. }
  146. renderPanels() {
  147. const panelElements = [];
  148. for (let panel of this.dashboard.panels) {
  149. const panelClasses = classNames({ panel: true, 'panel--fullscreen': panel.fullscreen });
  150. panelElements.push(
  151. <div key={panel.id.toString()} className={panelClasses}>
  152. <DashboardPanel panel={panel} getPanelContainer={this.props.getPanelContainer} />
  153. </div>
  154. );
  155. }
  156. return panelElements;
  157. }
  158. render() {
  159. return (
  160. <SizedReactLayoutGrid
  161. className={classNames({ layout: true, animated: this.state.animated })}
  162. layout={this.buildLayout()}
  163. isResizable={this.dashboard.meta.canEdit}
  164. isDraggable={this.dashboard.meta.canEdit}
  165. onLayoutChange={this.onLayoutChange}
  166. onWidthChange={this.onWidthChange}
  167. onDragStop={this.onDragStop}
  168. onResize={this.onResize}
  169. onResizeStop={this.onResizeStop}
  170. >
  171. {this.renderPanels()}
  172. </SizedReactLayoutGrid>
  173. );
  174. }
  175. }