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