Built files from Bizgaze WebServer
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

moveable_rows.js 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  3. var MoveRows = function MoveRows(table) {
  4. this.table = table; //hold Tabulator object
  5. this.placeholderElement = this.createPlaceholderElement();
  6. this.hoverElement = false; //floating row header element
  7. this.checkTimeout = false; //click check timeout holder
  8. this.checkPeriod = 150; //period to wait on mousedown to consider this a move and not a click
  9. this.moving = false; //currently moving row
  10. this.toRow = false; //destination row
  11. this.toRowAfter = false; //position of moving row relative to the desitnation row
  12. this.hasHandle = false; //row has handle instead of fully movable row
  13. this.startY = 0; //starting Y position within header element
  14. this.startX = 0; //starting X position within header element
  15. this.moveHover = this.moveHover.bind(this);
  16. this.endMove = this.endMove.bind(this);
  17. this.tableRowDropEvent = false;
  18. this.touchMove = false;
  19. this.connection = false;
  20. this.connections = [];
  21. this.connectedTable = false;
  22. this.connectedRow = false;
  23. };
  24. MoveRows.prototype.createPlaceholderElement = function () {
  25. var el = document.createElement("div");
  26. el.classList.add("tabulator-row");
  27. el.classList.add("tabulator-row-placeholder");
  28. return el;
  29. };
  30. MoveRows.prototype.initialize = function (handle) {
  31. this.connection = this.table.options.movableRowsConnectedTables;
  32. };
  33. MoveRows.prototype.setHandle = function (handle) {
  34. this.hasHandle = handle;
  35. };
  36. MoveRows.prototype.initializeGroupHeader = function (group) {
  37. var self = this,
  38. config = {},
  39. rowEl;
  40. //inter table drag drop
  41. config.mouseup = function (e) {
  42. self.tableRowDrop(e, row);
  43. }.bind(self);
  44. //same table drag drop
  45. config.mousemove = function (e) {
  46. if (e.pageY - Tabulator.prototype.helpers.elOffset(group.element).top + self.table.rowManager.element.scrollTop > group.getHeight() / 2) {
  47. if (self.toRow !== group || !self.toRowAfter) {
  48. var rowEl = group.getElement();
  49. rowEl.parentNode.insertBefore(self.placeholderElement, rowEl.nextSibling);
  50. self.moveRow(group, true);
  51. }
  52. } else {
  53. if (self.toRow !== group || self.toRowAfter) {
  54. var rowEl = group.getElement();
  55. if (rowEl.previousSibling) {
  56. rowEl.parentNode.insertBefore(self.placeholderElement, rowEl);
  57. self.moveRow(group, false);
  58. }
  59. }
  60. }
  61. }.bind(self);
  62. group.modules.moveRow = config;
  63. };
  64. MoveRows.prototype.initializeRow = function (row) {
  65. var self = this,
  66. config = {},
  67. rowEl;
  68. //inter table drag drop
  69. config.mouseup = function (e) {
  70. self.tableRowDrop(e, row);
  71. }.bind(self);
  72. //same table drag drop
  73. config.mousemove = function (e) {
  74. if (e.pageY - Tabulator.prototype.helpers.elOffset(row.element).top + self.table.rowManager.element.scrollTop > row.getHeight() / 2) {
  75. if (self.toRow !== row || !self.toRowAfter) {
  76. var rowEl = row.getElement();
  77. rowEl.parentNode.insertBefore(self.placeholderElement, rowEl.nextSibling);
  78. self.moveRow(row, true);
  79. }
  80. } else {
  81. if (self.toRow !== row || self.toRowAfter) {
  82. var rowEl = row.getElement();
  83. rowEl.parentNode.insertBefore(self.placeholderElement, rowEl);
  84. self.moveRow(row, false);
  85. }
  86. }
  87. }.bind(self);
  88. if (!this.hasHandle) {
  89. rowEl = row.getElement();
  90. rowEl.addEventListener("mousedown", function (e) {
  91. if (e.which === 1) {
  92. self.checkTimeout = setTimeout(function () {
  93. self.startMove(e, row);
  94. }, self.checkPeriod);
  95. }
  96. });
  97. rowEl.addEventListener("mouseup", function (e) {
  98. if (e.which === 1) {
  99. if (self.checkTimeout) {
  100. clearTimeout(self.checkTimeout);
  101. }
  102. }
  103. });
  104. this.bindTouchEvents(row, row.getElement());
  105. }
  106. row.modules.moveRow = config;
  107. };
  108. MoveRows.prototype.initializeCell = function (cell) {
  109. var self = this,
  110. cellEl = cell.getElement();
  111. cellEl.addEventListener("mousedown", function (e) {
  112. if (e.which === 1) {
  113. self.checkTimeout = setTimeout(function () {
  114. self.startMove(e, cell.row);
  115. }, self.checkPeriod);
  116. }
  117. });
  118. cellEl.addEventListener("mouseup", function (e) {
  119. if (e.which === 1) {
  120. if (self.checkTimeout) {
  121. clearTimeout(self.checkTimeout);
  122. }
  123. }
  124. });
  125. this.bindTouchEvents(cell.row, cell.getElement());
  126. };
  127. MoveRows.prototype.bindTouchEvents = function (row, element) {
  128. var self = this,
  129. startYMove = false,
  130. //shifting center position of the cell
  131. dir = false,
  132. currentRow,
  133. nextRow,
  134. prevRow,
  135. nextRowHeight,
  136. prevRowHeight,
  137. nextRowHeightLast,
  138. prevRowHeightLast;
  139. element.addEventListener("touchstart", function (e) {
  140. self.checkTimeout = setTimeout(function () {
  141. self.touchMove = true;
  142. currentRow = row;
  143. nextRow = row.nextRow();
  144. nextRowHeight = nextRow ? nextRow.getHeight() / 2 : 0;
  145. prevRow = row.prevRow();
  146. prevRowHeight = prevRow ? prevRow.getHeight() / 2 : 0;
  147. nextRowHeightLast = 0;
  148. prevRowHeightLast = 0;
  149. startYMove = false;
  150. self.startMove(e, row);
  151. }, self.checkPeriod);
  152. }, { passive: true });
  153. this.moving, this.toRow, this.toRowAfter;
  154. element.addEventListener("touchmove", function (e) {
  155. var halfCol, diff, moveToRow;
  156. if (self.moving) {
  157. e.preventDefault();
  158. self.moveHover(e);
  159. if (!startYMove) {
  160. startYMove = e.touches[0].pageY;
  161. }
  162. diff = e.touches[0].pageY - startYMove;
  163. if (diff > 0) {
  164. if (nextRow && diff - nextRowHeightLast > nextRowHeight) {
  165. moveToRow = nextRow;
  166. if (moveToRow !== row) {
  167. startYMove = e.touches[0].pageY;
  168. moveToRow.getElement().parentNode.insertBefore(self.placeholderElement, moveToRow.getElement().nextSibling);
  169. self.moveRow(moveToRow, true);
  170. }
  171. }
  172. } else {
  173. if (prevRow && -diff - prevRowHeightLast > prevRowHeight) {
  174. moveToRow = prevRow;
  175. if (moveToRow !== row) {
  176. startYMove = e.touches[0].pageY;
  177. moveToRow.getElement().parentNode.insertBefore(self.placeholderElement, moveToRow.getElement());
  178. self.moveRow(moveToRow, false);
  179. }
  180. }
  181. }
  182. if (moveToRow) {
  183. currentRow = moveToRow;
  184. nextRow = moveToRow.nextRow();
  185. nextRowHeightLast = nextRowHeight;
  186. nextRowHeight = nextRow ? nextRow.getHeight() / 2 : 0;
  187. prevRow = moveToRow.prevRow();
  188. prevRowHeightLast = prevRowHeight;
  189. prevRowHeight = prevRow ? prevRow.getHeight() / 2 : 0;
  190. }
  191. }
  192. });
  193. element.addEventListener("touchend", function (e) {
  194. if (self.checkTimeout) {
  195. clearTimeout(self.checkTimeout);
  196. }
  197. if (self.moving) {
  198. self.endMove(e);
  199. self.touchMove = false;
  200. }
  201. });
  202. };
  203. MoveRows.prototype._bindMouseMove = function () {
  204. var self = this;
  205. self.table.rowManager.getDisplayRows().forEach(function (row) {
  206. if ((row.type === "row" || row.type === "group") && row.modules.moveRow.mousemove) {
  207. row.getElement().addEventListener("mousemove", row.modules.moveRow.mousemove);
  208. }
  209. });
  210. };
  211. MoveRows.prototype._unbindMouseMove = function () {
  212. var self = this;
  213. self.table.rowManager.getDisplayRows().forEach(function (row) {
  214. if ((row.type === "row" || row.type === "group") && row.modules.moveRow.mousemove) {
  215. row.getElement().removeEventListener("mousemove", row.modules.moveRow.mousemove);
  216. }
  217. });
  218. };
  219. MoveRows.prototype.startMove = function (e, row) {
  220. var element = row.getElement();
  221. this.setStartPosition(e, row);
  222. this.moving = row;
  223. this.table.element.classList.add("tabulator-block-select");
  224. //create placeholder
  225. this.placeholderElement.style.width = row.getWidth() + "px";
  226. this.placeholderElement.style.height = row.getHeight() + "px";
  227. if (!this.connection) {
  228. element.parentNode.insertBefore(this.placeholderElement, element);
  229. element.parentNode.removeChild(element);
  230. } else {
  231. this.table.element.classList.add("tabulator-movingrow-sending");
  232. this.connectToTables(row);
  233. }
  234. //create hover element
  235. this.hoverElement = element.cloneNode(true);
  236. this.hoverElement.classList.add("tabulator-moving");
  237. if (this.connection) {
  238. document.body.appendChild(this.hoverElement);
  239. this.hoverElement.style.left = "0";
  240. this.hoverElement.style.top = "0";
  241. this.hoverElement.style.width = this.table.element.clientWidth + "px";
  242. this.hoverElement.style.whiteSpace = "nowrap";
  243. this.hoverElement.style.overflow = "hidden";
  244. this.hoverElement.style.pointerEvents = "none";
  245. } else {
  246. this.table.rowManager.getTableElement().appendChild(this.hoverElement);
  247. this.hoverElement.style.left = "0";
  248. this.hoverElement.style.top = "0";
  249. this._bindMouseMove();
  250. }
  251. document.body.addEventListener("mousemove", this.moveHover);
  252. document.body.addEventListener("mouseup", this.endMove);
  253. this.moveHover(e);
  254. };
  255. MoveRows.prototype.setStartPosition = function (e, row) {
  256. var pageX = this.touchMove ? e.touches[0].pageX : e.pageX,
  257. pageY = this.touchMove ? e.touches[0].pageY : e.pageY,
  258. element,
  259. position;
  260. element = row.getElement();
  261. if (this.connection) {
  262. position = element.getBoundingClientRect();
  263. this.startX = position.left - pageX + window.pageXOffset;
  264. this.startY = position.top - pageY + window.pageYOffset;
  265. } else {
  266. this.startY = pageY - element.getBoundingClientRect().top;
  267. }
  268. };
  269. MoveRows.prototype.endMove = function (e) {
  270. if (!e || e.which === 1 || this.touchMove) {
  271. this._unbindMouseMove();
  272. if (!this.connection) {
  273. this.placeholderElement.parentNode.insertBefore(this.moving.getElement(), this.placeholderElement.nextSibling);
  274. this.placeholderElement.parentNode.removeChild(this.placeholderElement);
  275. }
  276. this.hoverElement.parentNode.removeChild(this.hoverElement);
  277. this.table.element.classList.remove("tabulator-block-select");
  278. if (this.toRow) {
  279. this.table.rowManager.moveRow(this.moving, this.toRow, this.toRowAfter);
  280. }
  281. this.moving = false;
  282. this.toRow = false;
  283. this.toRowAfter = false;
  284. document.body.removeEventListener("mousemove", this.moveHover);
  285. document.body.removeEventListener("mouseup", this.endMove);
  286. if (this.connection) {
  287. this.table.element.classList.remove("tabulator-movingrow-sending");
  288. this.disconnectFromTables();
  289. }
  290. }
  291. };
  292. MoveRows.prototype.moveRow = function (row, after) {
  293. this.toRow = row;
  294. this.toRowAfter = after;
  295. };
  296. MoveRows.prototype.moveHover = function (e) {
  297. if (this.connection) {
  298. this.moveHoverConnections.call(this, e);
  299. } else {
  300. this.moveHoverTable.call(this, e);
  301. }
  302. };
  303. MoveRows.prototype.moveHoverTable = function (e) {
  304. var rowHolder = this.table.rowManager.getElement(),
  305. scrollTop = rowHolder.scrollTop,
  306. yPos = (this.touchMove ? e.touches[0].pageY : e.pageY) - rowHolder.getBoundingClientRect().top + scrollTop,
  307. scrollPos;
  308. this.hoverElement.style.top = yPos - this.startY + "px";
  309. };
  310. MoveRows.prototype.moveHoverConnections = function (e) {
  311. this.hoverElement.style.left = this.startX + (this.touchMove ? e.touches[0].pageX : e.pageX) + "px";
  312. this.hoverElement.style.top = this.startY + (this.touchMove ? e.touches[0].pageY : e.pageY) + "px";
  313. };
  314. //establish connection with other tables
  315. MoveRows.prototype.connectToTables = function (row) {
  316. var self = this,
  317. connections = this.table.modules.comms.getConnections(this.connection);
  318. this.table.options.movableRowsSendingStart.call(this.table, connections);
  319. this.table.modules.comms.send(this.connection, "moveRow", "connect", {
  320. row: row
  321. });
  322. };
  323. //disconnect from other tables
  324. MoveRows.prototype.disconnectFromTables = function () {
  325. var self = this,
  326. connections = this.table.modules.comms.getConnections(this.connection);
  327. this.table.options.movableRowsSendingStop.call(this.table, connections);
  328. this.table.modules.comms.send(this.connection, "moveRow", "disconnect");
  329. };
  330. //accept incomming connection
  331. MoveRows.prototype.connect = function (table, row) {
  332. var self = this;
  333. if (!this.connectedTable) {
  334. this.connectedTable = table;
  335. this.connectedRow = row;
  336. this.table.element.classList.add("tabulator-movingrow-receiving");
  337. self.table.rowManager.getDisplayRows().forEach(function (row) {
  338. if (row.type === "row" && row.modules.moveRow && row.modules.moveRow.mouseup) {
  339. row.getElement().addEventListener("mouseup", row.modules.moveRow.mouseup);
  340. }
  341. });
  342. self.tableRowDropEvent = self.tableRowDrop.bind(self);
  343. self.table.element.addEventListener("mouseup", self.tableRowDropEvent);
  344. this.table.options.movableRowsReceivingStart.call(this.table, row, table);
  345. return true;
  346. } else {
  347. console.warn("Move Row Error - Table cannot accept connection, already connected to table:", this.connectedTable);
  348. return false;
  349. }
  350. };
  351. //close incomming connection
  352. MoveRows.prototype.disconnect = function (table) {
  353. var self = this;
  354. if (table === this.connectedTable) {
  355. this.connectedTable = false;
  356. this.connectedRow = false;
  357. this.table.element.classList.remove("tabulator-movingrow-receiving");
  358. self.table.rowManager.getDisplayRows().forEach(function (row) {
  359. if (row.type === "row" && row.modules.moveRow && row.modules.moveRow.mouseup) {
  360. row.getElement().removeEventListener("mouseup", row.modules.moveRow.mouseup);
  361. }
  362. });
  363. self.table.element.removeEventListener("mouseup", self.tableRowDropEvent);
  364. this.table.options.movableRowsReceivingStop.call(this.table, table);
  365. } else {
  366. console.warn("Move Row Error - trying to disconnect from non connected table");
  367. }
  368. };
  369. MoveRows.prototype.dropComplete = function (table, row, success) {
  370. var sender = false;
  371. if (success) {
  372. switch (_typeof(this.table.options.movableRowsSender)) {
  373. case "string":
  374. sender = this.senders[this.table.options.movableRowsSender];
  375. break;
  376. case "function":
  377. sender = this.table.options.movableRowsSender;
  378. break;
  379. }
  380. if (sender) {
  381. sender.call(this, this.moving.getComponent(), row ? row.getComponent() : undefined, table);
  382. } else {
  383. if (this.table.options.movableRowsSender) {
  384. console.warn("Mover Row Error - no matching sender found:", this.table.options.movableRowsSender);
  385. }
  386. }
  387. this.table.options.movableRowsSent.call(this.table, this.moving.getComponent(), row ? row.getComponent() : undefined, table);
  388. } else {
  389. this.table.options.movableRowsSentFailed.call(this.table, this.moving.getComponent(), row ? row.getComponent() : undefined, table);
  390. }
  391. this.endMove();
  392. };
  393. MoveRows.prototype.tableRowDrop = function (e, row) {
  394. var receiver = false,
  395. success = false;
  396. e.stopImmediatePropagation();
  397. switch (_typeof(this.table.options.movableRowsReceiver)) {
  398. case "string":
  399. receiver = this.receivers[this.table.options.movableRowsReceiver];
  400. break;
  401. case "function":
  402. receiver = this.table.options.movableRowsReceiver;
  403. break;
  404. }
  405. if (receiver) {
  406. success = receiver.call(this, this.connectedRow.getComponent(), row ? row.getComponent() : undefined, this.connectedTable);
  407. } else {
  408. console.warn("Mover Row Error - no matching receiver found:", this.table.options.movableRowsReceiver);
  409. }
  410. if (success) {
  411. this.table.options.movableRowsReceived.call(this.table, this.connectedRow.getComponent(), row ? row.getComponent() : undefined, this.connectedTable);
  412. } else {
  413. this.table.options.movableRowsReceivedFailed.call(this.table, this.connectedRow.getComponent(), row ? row.getComponent() : undefined, this.connectedTable);
  414. }
  415. this.table.modules.comms.send(this.connectedTable, "moveRow", "dropcomplete", {
  416. row: row,
  417. success: success
  418. });
  419. };
  420. MoveRows.prototype.receivers = {
  421. insert: function insert(fromRow, toRow, fromTable) {
  422. this.table.addRow(fromRow.getData(), undefined, toRow);
  423. return true;
  424. },
  425. add: function add(fromRow, toRow, fromTable) {
  426. this.table.addRow(fromRow.getData());
  427. return true;
  428. },
  429. update: function update(fromRow, toRow, fromTable) {
  430. if (toRow) {
  431. toRow.update(fromRow.getData());
  432. return true;
  433. }
  434. return false;
  435. },
  436. replace: function replace(fromRow, toRow, fromTable) {
  437. if (toRow) {
  438. this.table.addRow(fromRow.getData(), undefined, toRow);
  439. toRow.delete();
  440. return true;
  441. }
  442. return false;
  443. }
  444. };
  445. MoveRows.prototype.senders = {
  446. delete: function _delete(fromRow, toRow, toTable) {
  447. fromRow.delete();
  448. }
  449. };
  450. MoveRows.prototype.commsReceived = function (table, action, data) {
  451. switch (action) {
  452. case "connect":
  453. return this.connect(table, data.row);
  454. break;
  455. case "disconnect":
  456. return this.disconnect(table);
  457. break;
  458. case "dropcomplete":
  459. return this.dropComplete(table, data.row, data.success);
  460. break;
  461. }
  462. };
  463. Tabulator.prototype.registerModule("moveRow", MoveRows);