GridItem.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = Object.setPrototypeOf ||
  4. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  5. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  6. return function (d, b) {
  7. extendStatics(d, b);
  8. function __() { this.constructor = d; }
  9. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  10. };
  11. })();
  12. var __assign = (this && this.__assign) || Object.assign || function(t) {
  13. for (var s, i = 1, n = arguments.length; i < n; i++) {
  14. s = arguments[i];
  15. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  16. t[p] = s[p];
  17. }
  18. return t;
  19. };
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. var React = require("react");
  22. var index_1 = require("./dragger/index");
  23. var correction_1 = require("./util/correction");
  24. var GridItem = /** @class */ (function (_super) {
  25. __extends(GridItem, _super);
  26. function GridItem(props) {
  27. var _this = _super.call(this, props) || this;
  28. _this.onDrag = _this.onDrag.bind(_this);
  29. _this.onDragStart = _this.onDragStart.bind(_this);
  30. _this.onDragEnd = _this.onDragEnd.bind(_this);
  31. _this.calGridXY = _this.calGridXY.bind(_this);
  32. _this.calColWidth = _this.calColWidth.bind(_this);
  33. return _this;
  34. }
  35. /** 计算容器的每一个格子多大 */
  36. GridItem.prototype.calColWidth = function () {
  37. var _a = this.props, containerWidth = _a.containerWidth, col = _a.col, containerPadding = _a.containerPadding, margin = _a.margin;
  38. if (margin) {
  39. return (containerWidth - containerPadding[0] * 2 - margin[0] * (col + 1)) / col;
  40. }
  41. return (containerWidth - containerPadding[0] * 2 - 0 * (col + 1)) / col;
  42. };
  43. /**转化,计算网格的GridX,GridY值 */
  44. GridItem.prototype.calGridXY = function (x, y) {
  45. var _a = this.props, margin = _a.margin, containerWidth = _a.containerWidth, col = _a.col, w = _a.w, rowHeight = _a.rowHeight;
  46. /**坐标转换成格子的时候,无须计算margin */
  47. var GridX = Math.round(x / containerWidth * col);
  48. var GridY = Math.round(y / (rowHeight + (margin ? margin[1] : 0)));
  49. // /**防止元素出container */
  50. return correction_1.checkInContainer(GridX, GridY, col, w);
  51. };
  52. /**给予一个grid的位置,算出元素具体的在容器中位置在哪里,单位是px */
  53. GridItem.prototype.calGridItemPosition = function (GridX, GridY) {
  54. var _a = this.props, margin = _a.margin, rowHeight = _a.rowHeight;
  55. if (!margin)
  56. margin = [0, 0];
  57. var x = Math.round(GridX * this.calColWidth() + (GridX + 1) * margin[0]);
  58. var y = Math.round(GridY * rowHeight + margin[1] * (GridY + 1));
  59. return {
  60. x: x,
  61. y: y
  62. };
  63. };
  64. GridItem.prototype.shouldComponentUpdate = function (props, state) {
  65. return this.props.GridX !== props.GridX ||
  66. this.props.GridY !== props.GridY ||
  67. this.props.isUserMove !== props.isUserMove;
  68. };
  69. /**宽和高计算成为px */
  70. GridItem.prototype.calWHtoPx = function (w, h) {
  71. var margin = this.props.margin;
  72. if (!margin)
  73. margin = [0, 0];
  74. var wPx = Math.round(w * this.calColWidth() + (w - 1) * margin[0]);
  75. var hPx = Math.round(h * this.props.rowHeight + (h - 1) * margin[1]);
  76. return { wPx: wPx, hPx: hPx };
  77. };
  78. GridItem.prototype.onDragStart = function (x, y) {
  79. var _a = this.props, w = _a.w, h = _a.h, UniqueKey = _a.UniqueKey;
  80. if (this.props.static)
  81. return;
  82. var _b = this.calGridXY(x, y), GridX = _b.GridX, GridY = _b.GridY;
  83. this.props.onDragStart && this.props.onDragStart({
  84. event: event, GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + ''
  85. });
  86. };
  87. GridItem.prototype.onDrag = function (event, x, y) {
  88. if (this.props.static)
  89. return;
  90. var _a = this.calGridXY(x, y), GridX = _a.GridX, GridY = _a.GridY;
  91. var _b = this.props, w = _b.w, h = _b.h, UniqueKey = _b.UniqueKey;
  92. this.props.onDrag && this.props.onDrag({ GridX: GridX, GridY: GridY, w: w, h: h, UniqueKey: UniqueKey + '', event: event });
  93. };
  94. GridItem.prototype.onDragEnd = function () {
  95. if (this.props.static)
  96. return;
  97. if (this.props.onDragEnd)
  98. this.props.onDragEnd(this.props.UniqueKey + '');
  99. };
  100. GridItem.prototype.render = function () {
  101. var _a = this.props, w = _a.w, h = _a.h, style = _a.style, bounds = _a.bounds, GridX = _a.GridX, GridY = _a.GridY;
  102. var _b = this.calGridItemPosition(GridX, GridY), x = _b.x, y = _b.y;
  103. var _c = this.calWHtoPx(w, h), wPx = _c.wPx, hPx = _c.hPx;
  104. return (React.createElement(index_1.Dragger, { style: __assign({}, style, { width: wPx, height: hPx, position: 'absolute', transition: this.props.isUserMove ? '' : 'all .2s' }), onDragStart: this.onDragStart, onMove: this.onDrag, onDragEnd: this.onDragEnd, x: x, y: y, isUserMove: this.props.isUserMove, bounds: bounds },
  105. React.createElement("div", { style: { width: wPx, height: hPx } }, React.Children.map(this.props.children, function (child) { return child; }))));
  106. };
  107. GridItem.defaultProps = {
  108. col: 12,
  109. containerWidth: 500,
  110. containerPadding: [0, 0],
  111. margin: [10, 10],
  112. rowHeight: 30,
  113. w: 1,
  114. h: 1
  115. };
  116. return GridItem;
  117. }(React.Component));
  118. exports.default = GridItem;