dragact.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 { getDataSet, stringJoin } from './utils';
  26. import { layoutItemForkey, syncLayout, MapLayoutTostate } 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. var sync = syncLayout(_this.state.layout, layoutItem);
  35. _this.setState({
  36. GridXMoving: GridX,
  37. GridYMoving: GridY,
  38. wMoving: w,
  39. hMoving: h,
  40. placeholderShow: true,
  41. placeholderMoving: true,
  42. layout: sync,
  43. dragType: 'resize'
  44. });
  45. };
  46. _this.onResizing = function (layoutItem) {
  47. var newLayout = layoutCheck(_this.state.layout, layoutItem, layoutItem.UniqueKey, layoutItem.UniqueKey, 0);
  48. var _a = compactLayout(newLayout, layoutItem), compacted = _a.compacted, mapLayout = _a.mapLayout;
  49. _this.setState({
  50. layout: compacted,
  51. wMoving: layoutItem.w,
  52. hMoving: layoutItem.h,
  53. mapLayout: mapLayout,
  54. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1])
  55. });
  56. };
  57. _this.onResizeEnd = function (layoutItem) {
  58. var _a = compactLayout(_this.state.layout, undefined), compacted = _a.compacted, mapLayout = _a.mapLayout;
  59. _this.setState({
  60. placeholderShow: false,
  61. layout: compacted,
  62. mapLayout: mapLayout,
  63. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1])
  64. });
  65. _this.props.onDragEnd && _this.props.onDragEnd(layoutItem);
  66. };
  67. _this.onDrag = _this.onDrag.bind(_this);
  68. _this.onDragStart = _this.onDragStart.bind(_this);
  69. _this.onDragEnd = _this.onDragEnd.bind(_this);
  70. var layout = props.layout ?
  71. MapLayoutTostate(props.layout, props.children)
  72. :
  73. getDataSet(props.children);
  74. _this.state = {
  75. GridXMoving: 0,
  76. GridYMoving: 0,
  77. wMoving: 0,
  78. hMoving: 0,
  79. placeholderShow: false,
  80. placeholderMoving: false,
  81. layout: layout,
  82. containerHeight: 500,
  83. dragType: 'drag',
  84. mapLayout: undefined
  85. };
  86. return _this;
  87. }
  88. Dragact.prototype.onDragStart = function (bundles) {
  89. var GridX = bundles.GridX, GridY = bundles.GridY, w = bundles.w, h = bundles.h;
  90. var newlayout = syncLayout(this.state.layout, bundles);
  91. this.setState({
  92. GridXMoving: GridX,
  93. GridYMoving: GridY,
  94. wMoving: w,
  95. hMoving: h,
  96. placeholderShow: true,
  97. placeholderMoving: true,
  98. layout: newlayout,
  99. dragType: 'drag'
  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), 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])
  114. });
  115. this.props.onDrag && this.props.onDrag(layoutItem);
  116. };
  117. Dragact.prototype.onDragEnd = function (layoutItem) {
  118. var _a = compactLayout(this.state.layout, undefined), 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])
  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;
  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 (!padding)
  133. padding = 0;
  134. 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, canDrag: false, canResize: false }));
  135. };
  136. Dragact.prototype.componentWillReceiveProps = function (nextProps) {
  137. if (this.props.children.length > nextProps.children.length) {
  138. var mapLayoutCopy_1 = __assign({}, this.state.mapLayout);
  139. nextProps.children.forEach(function (child) {
  140. if (mapLayoutCopy_1[child.key] !== void 666)
  141. delete mapLayoutCopy_1[child.key];
  142. });
  143. var _loop_1 = function (key) {
  144. var newLayout_1 = this_1.state.layout.filter(function (child) {
  145. if (child.key !== key)
  146. return child;
  147. });
  148. var _a = compactLayout(newLayout_1, undefined), compacted = _a.compacted, mapLayout = _a.mapLayout;
  149. this_1.setState({
  150. containerHeight: getMaxContainerHeight(compacted, this_1.props.rowHeight, this_1.props.margin[1]),
  151. layout: compacted,
  152. mapLayout: mapLayout
  153. });
  154. };
  155. var this_1 = this;
  156. for (var key in mapLayoutCopy_1) {
  157. _loop_1(key);
  158. }
  159. }
  160. if (this.props.children.length < nextProps.children.length) {
  161. var item;
  162. for (var idx in nextProps.children) {
  163. var i = nextProps.children[idx];
  164. if (this.state.mapLayout && !this.state.mapLayout[i.key]) {
  165. item = i;
  166. break;
  167. }
  168. }
  169. if (item !== void 666) {
  170. var dataSet = __assign({}, item.props['data-set'], { isUserMove: false, key: item.key });
  171. var newLayout = this.state.layout.concat([dataSet]);
  172. newLayout = correctLayout(newLayout, this.props.col);
  173. var _a = compactLayout(newLayout, undefined), compacted = _a.compacted, mapLayout = _a.mapLayout;
  174. console.log(mapLayout);
  175. // console.log(layout)
  176. this.setState({
  177. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1]),
  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), 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])
  193. });
  194. }, 1);
  195. };
  196. Dragact.prototype.getGridItem = function (child, index) {
  197. var _a = this.state, dragType = _a.dragType, mapLayout = _a.mapLayout;
  198. var _b = this.props, col = _b.col, width = _b.width, padding = _b.padding, rowHeight = _b.rowHeight, margin = _b.margin;
  199. if (mapLayout) {
  200. var renderItem = layoutItemForkey(mapLayout, child.key);
  201. if (!padding)
  202. padding = 0;
  203. 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, handle: renderItem.handle, canDrag: renderItem.canDrag, canResize: renderItem.canResize }, child));
  204. }
  205. };
  206. Dragact.prototype.render = function () {
  207. var _this = this;
  208. var _a = this.props, width = _a.width, className = _a.className;
  209. var containerHeight = this.state.containerHeight;
  210. return (React.createElement("div", { className: stringJoin('DraggerLayout', className + ''), style: { left: 100, width: width, height: containerHeight, zIndex: 1 } },
  211. React.Children.map(this.props.children, function (child, index) { return _this.getGridItem(child, index); }),
  212. this.renderPlaceholder()));
  213. };
  214. //api
  215. Dragact.prototype.getLayout = function () {
  216. return this.state.layout;
  217. };
  218. //api
  219. Dragact.prototype.deleteItem = function (key) {
  220. };
  221. return Dragact;
  222. }(React.Component));
  223. export { Dragact };