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

moveable_columns.js 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /* Tabulator v4.6.3 (c) Oliver Folkerd */
  2. var MoveColumns = function MoveColumns(table) {
  3. this.table = table; //hold Tabulator object
  4. this.placeholderElement = this.createPlaceholderElement();
  5. this.hoverElement = false; //floating column header element
  6. this.checkTimeout = false; //click check timeout holder
  7. this.checkPeriod = 250; //period to wait on mousedown to consider this a move and not a click
  8. this.moving = false; //currently moving column
  9. this.toCol = false; //destination column
  10. this.toColAfter = false; //position of moving column relative to the desitnation column
  11. this.startX = 0; //starting position within header element
  12. this.autoScrollMargin = 40; //auto scroll on edge when within margin
  13. this.autoScrollStep = 5; //auto scroll distance in pixels
  14. this.autoScrollTimeout = false; //auto scroll timeout
  15. this.touchMove = false;
  16. this.moveHover = this.moveHover.bind(this);
  17. this.endMove = this.endMove.bind(this);
  18. };
  19. MoveColumns.prototype.createPlaceholderElement = function () {
  20. var el = document.createElement("div");
  21. el.classList.add("tabulator-col");
  22. el.classList.add("tabulator-col-placeholder");
  23. return el;
  24. };
  25. MoveColumns.prototype.initializeColumn = function (column) {
  26. var self = this,
  27. config = {},
  28. colEl;
  29. if (!column.modules.frozen) {
  30. colEl = column.getElement();
  31. config.mousemove = function (e) {
  32. if (column.parent === self.moving.parent) {
  33. if ((self.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(colEl).left + self.table.columnManager.element.scrollLeft > column.getWidth() / 2) {
  34. if (self.toCol !== column || !self.toColAfter) {
  35. colEl.parentNode.insertBefore(self.placeholderElement, colEl.nextSibling);
  36. self.moveColumn(column, true);
  37. }
  38. } else {
  39. if (self.toCol !== column || self.toColAfter) {
  40. colEl.parentNode.insertBefore(self.placeholderElement, colEl);
  41. self.moveColumn(column, false);
  42. }
  43. }
  44. }
  45. }.bind(self);
  46. colEl.addEventListener("mousedown", function (e) {
  47. self.touchMove = false;
  48. if (e.which === 1) {
  49. self.checkTimeout = setTimeout(function () {
  50. self.startMove(e, column);
  51. }, self.checkPeriod);
  52. }
  53. });
  54. colEl.addEventListener("mouseup", function (e) {
  55. if (e.which === 1) {
  56. if (self.checkTimeout) {
  57. clearTimeout(self.checkTimeout);
  58. }
  59. }
  60. });
  61. self.bindTouchEvents(column);
  62. }
  63. column.modules.moveColumn = config;
  64. };
  65. MoveColumns.prototype.bindTouchEvents = function (column) {
  66. var self = this,
  67. colEl = column.getElement(),
  68. startXMove = false,
  69. //shifting center position of the cell
  70. dir = false,
  71. currentCol,
  72. nextCol,
  73. prevCol,
  74. nextColWidth,
  75. prevColWidth,
  76. nextColWidthLast,
  77. prevColWidthLast;
  78. colEl.addEventListener("touchstart", function (e) {
  79. self.checkTimeout = setTimeout(function () {
  80. self.touchMove = true;
  81. currentCol = column;
  82. nextCol = column.nextColumn();
  83. nextColWidth = nextCol ? nextCol.getWidth() / 2 : 0;
  84. prevCol = column.prevColumn();
  85. prevColWidth = prevCol ? prevCol.getWidth() / 2 : 0;
  86. nextColWidthLast = 0;
  87. prevColWidthLast = 0;
  88. startXMove = false;
  89. self.startMove(e, column);
  90. }, self.checkPeriod);
  91. }, { passive: true });
  92. colEl.addEventListener("touchmove", function (e) {
  93. var halfCol, diff, moveToCol;
  94. if (self.moving) {
  95. self.moveHover(e);
  96. if (!startXMove) {
  97. startXMove = e.touches[0].pageX;
  98. }
  99. diff = e.touches[0].pageX - startXMove;
  100. if (diff > 0) {
  101. if (nextCol && diff - nextColWidthLast > nextColWidth) {
  102. moveToCol = nextCol;
  103. if (moveToCol !== column) {
  104. startXMove = e.touches[0].pageX;
  105. moveToCol.getElement().parentNode.insertBefore(self.placeholderElement, moveToCol.getElement().nextSibling);
  106. self.moveColumn(moveToCol, true);
  107. }
  108. }
  109. } else {
  110. if (prevCol && -diff - prevColWidthLast > prevColWidth) {
  111. moveToCol = prevCol;
  112. if (moveToCol !== column) {
  113. startXMove = e.touches[0].pageX;
  114. moveToCol.getElement().parentNode.insertBefore(self.placeholderElement, moveToCol.getElement());
  115. self.moveColumn(moveToCol, false);
  116. }
  117. }
  118. }
  119. if (moveToCol) {
  120. currentCol = moveToCol;
  121. nextCol = moveToCol.nextColumn();
  122. nextColWidthLast = nextColWidth;
  123. nextColWidth = nextCol ? nextCol.getWidth() / 2 : 0;
  124. prevCol = moveToCol.prevColumn();
  125. prevColWidthLast = prevColWidth;
  126. prevColWidth = prevCol ? prevCol.getWidth() / 2 : 0;
  127. }
  128. }
  129. }, { passive: true });
  130. colEl.addEventListener("touchend", function (e) {
  131. if (self.checkTimeout) {
  132. clearTimeout(self.checkTimeout);
  133. }
  134. if (self.moving) {
  135. self.endMove(e);
  136. }
  137. });
  138. };
  139. MoveColumns.prototype.startMove = function (e, column) {
  140. var element = column.getElement();
  141. this.moving = column;
  142. this.startX = (this.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(element).left;
  143. this.table.element.classList.add("tabulator-block-select");
  144. //create placeholder
  145. this.placeholderElement.style.width = column.getWidth() + "px";
  146. this.placeholderElement.style.height = column.getHeight() + "px";
  147. element.parentNode.insertBefore(this.placeholderElement, element);
  148. element.parentNode.removeChild(element);
  149. //create hover element
  150. this.hoverElement = element.cloneNode(true);
  151. this.hoverElement.classList.add("tabulator-moving");
  152. this.table.columnManager.getElement().appendChild(this.hoverElement);
  153. this.hoverElement.style.left = "0";
  154. this.hoverElement.style.bottom = "0";
  155. if (!this.touchMove) {
  156. this._bindMouseMove();
  157. document.body.addEventListener("mousemove", this.moveHover);
  158. document.body.addEventListener("mouseup", this.endMove);
  159. }
  160. this.moveHover(e);
  161. };
  162. MoveColumns.prototype._bindMouseMove = function () {
  163. this.table.columnManager.columnsByIndex.forEach(function (column) {
  164. if (column.modules.moveColumn.mousemove) {
  165. column.getElement().addEventListener("mousemove", column.modules.moveColumn.mousemove);
  166. }
  167. });
  168. };
  169. MoveColumns.prototype._unbindMouseMove = function () {
  170. this.table.columnManager.columnsByIndex.forEach(function (column) {
  171. if (column.modules.moveColumn.mousemove) {
  172. column.getElement().removeEventListener("mousemove", column.modules.moveColumn.mousemove);
  173. }
  174. });
  175. };
  176. MoveColumns.prototype.moveColumn = function (column, after) {
  177. var movingCells = this.moving.getCells();
  178. this.toCol = column;
  179. this.toColAfter = after;
  180. if (after) {
  181. column.getCells().forEach(function (cell, i) {
  182. var cellEl = cell.getElement();
  183. cellEl.parentNode.insertBefore(movingCells[i].getElement(), cellEl.nextSibling);
  184. });
  185. } else {
  186. column.getCells().forEach(function (cell, i) {
  187. var cellEl = cell.getElement();
  188. cellEl.parentNode.insertBefore(movingCells[i].getElement(), cellEl);
  189. });
  190. }
  191. };
  192. MoveColumns.prototype.endMove = function (e) {
  193. if (e.which === 1 || this.touchMove) {
  194. this._unbindMouseMove();
  195. this.placeholderElement.parentNode.insertBefore(this.moving.getElement(), this.placeholderElement.nextSibling);
  196. this.placeholderElement.parentNode.removeChild(this.placeholderElement);
  197. this.hoverElement.parentNode.removeChild(this.hoverElement);
  198. this.table.element.classList.remove("tabulator-block-select");
  199. if (this.toCol) {
  200. this.table.columnManager.moveColumnActual(this.moving, this.toCol, this.toColAfter);
  201. }
  202. this.moving = false;
  203. this.toCol = false;
  204. this.toColAfter = false;
  205. if (!this.touchMove) {
  206. document.body.removeEventListener("mousemove", this.moveHover);
  207. document.body.removeEventListener("mouseup", this.endMove);
  208. }
  209. }
  210. };
  211. MoveColumns.prototype.moveHover = function (e) {
  212. var self = this,
  213. columnHolder = self.table.columnManager.getElement(),
  214. scrollLeft = columnHolder.scrollLeft,
  215. xPos = (self.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(columnHolder).left + scrollLeft,
  216. scrollPos;
  217. self.hoverElement.style.left = xPos - self.startX + "px";
  218. if (xPos - scrollLeft < self.autoScrollMargin) {
  219. if (!self.autoScrollTimeout) {
  220. self.autoScrollTimeout = setTimeout(function () {
  221. scrollPos = Math.max(0, scrollLeft - 5);
  222. self.table.rowManager.getElement().scrollLeft = scrollPos;
  223. self.autoScrollTimeout = false;
  224. }, 1);
  225. }
  226. }
  227. if (scrollLeft + columnHolder.clientWidth - xPos < self.autoScrollMargin) {
  228. if (!self.autoScrollTimeout) {
  229. self.autoScrollTimeout = setTimeout(function () {
  230. scrollPos = Math.min(columnHolder.clientWidth, scrollLeft + 5);
  231. self.table.rowManager.getElement().scrollLeft = scrollPos;
  232. self.autoScrollTimeout = false;
  233. }, 1);
  234. }
  235. }
  236. };
  237. Tabulator.prototype.registerModule("moveColumn", MoveColumns);