GridItem.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. var __extends = (this && this.__extends) || (function () {
  2. var extendStatics = Object.setPrototypeOf ||
  3. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  4. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  5. return function (d, b) {
  6. extendStatics(d, b);
  7. function __() { this.constructor = d; }
  8. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  9. };
  10. })();
  11. var __assign = (this && this.__assign) || Object.assign || function(t) {
  12. for (var s, i = 1, n = arguments.length; i < n; i++) {
  13. s = arguments[i];
  14. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  15. t[p] = s[p];
  16. }
  17. return t;
  18. };
  19. import * as React from "react";
  20. import { Dragger } from './dragger/index';
  21. import { checkInContainer } from './util/correction';
  22. var checkWidthHeight = function (GridX, w, h, col) {
  23. var newW = w;
  24. var newH = h;
  25. if (GridX + w > col - 1)
  26. newW = col - GridX; //右边界
  27. if (w < 1)
  28. newW = 1;
  29. if (h < 1)
  30. newH = 1;
  31. return {
  32. w: newW, h: newH
  33. };
  34. };
  35. var GridItem = /** @class */ (function (_super) {
  36. __extends(GridItem, _super);
  37. function GridItem(props) {
  38. var _this = _super.call(this, props) || this;
  39. _this.onResizeStart = function (event, wPx, hPx) {
  40. var _a = _this.props, GridX = _a.GridX, GridY = _a.GridY, UniqueKey = _a.UniqueKey, w = _a.w, h = _a.h;
  41. _this.props.onResizeStart && _this.props.onResizeStart({ GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + '', event: event });
  42. };
  43. _this.onResizing = function (event, wPx, hPx) {
  44. var _a = _this.calPxToWH(wPx, hPx), w = _a.w, h = _a.h;
  45. var _b = _this.props, GridX = _b.GridX, GridY = _b.GridY, UniqueKey = _b.UniqueKey;
  46. _this.props.onResizing && _this.props.onResizing({ GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + '', event: event });
  47. };
  48. _this.onResizeEnd = function (event, wPx, hPx) {
  49. var _a = _this.calPxToWH(wPx, hPx), w = _a.w, h = _a.h;
  50. var _b = _this.props, GridX = _b.GridX, GridY = _b.GridY, UniqueKey = _b.UniqueKey;
  51. _this.props.onResizeEnd && _this.props.onResizeEnd({ GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + '', event: event });
  52. };
  53. _this.onDrag = _this.onDrag.bind(_this);
  54. _this.onDragStart = _this.onDragStart.bind(_this);
  55. _this.onDragEnd = _this.onDragEnd.bind(_this);
  56. _this.calGridXY = _this.calGridXY.bind(_this);
  57. _this.calColWidth = _this.calColWidth.bind(_this);
  58. return _this;
  59. }
  60. /** 计算容器的每一个格子多大 */
  61. GridItem.prototype.calColWidth = function () {
  62. var _a = this.props, containerWidth = _a.containerWidth, col = _a.col, containerPadding = _a.containerPadding, margin = _a.margin;
  63. if (margin) {
  64. return (containerWidth - containerPadding[0] * 2 - margin[0] * (col + 1)) / col;
  65. }
  66. return (containerWidth - containerPadding[0] * 2 - 0 * (col + 1)) / col;
  67. };
  68. /**转化,计算网格的GridX,GridY值 */
  69. GridItem.prototype.calGridXY = function (x, y) {
  70. var _a = this.props, margin = _a.margin, containerWidth = _a.containerWidth, col = _a.col, w = _a.w, rowHeight = _a.rowHeight;
  71. /**坐标转换成格子的时候,无须计算margin */
  72. var GridX = Math.round(x / containerWidth * col);
  73. var GridY = Math.round(y / (rowHeight + (margin ? margin[1] : 0)));
  74. // /**防止元素出container */
  75. return checkInContainer(GridX, GridY, col, w);
  76. };
  77. /**给予一个grid的位置,算出元素具体的在容器中位置在哪里,单位是px */
  78. GridItem.prototype.calGridItemPosition = function (GridX, GridY) {
  79. var _a = this.props, margin = _a.margin, rowHeight = _a.rowHeight;
  80. if (!margin)
  81. margin = [0, 0];
  82. var x = Math.round(GridX * this.calColWidth() + (GridX + 1) * margin[0]);
  83. var y = Math.round(GridY * rowHeight + margin[1] * (GridY + 1));
  84. return {
  85. x: x,
  86. y: y
  87. };
  88. };
  89. GridItem.prototype.shouldComponentUpdate = function (props, state) {
  90. return this.props.GridX !== props.GridX ||
  91. this.props.GridY !== props.GridY ||
  92. this.props.isUserMove !== props.isUserMove ||
  93. this.props.w !== props.w ||
  94. this.props.h !== props.h;
  95. };
  96. /**宽和高计算成为px */
  97. GridItem.prototype.calWHtoPx = function (w, h) {
  98. var margin = this.props.margin;
  99. if (!margin)
  100. margin = [0, 0];
  101. var wPx = Math.round(w * this.calColWidth() + (w - 1) * margin[0]);
  102. var hPx = Math.round(h * this.props.rowHeight + (h - 1) * margin[1]);
  103. return { wPx: wPx, hPx: hPx };
  104. };
  105. GridItem.prototype.calPxToWH = function (wPx, hPx) {
  106. var calWidth = this.calColWidth();
  107. var w = Math.round((wPx - calWidth * 0.5) / calWidth);
  108. var h = Math.round((hPx - this.props.rowHeight * 0.5) / this.props.rowHeight);
  109. return checkWidthHeight(this.props.GridX, w, h, this.props.col);
  110. };
  111. GridItem.prototype.onDragStart = function (x, y) {
  112. var _a = this.props, w = _a.w, h = _a.h, UniqueKey = _a.UniqueKey;
  113. if (this.props.static)
  114. return;
  115. var _b = this.calGridXY(x, y), GridX = _b.GridX, GridY = _b.GridY;
  116. this.props.onDragStart && this.props.onDragStart({
  117. event: event, GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + ''
  118. });
  119. };
  120. GridItem.prototype.onDrag = function (event, x, y) {
  121. if (this.props.static)
  122. return;
  123. var _a = this.calGridXY(x, y), GridX = _a.GridX, GridY = _a.GridY;
  124. var _b = this.props, w = _b.w, h = _b.h, UniqueKey = _b.UniqueKey;
  125. this.props.onDrag && this.props.onDrag({ GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + '', event: event });
  126. };
  127. GridItem.prototype.onDragEnd = function (event, x, y) {
  128. if (this.props.static)
  129. return;
  130. var _a = this.calGridXY(x, y), GridX = _a.GridX, GridY = _a.GridY;
  131. var _b = this.props, w = _b.w, h = _b.h, UniqueKey = _b.UniqueKey;
  132. if (this.props.onDragEnd)
  133. this.props.onDragEnd({ GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + '', event: event });
  134. };
  135. GridItem.prototype.render = function () {
  136. var _this = this;
  137. var _a = this.props, w = _a.w, h = _a.h, style = _a.style, bounds = _a.bounds, GridX = _a.GridX, GridY = _a.GridY, handle = _a.handle, canDrag = _a.canDrag, canResize = _a.canResize;
  138. var _b = this.calGridItemPosition(GridX, GridY), x = _b.x, y = _b.y;
  139. var _c = this.calWHtoPx(w, h), wPx = _c.wPx, hPx = _c.hPx;
  140. return (React.createElement(Dragger, { style: __assign({}, style, { width: wPx, height: hPx, position: 'absolute', transition: this.props.isUserMove ? '' : 'all .2s ease-out', zIndex: this.props.isUserMove ? (this.props.dragType === 'drag' ? 10 : 2) : 2 }), onDragStart: this.onDragStart, onMove: this.onDrag, onDragEnd: this.onDragEnd, onResizeStart: this.onResizeStart, onResizing: this.onResizing, onResizeEnd: this.onResizeEnd, x: x, y: y, w: wPx, h: hPx, isUserMove: this.props.isUserMove, bounds: bounds, handle: handle, canDrag: canDrag, canResize: canResize }, function (provided, draggerProps, resizerProps) { return _this.props.children(provided, draggerProps, resizerProps); }));
  141. };
  142. GridItem.defaultProps = {
  143. col: 12,
  144. containerWidth: 500,
  145. containerPadding: [0, 0],
  146. margin: [10, 10],
  147. rowHeight: 30,
  148. w: 1,
  149. h: 1
  150. };
  151. return GridItem;
  152. }(React.Component));
  153. export default GridItem;