DashboardGrid.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. }) {
  23. if (size.width === 0) {
  24. console.log('size is zero!');
  25. }
  26. const width = size.width > 0 ? size.width : lastGridWidth;
  27. if (width !== lastGridWidth) {
  28. onWidthChange();
  29. lastGridWidth = width;
  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. dashboard.on('panel-type-changed', this.triggerForceUpdate.bind(this));
  78. }
  79. buildLayout() {
  80. const layout = [];
  81. this.panelMap = {};
  82. for (const panel of this.props.dashboard.panels) {
  83. const stringId = panel.id.toString();
  84. this.panelMap[stringId] = panel;
  85. if (!panel.gridPos) {
  86. console.log('panel without gridpos');
  87. continue;
  88. }
  89. const panelPos: any = {
  90. i: stringId,
  91. x: panel.gridPos.x,
  92. y: panel.gridPos.y,
  93. w: panel.gridPos.w,
  94. h: panel.gridPos.h,
  95. };
  96. if (panel.type === 'row') {
  97. panelPos.w = GRID_COLUMN_COUNT;
  98. panelPos.h = 1;
  99. panelPos.isResizable = false;
  100. panelPos.isDraggable = panel.collapsed;
  101. }
  102. layout.push(panelPos);
  103. }
  104. return layout;
  105. }
  106. onLayoutChange(newLayout) {
  107. for (const newPos of newLayout) {
  108. this.panelMap[newPos.i].updateGridPos(newPos);
  109. }
  110. this.props.dashboard.sortPanelsByGridPos();
  111. }
  112. triggerForceUpdate() {
  113. this.forceUpdate();
  114. }
  115. onWidthChange() {
  116. for (const panel of this.props.dashboard.panels) {
  117. panel.resizeDone();
  118. }
  119. }
  120. onViewModeChanged(payload) {
  121. this.setState({ animated: !payload.fullscreen });
  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({ animated: true });
  142. });
  143. }
  144. renderPanels() {
  145. const panelElements = [];
  146. console.log('render panels');
  147. for (const panel of this.props.dashboard.panels) {
  148. const panelClasses = classNames({ panel: true, 'panel--fullscreen': panel.fullscreen });
  149. panelElements.push(
  150. <div key={panel.id.toString()} className={panelClasses} id={`panel-${panel.id}`}>
  151. <DashboardPanel panel={panel} dashboard={this.props.dashboard} panelType={panel.type} />
  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.props.dashboard.meta.canEdit}
  163. isDraggable={this.props.dashboard.meta.canEdit}
  164. onLayoutChange={this.onLayoutChange}
  165. onWidthChange={this.onWidthChange}
  166. onDragStop={this.onDragStop}
  167. onResize={this.onResize}
  168. onResizeStop={this.onResizeStop}
  169. >
  170. {this.renderPanels()}
  171. </SizedReactLayoutGrid>
  172. );
  173. }
  174. }