3 var MoveColumns =
function MoveColumns(table) {
5 this.placeholderElement = this.createPlaceholderElement();
6 this.hoverElement =
false;
7 this.checkTimeout =
false;
8 this.checkPeriod = 250;
11 this.toColAfter =
false;
13 this.autoScrollMargin = 40;
14 this.autoScrollStep = 5;
15 this.autoScrollTimeout =
false;
16 this.touchMove =
false;
18 this.moveHover = this.moveHover.bind(
this);
19 this.endMove = this.endMove.bind(
this);
22 MoveColumns.prototype.createPlaceholderElement =
function () {
23 var el = document.createElement(
"div");
25 el.classList.add(
"tabulator-col");
26 el.classList.add(
"tabulator-col-placeholder");
31 MoveColumns.prototype.initializeColumn =
function (column) {
36 if (!column.modules.frozen) {
38 colEl = column.getElement();
40 config.mousemove =
function (e) {
41 if (column.parent ===
self.moving.parent) {
42 if ((
self.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(colEl).left +
self.table.columnManager.element.scrollLeft > column.getWidth() / 2) {
43 if (
self.toCol !== column || !
self.toColAfter) {
44 colEl.parentNode.insertBefore(
self.placeholderElement, colEl.nextSibling);
45 self.moveColumn(column,
true);
48 if (
self.toCol !== column ||
self.toColAfter) {
49 colEl.parentNode.insertBefore(
self.placeholderElement, colEl);
50 self.moveColumn(column,
false);
56 colEl.addEventListener(
"mousedown",
function (e) {
57 self.touchMove =
false;
59 self.checkTimeout = setTimeout(
function () {
60 self.startMove(e, column);
65 colEl.addEventListener(
"mouseup",
function (e) {
67 if (
self.checkTimeout) {
68 clearTimeout(
self.checkTimeout);
73 self.bindTouchEvents(column);
76 column.modules.moveColumn = config;
79 MoveColumns.prototype.bindTouchEvents =
function (column) {
81 colEl = column.getElement(),
93 colEl.addEventListener(
"touchstart",
function (e) {
94 self.checkTimeout = setTimeout(
function () {
95 self.touchMove =
true;
97 nextCol = column.nextColumn();
98 nextColWidth = nextCol ? nextCol.getWidth() / 2 : 0;
99 prevCol = column.prevColumn();
100 prevColWidth = prevCol ? prevCol.getWidth() / 2 : 0;
101 nextColWidthLast = 0;
102 prevColWidthLast = 0;
105 self.startMove(e, column);
106 },
self.checkPeriod);
107 }, { passive:
true });
109 colEl.addEventListener(
"touchmove",
function (e) {
110 var halfCol, diff, moveToCol;
116 startXMove = e.touches[0].pageX;
119 diff = e.touches[0].pageX - startXMove;
122 if (nextCol && diff - nextColWidthLast > nextColWidth) {
125 if (moveToCol !== column) {
126 startXMove = e.touches[0].pageX;
127 moveToCol.getElement().parentNode.insertBefore(
self.placeholderElement, moveToCol.getElement().nextSibling);
128 self.moveColumn(moveToCol,
true);
132 if (prevCol && -diff - prevColWidthLast > prevColWidth) {
135 if (moveToCol !== column) {
136 startXMove = e.touches[0].pageX;
137 moveToCol.getElement().parentNode.insertBefore(
self.placeholderElement, moveToCol.getElement());
138 self.moveColumn(moveToCol,
false);
144 currentCol = moveToCol;
145 nextCol = moveToCol.nextColumn();
146 nextColWidthLast = nextColWidth;
147 nextColWidth = nextCol ? nextCol.getWidth() / 2 : 0;
148 prevCol = moveToCol.prevColumn();
149 prevColWidthLast = prevColWidth;
150 prevColWidth = prevCol ? prevCol.getWidth() / 2 : 0;
153 }, { passive:
true });
155 colEl.addEventListener(
"touchend",
function (e) {
156 if (
self.checkTimeout) {
157 clearTimeout(
self.checkTimeout);
165 MoveColumns.prototype.startMove =
function (e, column) {
166 var element = column.getElement();
168 this.moving = column;
169 this.startX = (this.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(element).left;
171 this.table.element.classList.add(
"tabulator-block-select");
174 this.placeholderElement.style.width = column.getWidth() +
"px";
175 this.placeholderElement.style.height = column.getHeight() +
"px";
177 element.parentNode.insertBefore(this.placeholderElement, element);
178 element.parentNode.removeChild(element);
181 this.hoverElement = element.cloneNode(
true);
182 this.hoverElement.classList.add(
"tabulator-moving");
184 this.table.columnManager.getElement().appendChild(this.hoverElement);
186 this.hoverElement.style.left =
"0";
187 this.hoverElement.style.bottom =
"0";
189 if (!this.touchMove) {
190 this._bindMouseMove();
192 document.body.addEventListener(
"mousemove", this.moveHover);
193 document.body.addEventListener(
"mouseup", this.endMove);
199 MoveColumns.prototype._bindMouseMove =
function () {
200 this.table.columnManager.columnsByIndex.forEach(
function (column) {
201 if (column.modules.moveColumn.mousemove) {
202 column.getElement().addEventListener(
"mousemove", column.modules.moveColumn.mousemove);
207 MoveColumns.prototype._unbindMouseMove =
function () {
208 this.table.columnManager.columnsByIndex.forEach(
function (column) {
209 if (column.modules.moveColumn.mousemove) {
210 column.getElement().removeEventListener(
"mousemove", column.modules.moveColumn.mousemove);
215 MoveColumns.prototype.moveColumn =
function (column, after) {
216 var movingCells = this.moving.getCells();
219 this.toColAfter = after;
222 column.getCells().forEach(
function (cell, i) {
223 var cellEl = cell.getElement();
224 cellEl.parentNode.insertBefore(movingCells[i].getElement(), cellEl.nextSibling);
227 column.getCells().forEach(
function (cell, i) {
228 var cellEl = cell.getElement();
229 cellEl.parentNode.insertBefore(movingCells[i].getElement(), cellEl);
234 MoveColumns.prototype.endMove =
function (e) {
235 if (e.which === 1 ||
this.touchMove) {
236 this._unbindMouseMove();
238 this.placeholderElement.parentNode.insertBefore(this.moving.getElement(), this.placeholderElement.nextSibling);
239 this.placeholderElement.parentNode.removeChild(this.placeholderElement);
240 this.hoverElement.parentNode.removeChild(this.hoverElement);
242 this.table.element.classList.remove(
"tabulator-block-select");
245 this.table.columnManager.moveColumnActual(this.moving, this.toCol, this.toColAfter);
250 this.toColAfter =
false;
252 if (!this.touchMove) {
253 document.body.removeEventListener(
"mousemove", this.moveHover);
254 document.body.removeEventListener(
"mouseup", this.endMove);
259 MoveColumns.prototype.moveHover =
function (e) {
261 columnHolder =
self.table.columnManager.getElement(),
262 scrollLeft = columnHolder.scrollLeft,
263 xPos = (
self.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(columnHolder).left + scrollLeft,
266 self.hoverElement.style.left = xPos -
self.startX +
"px";
268 if (xPos - scrollLeft <
self.autoScrollMargin) {
269 if (!
self.autoScrollTimeout) {
270 self.autoScrollTimeout = setTimeout(
function () {
271 scrollPos = Math.max(0, scrollLeft - 5);
272 self.table.rowManager.getElement().scrollLeft = scrollPos;
273 self.autoScrollTimeout =
false;
278 if (scrollLeft + columnHolder.clientWidth - xPos <
self.autoScrollMargin) {
279 if (!
self.autoScrollTimeout) {
280 self.autoScrollTimeout = setTimeout(
function () {
281 scrollPos = Math.min(columnHolder.clientWidth, scrollLeft + 5);
282 self.table.rowManager.getElement().scrollLeft = scrollPos;
283 self.autoScrollTimeout =
false;
289 Tabulator.prototype.registerModule(
"moveColumn", MoveColumns);