Chris@0: /**
Chris@0: * DO NOT EDIT THIS FILE.
Chris@0: * See the following change record for more information,
Chris@0: * https://www.drupal.org/node/2815083
Chris@0: * @preserve
Chris@0: **/
Chris@17: 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; };
Chris@0:
Chris@0: (function ($, Drupal, drupalSettings) {
Chris@0: var showWeight = JSON.parse(localStorage.getItem('Drupal.tableDrag.showWeight'));
Chris@0:
Chris@0: Drupal.behaviors.tableDrag = {
Chris@0: attach: function attach(context, settings) {
Chris@0: function initTableDrag(table, base) {
Chris@0: if (table.length) {
Chris@0: Drupal.tableDrag[base] = new Drupal.tableDrag(table[0], settings.tableDrag[base]);
Chris@0: }
Chris@0: }
Chris@0:
Chris@14: Object.keys(settings.tableDrag || {}).forEach(function (base) {
Chris@14: initTableDrag($(context).find('#' + base).once('tabledrag'), base);
Chris@14: });
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag = function (table, tableSettings) {
Chris@14: var _this = this;
Chris@14:
Chris@0: var self = this;
Chris@0: var $table = $(table);
Chris@0:
Chris@0: this.$table = $(table);
Chris@0:
Chris@0: this.table = table;
Chris@0:
Chris@0: this.tableSettings = tableSettings;
Chris@0:
Chris@0: this.dragObject = null;
Chris@0:
Chris@0: this.rowObject = null;
Chris@0:
Chris@0: this.oldRowElement = null;
Chris@0:
Chris@0: this.oldY = 0;
Chris@0:
Chris@0: this.changed = false;
Chris@0:
Chris@0: this.maxDepth = 0;
Chris@0:
Chris@0: this.rtl = $(this.table).css('direction') === 'rtl' ? -1 : 1;
Chris@0:
Chris@0: this.striping = $(this.table).data('striping') === 1;
Chris@0:
Chris@0: this.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
Chris@0:
Chris@0: this.scrollInterval = null;
Chris@0:
Chris@0: this.scrollY = 0;
Chris@0:
Chris@0: this.windowHeight = 0;
Chris@0:
Chris@0: this.indentEnabled = false;
Chris@14: Object.keys(tableSettings || {}).forEach(function (group) {
Chris@14: Object.keys(tableSettings[group] || {}).forEach(function (n) {
Chris@14: if (tableSettings[group][n].relationship === 'parent') {
Chris@14: _this.indentEnabled = true;
Chris@0: }
Chris@14: if (tableSettings[group][n].limit > 0) {
Chris@14: _this.maxDepth = tableSettings[group][n].limit;
Chris@14: }
Chris@14: });
Chris@14: });
Chris@0: if (this.indentEnabled) {
Chris@0: this.indentCount = 1;
Chris@0:
Chris@0: var indent = Drupal.theme('tableDragIndentation');
Chris@0: var testRow = $('
').addClass('draggable').appendTo(table);
Chris@0: var testCell = $(' | ').appendTo(testRow).prepend(indent).prepend(indent);
Chris@0: var $indentation = testCell.find('.js-indentation');
Chris@0:
Chris@0: this.indentAmount = $indentation.get(1).offsetLeft - $indentation.get(0).offsetLeft;
Chris@0: testRow.remove();
Chris@0: }
Chris@0:
Chris@0: $table.find('> tr.draggable, > tbody > tr.draggable').each(function () {
Chris@0: self.makeDraggable(this);
Chris@0: });
Chris@0:
Chris@18: $table.before($('').on('click', $.proxy(function (e) {
Chris@0: e.preventDefault();
Chris@0: this.toggleColumns();
Chris@0: }, this)).wrap('').parent());
Chris@0:
Chris@0: self.initColumns();
Chris@0:
Chris@0: $(document).on('touchmove', function (event) {
Chris@0: return self.dragRow(event.originalEvent.touches[0], self);
Chris@0: });
Chris@0: $(document).on('touchend', function (event) {
Chris@0: return self.dropRow(event.originalEvent.touches[0], self);
Chris@0: });
Chris@0: $(document).on('mousemove pointermove', function (event) {
Chris@0: return self.dragRow(event, self);
Chris@0: });
Chris@0: $(document).on('mouseup pointerup', function (event) {
Chris@0: return self.dropRow(event, self);
Chris@0: });
Chris@0:
Chris@0: $(window).on('storage', $.proxy(function (e) {
Chris@0: if (e.originalEvent.key === 'Drupal.tableDrag.showWeight') {
Chris@0: showWeight = JSON.parse(e.originalEvent.newValue);
Chris@0: this.displayColumns(showWeight);
Chris@0: }
Chris@0: }, this));
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.initColumns = function () {
Chris@14: var _this2 = this;
Chris@14:
Chris@0: var $table = this.$table;
Chris@0: var hidden = void 0;
Chris@0: var cell = void 0;
Chris@0: var columnIndex = void 0;
Chris@14: Object.keys(this.tableSettings || {}).forEach(function (group) {
Chris@17: Object.keys(_this2.tableSettings[group]).some(function (tableSetting) {
Chris@17: var field = $table.find('.' + _this2.tableSettings[group][tableSetting].target).eq(0);
Chris@17: if (field.length && _this2.tableSettings[group][tableSetting].hidden) {
Chris@17: hidden = _this2.tableSettings[group][tableSetting].hidden;
Chris@17: cell = field.closest('td');
Chris@17: return true;
Chris@0: }
Chris@17: return false;
Chris@17: });
Chris@0:
Chris@14: if (hidden && cell[0]) {
Chris@14: columnIndex = cell.parent().find('> td').index(cell.get(0)) + 1;
Chris@14: $table.find('> thead > tr, > tbody > tr, > tr').each(_this2.addColspanClass(columnIndex));
Chris@0: }
Chris@14: });
Chris@0: this.displayColumns(showWeight);
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.addColspanClass = function (columnIndex) {
Chris@0: return function () {
Chris@0: var $row = $(this);
Chris@0: var index = columnIndex;
Chris@0: var cells = $row.children();
Chris@0: var cell = void 0;
Chris@0: cells.each(function (n) {
Chris@0: if (n < index && this.colSpan && this.colSpan > 1) {
Chris@0: index -= this.colSpan - 1;
Chris@0: }
Chris@0: });
Chris@0: if (index > 0) {
Chris@0: cell = cells.filter(':nth-child(' + index + ')');
Chris@0: if (cell[0].colSpan && cell[0].colSpan > 1) {
Chris@0: cell.addClass('tabledrag-has-colspan');
Chris@0: } else {
Chris@0: cell.addClass('tabledrag-hide');
Chris@0: }
Chris@0: }
Chris@0: };
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.displayColumns = function (displayWeight) {
Chris@0: if (displayWeight) {
Chris@0: this.showColumns();
Chris@0: } else {
Chris@0: this.hideColumns();
Chris@0: }
Chris@0:
Chris@0: $('table').findOnce('tabledrag').trigger('columnschange', !!displayWeight);
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.toggleColumns = function () {
Chris@0: showWeight = !showWeight;
Chris@0: this.displayColumns(showWeight);
Chris@0: if (showWeight) {
Chris@0: localStorage.setItem('Drupal.tableDrag.showWeight', showWeight);
Chris@0: } else {
Chris@0: localStorage.removeItem('Drupal.tableDrag.showWeight');
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.hideColumns = function () {
Chris@0: var $tables = $('table').findOnce('tabledrag');
Chris@0:
Chris@0: $tables.find('.tabledrag-hide').css('display', 'none');
Chris@0:
Chris@0: $tables.find('.tabledrag-handle').css('display', '');
Chris@0:
Chris@0: $tables.find('.tabledrag-has-colspan').each(function () {
Chris@0: this.colSpan = this.colSpan - 1;
Chris@0: });
Chris@0:
Chris@0: $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights'));
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.showColumns = function () {
Chris@0: var $tables = $('table').findOnce('tabledrag');
Chris@0:
Chris@0: $tables.find('.tabledrag-hide').css('display', '');
Chris@0:
Chris@0: $tables.find('.tabledrag-handle').css('display', 'none');
Chris@0:
Chris@0: $tables.find('.tabledrag-has-colspan').each(function () {
Chris@0: this.colSpan = this.colSpan + 1;
Chris@0: });
Chris@0:
Chris@0: $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights'));
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.rowSettings = function (group, row) {
Chris@0: var field = $(row).find('.' + group);
Chris@0: var tableSettingsGroup = this.tableSettings[group];
Chris@17: return Object.keys(tableSettingsGroup).map(function (delta) {
Chris@17: var targetClass = tableSettingsGroup[delta].target;
Chris@17: var rowSettings = void 0;
Chris@17: if (field.is('.' + targetClass)) {
Chris@17: rowSettings = {};
Chris@17: Object.keys(tableSettingsGroup[delta]).forEach(function (n) {
Chris@17: rowSettings[n] = tableSettingsGroup[delta][n];
Chris@17: });
Chris@0: }
Chris@17: return rowSettings;
Chris@17: }).filter(function (rowSetting) {
Chris@17: return rowSetting;
Chris@17: })[0];
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.makeDraggable = function (item) {
Chris@0: var self = this;
Chris@0: var $item = $(item);
Chris@0:
Chris@0: $item.find('td:first-of-type').find('a').addClass('menu-item__link');
Chris@0:
Chris@0: var handle = $('
').attr('title', Drupal.t('Drag to re-order'));
Chris@0:
Chris@0: var $indentationLast = $item.find('td:first-of-type').find('.js-indentation').eq(-1);
Chris@0: if ($indentationLast.length) {
Chris@0: $indentationLast.after(handle);
Chris@0:
Chris@0: self.indentCount = Math.max($item.find('.js-indentation').length, self.indentCount);
Chris@0: } else {
Chris@0: $item.find('td').eq(0).prepend(handle);
Chris@0: }
Chris@0:
Chris@0: handle.on('mousedown touchstart pointerdown', function (event) {
Chris@0: event.preventDefault();
Chris@0: if (event.originalEvent.type === 'touchstart') {
Chris@0: event = event.originalEvent.touches[0];
Chris@0: }
Chris@0: self.dragStart(event, self, item);
Chris@0: });
Chris@0:
Chris@0: handle.on('click', function (e) {
Chris@0: e.preventDefault();
Chris@0: });
Chris@0:
Chris@0: handle.on('focus', function () {
Chris@0: self.safeBlur = true;
Chris@0: });
Chris@0:
Chris@0: handle.on('blur', function (event) {
Chris@0: if (self.rowObject && self.safeBlur) {
Chris@0: self.dropRow(event, self);
Chris@0: }
Chris@0: });
Chris@0:
Chris@0: handle.on('keydown', function (event) {
Chris@0: if (event.keyCode !== 9 && !self.rowObject) {
Chris@0: self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
Chris@0: }
Chris@0:
Chris@0: var keyChange = false;
Chris@0: var groupHeight = void 0;
Chris@0:
Chris@0: switch (event.keyCode) {
Chris@0: case 37:
Chris@0: case 63234:
Chris@0: keyChange = true;
Chris@0: self.rowObject.indent(-1 * self.rtl);
Chris@0: break;
Chris@0:
Chris@0: case 38:
Chris@0: case 63232:
Chris@14: {
Chris@17: var $previousRow = $(self.rowObject.element).prev('tr').eq(0);
Chris@14: var previousRow = $previousRow.get(0);
Chris@14: while (previousRow && $previousRow.is(':hidden')) {
Chris@17: $previousRow = $(previousRow).prev('tr').eq(0);
Chris@14: previousRow = $previousRow.get(0);
Chris@14: }
Chris@14: if (previousRow) {
Chris@14: self.safeBlur = false;
Chris@14: self.rowObject.direction = 'up';
Chris@14: keyChange = true;
Chris@14:
Chris@14: if ($(item).is('.tabledrag-root')) {
Chris@14: groupHeight = 0;
Chris@14: while (previousRow && $previousRow.find('.js-indentation').length) {
Chris@17: $previousRow = $(previousRow).prev('tr').eq(0);
Chris@14: previousRow = $previousRow.get(0);
Chris@14: groupHeight += $previousRow.is(':hidden') ? 0 : previousRow.offsetHeight;
Chris@14: }
Chris@14: if (previousRow) {
Chris@14: self.rowObject.swap('before', previousRow);
Chris@14:
Chris@14: window.scrollBy(0, -groupHeight);
Chris@14: }
Chris@14: } else if (self.table.tBodies[0].rows[0] !== previousRow || $previousRow.is('.draggable')) {
Chris@14: self.rowObject.swap('before', previousRow);
Chris@14: self.rowObject.interval = null;
Chris@14: self.rowObject.indent(0);
Chris@14: window.scrollBy(0, -parseInt(item.offsetHeight, 10));
Chris@14: }
Chris@14:
Chris@14: handle.trigger('focus');
Chris@14: }
Chris@14: break;
Chris@0: }
Chris@0:
Chris@0: case 39:
Chris@0: case 63235:
Chris@0: keyChange = true;
Chris@0: self.rowObject.indent(self.rtl);
Chris@0: break;
Chris@0:
Chris@0: case 40:
Chris@0: case 63233:
Chris@14: {
Chris@17: var $nextRow = $(self.rowObject.group).eq(-1).next('tr').eq(0);
Chris@14: var nextRow = $nextRow.get(0);
Chris@14: while (nextRow && $nextRow.is(':hidden')) {
Chris@17: $nextRow = $(nextRow).next('tr').eq(0);
Chris@14: nextRow = $nextRow.get(0);
Chris@14: }
Chris@14: if (nextRow) {
Chris@14: self.safeBlur = false;
Chris@14: self.rowObject.direction = 'down';
Chris@14: keyChange = true;
Chris@14:
Chris@14: if ($(item).is('.tabledrag-root')) {
Chris@14: groupHeight = 0;
Chris@14: var nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
Chris@14: if (nextGroup) {
Chris@14: $(nextGroup.group).each(function () {
Chris@14: groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
Chris@14: });
Chris@14: var nextGroupRow = $(nextGroup.group).eq(-1).get(0);
Chris@14: self.rowObject.swap('after', nextGroupRow);
Chris@14:
Chris@14: window.scrollBy(0, parseInt(groupHeight, 10));
Chris@14: }
Chris@14: } else {
Chris@14: self.rowObject.swap('after', nextRow);
Chris@14: self.rowObject.interval = null;
Chris@14: self.rowObject.indent(0);
Chris@14: window.scrollBy(0, parseInt(item.offsetHeight, 10));
Chris@14: }
Chris@14:
Chris@14: handle.trigger('focus');
Chris@14: }
Chris@14: break;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: if (self.rowObject && self.rowObject.changed === true) {
Chris@0: $(item).addClass('drag');
Chris@0: if (self.oldRowElement) {
Chris@0: $(self.oldRowElement).removeClass('drag-previous');
Chris@0: }
Chris@0: self.oldRowElement = item;
Chris@0: if (self.striping === true) {
Chris@0: self.restripeTable();
Chris@0: }
Chris@0: self.onDrag();
Chris@0: }
Chris@0:
Chris@0: if (keyChange) {
Chris@0: return false;
Chris@0: }
Chris@0: });
Chris@0:
Chris@0: handle.on('keypress', function (event) {
Chris@0:
Chris@0: switch (event.keyCode) {
Chris@0: case 37:
Chris@0: case 38:
Chris@0: case 39:
Chris@0: case 40:
Chris@0: return false;
Chris@0: }
Chris@0: });
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.dragStart = function (event, self, item) {
Chris@0: self.dragObject = {};
Chris@0: self.dragObject.initOffset = self.getPointerOffset(item, event);
Chris@0: self.dragObject.initPointerCoords = self.pointerCoords(event);
Chris@0: if (self.indentEnabled) {
Chris@0: self.dragObject.indentPointerPos = self.dragObject.initPointerCoords;
Chris@0: }
Chris@0:
Chris@0: if (self.rowObject) {
Chris@0: $(self.rowObject.element).find('a.tabledrag-handle').trigger('blur');
Chris@0: }
Chris@0:
Chris@0: self.rowObject = new self.row(item, 'pointer', self.indentEnabled, self.maxDepth, true);
Chris@0:
Chris@0: self.table.topY = $(self.table).offset().top;
Chris@0: self.table.bottomY = self.table.topY + self.table.offsetHeight;
Chris@0:
Chris@0: $(item).addClass('drag');
Chris@0:
Chris@0: $('body').addClass('drag');
Chris@0: if (self.oldRowElement) {
Chris@0: $(self.oldRowElement).removeClass('drag-previous');
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.dragRow = function (event, self) {
Chris@0: if (self.dragObject) {
Chris@0: self.currentPointerCoords = self.pointerCoords(event);
Chris@0: var y = self.currentPointerCoords.y - self.dragObject.initOffset.y;
Chris@0: var x = self.currentPointerCoords.x - self.dragObject.initOffset.x;
Chris@0:
Chris@0: if (y !== self.oldY) {
Chris@0: self.rowObject.direction = y > self.oldY ? 'down' : 'up';
Chris@0:
Chris@0: self.oldY = y;
Chris@0:
Chris@0: var scrollAmount = self.checkScroll(self.currentPointerCoords.y);
Chris@0:
Chris@0: clearInterval(self.scrollInterval);
Chris@0:
Chris@0: if (scrollAmount > 0 && self.rowObject.direction === 'down' || scrollAmount < 0 && self.rowObject.direction === 'up') {
Chris@0: self.setScroll(scrollAmount);
Chris@0: }
Chris@0:
Chris@0: var currentRow = self.findDropTargetRow(x, y);
Chris@0: if (currentRow) {
Chris@0: if (self.rowObject.direction === 'down') {
Chris@0: self.rowObject.swap('after', currentRow, self);
Chris@0: } else {
Chris@0: self.rowObject.swap('before', currentRow, self);
Chris@0: }
Chris@0: if (self.striping === true) {
Chris@0: self.restripeTable();
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: if (self.indentEnabled) {
Chris@0: var xDiff = self.currentPointerCoords.x - self.dragObject.indentPointerPos.x;
Chris@0:
Chris@0: var indentDiff = Math.round(xDiff / self.indentAmount);
Chris@0:
Chris@0: var indentChange = self.rowObject.indent(indentDiff);
Chris@0:
Chris@0: self.dragObject.indentPointerPos.x += self.indentAmount * indentChange * self.rtl;
Chris@0: self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
Chris@0: }
Chris@0:
Chris@0: return false;
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.dropRow = function (event, self) {
Chris@0: var droppedRow = void 0;
Chris@0: var $droppedRow = void 0;
Chris@0:
Chris@0: if (self.rowObject !== null) {
Chris@0: droppedRow = self.rowObject.element;
Chris@0: $droppedRow = $(droppedRow);
Chris@0:
Chris@0: if (self.rowObject.changed === true) {
Chris@0: self.updateFields(droppedRow);
Chris@0:
Chris@14: Object.keys(self.tableSettings || {}).forEach(function (group) {
Chris@14: var rowSettings = self.rowSettings(group, droppedRow);
Chris@14: if (rowSettings.relationship === 'group') {
Chris@14: Object.keys(self.rowObject.children || {}).forEach(function (n) {
Chris@14: self.updateField(self.rowObject.children[n], group);
Chris@14: });
Chris@0: }
Chris@14: });
Chris@0:
Chris@0: self.rowObject.markChanged();
Chris@0: if (self.changed === false) {
Chris@0: $(Drupal.theme('tableDragChangedWarning')).insertBefore(self.table).hide().fadeIn('slow');
Chris@0: self.changed = true;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: if (self.indentEnabled) {
Chris@0: self.rowObject.removeIndentClasses();
Chris@0: }
Chris@0: if (self.oldRowElement) {
Chris@0: $(self.oldRowElement).removeClass('drag-previous');
Chris@0: }
Chris@0: $droppedRow.removeClass('drag').addClass('drag-previous');
Chris@0: self.oldRowElement = droppedRow;
Chris@0: self.onDrop();
Chris@0: self.rowObject = null;
Chris@0: }
Chris@0:
Chris@0: if (self.dragObject !== null) {
Chris@0: self.dragObject = null;
Chris@0: $('body').removeClass('drag');
Chris@0: clearInterval(self.scrollInterval);
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.pointerCoords = function (event) {
Chris@0: if (event.pageX || event.pageY) {
Chris@0: return { x: event.pageX, y: event.pageY };
Chris@0: }
Chris@0: return {
Chris@0: x: event.clientX + document.body.scrollLeft - document.body.clientLeft,
Chris@0: y: event.clientY + document.body.scrollTop - document.body.clientTop
Chris@0: };
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.getPointerOffset = function (target, event) {
Chris@0: var docPos = $(target).offset();
Chris@0: var pointerPos = this.pointerCoords(event);
Chris@0: return { x: pointerPos.x - docPos.left, y: pointerPos.y - docPos.top };
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) {
Chris@17: var _this3 = this;
Chris@17:
Chris@0: var rows = $(this.table.tBodies[0].rows).not(':hidden');
Chris@17:
Chris@17: var _loop = function _loop(n) {
Chris@0: var row = rows[n];
Chris@0: var $row = $(row);
Chris@0: var rowY = $row.offset().top;
Chris@14: var rowHeight = void 0;
Chris@0:
Chris@0: if (row.offsetHeight === 0) {
Chris@0: rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
Chris@0: } else {
Chris@0: rowHeight = parseInt(row.offsetHeight, 10) / 2;
Chris@0: }
Chris@0:
Chris@0: if (y > rowY - rowHeight && y < rowY + rowHeight) {
Chris@17: if (_this3.indentEnabled) {
Chris@17: if (Object.keys(_this3.rowObject.group).some(function (o) {
Chris@17: return _this3.rowObject.group[o] === row;
Chris@17: })) {
Chris@17: return {
Chris@17: v: null
Chris@17: };
Chris@0: }
Chris@17: } else if (row === _this3.rowObject.element) {
Chris@17: return {
Chris@17: v: null
Chris@17: };
Chris@0: }
Chris@0:
Chris@17: if (!_this3.rowObject.isValidSwap(row)) {
Chris@17: return {
Chris@17: v: null
Chris@17: };
Chris@0: }
Chris@0:
Chris@0: while ($row.is(':hidden') && $row.prev('tr').is(':hidden')) {
Chris@0: $row = $row.prev('tr:first-of-type');
Chris@0: row = $row.get(0);
Chris@0: }
Chris@17: return {
Chris@17: v: row
Chris@17: };
Chris@0: }
Chris@17: };
Chris@17:
Chris@17: for (var n = 0; n < rows.length; n++) {
Chris@17: var _ret = _loop(n);
Chris@17:
Chris@17: if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
Chris@0: }
Chris@0: return null;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.updateFields = function (changedRow) {
Chris@17: var _this4 = this;
Chris@14:
Chris@14: Object.keys(this.tableSettings || {}).forEach(function (group) {
Chris@17: _this4.updateField(changedRow, group);
Chris@14: });
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
Chris@0: var rowSettings = this.rowSettings(group, changedRow);
Chris@0: var $changedRow = $(changedRow);
Chris@0: var sourceRow = void 0;
Chris@0: var $previousRow = void 0;
Chris@0: var previousRow = void 0;
Chris@0: var useSibling = void 0;
Chris@0:
Chris@0: if (rowSettings.relationship === 'self' || rowSettings.relationship === 'group') {
Chris@0: sourceRow = changedRow;
Chris@0: } else if (rowSettings.relationship === 'sibling') {
Chris@0: $previousRow = $changedRow.prev('tr:first-of-type');
Chris@0: previousRow = $previousRow.get(0);
Chris@0: var $nextRow = $changedRow.next('tr:first-of-type');
Chris@0: var nextRow = $nextRow.get(0);
Chris@0: sourceRow = changedRow;
Chris@0: if ($previousRow.is('.draggable') && $previousRow.find('.' + group).length) {
Chris@0: if (this.indentEnabled) {
Chris@0: if ($previousRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
Chris@0: sourceRow = previousRow;
Chris@0: }
Chris@0: } else {
Chris@0: sourceRow = previousRow;
Chris@0: }
Chris@0: } else if ($nextRow.is('.draggable') && $nextRow.find('.' + group).length) {
Chris@0: if (this.indentEnabled) {
Chris@0: if ($nextRow.find('.js-indentations').length === $changedRow.find('.js-indentations').length) {
Chris@0: sourceRow = nextRow;
Chris@0: }
Chris@0: } else {
Chris@0: sourceRow = nextRow;
Chris@0: }
Chris@0: }
Chris@0: } else if (rowSettings.relationship === 'parent') {
Chris@0: $previousRow = $changedRow.prev('tr');
Chris@0: previousRow = $previousRow;
Chris@0: while ($previousRow.length && $previousRow.find('.js-indentation').length >= this.rowObject.indents) {
Chris@0: $previousRow = $previousRow.prev('tr');
Chris@0: previousRow = $previousRow;
Chris@0: }
Chris@0:
Chris@0: if ($previousRow.length) {
Chris@0: sourceRow = $previousRow.get(0);
Chris@0: } else {
Chris@0: sourceRow = $(this.table).find('tr.draggable:first-of-type').get(0);
Chris@0: if (sourceRow === this.rowObject.element) {
Chris@0: sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
Chris@0: }
Chris@0: useSibling = true;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: this.copyDragClasses(sourceRow, changedRow, group);
Chris@0: rowSettings = this.rowSettings(group, changedRow);
Chris@0:
Chris@0: if (useSibling) {
Chris@0: rowSettings.relationship = 'sibling';
Chris@0: rowSettings.source = rowSettings.target;
Chris@0: }
Chris@0:
Chris@0: var targetClass = '.' + rowSettings.target;
Chris@0: var targetElement = $changedRow.find(targetClass).get(0);
Chris@0:
Chris@0: if (targetElement) {
Chris@0: var sourceClass = '.' + rowSettings.source;
Chris@0: var sourceElement = $(sourceClass, sourceRow).get(0);
Chris@0: switch (rowSettings.action) {
Chris@0: case 'depth':
Chris@0: targetElement.value = $(sourceElement).closest('tr').find('.js-indentation').length;
Chris@0: break;
Chris@0:
Chris@0: case 'match':
Chris@0: targetElement.value = sourceElement.value;
Chris@0: break;
Chris@0:
Chris@0: case 'order':
Chris@14: {
Chris@14: var siblings = this.rowObject.findSiblings(rowSettings);
Chris@14: if ($(targetElement).is('select')) {
Chris@14: var values = [];
Chris@14: $(targetElement).find('option').each(function () {
Chris@14: values.push(this.value);
Chris@14: });
Chris@14: var maxVal = values[values.length - 1];
Chris@0:
Chris@14: $(siblings).find(targetClass).each(function () {
Chris@14: if (values.length > 0) {
Chris@14: this.value = values.shift();
Chris@14: } else {
Chris@14: this.value = maxVal;
Chris@14: }
Chris@14: });
Chris@14: } else {
Chris@14: var weight = parseInt($(siblings[0]).find(targetClass).val(), 10) || 0;
Chris@14: $(siblings).find(targetClass).each(function () {
Chris@14: this.value = weight;
Chris@14: weight++;
Chris@14: });
Chris@14: }
Chris@14: break;
Chris@0: }
Chris@0: }
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
Chris@0: var sourceElement = $(sourceRow).find('.' + group);
Chris@0: var targetElement = $(targetRow).find('.' + group);
Chris@0: if (sourceElement.length && targetElement.length) {
Chris@0: targetElement[0].className = sourceElement[0].className;
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
Chris@0: var de = document.documentElement;
Chris@0: var b = document.body;
Chris@0:
Chris@14: var windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth !== 0 ? de.clientHeight : b.offsetHeight);
Chris@14: this.windowHeight = windowHeight;
Chris@0: var scrollY = void 0;
Chris@0: if (document.all) {
Chris@14: scrollY = !de.scrollTop ? b.scrollTop : de.scrollTop;
Chris@0: } else {
Chris@14: scrollY = window.pageYOffset ? window.pageYOffset : window.scrollY;
Chris@0: }
Chris@14: this.scrollY = scrollY;
Chris@0: var trigger = this.scrollSettings.trigger;
Chris@0: var delta = 0;
Chris@0:
Chris@0: if (cursorY - scrollY > windowHeight - trigger) {
Chris@0: delta = trigger / (windowHeight + scrollY - cursorY);
Chris@0: delta = delta > 0 && delta < trigger ? delta : trigger;
Chris@0: return delta * this.scrollSettings.amount;
Chris@17: }
Chris@17: if (cursorY - scrollY < trigger) {
Chris@0: delta = trigger / (cursorY - scrollY);
Chris@0: delta = delta > 0 && delta < trigger ? delta : trigger;
Chris@0: return -delta * this.scrollSettings.amount;
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.setScroll = function (scrollAmount) {
Chris@0: var self = this;
Chris@0:
Chris@0: this.scrollInterval = setInterval(function () {
Chris@0: self.checkScroll(self.currentPointerCoords.y);
Chris@0: var aboveTable = self.scrollY > self.table.topY;
Chris@0: var belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
Chris@0: if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) {
Chris@0: window.scrollBy(0, scrollAmount);
Chris@0: }
Chris@0: }, this.scrollSettings.interval);
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.restripeTable = function () {
Chris@0: $(this.table).find('> tbody > tr.draggable, > tr.draggable').filter(':visible').filter(':odd').removeClass('odd').addClass('even').end().filter(':even').removeClass('even').addClass('odd');
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.onDrag = function () {
Chris@0: return null;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.onDrop = function () {
Chris@0: return null;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) {
Chris@0: var $tableRow = $(tableRow);
Chris@0:
Chris@0: this.element = tableRow;
Chris@0: this.method = method;
Chris@0: this.group = [tableRow];
Chris@0: this.groupDepth = $tableRow.find('.js-indentation').length;
Chris@0: this.changed = false;
Chris@0: this.table = $tableRow.closest('table')[0];
Chris@0: this.indentEnabled = indentEnabled;
Chris@0: this.maxDepth = maxDepth;
Chris@0:
Chris@0: this.direction = '';
Chris@0: if (this.indentEnabled) {
Chris@0: this.indents = $tableRow.find('.js-indentation').length;
Chris@0: this.children = this.findChildren(addClasses);
Chris@0: this.group = $.merge(this.group, this.children);
Chris@0:
Chris@0: for (var n = 0; n < this.group.length; n++) {
Chris@0: this.groupDepth = Math.max($(this.group[n]).find('.js-indentation').length, this.groupDepth);
Chris@0: }
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
Chris@0: var parentIndentation = this.indents;
Chris@0: var currentRow = $(this.element, this.table).next('tr.draggable');
Chris@0: var rows = [];
Chris@0: var child = 0;
Chris@0:
Chris@0: function rowIndentation(indentNum, el) {
Chris@0: var self = $(el);
Chris@0: if (child === 1 && indentNum === parentIndentation) {
Chris@0: self.addClass('tree-child-first');
Chris@0: }
Chris@0: if (indentNum === parentIndentation) {
Chris@0: self.addClass('tree-child');
Chris@0: } else if (indentNum > parentIndentation) {
Chris@0: self.addClass('tree-child-horizontal');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: while (currentRow.length) {
Chris@0: if (currentRow.find('.js-indentation').length > parentIndentation) {
Chris@0: child++;
Chris@0: rows.push(currentRow[0]);
Chris@0: if (addClasses) {
Chris@0: currentRow.find('.js-indentation').each(rowIndentation);
Chris@0: }
Chris@0: } else {
Chris@0: break;
Chris@0: }
Chris@0: currentRow = currentRow.next('tr.draggable');
Chris@0: }
Chris@0: if (addClasses && rows.length) {
Chris@0: $(rows[rows.length - 1]).find('.js-indentation:nth-child(' + (parentIndentation + 1) + ')').addClass('tree-child-last');
Chris@0: }
Chris@0: return rows;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
Chris@0: var $row = $(row);
Chris@0: if (this.indentEnabled) {
Chris@0: var prevRow = void 0;
Chris@0: var nextRow = void 0;
Chris@0: if (this.direction === 'down') {
Chris@0: prevRow = row;
Chris@0: nextRow = $row.next('tr').get(0);
Chris@0: } else {
Chris@0: prevRow = $row.prev('tr').get(0);
Chris@0: nextRow = row;
Chris@0: }
Chris@0: this.interval = this.validIndentInterval(prevRow, nextRow);
Chris@0:
Chris@0: if (this.interval.min > this.interval.max) {
Chris@0: return false;
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: if (this.table.tBodies[0].rows[0] === row && $row.is(':not(.draggable)')) {
Chris@0: return false;
Chris@0: }
Chris@0:
Chris@0: return true;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) {
Chris@0: this.group.forEach(function (row) {
Chris@0: Drupal.detachBehaviors(row, drupalSettings, 'move');
Chris@0: });
Chris@0: $(row)[position](this.group);
Chris@0:
Chris@0: this.group.forEach(function (row) {
Chris@0: Drupal.attachBehaviors(row, drupalSettings);
Chris@0: });
Chris@0: this.changed = true;
Chris@0: this.onSwap(row);
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
Chris@0: var $prevRow = $(prevRow);
Chris@0: var maxIndent = void 0;
Chris@0:
Chris@14: var minIndent = nextRow ? $(nextRow).find('.js-indentation').length : 0;
Chris@0:
Chris@0: if (!prevRow || $prevRow.is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
Chris@0: maxIndent = 0;
Chris@0: } else {
Chris@0: maxIndent = $prevRow.find('.js-indentation').length + ($prevRow.is('.tabledrag-leaf') ? 0 : 1);
Chris@0:
Chris@0: if (this.maxDepth) {
Chris@0: maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: return { min: minIndent, max: maxIndent };
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) {
Chris@0: var $group = $(this.group);
Chris@0:
Chris@0: if (!this.interval) {
Chris@0: var prevRow = $(this.element).prev('tr').get(0);
Chris@0: var nextRow = $group.eq(-1).next('tr').get(0);
Chris@0: this.interval = this.validIndentInterval(prevRow, nextRow);
Chris@0: }
Chris@0:
Chris@0: var indent = this.indents + indentDiff;
Chris@0: indent = Math.max(indent, this.interval.min);
Chris@0: indent = Math.min(indent, this.interval.max);
Chris@0: indentDiff = indent - this.indents;
Chris@0:
Chris@0: for (var n = 1; n <= Math.abs(indentDiff); n++) {
Chris@0: if (indentDiff < 0) {
Chris@0: $group.find('.js-indentation:first-of-type').remove();
Chris@0: this.indents--;
Chris@0: } else {
Chris@0: $group.find('td:first-of-type').prepend(Drupal.theme('tableDragIndentation'));
Chris@0: this.indents++;
Chris@0: }
Chris@0: }
Chris@0: if (indentDiff) {
Chris@0: this.changed = true;
Chris@0: this.groupDepth += indentDiff;
Chris@0: this.onIndent();
Chris@0: }
Chris@0:
Chris@0: return indentDiff;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) {
Chris@0: var siblings = [];
Chris@0: var directions = ['prev', 'next'];
Chris@0: var rowIndentation = this.indents;
Chris@0: var checkRowIndentation = void 0;
Chris@0: for (var d = 0; d < directions.length; d++) {
Chris@0: var checkRow = $(this.element)[directions[d]]();
Chris@0: while (checkRow.length) {
Chris@0: if (checkRow.find('.' + rowSettings.target)) {
Chris@0: if (this.indentEnabled) {
Chris@0: checkRowIndentation = checkRow.find('.js-indentation').length;
Chris@0: }
Chris@0:
Chris@0: if (!this.indentEnabled || checkRowIndentation === rowIndentation) {
Chris@0: siblings.push(checkRow[0]);
Chris@0: } else if (checkRowIndentation < rowIndentation) {
Chris@0: break;
Chris@0: }
Chris@0: } else {
Chris@0: break;
Chris@0: }
Chris@0: checkRow = checkRow[directions[d]]();
Chris@0: }
Chris@0:
Chris@0: if (directions[d] === 'prev') {
Chris@0: siblings.reverse();
Chris@0: siblings.push(this.element);
Chris@0: }
Chris@0: }
Chris@0: return siblings;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () {
Chris@17: var _this5 = this;
Chris@14:
Chris@14: Object.keys(this.children || {}).forEach(function (n) {
Chris@17: $(_this5.children[n]).find('.js-indentation').removeClass('tree-child').removeClass('tree-child-first').removeClass('tree-child-last').removeClass('tree-child-horizontal');
Chris@14: });
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
Chris@0: var marker = Drupal.theme('tableDragChangedMarker');
Chris@0: var cell = $(this.element).find('td:first-of-type');
Chris@0: if (cell.find('abbr.tabledrag-changed').length === 0) {
Chris@0: cell.append(marker);
Chris@0: }
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
Chris@0: return null;
Chris@0: };
Chris@0:
Chris@0: Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
Chris@0: return null;
Chris@0: };
Chris@0:
Chris@0: $.extend(Drupal.theme, {
Chris@0: tableDragChangedMarker: function tableDragChangedMarker() {
Chris@0: return '*';
Chris@0: },
Chris@0: tableDragIndentation: function tableDragIndentation() {
Chris@0: return '
';
Chris@0: },
Chris@0: tableDragChangedWarning: function tableDragChangedWarning() {
Chris@0: return '' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('You have unsaved changes.') + '
';
Chris@0: }
Chris@0: });
Chris@0: })(jQuery, Drupal, drupalSettings);