dragactList.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 { compactLayout } from '../util/compact';
  21. // import { getMaxContainerHeight } from '../util/sort';
  22. // import { layoutCheck } from '../util/collison';
  23. import { stringJoin } from '../utils';
  24. // import { syncLayout } from '../util/initiate';
  25. import './index.css';
  26. import { Dragger } from "../dragger/index";
  27. // interface DragactState {
  28. // GridXMoving: number
  29. // GridYMoving: number
  30. // wMoving: number
  31. // hMoving: number
  32. // placeholderShow: Boolean
  33. // placeholderMoving: Boolean
  34. // layout: any;
  35. // containerHeight: number
  36. // dragType: 'drag' | 'resize'
  37. // mapLayout: mapLayout | undefined
  38. // col: number
  39. // }
  40. var Column = /** @class */ (function (_super) {
  41. __extends(Column, _super);
  42. function Column() {
  43. return _super !== null && _super.apply(this, arguments) || this;
  44. }
  45. Column.prototype.render = function () {
  46. return this.props.children;
  47. };
  48. return Column;
  49. }(React.Component));
  50. export { Column };
  51. var checkInContainer = function (GridX, GridY) {
  52. /**防止元素出container */
  53. // if (GridY < 0) GridY = 0//上边界
  54. return { GridX: GridX, GridY: GridY };
  55. };
  56. var ListCell = /** @class */ (function (_super) {
  57. __extends(ListCell, _super);
  58. function ListCell() {
  59. var _this = _super !== null && _super.apply(this, arguments) || this;
  60. _this.onDragStart = function (x, y) {
  61. var _a = _this.calGridXY(x, y), GridX = _a.GridX, GridY = _a.GridY;
  62. var _b = _this.props, UniqueKey = _b.UniqueKey, currentListIndex = _b.currentListIndex;
  63. _this.props.onDragStart({ key: UniqueKey, x: GridX, y: GridY, currentListIndex: currentListIndex });
  64. };
  65. _this.onDrag = function (event, x, y) {
  66. var _a = _this.calGridXY(x, y), GridX = _a.GridX, GridY = _a.GridY;
  67. var _b = _this.props, UniqueKey = _b.UniqueKey, currentListIndex = _b.currentListIndex;
  68. _this.props.onDrag({ key: UniqueKey, x: GridX, y: GridY, currentListIndex: currentListIndex });
  69. };
  70. _this.onDragEnd = function (event, x, y) {
  71. var _a = _this.calGridXY(x, y), GridX = _a.GridX, GridY = _a.GridY;
  72. var _b = _this.props, UniqueKey = _b.UniqueKey, currentListIndex = _b.currentListIndex;
  73. console.log(_this.props);
  74. _this.props.onDragEnd({ key: UniqueKey, x: GridX, y: GridY, currentListIndex: currentListIndex });
  75. };
  76. return _this;
  77. }
  78. ListCell.prototype.calGridXY = function (x, y) {
  79. var _a = this.props, margin = _a.margin, width = _a.width, col = _a.col, rowHeight = _a.rowHeight;
  80. var containerWidth = width * col + margin[0];
  81. /**坐标转换成格子的时候,无须计算margin */
  82. var GridX = Math.round(x / containerWidth * col);
  83. var GridY = Math.round(y / (rowHeight + (margin ? margin[1] : 0)));
  84. // /**防止元素出container */
  85. return checkInContainer(GridX, GridY);
  86. };
  87. ListCell.prototype.render = function () {
  88. var _a = this.props, currentListIndex = _a.currentListIndex, y = _a.y, isUserMove = _a.isUserMove, width = _a.width, style = _a.style, rowHeight = _a.rowHeight, margin = _a.margin;
  89. return (React.createElement(Dragger, { x: currentListIndex * width, y: Math.round(y * rowHeight + margin[1] * (y + 1)), style: __assign({}, style, { width: width, transition: this.props.isUserMove ? '' : 'all .2s' }), isUserMove: isUserMove, onDragStart: this.onDragStart, onMove: this.onDrag, canResize: false, onDragEnd: this.onDragEnd }, this.props.children));
  90. };
  91. return ListCell;
  92. }(React.Component));
  93. export { ListCell };
  94. export var collision = function (a, b) {
  95. if (a === b) {
  96. return 1;
  97. }
  98. if (a + 1 <= b)
  99. return -1;
  100. if (a >= b + 1)
  101. return -1;
  102. return 2;
  103. };
  104. var swapList = function (list, movingItem, firstKey) {
  105. var moving = [];
  106. var newList = list.map(function (oldItem) {
  107. if (oldItem.key !== movingItem.key) {
  108. var num = collision(oldItem.y, movingItem.y);
  109. if (num > 0) {
  110. // console.log(num)
  111. var offset = movingItem.y - 1;
  112. // if (movingItem.y > oldItem.y && movingItem.y < oldItem.y + 1) {
  113. // /**
  114. // * 元素向上移动时,元素的上面空间不足,则不移动这个元素
  115. // * 当元素移动到GridY>所要向上交换的元素时,就不会进入这里,直接交换元素
  116. // */
  117. // console.log('你来了')
  118. // offset = oldItem.y
  119. // }
  120. // }
  121. moving.push(__assign({}, oldItem, { y: offset, isUserMove: false }));
  122. return __assign({}, oldItem, { y: offset, isUserMove: false });
  123. }
  124. return oldItem;
  125. }
  126. else if (movingItem.key === firstKey) {
  127. /**永远保持用户移动的块是 isUserMove === true */
  128. return __assign({}, oldItem, movingItem);
  129. }
  130. return oldItem;
  131. });
  132. for (var i in moving) {
  133. newList = swapList(newList, moving[i], firstKey);
  134. }
  135. return newList;
  136. };
  137. /**获取layout中,item第一个碰撞到的物体 */
  138. export var getFirstCollison = function (layout, item) {
  139. for (var i = 0, length_1 = layout.length; i < length_1; i++) {
  140. if (collision(layout[i].y, item.y) > 0) {
  141. return layout[i];
  142. }
  143. }
  144. return null;
  145. };
  146. var compactCell = function (partialList, cell) {
  147. if (partialList.length === 0) {
  148. return __assign({}, cell, { y: 0 });
  149. }
  150. var newCell = __assign({}, cell);
  151. /**
  152. * 类似一个递归调用
  153. */
  154. while (true) {
  155. var FirstCollison = getFirstCollison(partialList, newCell);
  156. if (FirstCollison) {
  157. /**第一次发生碰撞时,就可以返回了 */
  158. return __assign({}, newCell, { y: FirstCollison.y + 1 });
  159. }
  160. newCell.y--;
  161. if (newCell.y < 0)
  162. return __assign({}, newCell, { y: 0 }); /**碰到边界的时候,返回 */
  163. }
  164. };
  165. export var compactList = function (list, movingItem) {
  166. var sort = list.sort(function (a, b) {
  167. if (a.y === b.y) {
  168. if (b.isUserMove)
  169. return 1;
  170. }
  171. if (a.y > b.y)
  172. return 1;
  173. return 0;
  174. });
  175. var needCompact = Array(list.length);
  176. var after = [];
  177. var mapList = {};
  178. for (var i in sort) {
  179. var finished = compactCell(after, sort[i]);
  180. if (movingItem) {
  181. if (movingItem.key === finished.key) {
  182. // finished.y = movingItem.y;
  183. finished.isUserMove = true;
  184. }
  185. else
  186. finished.isUserMove = false;
  187. }
  188. else
  189. finished.isUserMove = false;
  190. after.push(finished);
  191. needCompact[i] = finished;
  192. mapList[finished.key] = finished;
  193. }
  194. return {
  195. compacted: needCompact,
  196. maped: mapList
  197. };
  198. };
  199. var DragactList = /** @class */ (function (_super) {
  200. __extends(DragactList, _super);
  201. function DragactList(props) {
  202. var _this = _super.call(this, props) || this;
  203. _this.onDragStart = function (e) {
  204. var key = e.key, x = e.x;
  205. for (var idx in _this.state.layout[x]) {
  206. if (_this.state.layout[x][idx].key === key) {
  207. _this.state.layout[x][idx].isUserMove = true;
  208. break;
  209. }
  210. }
  211. _this.setState({
  212. layout: _this.state.layout,
  213. lastList: x
  214. });
  215. };
  216. _this.onDrag = function (e) {
  217. var key = e.key, x = e.x, y = e.y, currentListIndex = e.currentListIndex;
  218. if (!_this.state.layout[x])
  219. return; //如果超出列表,就返回
  220. if (x !== e.currentListIndex) {
  221. //移动到别的列表
  222. var i = _this.state.maped[x][key];
  223. if (!i) {
  224. _this.state.layout[x].push({ y: y, isUserMove: false, key: key, content: 'placeholder', currentListIndex: currentListIndex });
  225. }
  226. }
  227. // if (x !== this.state.lastList && this.state.lastList !== e.currentListIndex) {
  228. // const { lastList } = this.state;
  229. // //跟上一个不一样的时候
  230. // this.state.layout[lastList] = this.state.layout[lastList].filter((item: any) => {
  231. // if (item.key !== key) {
  232. // return item
  233. // }
  234. // })
  235. // const compact = compactList(this.state.layout[lastList], undefined);
  236. // this.state.layout[lastList] = compact.compacted;
  237. // console.log(`上一次:${lastList}`, this.state.layout)
  238. // delete this.state.maped[lastList][key]
  239. // console.log(this.state.maped)
  240. // }
  241. console.log(e);
  242. var newList = swapList(_this.state.layout[x], { y: y, key: key }, key);
  243. var _a = compactList(newList, { y: y, key: key }), compacted = _a.compacted, maped = _a.maped;
  244. _this.state.layout[x] = compacted;
  245. _this.state.maped[x] = maped;
  246. _this.setState({
  247. layout: _this.state.layout.slice(),
  248. maped: _this.state.maped.slice(),
  249. lastList: x
  250. });
  251. };
  252. _this.onDragEnd = function (e) {
  253. var x = e.x, key = e.key, currentListIndex = e.currentListIndex;
  254. if (x !== currentListIndex) {
  255. //1.删除原来list中的数据
  256. _this.state.layout[currentListIndex] = _this.state.layout[currentListIndex].filter(function (item) {
  257. if (item.key !== key) {
  258. return item;
  259. }
  260. });
  261. //2.删除原来map中的数据
  262. delete _this.state.maped[currentListIndex][key];
  263. var compact = compactList(_this.state.layout[currentListIndex], undefined);
  264. _this.state.layout[currentListIndex] = compact.compacted;
  265. _this.state.maped[currentListIndex] = compact.maped;
  266. _this.state.layout[x] = _this.state.layout[x].map(function (item) {
  267. if (key === item.key) {
  268. return __assign({}, item, { currentListIndex: x });
  269. }
  270. return item;
  271. });
  272. }
  273. var _a = compactList(_this.state.layout[x], undefined), compacted = _a.compacted, maped = _a.maped;
  274. _this.state.layout[x] = compacted;
  275. _this.state.maped[x] = maped;
  276. console.log(_this.state.layout);
  277. _this.setState({
  278. layout: _this.state.layout.slice(),
  279. maped: _this.state.maped.slice()
  280. });
  281. // if (this.state.currentList !== x) {
  282. // this.state.layout[x].push({ y, key: "10203", isUserMove: false })
  283. // const compacted = compactList(this.state.layout[x], undefined);
  284. // this.state.layout[x] = compacted;
  285. // this.setState({
  286. // layout: [...this.state.layout]
  287. // })
  288. // } else {
  289. // }
  290. };
  291. _this.renderList = function () {
  292. // console.log('set', this.props.layout)
  293. return _this.props.layout.map(function (child, index) {
  294. return React.createElement("div", { className: 'list-oneof', style: {
  295. background: 'red',
  296. height: 60 * _this.state.layout[index].length,
  297. width: 400
  298. }, key: index }, _this.renderColumn(child, index));
  299. });
  300. };
  301. _this.renderColumn = function (child, index) {
  302. // const column = this.state.layout[index];
  303. var _a = _this.props, width = _a.width, margin = _a.margin, rowHeight = _a.rowHeight;
  304. return child.map(function (c, idx) {
  305. var key = c.key;
  306. var renderItem;
  307. // renderItem = this.state.maped[index][key];
  308. for (var i in _this.state.maped) {
  309. renderItem = _this.state.maped[i][key];
  310. if (renderItem)
  311. break;
  312. }
  313. // console.log(renderItem)
  314. // if (renderItem.key === '1') console.log(renderItem.listPosition)
  315. return React.createElement(ListCell, { margin: margin, rowHeight: rowHeight, width: width, col: _this.state.layout.length, currentListIndex: renderItem.currentListIndex, y: renderItem ? renderItem.y : c.y, style: { position: 'absolute' }, key: idx, UniqueKey: key, isUserMove: renderItem ? renderItem.isUserMove : false, onDragStart: _this.onDragStart, onDrag: _this.onDrag, onDragEnd: _this.onDragEnd }, _this.props.children(c, idx));
  316. });
  317. };
  318. // const layout = getDataSet(props.children);
  319. var array = [];
  320. var layout = props.layout.map(function (child, idx) {
  321. return child.map(function (el, index) {
  322. if (!array[idx])
  323. array[idx] = {};
  324. array[idx][el.key] = __assign({}, el, { key: el.key, isUserMove: false, currentListIndex: idx });
  325. return __assign({}, el, { key: el.key, isUserMove: false, currentListIndex: idx });
  326. });
  327. });
  328. console.log(array);
  329. _this.state = {
  330. GridXMoving: 0,
  331. GridYMoving: 0,
  332. wMoving: 0,
  333. hMoving: 0,
  334. placeholderShow: false,
  335. placeholderMoving: false,
  336. layout: layout,
  337. containerHeight: 500,
  338. dragType: 'drag',
  339. mapLayout: undefined,
  340. col: 3,
  341. currentList: 0,
  342. maped: array,
  343. lastList: void 666
  344. };
  345. return _this;
  346. }
  347. DragactList.prototype.componentWillReceiveProps = function (nextProps) {
  348. // if (this.props.children.length > nextProps.children.length) { //remove
  349. // const mapLayoutCopy = { ...this.state.mapLayout };
  350. // nextProps.children.forEach((child: any) => {
  351. // if ((mapLayoutCopy as any)[child.key] !== void 666) delete (mapLayoutCopy as any)[child.key];
  352. // })
  353. // for (const key in mapLayoutCopy) {
  354. // const newLayout = this.state.layout.filter((child) => {
  355. // if (child.key !== key) return child
  356. // })
  357. // const { compacted, mapLayout } = compactLayout(newLayout, undefined);
  358. // this.setState({
  359. // containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1]),
  360. // layout: compacted,
  361. // mapLayout
  362. // })
  363. // }
  364. // }
  365. // if (this.props.children.length < nextProps.children.length) { //add
  366. // var item;
  367. // for (const idx in nextProps.children) {
  368. // const i = nextProps.children[idx];
  369. // if (this.state.mapLayout && !this.state.mapLayout[i.key]) {
  370. // item = i;
  371. // break;
  372. // }
  373. // }
  374. // if (item !== void 666) {
  375. // const dataSet = { ...item.props['data-set'], isUserMove: false, key: item.key };
  376. // var newLayout = [...this.state.layout, dataSet]
  377. // newLayout = correctLayout(newLayout, this.state.col)
  378. // const { compacted, mapLayout } = compactLayout(newLayout, undefined);
  379. // console.log(mapLayout)
  380. // // console.log(layout)
  381. // this.setState({
  382. // containerHeight: getMaxContainerHeight(compacted, this.props.rowHeight, this.props.margin[1]),
  383. // layout: compacted,
  384. // mapLayout
  385. // })
  386. // }
  387. // }
  388. };
  389. DragactList.prototype.componentDidMount = function () {
  390. // setTimeout(() => {
  391. // const swp = [...this.state.layout[0]]
  392. // this.state.layout[0] = this.state.layout[1];
  393. // this.state.layout[1] = swp;
  394. // const swpMap = { ...this.state.maped[0] }
  395. // this.state.maped[0] = this.state.maped[1];
  396. // this.state.maped[1] = swpMap;
  397. // this.setState({
  398. // layout: this.state.layout,
  399. // maped: this.state.maped
  400. // })
  401. // // console.log(this.state.layout)
  402. // }, 1000);
  403. };
  404. DragactList.prototype.render = function () {
  405. var _a = this.props, width = _a.width, className = _a.className;
  406. var numberOfCol = this.state.layout.length;
  407. return (React.createElement("div", { className: stringJoin('DraggerLayout', className + ''), style: { left: 100, width: width * numberOfCol, height: '', zIndex: 1, display: 'flex' } }, this.renderList()));
  408. };
  409. return DragactList;
  410. }(React.Component));
  411. export { DragactList };