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