dragact.js 12 KB

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