1 var MoveColumns =
function(table){
3 this.placeholderElement = this.createPlaceholderElement();
4 this.hoverElement =
false;
5 this.checkTimeout =
false;
6 this.checkPeriod = 250;
9 this.toColAfter =
false;
11 this.autoScrollMargin = 40;
12 this.autoScrollStep = 5;
13 this.autoScrollTimeout =
false;
14 this.touchMove =
false;
16 this.moveHover = this.moveHover.bind(
this);
17 this.endMove = this.endMove.bind(
this);
21 MoveColumns.prototype.createPlaceholderElement =
function(){
22 var el = document.createElement(
"div");
24 el.classList.add(
"tabulator-col");
25 el.classList.add(
"tabulator-col-placeholder");
30 MoveColumns.prototype.initializeColumn =
function(column){
35 if(!column.modules.frozen){
37 colEl = column.getElement();
39 config.mousemove =
function(e){
40 if(column.parent ===
self.moving.parent){
41 if((((
self.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(colEl).left) +
self.table.columnManager.element.scrollLeft) > (column.getWidth() / 2)){
42 if(
self.toCol !== column || !
self.toColAfter){
43 colEl.parentNode.insertBefore(
self.placeholderElement, colEl.nextSibling);
44 self.moveColumn(column,
true);
47 if(
self.toCol !== column ||
self.toColAfter){
48 colEl.parentNode.insertBefore(
self.placeholderElement, colEl);
49 self.moveColumn(column,
false);
55 colEl.addEventListener(
"mousedown",
function(e){
56 self.touchMove =
false;
58 self.checkTimeout = setTimeout(
function(){
59 self.startMove(e, column);
64 colEl.addEventListener(
"mouseup",
function(e){
66 if(
self.checkTimeout){
67 clearTimeout(
self.checkTimeout);
72 self.bindTouchEvents(column);
75 column.modules.moveColumn = config;
78 MoveColumns.prototype.bindTouchEvents =
function(column){
80 colEl = column.getElement(),
83 currentCol, nextCol, prevCol, nextColWidth, prevColWidth, nextColWidthLast, prevColWidthLast;
85 colEl.addEventListener(
"touchstart",
function(e){
86 self.checkTimeout = setTimeout(
function(){
87 self.touchMove =
true;
89 nextCol = column.nextColumn();
90 nextColWidth = nextCol ? nextCol.getWidth()/2 : 0;
91 prevCol = column.prevColumn();
92 prevColWidth = prevCol ? prevCol.getWidth()/2 : 0;
97 self.startMove(e, column);
101 colEl.addEventListener(
"touchmove",
function(e){
102 var halfCol, diff, moveToCol;
108 startXMove = e.touches[0].pageX;
111 diff = e.touches[0].pageX - startXMove;
114 if(nextCol && diff - nextColWidthLast > nextColWidth){
117 if(moveToCol !== column){
118 startXMove = e.touches[0].pageX;
119 moveToCol.getElement().parentNode.insertBefore(
self.placeholderElement, moveToCol.getElement().nextSibling);
120 self.moveColumn(moveToCol,
true);
124 if(prevCol && -diff - prevColWidthLast > prevColWidth){
127 if(moveToCol !== column){
128 startXMove = e.touches[0].pageX;
129 moveToCol.getElement().parentNode.insertBefore(
self.placeholderElement, moveToCol.getElement());
130 self.moveColumn(moveToCol,
false);
136 currentCol = moveToCol;
137 nextCol = moveToCol.nextColumn();
138 nextColWidthLast = nextColWidth;
139 nextColWidth = nextCol ? nextCol.getWidth() / 2 : 0;
140 prevCol = moveToCol.prevColumn();
141 prevColWidthLast = prevColWidth;
142 prevColWidth = prevCol ? prevCol.getWidth() / 2 : 0;
147 colEl.addEventListener(
"touchend",
function(e){
148 if(
self.checkTimeout){
149 clearTimeout(
self.checkTimeout);
157 MoveColumns.prototype.startMove =
function(e, column){
158 var element = column.getElement();
160 this.moving = column;
161 this.startX = (this.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(element).left;
163 this.table.element.classList.add(
"tabulator-block-select");
166 this.placeholderElement.style.width = column.getWidth() +
"px";
167 this.placeholderElement.style.height = column.getHeight() +
"px";
169 element.parentNode.insertBefore(this.placeholderElement, element);
170 element.parentNode.removeChild(element);
173 this.hoverElement = element.cloneNode(
true);
174 this.hoverElement.classList.add(
"tabulator-moving");
176 this.table.columnManager.getElement().appendChild(this.hoverElement);
178 this.hoverElement.style.left =
"0";
179 this.hoverElement.style.bottom =
"0";
182 this._bindMouseMove();
184 document.body.addEventListener(
"mousemove", this.moveHover);
185 document.body.addEventListener(
"mouseup", this.endMove);
191 MoveColumns.prototype._bindMouseMove =
function(){
192 this.table.columnManager.columnsByIndex.forEach(
function(column){
193 if(column.modules.moveColumn.mousemove){
194 column.getElement().addEventListener(
"mousemove", column.modules.moveColumn.mousemove);
199 MoveColumns.prototype._unbindMouseMove =
function(){
200 this.table.columnManager.columnsByIndex.forEach(
function(column){
201 if(column.modules.moveColumn.mousemove){
202 column.getElement().removeEventListener(
"mousemove", column.modules.moveColumn.mousemove);
207 MoveColumns.prototype.moveColumn =
function(column, after){
208 var movingCells = this.moving.getCells();
211 this.toColAfter = after;
214 column.getCells().forEach(
function(cell, i){
215 var cellEl = cell.getElement();
216 cellEl.parentNode.insertBefore(movingCells[i].getElement(), cellEl.nextSibling);
219 column.getCells().forEach(
function(cell, i){
220 var cellEl = cell.getElement();
221 cellEl.parentNode.insertBefore(movingCells[i].getElement(), cellEl);
226 MoveColumns.prototype.endMove =
function(e){
227 if(e.which === 1 ||
this.touchMove){
228 this._unbindMouseMove();
230 this.placeholderElement.parentNode.insertBefore(this.moving.getElement(), this.placeholderElement.nextSibling);
231 this.placeholderElement.parentNode.removeChild(this.placeholderElement);
232 this.hoverElement.parentNode.removeChild(this.hoverElement);
234 this.table.element.classList.remove(
"tabulator-block-select");
237 this.table.columnManager.moveColumnActual(this.moving, this.toCol, this.toColAfter);
242 this.toColAfter =
false;
245 document.body.removeEventListener(
"mousemove", this.moveHover);
246 document.body.removeEventListener(
"mouseup", this.endMove);
251 MoveColumns.prototype.moveHover =
function(e){
253 columnHolder =
self.table.columnManager.getElement(),
254 scrollLeft = columnHolder.scrollLeft,
255 xPos = ((
self.touchMove ? e.touches[0].pageX : e.pageX) - Tabulator.prototype.helpers.elOffset(columnHolder).left) + scrollLeft,
258 self.hoverElement.style.left = (xPos -
self.startX) +
"px";
260 if(xPos - scrollLeft <
self.autoScrollMargin){
261 if(!
self.autoScrollTimeout){
262 self.autoScrollTimeout = setTimeout(
function(){
263 scrollPos = Math.max(0,scrollLeft-5);
264 self.table.rowManager.getElement().scrollLeft = scrollPos;
265 self.autoScrollTimeout =
false;
270 if(scrollLeft + columnHolder.clientWidth - xPos <
self.autoScrollMargin){
271 if(!
self.autoScrollTimeout){
272 self.autoScrollTimeout = setTimeout(
function(){
273 scrollPos = Math.min(columnHolder.clientWidth, scrollLeft+5);
274 self.table.rowManager.getElement().scrollLeft = scrollPos;
275 self.autoScrollTimeout =
false;
281 Tabulator.prototype.registerModule(
"moveColumn", MoveColumns);