dragact.js 13 KB

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