Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / misc / tabledrag.js
1 /**
2 * DO NOT EDIT THIS FILE.
3 * See the following change record for more information,
4 * https://www.drupal.org/node/2815083
5 * @preserve
6 **/
7 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; };
8
9 (function ($, Drupal, drupalSettings) {
10   var showWeight = JSON.parse(localStorage.getItem('Drupal.tableDrag.showWeight'));
11
12   Drupal.behaviors.tableDrag = {
13     attach: function attach(context, settings) {
14       function initTableDrag(table, base) {
15         if (table.length) {
16           Drupal.tableDrag[base] = new Drupal.tableDrag(table[0], settings.tableDrag[base]);
17         }
18       }
19
20       Object.keys(settings.tableDrag || {}).forEach(function (base) {
21         initTableDrag($(context).find('#' + base).once('tabledrag'), base);
22       });
23     }
24   };
25
26   Drupal.tableDrag = function (table, tableSettings) {
27     var _this = this;
28
29     var self = this;
30     var $table = $(table);
31
32     this.$table = $(table);
33
34     this.table = table;
35
36     this.tableSettings = tableSettings;
37
38     this.dragObject = null;
39
40     this.rowObject = null;
41
42     this.oldRowElement = null;
43
44     this.oldY = 0;
45
46     this.changed = false;
47
48     this.maxDepth = 0;
49
50     this.rtl = $(this.table).css('direction') === 'rtl' ? -1 : 1;
51
52     this.striping = $(this.table).data('striping') === 1;
53
54     this.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
55
56     this.scrollInterval = null;
57
58     this.scrollY = 0;
59
60     this.windowHeight = 0;
61
62     this.indentEnabled = false;
63     Object.keys(tableSettings || {}).forEach(function (group) {
64       Object.keys(tableSettings[group] || {}).forEach(function (n) {
65         if (tableSettings[group][n].relationship === 'parent') {
66           _this.indentEnabled = true;
67         }
68         if (tableSettings[group][n].limit > 0) {
69           _this.maxDepth = tableSettings[group][n].limit;
70         }
71       });
72     });
73     if (this.indentEnabled) {
74       this.indentCount = 1;
75
76       var indent = Drupal.theme('tableDragIndentation');
77       var testRow = $('<tr/>').addClass('draggable').appendTo(table);
78       var testCell = $('<td/>').appendTo(testRow).prepend(indent).prepend(indent);
79       var $indentation = testCell.find('.js-indentation');
80
81       this.indentAmount = $indentation.get(1).offsetLeft - $indentation.get(0).offsetLeft;
82       testRow.remove();
83     }
84
85     $table.find('> tr.draggable, > tbody > tr.draggable').each(function () {
86       self.makeDraggable(this);
87     });
88
89     $table.before($('<button type="button" class="link tabledrag-toggle-weight"></button>').attr('title', Drupal.t('Re-order rows by numerical weight instead of dragging.')).on('click', $.proxy(function (e) {
90       e.preventDefault();
91       this.toggleColumns();
92     }, this)).wrap('<div class="tabledrag-toggle-weight-wrapper"></div>').parent());
93
94     self.initColumns();
95
96     $(document).on('touchmove', function (event) {
97       return self.dragRow(event.originalEvent.touches[0], self);
98     });
99     $(document).on('touchend', function (event) {
100       return self.dropRow(event.originalEvent.touches[0], self);
101     });
102     $(document).on('mousemove pointermove', function (event) {
103       return self.dragRow(event, self);
104     });
105     $(document).on('mouseup pointerup', function (event) {
106       return self.dropRow(event, self);
107     });
108
109     $(window).on('storage', $.proxy(function (e) {
110       if (e.originalEvent.key === 'Drupal.tableDrag.showWeight') {
111         showWeight = JSON.parse(e.originalEvent.newValue);
112         this.displayColumns(showWeight);
113       }
114     }, this));
115   };
116
117   Drupal.tableDrag.prototype.initColumns = function () {
118     var _this2 = this;
119
120     var $table = this.$table;
121     var hidden = void 0;
122     var cell = void 0;
123     var columnIndex = void 0;
124     Object.keys(this.tableSettings || {}).forEach(function (group) {
125       Object.keys(_this2.tableSettings[group]).some(function (tableSetting) {
126         var field = $table.find('.' + _this2.tableSettings[group][tableSetting].target).eq(0);
127         if (field.length && _this2.tableSettings[group][tableSetting].hidden) {
128           hidden = _this2.tableSettings[group][tableSetting].hidden;
129           cell = field.closest('td');
130           return true;
131         }
132         return false;
133       });
134
135       if (hidden && cell[0]) {
136         columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1;
137         $table.find('> thead > tr, > tbody > tr, > tr').each(_this2.addColspanClass(columnIndex));
138       }
139     });
140     this.displayColumns(showWeight);
141   };
142
143   Drupal.tableDrag.prototype.addColspanClass = function (columnIndex) {
144     return function () {
145       var $row = $(this);
146       var index = columnIndex;
147       var cells = $row.children();
148       var cell = void 0;
149       cells.each(function (n) {
150         if (n < index && this.colSpan && this.colSpan > 1) {
151           index -= this.colSpan - 1;
152         }
153       });
154       if (index > 0) {
155         cell = cells.filter(':nth-child(' + index + ')');
156         if (cell[0].colSpan && cell[0].colSpan > 1) {
157           cell.addClass('tabledrag-has-colspan');
158         } else {
159           cell.addClass('tabledrag-hide');
160         }
161       }
162     };
163   };
164
165   Drupal.tableDrag.prototype.displayColumns = function (displayWeight) {
166     if (displayWeight) {
167       this.showColumns();
168     } else {
169         this.hideColumns();
170       }
171
172     $('table').findOnce('tabledrag').trigger('columnschange', !!displayWeight);
173   };
174
175   Drupal.tableDrag.prototype.toggleColumns = function () {
176     showWeight = !showWeight;
177     this.displayColumns(showWeight);
178     if (showWeight) {
179       localStorage.setItem('Drupal.tableDrag.showWeight', showWeight);
180     } else {
181       localStorage.removeItem('Drupal.tableDrag.showWeight');
182     }
183   };
184
185   Drupal.tableDrag.prototype.hideColumns = function () {
186     var $tables = $('table').findOnce('tabledrag');
187
188     $tables.find('.tabledrag-hide').css('display', 'none');
189
190     $tables.find('.tabledrag-handle').css('display', '');
191
192     $tables.find('.tabledrag-has-colspan').each(function () {
193       this.colSpan = this.colSpan - 1;
194     });
195
196     $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights'));
197   };
198
199   Drupal.tableDrag.prototype.showColumns = function () {
200     var $tables = $('table').findOnce('tabledrag');
201
202     $tables.find('.tabledrag-hide').css('display', '');
203
204     $tables.find('.tabledrag-handle').css('display', 'none');
205
206     $tables.find('.tabledrag-has-colspan').each(function () {
207       this.colSpan = this.colSpan + 1;
208     });
209
210     $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights'));
211   };
212
213   Drupal.tableDrag.prototype.rowSettings = function (group, row) {
214     var field = $(row).find('.' + group);
215     var tableSettingsGroup = this.tableSettings[group];
216     return Object.keys(tableSettingsGroup).map(function (delta) {
217       var targetClass = tableSettingsGroup[delta].target;
218       var rowSettings = void 0;
219       if (field.is('.' + targetClass)) {
220         rowSettings = {};
221         Object.keys(tableSettingsGroup[delta]).forEach(function (n) {
222           rowSettings[n] = tableSettingsGroup[delta][n];
223         });
224       }
225       return rowSettings;
226     }).filter(function (rowSetting) {
227       return rowSetting;
228     })[0];
229   };
230
231   Drupal.tableDrag.prototype.makeDraggable = function (item) {
232     var self = this;
233     var $item = $(item);
234
235     $item.find('td:first-of-type').find('a').addClass('menu-item__link');
236
237     var handle = $('<a href="#" class="tabledrag-handle"><div class="handle">&nbsp;</div></a>').attr('title', Drupal.t('Drag to re-order'));
238
239     var $indentationLast = $item.find('td:first-of-type').find('.js-indentation').eq(-1);
240     if ($indentationLast.length) {
241       $indentationLast.after(handle);
242
243       self.indentCount = Math.max($item.find('.js-indentation').length, self.indentCount);
244     } else {
245       $item.find('td').eq(0).prepend(handle);
246     }
247
248     handle.on('mousedown touchstart pointerdown', function (event) {
249       event.preventDefault();
250       if (event.originalEvent.type === 'touchstart') {
251         event = event.originalEvent.touches[0];
252       }
253       self.dragStart(event, self, item);
254     });
255
256     handle.on('click', function (e) {
257       e.preventDefault();
258     });
259
260     handle.on('focus', function () {
261       self.safeBlur = true;
262     });
263
264     handle.on('blur', function (event) {
265       if (self.rowObject && self.safeBlur) {
266         self.dropRow(event, self);
267       }
268     });
269
270     handle.on('keydown', function (event) {
271       if (event.keyCode !== 9 && !self.rowObject) {
272         self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
273       }
274
275       var keyChange = false;
276       var groupHeight = void 0;
277
278       switch (event.keyCode) {
279         case 37:
280         case 63234:
281           keyChange = true;
282           self.rowObject.indent(-1 * self.rtl);
283           break;
284
285         case 38:
286         case 63232:
287           {
288             var $previousRow = $(self.rowObject.element).prev('tr:first-of-type');
289             var previousRow = $previousRow.get(0);
290             while (previousRow && $previousRow.is(':hidden')) {
291               $previousRow = $(previousRow).prev('tr:first-of-type');
292               previousRow = $previousRow.get(0);
293             }
294             if (previousRow) {
295               self.safeBlur = false;
296               self.rowObject.direction = 'up';
297               keyChange = true;
298
299               if ($(item).is('.tabledrag-root')) {
300                 groupHeight = 0;
301                 while (previousRow && $previousRow.find('.js-indentation').length) {
302                   $previousRow = $(previousRow).prev('tr:first-of-type');
303                   previousRow = $previousRow.get(0);
304                   groupHeight += $previousRow.is(':hidden') ? 0 : previousRow.offsetHeight;
305                 }
306                 if (previousRow) {
307                   self.rowObject.swap('before', previousRow);
308
309                   window.scrollBy(0, -groupHeight);
310                 }
311               } else if (self.table.tBodies[0].rows[0] !== previousRow || $previousRow.is('.draggable')) {
312                 self.rowObject.swap('before', previousRow);
313                 self.rowObject.interval = null;
314                 self.rowObject.indent(0);
315                 window.scrollBy(0, -parseInt(item.offsetHeight, 10));
316               }
317
318               handle.trigger('focus');
319             }
320             break;
321           }
322
323         case 39:
324         case 63235:
325           keyChange = true;
326           self.rowObject.indent(self.rtl);
327           break;
328
329         case 40:
330         case 63233:
331           {
332             var $nextRow = $(self.rowObject.group).eq(-1).next('tr:first-of-type');
333             var nextRow = $nextRow.get(0);
334             while (nextRow && $nextRow.is(':hidden')) {
335               $nextRow = $(nextRow).next('tr:first-of-type');
336               nextRow = $nextRow.get(0);
337             }
338             if (nextRow) {
339               self.safeBlur = false;
340               self.rowObject.direction = 'down';
341               keyChange = true;
342
343               if ($(item).is('.tabledrag-root')) {
344                 groupHeight = 0;
345                 var nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
346                 if (nextGroup) {
347                   $(nextGroup.group).each(function () {
348                     groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
349                   });
350                   var nextGroupRow = $(nextGroup.group).eq(-1).get(0);
351                   self.rowObject.swap('after', nextGroupRow);
352
353                   window.scrollBy(0, parseInt(groupHeight, 10));
354                 }
355               } else {
356                 self.rowObject.swap('after', nextRow);
357                 self.rowObject.interval = null;
358                 self.rowObject.indent(0);
359                 window.scrollBy(0, parseInt(item.offsetHeight, 10));
360               }
361
362               handle.trigger('focus');
363             }
364             break;
365           }
366       }
367
368       if (self.rowObject && self.rowObject.changed === true) {
369         $(item).addClass('drag');
370         if (self.oldRowElement) {
371           $(self.oldRowElement).removeClass('drag-previous');
372         }
373         self.oldRowElement = item;
374         if (self.striping === true) {
375           self.restripeTable();
376         }
377         self.onDrag();
378       }
379
380       if (keyChange) {
381         return false;
382       }
383     });
384
385     handle.on('keypress', function (event) {
386
387       switch (event.keyCode) {
388         case 37:
389         case 38:
390         case 39:
391         case 40:
392           return false;
393       }
394     });
395   };
396
397   Drupal.tableDrag.prototype.dragStart = function (event, self, item) {
398     self.dragObject = {};
399     self.dragObject.initOffset = self.getPointerOffset(item, event);
400     self.dragObject.initPointerCoords = self.pointerCoords(event);
401     if (self.indentEnabled) {
402       self.dragObject.indentPointerPos = self.dragObject.initPointerCoords;
403     }
404
405     if (self.rowObject) {
406       $(self.rowObject.element).find('a.tabledrag-handle').trigger('blur');
407     }
408
409     self.rowObject = new self.row(item, 'pointer', self.indentEnabled, self.maxDepth, true);
410
411     self.table.topY = $(self.table).offset().top;
412     self.table.bottomY = self.table.topY + self.table.offsetHeight;
413
414     $(item).addClass('drag');
415
416     $('body').addClass('drag');
417     if (self.oldRowElement) {
418       $(self.oldRowElement).removeClass('drag-previous');
419     }
420   };
421
422   Drupal.tableDrag.prototype.dragRow = function (event, self) {
423     if (self.dragObject) {
424       self.currentPointerCoords = self.pointerCoords(event);
425       var y = self.currentPointerCoords.y - self.dragObject.initOffset.y;
426       var x = self.currentPointerCoords.x - self.dragObject.initOffset.x;
427
428       if (y !== self.oldY) {
429         self.rowObject.direction = y > self.oldY ? 'down' : 'up';
430
431         self.oldY = y;
432
433         var scrollAmount = self.checkScroll(self.currentPointerCoords.y);
434
435         clearInterval(self.scrollInterval);
436
437         if (scrollAmount > 0 && self.rowObject.direction === 'down' || scrollAmount < 0 && self.rowObject.direction === 'up') {
438           self.setScroll(scrollAmount);
439         }
440
441         var currentRow = self.findDropTargetRow(x, y);
442         if (currentRow) {
443           if (self.rowObject.direction === 'down') {
444             self.rowObject.swap('after', currentRow, self);
445           } else {
446             self.rowObject.swap('before', currentRow, self);
447           }
448           if (self.striping === true) {
449             self.restripeTable();
450           }
451         }
452       }
453
454       if (self.indentEnabled) {
455         var xDiff = self.currentPointerCoords.x - self.dragObject.indentPointerPos.x;
456
457         var indentDiff = Math.round(xDiff / self.indentAmount);
458
459         var indentChange = self.rowObject.indent(indentDiff);
460
461         self.dragObject.indentPointerPos.x += self.indentAmount * indentChange * self.rtl;
462         self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
463       }
464
465       return false;
466     }
467   };
468
469   Drupal.tableDrag.prototype.dropRow = function (event, self) {
470     var droppedRow = void 0;
471     var $droppedRow = void 0;
472
473     if (self.rowObject !== null) {
474       droppedRow = self.rowObject.element;
475       $droppedRow = $(droppedRow);
476
477       if (self.rowObject.changed === true) {
478         self.updateFields(droppedRow);
479
480         Object.keys(self.tableSettings || {}).forEach(function (group) {
481           var rowSettings = self.rowSettings(group, droppedRow);
482           if (rowSettings.relationship === 'group') {
483             Object.keys(self.rowObject.children || {}).forEach(function (n) {
484               self.updateField(self.rowObject.children[n], group);
485             });
486           }
487         });
488
489         self.rowObject.markChanged();
490         if (self.changed === false) {
491           $(Drupal.theme('tableDragChangedWarning')).insertBefore(self.table).hide().fadeIn('slow');
492           self.changed = true;
493         }
494       }
495
496       if (self.indentEnabled) {
497         self.rowObject.removeIndentClasses();
498       }
499       if (self.oldRowElement) {
500         $(self.oldRowElement).removeClass('drag-previous');
501       }
502       $droppedRow.removeClass('drag').addClass('drag-previous');
503       self.oldRowElement = droppedRow;
504       self.onDrop();
505       self.rowObject = null;
506     }
507
508     if (self.dragObject !== null) {
509       self.dragObject = null;
510       $('body').removeClass('drag');
511       clearInterval(self.scrollInterval);
512     }
513   };
514
515   Drupal.tableDrag.prototype.pointerCoords = function (event) {
516     if (event.pageX || event.pageY) {
517       return { x: event.pageX, y: event.pageY };
518     }
519     return {
520       x: event.clientX + document.body.scrollLeft - document.body.clientLeft,
521       y: event.clientY + document.body.scrollTop - document.body.clientTop
522     };
523   };
524
525   Drupal.tableDrag.prototype.getPointerOffset = function (target, event) {
526     var docPos = $(target).offset();
527     var pointerPos = this.pointerCoords(event);
528     return { x: pointerPos.x - docPos.left, y: pointerPos.y - docPos.top };
529   };
530
531   Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) {
532     var _this3 = this;
533
534     var rows = $(this.table.tBodies[0].rows).not(':hidden');
535
536     var _loop = function _loop(n) {
537       var row = rows[n];
538       var $row = $(row);
539       var rowY = $row.offset().top;
540       var rowHeight = void 0;
541
542       if (row.offsetHeight === 0) {
543         rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
544       } else {
545           rowHeight = parseInt(row.offsetHeight, 10) / 2;
546         }
547
548       if (y > rowY - rowHeight && y < rowY + rowHeight) {
549         if (_this3.indentEnabled) {
550           if (Object.keys(_this3.rowObject.group).some(function (o) {
551             return _this3.rowObject.group[o] === row;
552           })) {
553             return {
554               v: null
555             };
556           }
557         } else if (row === _this3.rowObject.element) {
558             return {
559               v: null
560             };
561           }
562
563         if (!_this3.rowObject.isValidSwap(row)) {
564           return {
565             v: null
566           };
567         }
568
569         while ($row.is(':hidden') && $row.prev('tr').is(':hidden')) {
570           $row = $row.prev('tr:first-of-type');
571           row = $row.get(0);
572         }
573         return {
574           v: row
575         };
576       }
577     };
578
579     for (var n = 0; n < rows.length; n++) {
580       var _ret = _loop(n);
581
582       if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
583     }
584     return null;
585   };
586
587   Drupal.tableDrag.prototype.updateFields = function (changedRow) {
588     var _this4 = this;
589
590     Object.keys(this.tableSettings || {}).forEach(function (group) {
591       _this4.updateField(changedRow, group);
592     });
593   };
594
595   Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
596     var rowSettings = this.rowSettings(group, changedRow);
597     var $changedRow = $(changedRow);
598     var sourceRow = void 0;
599     var $previousRow = void 0;
600     var previousRow = void 0;
601     var useSibling = void 0;
602
603     if (rowSettings.relationship === 'self' || rowSettings.relationship === 'group') {
604       sourceRow = changedRow;
605     } else if (rowSettings.relationship === 'sibling') {
606         $previousRow = $changedRow.prev('tr:first-of-type');
607         previousRow = $previousRow.get(0);
608         var $nextRow = $changedRow.next('tr:first-of-type');
609         var nextRow = $nextRow.get(0);
610         sourceRow = changedRow;
611         if ($previousRow.is('.draggable') && $previousRow.find('.' + group).length) {
612           if (this.indentEnabled) {
613             if ($previousRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
614               sourceRow = previousRow;
615             }
616           } else {
617             sourceRow = previousRow;
618           }
619         } else if ($nextRow.is('.draggable') && $nextRow.find('.' + group).length) {
620           if (this.indentEnabled) {
621             if ($nextRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
622               sourceRow = nextRow;
623             }
624           } else {
625             sourceRow = nextRow;
626           }
627         }
628       } else if (rowSettings.relationship === 'parent') {
629           $previousRow = $changedRow.prev('tr');
630           previousRow = $previousRow;
631           while ($previousRow.length && $previousRow.find('.js-indentation').length >= this.rowObject.indents) {
632             $previousRow = $previousRow.prev('tr');
633             previousRow = $previousRow;
634           }
635
636           if ($previousRow.length) {
637             sourceRow = $previousRow.get(0);
638           } else {
639               sourceRow = $(this.table).find('tr.draggable:first-of-type').get(0);
640               if (sourceRow === this.rowObject.element) {
641                 sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
642               }
643               useSibling = true;
644             }
645         }
646
647     this.copyDragClasses(sourceRow, changedRow, group);
648     rowSettings = this.rowSettings(group, changedRow);
649
650     if (useSibling) {
651       rowSettings.relationship = 'sibling';
652       rowSettings.source = rowSettings.target;
653     }
654
655     var targetClass = '.' + rowSettings.target;
656     var targetElement = $changedRow.find(targetClass).get(0);
657
658     if (targetElement) {
659       var sourceClass = '.' + rowSettings.source;
660       var sourceElement = $(sourceClass, sourceRow).get(0);
661       switch (rowSettings.action) {
662         case 'depth':
663           targetElement.value = $(sourceElement).closest('tr').find('.js-indentation').length;
664           break;
665
666         case 'match':
667           targetElement.value = sourceElement.value;
668           break;
669
670         case 'order':
671           {
672             var siblings = this.rowObject.findSiblings(rowSettings);
673             if ($(targetElement).is('select')) {
674               var values = [];
675               $(targetElement).find('option').each(function () {
676                 values.push(this.value);
677               });
678               var maxVal = values[values.length - 1];
679
680               $(siblings).find(targetClass).each(function () {
681                 if (values.length > 0) {
682                   this.value = values.shift();
683                 } else {
684                   this.value = maxVal;
685                 }
686               });
687             } else {
688               var weight = parseInt($(siblings[0]).find(targetClass).val(), 10) || 0;
689               $(siblings).find(targetClass).each(function () {
690                 this.value = weight;
691                 weight++;
692               });
693             }
694             break;
695           }
696       }
697     }
698   };
699
700   Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
701     var sourceElement = $(sourceRow).find('.' + group);
702     var targetElement = $(targetRow).find('.' + group);
703     if (sourceElement.length && targetElement.length) {
704       targetElement[0].className = sourceElement[0].className;
705     }
706   };
707
708   Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
709     var de = document.documentElement;
710     var b = document.body;
711
712     var windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth !== 0 ? de.clientHeight : b.offsetHeight);
713     this.windowHeight = windowHeight;
714     var scrollY = void 0;
715     if (document.all) {
716       scrollY = !de.scrollTop ? b.scrollTop : de.scrollTop;
717     } else {
718       scrollY = window.pageYOffset ? window.pageYOffset : window.scrollY;
719     }
720     this.scrollY = scrollY;
721     var trigger = this.scrollSettings.trigger;
722     var delta = 0;
723
724     if (cursorY - scrollY > windowHeight - trigger) {
725       delta = trigger / (windowHeight + scrollY - cursorY);
726       delta = delta > 0 && delta < trigger ? delta : trigger;
727       return delta * this.scrollSettings.amount;
728     }
729     if (cursorY - scrollY < trigger) {
730       delta = trigger / (cursorY - scrollY);
731       delta = delta > 0 && delta < trigger ? delta : trigger;
732       return -delta * this.scrollSettings.amount;
733     }
734   };
735
736   Drupal.tableDrag.prototype.setScroll = function (scrollAmount) {
737     var self = this;
738
739     this.scrollInterval = setInterval(function () {
740       self.checkScroll(self.currentPointerCoords.y);
741       var aboveTable = self.scrollY > self.table.topY;
742       var belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
743       if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) {
744         window.scrollBy(0, scrollAmount);
745       }
746     }, this.scrollSettings.interval);
747   };
748
749   Drupal.tableDrag.prototype.restripeTable = function () {
750     $(this.table).find('> tbody > tr.draggable, > tr.draggable').filter(':visible').filter(':odd').removeClass('odd').addClass('even').end().filter(':even').removeClass('even').addClass('odd');
751   };
752
753   Drupal.tableDrag.prototype.onDrag = function () {
754     return null;
755   };
756
757   Drupal.tableDrag.prototype.onDrop = function () {
758     return null;
759   };
760
761   Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) {
762     var $tableRow = $(tableRow);
763
764     this.element = tableRow;
765     this.method = method;
766     this.group = [tableRow];
767     this.groupDepth = $tableRow.find('.js-indentation').length;
768     this.changed = false;
769     this.table = $tableRow.closest('table')[0];
770     this.indentEnabled = indentEnabled;
771     this.maxDepth = maxDepth;
772
773     this.direction = '';
774     if (this.indentEnabled) {
775       this.indents = $tableRow.find('.js-indentation').length;
776       this.children = this.findChildren(addClasses);
777       this.group = $.merge(this.group, this.children);
778
779       for (var n = 0; n < this.group.length; n++) {
780         this.groupDepth = Math.max($(this.group[n]).find('.js-indentation').length, this.groupDepth);
781       }
782     }
783   };
784
785   Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
786     var parentIndentation = this.indents;
787     var currentRow = $(this.element, this.table).next('tr.draggable');
788     var rows = [];
789     var child = 0;
790
791     function rowIndentation(indentNum, el) {
792       var self = $(el);
793       if (child === 1 && indentNum === parentIndentation) {
794         self.addClass('tree-child-first');
795       }
796       if (indentNum === parentIndentation) {
797         self.addClass('tree-child');
798       } else if (indentNum > parentIndentation) {
799         self.addClass('tree-child-horizontal');
800       }
801     }
802
803     while (currentRow.length) {
804       if (currentRow.find('.js-indentation').length > parentIndentation) {
805         child++;
806         rows.push(currentRow[0]);
807         if (addClasses) {
808           currentRow.find('.js-indentation').each(rowIndentation);
809         }
810       } else {
811         break;
812       }
813       currentRow = currentRow.next('tr.draggable');
814     }
815     if (addClasses && rows.length) {
816       $(rows[rows.length - 1]).find('.js-indentation:nth-child(' + (parentIndentation + 1) + ')').addClass('tree-child-last');
817     }
818     return rows;
819   };
820
821   Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
822     var $row = $(row);
823     if (this.indentEnabled) {
824       var prevRow = void 0;
825       var nextRow = void 0;
826       if (this.direction === 'down') {
827         prevRow = row;
828         nextRow = $row.next('tr').get(0);
829       } else {
830         prevRow = $row.prev('tr').get(0);
831         nextRow = row;
832       }
833       this.interval = this.validIndentInterval(prevRow, nextRow);
834
835       if (this.interval.min > this.interval.max) {
836         return false;
837       }
838     }
839
840     if (this.table.tBodies[0].rows[0] === row && $row.is(':not(.draggable)')) {
841       return false;
842     }
843
844     return true;
845   };
846
847   Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) {
848     this.group.forEach(function (row) {
849       Drupal.detachBehaviors(row, drupalSettings, 'move');
850     });
851     $(row)[position](this.group);
852
853     this.group.forEach(function (row) {
854       Drupal.attachBehaviors(row, drupalSettings);
855     });
856     this.changed = true;
857     this.onSwap(row);
858   };
859
860   Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
861     var $prevRow = $(prevRow);
862     var maxIndent = void 0;
863
864     var minIndent = nextRow ? $(nextRow).find('.js-indentation').length : 0;
865
866     if (!prevRow || $prevRow.is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
867       maxIndent = 0;
868     } else {
869       maxIndent = $prevRow.find('.js-indentation').length + ($prevRow.is('.tabledrag-leaf') ? 0 : 1);
870
871       if (this.maxDepth) {
872         maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
873       }
874     }
875
876     return { min: minIndent, max: maxIndent };
877   };
878
879   Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) {
880     var $group = $(this.group);
881
882     if (!this.interval) {
883       var prevRow = $(this.element).prev('tr').get(0);
884       var nextRow = $group.eq(-1).next('tr').get(0);
885       this.interval = this.validIndentInterval(prevRow, nextRow);
886     }
887
888     var indent = this.indents + indentDiff;
889     indent = Math.max(indent, this.interval.min);
890     indent = Math.min(indent, this.interval.max);
891     indentDiff = indent - this.indents;
892
893     for (var n = 1; n <= Math.abs(indentDiff); n++) {
894       if (indentDiff < 0) {
895         $group.find('.js-indentation:first-of-type').remove();
896         this.indents--;
897       } else {
898         $group.find('td:first-of-type').prepend(Drupal.theme('tableDragIndentation'));
899         this.indents++;
900       }
901     }
902     if (indentDiff) {
903       this.changed = true;
904       this.groupDepth += indentDiff;
905       this.onIndent();
906     }
907
908     return indentDiff;
909   };
910
911   Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) {
912     var siblings = [];
913     var directions = ['prev', 'next'];
914     var rowIndentation = this.indents;
915     var checkRowIndentation = void 0;
916     for (var d = 0; d < directions.length; d++) {
917       var checkRow = $(this.element)[directions[d]]();
918       while (checkRow.length) {
919         if (checkRow.find('.' + rowSettings.target)) {
920           if (this.indentEnabled) {
921             checkRowIndentation = checkRow.find('.js-indentation').length;
922           }
923
924           if (!this.indentEnabled || checkRowIndentation === rowIndentation) {
925             siblings.push(checkRow[0]);
926           } else if (checkRowIndentation < rowIndentation) {
927             break;
928           }
929         } else {
930           break;
931         }
932         checkRow = checkRow[directions[d]]();
933       }
934
935       if (directions[d] === 'prev') {
936         siblings.reverse();
937         siblings.push(this.element);
938       }
939     }
940     return siblings;
941   };
942
943   Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () {
944     var _this5 = this;
945
946     Object.keys(this.children || {}).forEach(function (n) {
947       $(_this5.children[n]).find('.js-indentation').removeClass('tree-child').removeClass('tree-child-first').removeClass('tree-child-last').removeClass('tree-child-horizontal');
948     });
949   };
950
951   Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
952     var marker = Drupal.theme('tableDragChangedMarker');
953     var cell = $(this.element).find('td:first-of-type');
954     if (cell.find('abbr.tabledrag-changed').length === 0) {
955       cell.append(marker);
956     }
957   };
958
959   Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
960     return null;
961   };
962
963   Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
964     return null;
965   };
966
967   $.extend(Drupal.theme, {
968     tableDragChangedMarker: function tableDragChangedMarker() {
969       return '<abbr class="warning tabledrag-changed" title="' + Drupal.t('Changed') + '">*</abbr>';
970     },
971     tableDragIndentation: function tableDragIndentation() {
972       return '<div class="js-indentation indentation">&nbsp;</div>';
973     },
974     tableDragChangedWarning: function tableDragChangedWarning() {
975       return '<div class="tabledrag-changed-warning messages messages--warning" role="alert">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('You have unsaved changes.') + '</div>';
976     }
977   });
978 })(jQuery, Drupal, drupalSettings);