dragact.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. var newlayout = syncLayout(this.state.mapLayout, bundles);
  91. this.setState({
  92. GridXMoving: GridX,
  93. GridYMoving: GridY,
  94. wMoving: w,
  95. hMoving: h,
  96. placeholderShow: true,
  97. placeholderMoving: true,
  98. mapLayout: newlayout,
  99. dragType: 'drag'
  100. });
  101. }
  102. this.props.onDragStart && this.props.onDragStart(bundles);
  103. };
  104. Dragact.prototype.onDrag = function (layoutItem) {
  105. var GridY = layoutItem.GridY, UniqueKey = layoutItem.UniqueKey;
  106. var moving = GridY - this.state.GridYMoving;
  107. var newLayout = layoutCheck(this.state.layout, layoutItem, UniqueKey, UniqueKey /*用户移动方块的key */, moving);
  108. var _a = compactLayout(newLayout, layoutItem, this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  109. this.setState({
  110. GridXMoving: layoutItem.GridX,
  111. GridYMoving: layoutItem.GridY,
  112. layout: compacted,
  113. mapLayout: mapLayout,
  114. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  115. });
  116. this.props.onDrag && this.props.onDrag(layoutItem);
  117. };
  118. Dragact.prototype.onDragEnd = function (layoutItem) {
  119. var _a = compactLayout(this.state.layout, undefined, this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  120. this.setState({
  121. placeholderShow: false,
  122. layout: compacted,
  123. mapLayout: mapLayout,
  124. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight)
  125. });
  126. this.props.onDragEnd && this.props.onDragEnd(layoutItem);
  127. };
  128. Dragact.prototype.renderPlaceholder = function () {
  129. if (!this.state.placeholderShow)
  130. return null;
  131. var _a = this.props, col = _a.col, width = _a.width, padding = _a.padding, rowHeight = _a.rowHeight, margin = _a.margin, placeholder = _a.placeholder;
  132. var _b = this.state, GridXMoving = _b.GridXMoving, GridYMoving = _b.GridYMoving, wMoving = _b.wMoving, hMoving = _b.hMoving, placeholderMoving = _b.placeholderMoving, dragType = _b.dragType;
  133. if (!placeholder)
  134. return null;
  135. if (!padding)
  136. padding = 0;
  137. 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 }));
  138. };
  139. Dragact.prototype.componentWillReceiveProps = function (nextProps) {
  140. if (this.props.children.length > nextProps.children.length) {
  141. var mapLayoutCopy_1 = __assign({}, this.state.mapLayout);
  142. nextProps.children.forEach(function (child) {
  143. if (mapLayoutCopy_1[child.key] !== void 666)
  144. delete mapLayoutCopy_1[child.key];
  145. });
  146. var _loop_1 = function (key) {
  147. var newLayout_1 = this_1.state.layout.filter(function (child) {
  148. if (child.key !== key)
  149. return child;
  150. });
  151. var _a = compactLayout(newLayout_1, undefined, this_1.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  152. this_1.setState({
  153. containerHeight: getMaxContainerHeight(compacted, this_1.props.rowHeight, this_1.props.margin[1], this_1.state.containerHeight),
  154. layout: compacted,
  155. mapLayout: mapLayout
  156. });
  157. };
  158. var this_1 = this;
  159. for (var key in mapLayoutCopy_1) {
  160. _loop_1(key);
  161. }
  162. }
  163. if (this.props.children.length < nextProps.children.length) {
  164. var item;
  165. for (var idx in nextProps.children) {
  166. var i = nextProps.children[idx];
  167. if (this.state.mapLayout && !this.state.mapLayout[i.key]) {
  168. item = i;
  169. break;
  170. }
  171. }
  172. if (item !== void 666) {
  173. var dataSet = __assign({}, item.props['data-set'], { isUserMove: false, key: item.key });
  174. var newLayout = this.state.layout.concat([dataSet]);
  175. newLayout = correctLayout(newLayout, this.props.col);
  176. var _a = compactLayout(newLayout, undefined, this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  177. this.setState({
  178. containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1], this.state.containerHeight),
  179. layout: compacted,
  180. mapLayout: mapLayout
  181. });
  182. }
  183. }
  184. };
  185. Dragact.prototype.componentDidMount = function () {
  186. var _this = this;
  187. setTimeout(function () {
  188. var layout = correctLayout(_this.state.layout, _this.props.col);
  189. var _a = compactLayout(layout, undefined, _this.state.mapLayout), compacted = _a.compacted, mapLayout = _a.mapLayout;
  190. _this.setState({
  191. layout: compacted,
  192. mapLayout: mapLayout,
  193. containerHeight: getMaxContainerHeight(compacted, _this.props.rowHeight, _this.props.margin[1], _this.state.containerHeight)
  194. });
  195. }, 1);
  196. };
  197. Dragact.prototype.getGridItem = function (child, index) {
  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 = layoutItemForkey(mapLayout, child.key);
  202. if (!padding)
  203. padding = 0;
  204. return (React.createElement(GridItem, __assign({}, renderItem, { margin: margin, col: col, containerWidth: width, containerPadding: [padding, padding], rowHeight: rowHeight, onDrag: this.onDrag, onDragStart: this.onDragStart, onDragEnd: this.onDragEnd, isUserMove: renderItem.isUserMove !== void 666 ? renderItem.isUserMove : false, UniqueKey: child.key, onResizing: this.onResizing, onResizeStart: this.onResizeStart, onResizeEnd: this.onResizeEnd, dragType: dragType, key: child.key }), this.props.children(child, renderItem.isUserMove)));
  205. }
  206. };
  207. Dragact.prototype.render = function () {
  208. var _this = this;
  209. var _a = this.props, width = _a.width, className = _a.className, layout = _a.layout, style = _a.style;
  210. var containerHeight = this.state.containerHeight;
  211. return (React.createElement("div", { className: stringJoin('DraggerLayout', className + ''), style: __assign({}, style, { left: 100, width: width, height: containerHeight, zIndex: 1 }) },
  212. layout.map(function (item, index) {
  213. return _this.getGridItem(item, index);
  214. }),
  215. this.renderPlaceholder()));
  216. };
  217. //api
  218. Dragact.prototype.getLayout = function () {
  219. return this.state.layout;
  220. };
  221. //api
  222. Dragact.prototype.deleteItem = function (key) {
  223. };
  224. return Dragact;
  225. }(React.Component));
  226. export { Dragact };