dragact.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. Object.defineProperty(exports, "__esModule", { value: true });
  13. var React = require("react");
  14. var GridItem_1 = require("./GridItem");
  15. var compact_1 = require("./util/compact");
  16. var sort_1 = require("./util/sort");
  17. var collison_1 = require("./util/collison");
  18. var correction_1 = require("./util/correction");
  19. var utils_1 = require("./utils");
  20. var initiate_1 = require("./util/initiate");
  21. require("./style.css");
  22. var Dragact = /** @class */ (function (_super) {
  23. __extends(Dragact, _super);
  24. function Dragact(props) {
  25. var _this = _super.call(this, props) || this;
  26. _this.onDrag = _this.onDrag.bind(_this);
  27. _this.onDragStart = _this.onDragStart.bind(_this);
  28. _this.onDragEnd = _this.onDragEnd.bind(_this);
  29. var layout = props.layout ?
  30. initiate_1.MapLayoutTostate(props.layout, props.children)
  31. :
  32. utils_1.getDataSet(props.children);
  33. _this.state = {
  34. GridXMoving: 0,
  35. GridYMoving: 0,
  36. wMoving: 0,
  37. hMoving: 0,
  38. placeholderShow: false,
  39. placeholderMoving: false,
  40. layout: layout,
  41. containerHeight: 500
  42. };
  43. return _this;
  44. }
  45. Dragact.prototype.onDragStart = function (bundles) {
  46. var GridX = bundles.GridX, GridY = bundles.GridY, w = bundles.w, h = bundles.h, UniqueKey = bundles.UniqueKey;
  47. var newlayout = initiate_1.syncLayout(this.state.layout, UniqueKey, GridX, GridY, true);
  48. this.setState({
  49. GridXMoving: GridX,
  50. GridYMoving: GridY,
  51. wMoving: w,
  52. hMoving: h,
  53. placeholderShow: true,
  54. placeholderMoving: true,
  55. layout: newlayout,
  56. });
  57. this.props.onDragStart && this.props.onDragStart(bundles);
  58. };
  59. Dragact.prototype.onDrag = function (layoutItem) {
  60. var GridY = layoutItem.GridY, UniqueKey = layoutItem.UniqueKey;
  61. var moving = GridY - this.state.GridYMoving;
  62. var newLayout = collison_1.layoutCheck(this.state.layout, layoutItem, UniqueKey, UniqueKey /*用户移动方块的key */, moving);
  63. var compactedLayout = compact_1.compactLayout(newLayout);
  64. for (var i = 0; i < compactedLayout.length; i++) {
  65. var compactedItem = compactedLayout[i];
  66. if (UniqueKey === compactedItem.key) {
  67. /**
  68. * 特殊点:当我们移动元素的时候,元素在layout中的位置不断改变
  69. * 但是当isUserMove=true的时候,鼠标拖拽的元素不会随着位图变化而变化
  70. * 但是实际layout中的位置还是会改变
  71. * (isUserMove=true用于解除placeholder和元素的绑定)
  72. */
  73. compactedItem.isUserMove = true;
  74. layoutItem.GridX = compactedItem.GridX;
  75. layoutItem.GridY = compactedItem.GridY;
  76. break;
  77. }
  78. }
  79. this.setState({
  80. GridXMoving: layoutItem.GridX,
  81. GridYMoving: layoutItem.GridY,
  82. layout: compactedLayout,
  83. containerHeight: sort_1.getMaxContainerHeight(compactedLayout, this.props.rowHeight, this.props.margin[1])
  84. });
  85. this.props.onDrag && this.props.onDrag(layoutItem);
  86. };
  87. Dragact.prototype.onDragEnd = function (key) {
  88. var compactedLayout = compact_1.compactLayout(this.state.layout);
  89. this.setState({
  90. placeholderShow: false,
  91. layout: compactedLayout,
  92. containerHeight: sort_1.getMaxContainerHeight(compactedLayout, this.props.rowHeight, this.props.margin[1])
  93. });
  94. this.props.onDragEnd && this.props.onDragEnd(key);
  95. };
  96. Dragact.prototype.renderPlaceholder = function () {
  97. if (!this.state.placeholderShow)
  98. return null;
  99. var _a = this.props, col = _a.col, width = _a.width, padding = _a.padding, rowHeight = _a.rowHeight, margin = _a.margin;
  100. var _b = this.state, GridXMoving = _b.GridXMoving, GridYMoving = _b.GridYMoving, wMoving = _b.wMoving, hMoving = _b.hMoving, placeholderMoving = _b.placeholderMoving;
  101. if (!padding)
  102. padding = 0;
  103. return (React.createElement(GridItem_1.default, { margin: margin, col: col, containerWidth: width, containerPadding: [padding, padding], rowHeight: rowHeight, GridX: GridXMoving, GridY: GridYMoving, w: wMoving, h: hMoving, style: { background: '#d6e4ff', zIndex: 1, transition: ' all .15s' }, isUserMove: !placeholderMoving }));
  104. };
  105. Dragact.prototype.componentDidMount = function () {
  106. var _this = this;
  107. setTimeout(function () {
  108. var layout = correction_1.correctLayout(_this.state.layout, _this.props.col);
  109. var compacted = compact_1.compactLayout(layout);
  110. _this.setState({
  111. layout: compacted,
  112. containerHeight: sort_1.getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1])
  113. });
  114. }, 1);
  115. };
  116. Dragact.prototype.getGridItem = function (child, index) {
  117. var layout = this.state.layout;
  118. var _a = this.props, col = _a.col, width = _a.width, padding = _a.padding, rowHeight = _a.rowHeight, margin = _a.margin;
  119. var renderItem = initiate_1.layoutItemForkey(layout, child.key); //TODO:可以优化速度,这一步不是必须;
  120. if (renderItem) {
  121. if (!padding)
  122. padding = 0;
  123. return (React.createElement(GridItem_1.default, { margin: margin, col: col, containerWidth: width, containerPadding: [padding, padding], rowHeight: rowHeight, GridX: renderItem.GridX, GridY: renderItem.GridY, w: renderItem.w, h: renderItem.h, onDrag: this.onDrag, onDragStart: this.onDragStart, onDragEnd: this.onDragEnd, isUserMove: renderItem.isUserMove !== void 666 ? renderItem.isUserMove : false, UniqueKey: child.key, style: { zIndex: 2 }, static: renderItem.static }, child));
  124. }
  125. };
  126. Dragact.prototype.render = function () {
  127. var _this = this;
  128. var _a = this.props, width = _a.width, className = _a.className;
  129. var containerHeight = this.state.containerHeight;
  130. return (React.createElement("div", { className: utils_1.stringJoin('DraggerLayout', className + ''), style: { left: 100, width: width, height: containerHeight, zIndex: 1 } },
  131. React.Children.map(this.props.children, function (child, index) { return _this.getGridItem(child, index); }),
  132. this.renderPlaceholder()));
  133. };
  134. return Dragact;
  135. }(React.Component));
  136. exports.Dragact = Dragact;