dragact.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. import * as React from "react";
  12. import GridItem from './GridItem';
  13. import { compactLayout } from './util/compact';
  14. import { getMaxContainerHeight } from './util/sort';
  15. import { layoutCheck } from './util/collison';
  16. import { correctLayout } from './util/correction';
  17. import { getDataSet, stringJoin } from './utils';
  18. import { layoutItemForkey, syncLayout, MapLayoutTostate } from './util/initiate';
  19. import './style.css';
  20. var Dragact = /** @class */ (function (_super) {
  21. __extends(Dragact, _super);
  22. function Dragact(props) {
  23. var _this = _super.call(this, props) || this;
  24. _this.onResizeStart = function (layoutItem) {
  25. var GridX = layoutItem.GridX, GridY = layoutItem.GridY, w = layoutItem.w, h = layoutItem.h, UniqueKey = layoutItem.UniqueKey;
  26. var sync = syncLayout(_this.state.layout, UniqueKey, GridX, GridY, true);
  27. _this.setState({
  28. GridXMoving: GridX,
  29. GridYMoving: GridY,
  30. wMoving: w,
  31. hMoving: h,
  32. placeholderShow: true,
  33. placeholderMoving: true,
  34. layout: sync,
  35. dragType: 'resize'
  36. });
  37. };
  38. _this.onResizing = function (layoutItem) {
  39. var newLayout = layoutCheck(_this.state.layout, layoutItem, layoutItem.UniqueKey, layoutItem.UniqueKey, 0);
  40. var compacted = compactLayout(newLayout);
  41. for (var i = 0; i < compacted.length; i++) {
  42. var compactedItem = compacted[i];
  43. if (layoutItem.UniqueKey === compactedItem.key) {
  44. /**
  45. * 特殊点:当我们移动元素的时候,元素在layout中的位置不断改变
  46. * 但是当isUserMove=true的时候,鼠标拖拽的元素不会随着位图变化而变化
  47. * 但是实际layout中的位置还是会改变
  48. * (isUserMove=true用于解除placeholder和元素的绑定)
  49. */
  50. compactedItem.isUserMove = true;
  51. break;
  52. }
  53. }
  54. _this.setState({
  55. layout: compacted,
  56. wMoving: layoutItem.w,
  57. hMoving: layoutItem.h,
  58. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1])
  59. });
  60. };
  61. _this.onResizeEnd = function (layoutItem) {
  62. var compactedLayout = compactLayout(_this.state.layout);
  63. _this.setState({
  64. placeholderShow: false,
  65. layout: compactedLayout,
  66. containerHeight: getMaxContainerHeight(compactedLayout, _this.props.rowHeight, _this.props.margin[1])
  67. });
  68. _this.props.onDragEnd && _this.props.onDragEnd(layoutItem);
  69. };
  70. _this.onDrag = _this.onDrag.bind(_this);
  71. _this.onDragStart = _this.onDragStart.bind(_this);
  72. _this.onDragEnd = _this.onDragEnd.bind(_this);
  73. var layout = props.layout ?
  74. MapLayoutTostate(props.layout, props.children)
  75. :
  76. getDataSet(props.children);
  77. _this.state = {
  78. GridXMoving: 0,
  79. GridYMoving: 0,
  80. wMoving: 0,
  81. hMoving: 0,
  82. placeholderShow: false,
  83. placeholderMoving: false,
  84. layout: layout,
  85. containerHeight: 500,
  86. dragType: 'drag'
  87. };
  88. return _this;
  89. }
  90. Dragact.prototype.onDragStart = function (bundles) {
  91. var GridX = bundles.GridX, GridY = bundles.GridY, w = bundles.w, h = bundles.h, UniqueKey = bundles.UniqueKey;
  92. var newlayout = syncLayout(this.state.layout, UniqueKey, GridX, GridY, true);
  93. this.setState({
  94. GridXMoving: GridX,
  95. GridYMoving: GridY,
  96. wMoving: w,
  97. hMoving: h,
  98. placeholderShow: true,
  99. placeholderMoving: true,
  100. layout: newlayout,
  101. dragType: 'drag'
  102. });
  103. this.props.onDragStart && this.props.onDragStart(bundles);
  104. };
  105. Dragact.prototype.onDrag = function (layoutItem) {
  106. var GridY = layoutItem.GridY, UniqueKey = layoutItem.UniqueKey;
  107. var moving = GridY - this.state.GridYMoving;
  108. var newLayout = layoutCheck(this.state.layout, layoutItem, UniqueKey, UniqueKey /*用户移动方块的key */, moving);
  109. var compactedLayout = compactLayout(newLayout);
  110. for (var i = 0; i < compactedLayout.length; i++) {
  111. var compactedItem = compactedLayout[i];
  112. if (UniqueKey === compactedItem.key) {
  113. /**
  114. * 特殊点:当我们移动元素的时候,元素在layout中的位置不断改变
  115. * 但是当isUserMove=true的时候,鼠标拖拽的元素不会随着位图变化而变化
  116. * 但是实际layout中的位置还是会改变
  117. * (isUserMove=true用于解除placeholder和元素的绑定)
  118. */
  119. compactedItem.isUserMove = true;
  120. layoutItem.GridX = compactedItem.GridX;
  121. layoutItem.GridY = compactedItem.GridY;
  122. break;
  123. }
  124. }
  125. this.setState({
  126. GridXMoving: layoutItem.GridX,
  127. GridYMoving: layoutItem.GridY,
  128. layout: compactedLayout,
  129. containerHeight: getMaxContainerHeight(compactedLayout, this.props.rowHeight, this.props.margin[1])
  130. });
  131. this.props.onDrag && this.props.onDrag(layoutItem);
  132. };
  133. Dragact.prototype.onDragEnd = function (layoutItem) {
  134. var compactedLayout = compactLayout(this.state.layout);
  135. this.setState({
  136. placeholderShow: false,
  137. layout: compactedLayout,
  138. containerHeight: getMaxContainerHeight(compactedLayout, this.props.rowHeight, this.props.margin[1])
  139. });
  140. this.props.onDragEnd && this.props.onDragEnd(layoutItem);
  141. };
  142. Dragact.prototype.renderPlaceholder = function () {
  143. if (!this.state.placeholderShow)
  144. return null;
  145. var _a = this.props, col = _a.col, width = _a.width, padding = _a.padding, rowHeight = _a.rowHeight, margin = _a.margin;
  146. var _b = this.state, GridXMoving = _b.GridXMoving, GridYMoving = _b.GridYMoving, wMoving = _b.wMoving, hMoving = _b.hMoving, placeholderMoving = _b.placeholderMoving, dragType = _b.dragType;
  147. if (!padding)
  148. padding = 0;
  149. return (React.createElement(GridItem, { margin: margin, col: col, containerWidth: width, containerPadding: [padding, padding], rowHeight: rowHeight, GridX: GridXMoving, GridY: GridYMoving, w: wMoving, h: hMoving, style: { background: 'rgba(15,15,15,0.3)', zIndex: dragType === 'drag' ? 1 : 10, transition: ' all .15s' }, isUserMove: !placeholderMoving, dragType: dragType }));
  150. };
  151. Dragact.prototype.componentDidMount = function () {
  152. var _this = this;
  153. setTimeout(function () {
  154. var layout = correctLayout(_this.state.layout, _this.props.col);
  155. var compacted = compactLayout(layout);
  156. _this.setState({
  157. layout: compacted,
  158. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1])
  159. });
  160. }, 1);
  161. };
  162. Dragact.prototype.getGridItem = function (child, index) {
  163. var _a = this.state, layout = _a.layout, dragType = _a.dragType;
  164. var _b = this.props, col = _b.col, width = _b.width, padding = _b.padding, rowHeight = _b.rowHeight, margin = _b.margin;
  165. var renderItem = layoutItemForkey(layout, child.key); //TODO:可以优化速度,这一步不是必须;
  166. if (renderItem) {
  167. if (!padding)
  168. padding = 0;
  169. return (React.createElement(GridItem, { 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, static: renderItem.static, onResizing: this.onResizing, onResizeStart: this.onResizeStart, onResizeEnd: this.onResizeEnd, dragType: dragType }, child));
  170. }
  171. };
  172. Dragact.prototype.render = function () {
  173. var _this = this;
  174. var _a = this.props, width = _a.width, className = _a.className;
  175. var containerHeight = this.state.containerHeight;
  176. return (React.createElement("div", { className: stringJoin('DraggerLayout', className + ''), style: { left: 100, width: width, height: containerHeight, zIndex: 1 } },
  177. React.Children.map(this.props.children, function (child, index) { return _this.getGridItem(child, index); }),
  178. this.renderPlaceholder()));
  179. };
  180. //api
  181. Dragact.prototype.getLayout = function () {
  182. return this.state.layout;
  183. };
  184. return Dragact;
  185. }(React.Component));
  186. export { Dragact };