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