dragact.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 GridItem from './GridItem';
  21. import { compactLayout } from './util/compact';
  22. import { getMaxContainerHeight } from './util/sort';
  23. import { layoutCheck } from './util/collison';
  24. import { correctLayout } from './util/correction';
  25. import { stringJoin } from './utils';
  26. import { layoutItemForkey, syncLayout } from './util/initiate';
  27. import './style.css';
  28. var Dragact = /** @class */ (function (_super) {
  29. __extends(Dragact, _super);
  30. function Dragact(props) {
  31. var _this = _super.call(this, props) || this;
  32. _this.onResizeStart = function (layoutItem) {
  33. var GridX = layoutItem.GridX, GridY = layoutItem.GridY, w = layoutItem.w, h = layoutItem.h;
  34. if (_this.state.mapLayout) {
  35. var newlayout = syncLayout(_this.state.mapLayout, layoutItem);
  36. _this.setState({
  37. GridXMoving: GridX,
  38. GridYMoving: GridY,
  39. wMoving: w,
  40. hMoving: h,
  41. placeholderShow: true,
  42. placeholderMoving: true,
  43. mapLayout: newlayout,
  44. dragType: 'resize'
  45. });
  46. }
  47. };
  48. _this.onResizing = function (layoutItem) {
  49. var newLayout = layoutCheck(_this.state.layout, layoutItem, layoutItem.UniqueKey, layoutItem.UniqueKey, 0);
  50. var _a = compactLayout(newLayout, layoutItem, _this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  51. _this.setState({
  52. layout: compacted,
  53. wMoving: layoutItem.w,
  54. hMoving: layoutItem.h,
  55. mapLayout: mapLayout,
  56. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1], _this.state.containerHeight)
  57. });
  58. };
  59. _this.onResizeEnd = function (layoutItem) {
  60. var _a = compactLayout(_this.state.layout, undefined, _this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  61. _this.setState({
  62. placeholderShow: false,
  63. layout: compacted,
  64. mapLayout: mapLayout,
  65. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1], _this.state.containerHeight)
  66. });
  67. _this.props.onDragEnd && _this.props.onDragEnd(layoutItem);
  68. };
  69. _this.onDrag = _this.onDrag.bind(_this);
  70. _this.onDragStart = _this.onDragStart.bind(_this);
  71. _this.onDragEnd = _this.onDragEnd.bind(_this);
  72. var layout = props.layout;
  73. _this.state = {
  74. GridXMoving: 0,
  75. GridYMoving: 0,
  76. wMoving: 0,
  77. hMoving: 0,
  78. placeholderShow: false,
  79. placeholderMoving: false,
  80. layout: layout,
  81. containerHeight: 500,
  82. dragType: 'drag',
  83. mapLayout: undefined
  84. };
  85. return _this;
  86. }
  87. Dragact.prototype.onDragStart = function (bundles) {
  88. var GridX = bundles.GridX, GridY = bundles.GridY, w = bundles.w, h = bundles.h;
  89. if (this.state.mapLayout) {
  90. this.setState({
  91. GridXMoving: GridX,
  92. GridYMoving: GridY,
  93. wMoving: w,
  94. hMoving: h,
  95. placeholderShow: true,
  96. placeholderMoving: true,
  97. mapLayout: syncLayout(this.state.mapLayout, bundles),
  98. dragType: 'drag'
  99. });
  100. }
  101. this.props.onDragStart && this.props.onDragStart(bundles);
  102. };
  103. Dragact.prototype.onDrag = function (layoutItem) {
  104. var GridY = layoutItem.GridY, UniqueKey = layoutItem.UniqueKey;
  105. var moving = GridY - this.state.GridYMoving;
  106. var newLayout = layoutCheck(this.state.layout, layoutItem, UniqueKey, UniqueKey /*用户移动方块的key */, moving);
  107. var _a = compactLayout(newLayout, layoutItem, this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  108. this.setState({
  109. GridXMoving: layoutItem.GridX,
  110. GridYMoving: layoutItem.GridY,
  111. layout: compacted,
  112. mapLayout: mapLayout,
  113. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  114. });
  115. this.props.onDrag && this.props.onDrag(layoutItem);
  116. };
  117. Dragact.prototype.onDragEnd = function (layoutItem) {
  118. var _a = compactLayout(this.state.layout, undefined, this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  119. this.setState({
  120. placeholderShow: false,
  121. layout: compacted,
  122. mapLayout: mapLayout,
  123. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  124. });
  125. this.props.onDragEnd && this.props.onDragEnd(layoutItem);
  126. };
  127. Dragact.prototype.renderPlaceholder = function () {
  128. if (!this.state.placeholderShow)
  129. return null;
  130. var _a = this.props, col = _a.col, width = _a.width, padding = _a.padding, rowHeight = _a.rowHeight, margin = _a.margin, placeholder = _a.placeholder;
  131. var _b = this.state, GridXMoving = _b.GridXMoving, GridYMoving = _b.GridYMoving, wMoving = _b.wMoving, hMoving = _b.hMoving, placeholderMoving = _b.placeholderMoving, dragType = _b.dragType;
  132. if (!placeholder)
  133. return null;
  134. if (!padding)
  135. padding = 0;
  136. 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 ease-out' }, isUserMove: !placeholderMoving, dragType: dragType, canDrag: false, canResize: false }, function (p, resizerProps) { return React.createElement("div", __assign({}, p)); }));
  137. };
  138. Dragact.prototype.componentWillReceiveProps = function (nextProps) {
  139. if (this.props.children.length > nextProps.children.length) {
  140. var mapLayoutCopy_1 = __assign({}, this.state.mapLayout);
  141. nextProps.children.forEach(function (child) {
  142. if (mapLayoutCopy_1[child.key] !== void 666)
  143. delete mapLayoutCopy_1[child.key];
  144. });
  145. var _loop_1 = function (key) {
  146. var newLayout_1 = this_1.state.layout.filter(function (child) {
  147. if (child.key !== key)
  148. return child;
  149. });
  150. var _a = compactLayout(newLayout_1, undefined, this_1.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  151. this_1.setState({
  152. containerHeight: getMaxContainerHeight(compacted, this_1.props.rowHeight, this_1.props.margin[1], this_1.state.containerHeight),
  153. layout: compacted,
  154. mapLayout: mapLayout
  155. });
  156. };
  157. var this_1 = this;
  158. for (var key in mapLayoutCopy_1) {
  159. _loop_1(key);
  160. }
  161. }
  162. if (this.props.children.length < nextProps.children.length) {
  163. var item;
  164. for (var idx in nextProps.children) {
  165. var i = nextProps.children[idx];
  166. if (this.state.mapLayout && !this.state.mapLayout[i.key]) {
  167. item = i;
  168. break;
  169. }
  170. }
  171. if (item !== void 666) {
  172. var dataSet = __assign({}, item.props['data-set'], { isUserMove: false, key: item.key });
  173. var newLayout = this.state.layout.concat([dataSet]);
  174. newLayout = correctLayout(newLayout, this.props.col);
  175. var _a = compactLayout(newLayout, undefined, this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  176. this.setState({
  177. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight),
  178. layout: compacted,
  179. mapLayout: mapLayout
  180. });
  181. }
  182. }
  183. };
  184. Dragact.prototype.componentDidMount = function () {
  185. var _this = this;
  186. setTimeout(function () {
  187. var layout = correctLayout(_this.state.layout, _this.props.col);
  188. var _a = compactLayout(layout, undefined, _this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  189. _this.setState({
  190. layout: compacted,
  191. mapLayout: mapLayout,
  192. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1], _this.state.containerHeight)
  193. });
  194. }, 1);
  195. };
  196. Dragact.prototype.getGridItem = function (child, index) {
  197. var _this = this;
  198. var _a = this.state, dragType = _a.dragType, mapLayout = _a.mapLayout;
  199. var _b = this.props, col = _b.col, width = _b.width, padding = _b.padding, rowHeight = _b.rowHeight, margin = _b.margin;
  200. if (mapLayout) {
  201. var renderItem_1 = layoutItemForkey(mapLayout, child.key);
  202. if (!padding)
  203. padding = 0;
  204. return (React.createElement(GridItem, __assign({}, renderItem_1, { margin: margin, col: col, containerWidth: width, containerPadding: [padding, padding], rowHeight: rowHeight, onDrag: this.onDrag, onDragStart: this.onDragStart, onDragEnd: this.onDragEnd, isUserMove: renderItem_1.isUserMove !== void 666 ? renderItem_1.isUserMove : false, UniqueKey: child.key, onResizing: this.onResizing, onResizeStart: this.onResizeStart, onResizeEnd: this.onResizeEnd, dragType: dragType, key: child.key }), function (GridItemProvided, dragHandle, resizeHandle) { return _this.props.children(child, {
  205. isDragging: renderItem_1.isUserMove !== void 666 ? renderItem_1.isUserMove : false,
  206. props: GridItemProvided,
  207. dragHandle: dragHandle,
  208. resizeHandle: resizeHandle
  209. }); }));
  210. }
  211. };
  212. Dragact.prototype.render = function () {
  213. var _this = this;
  214. var _a = this.props, width = _a.width, className = _a.className, layout = _a.layout, style = _a.style;
  215. var containerHeight = this.state.containerHeight;
  216. return (React.createElement("div", { className: stringJoin('DraggerLayout', className + ''), style: __assign({}, style, { left: 100, width: width, height: containerHeight, zIndex: 1 }) },
  217. layout.map(function (item, index) {
  218. return _this.getGridItem(item, index);
  219. }),
  220. this.renderPlaceholder()));
  221. };
  222. //api
  223. Dragact.prototype.getLayout = function () {
  224. return this.state.layout;
  225. };
  226. //api
  227. Dragact.prototype.deleteItem = function (key) {
  228. };
  229. return Dragact;
  230. }(React.Component));
  231. export { Dragact };