123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176 |
- (function ($) {
- Drupal.behaviors.tableDrag = {
- attach: function (context, settings) {
- for (var base in settings.tableDrag) {
- $('#' + base, context).once('tabledrag', function () {
-
-
- Drupal.tableDrag[base] = new Drupal.tableDrag(this, settings.tableDrag[base]);
- });
- }
- }
- };
- Drupal.tableDrag = function (table, tableSettings) {
- var self = this;
-
- 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.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
- this.scrollInterval = null;
- this.scrollY = 0;
- this.windowHeight = 0;
-
-
-
- this.indentEnabled = false;
- for (var group in tableSettings) {
- for (var n in tableSettings[group]) {
- 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;
-
-
-
-
- var indent = Drupal.theme('tableDragIndentation');
- var testRow = $('<tr/>').addClass('draggable').appendTo(table);
- var testCell = $('<td/>').appendTo(testRow).prepend(indent).prepend(indent);
- this.indentAmount = $('.indentation', testCell).get(1).offsetLeft - $('.indentation', testCell).get(0).offsetLeft;
- testRow.remove();
- }
-
-
- $('> tr.draggable, > tbody > tr.draggable', table).each(function () { self.makeDraggable(this); });
-
- $(table).before($('<a href="#" class="tabledrag-toggle-weight"></a>')
- .attr('title', Drupal.t('Re-order rows by numerical weight instead of dragging.'))
- .click(function () {
- if ($.cookie('Drupal.tableDrag.showWeight') == 1) {
- self.hideColumns();
- }
- else {
- self.showColumns();
- }
- return false;
- })
- .wrap('<div class="tabledrag-toggle-weight-wrapper"></div>')
- .parent()
- );
-
-
-
-
- self.initColumns();
-
-
- $(document).bind('mousemove', function (event) { return self.dragRow(event, self); });
- $(document).bind('mouseup', function (event) { return self.dropRow(event, self); });
- };
- Drupal.tableDrag.prototype.initColumns = function () {
- for (var group in this.tableSettings) {
-
- for (var d in this.tableSettings[group]) {
- var field = $('.' + this.tableSettings[group][d].target + ':first', this.table);
- if (field.length && this.tableSettings[group][d].hidden) {
- var hidden = this.tableSettings[group][d].hidden;
- var cell = field.closest('td');
- break;
- }
- }
-
- if (hidden && cell[0]) {
-
-
- var columnIndex = $('> td', cell.parent()).index(cell.get(0)) + 1;
- $('> thead > tr, > tbody > tr, > tr', this.table).each(function () {
-
- var index = columnIndex;
- var cells = $(this).children();
- 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');
- }
- }
- });
- }
- }
-
-
- if ($.cookie('Drupal.tableDrag.showWeight') === null) {
- $.cookie('Drupal.tableDrag.showWeight', 0, {
- path: Drupal.settings.basePath,
-
- expires: 365
- });
- this.hideColumns();
- }
-
- else {
- if ($.cookie('Drupal.tableDrag.showWeight') == 1) {
- this.showColumns();
- }
- else {
- this.hideColumns();
- }
- }
- };
- Drupal.tableDrag.prototype.hideColumns = function () {
-
- $('.tabledrag-hide', 'table.tabledrag-processed').css('display', 'none');
-
- $('.tabledrag-handle', 'table.tabledrag-processed').css('display', '');
-
- $('.tabledrag-has-colspan', 'table.tabledrag-processed').each(function () {
- this.colSpan = this.colSpan - 1;
- });
-
- $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights'));
-
- $.cookie('Drupal.tableDrag.showWeight', 0, {
- path: Drupal.settings.basePath,
-
- expires: 365
- });
-
- $('table.tabledrag-processed').trigger('columnschange', 'hide');
- };
- Drupal.tableDrag.prototype.showColumns = function () {
-
- $('.tabledrag-hide', 'table.tabledrag-processed').css('display', '');
-
- $('.tabledrag-handle', 'table.tabledrag-processed').css('display', 'none');
-
- $('.tabledrag-has-colspan', 'table.tabledrag-processed').each(function () {
- this.colSpan = this.colSpan + 1;
- });
-
- $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights'));
-
- $.cookie('Drupal.tableDrag.showWeight', 1, {
- path: Drupal.settings.basePath,
-
- expires: 365
- });
-
- $('table.tabledrag-processed').trigger('columnschange', 'show');
- };
- Drupal.tableDrag.prototype.rowSettings = function (group, row) {
- var field = $('.' + group, row);
- for (var delta in this.tableSettings[group]) {
- var targetClass = this.tableSettings[group][delta].target;
- if (field.is('.' + targetClass)) {
-
- var rowSettings = {};
- for (var n in this.tableSettings[group][delta]) {
- rowSettings[n] = this.tableSettings[group][delta][n];
- }
- return rowSettings;
- }
- }
- };
- Drupal.tableDrag.prototype.makeDraggable = function (item) {
- var self = this;
-
- var handle = $('<a href="#" class="tabledrag-handle"><div class="handle"> </div></a>').attr('title', Drupal.t('Drag to re-order'));
-
- if ($('td:first .indentation:last', item).length) {
- $('td:first .indentation:last', item).after(handle);
-
- self.indentCount = Math.max($('.indentation', item).length, self.indentCount);
- }
- else {
- $('td:first', item).prepend(handle);
- }
-
- handle.hover(function () {
- self.dragObject == null ? $(this).addClass('tabledrag-handle-hover') : null;
- }, function () {
- self.dragObject == null ? $(this).removeClass('tabledrag-handle-hover') : null;
- });
-
- handle.mousedown(function (event) {
-
- self.dragObject = {};
- self.dragObject.initMouseOffset = self.getMouseOffset(item, event);
- self.dragObject.initMouseCoords = self.mouseCoords(event);
- if (self.indentEnabled) {
- self.dragObject.indentMousePos = self.dragObject.initMouseCoords;
- }
-
- if (self.rowObject) {
- $('a.tabledrag-handle', self.rowObject.element).blur();
- }
-
- self.rowObject = new self.row(item, 'mouse', self.indentEnabled, self.maxDepth, true);
-
- self.table.topY = $(self.table).offset().top;
- self.table.bottomY = self.table.topY + self.table.offsetHeight;
-
- $(this).addClass('tabledrag-handle-hover');
- $(item).addClass('drag');
-
- $('body').addClass('drag');
- if (self.oldRowElement) {
- $(self.oldRowElement).removeClass('drag-previous');
- }
-
- if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
- $('select', this.table).css('display', 'none');
- }
-
-
- self.safeBlur = false;
-
- self.onDrag();
- return false;
- });
-
- handle.click(function () {
- return false;
- });
-
- handle.focus(function () {
- $(this).addClass('tabledrag-handle-hover');
- self.safeBlur = true;
- });
-
- handle.blur(function (event) {
- $(this).removeClass('tabledrag-handle-hover');
- if (self.rowObject && self.safeBlur) {
- self.dropRow(event, self);
- }
- });
-
- handle.keydown(function (event) {
-
- if (event.keyCode != 9 && !self.rowObject) {
- self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
- }
- var keyChange = false;
- switch (event.keyCode) {
- case 37:
- case 63234:
- keyChange = true;
- self.rowObject.indent(-1 * self.rtl);
- break;
- case 38:
- case 63232:
- var previousRow = $(self.rowObject.element).prev('tr').get(0);
- while (previousRow && $(previousRow).is(':hidden')) {
- previousRow = $(previousRow).prev('tr').get(0);
- }
- if (previousRow) {
- self.safeBlur = false;
- self.rowObject.direction = 'up';
- keyChange = true;
- if ($(item).is('.tabledrag-root')) {
-
- var groupHeight = 0;
- while (previousRow && $('.indentation', previousRow).length) {
- previousRow = $(previousRow).prev('tr').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.get(0).focus();
- }
- break;
- case 39:
- case 63235:
- keyChange = true;
- self.rowObject.indent(1 * self.rtl);
- break;
- case 40:
- case 63233:
- var nextRow = $(self.rowObject.group).filter(':last').next('tr').get(0);
- while (nextRow && $(nextRow).is(':hidden')) {
- nextRow = $(nextRow).next('tr').get(0);
- }
- if (nextRow) {
- self.safeBlur = false;
- self.rowObject.direction = 'down';
- keyChange = true;
- if ($(item).is('.tabledrag-root')) {
-
- var groupHeight = 0;
- var nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
- if (nextGroup) {
- $(nextGroup.group).each(function () {
- groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight;
- });
- var nextGroupRow = $(nextGroup.group).filter(':last').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.get(0).focus();
- }
- break;
- }
- if (self.rowObject && self.rowObject.changed == true) {
- $(item).addClass('drag');
- if (self.oldRowElement) {
- $(self.oldRowElement).removeClass('drag-previous');
- }
- self.oldRowElement = item;
- self.restripeTable();
- self.onDrag();
- }
-
- if (keyChange) {
- return false;
- }
- });
-
-
-
- handle.keypress(function (event) {
- switch (event.keyCode) {
- case 37:
- case 38:
- case 39:
- case 40:
- return false;
- }
- });
- };
- Drupal.tableDrag.prototype.dragRow = function (event, self) {
- if (self.dragObject) {
- self.currentMouseCoords = self.mouseCoords(event);
- var y = self.currentMouseCoords.y - self.dragObject.initMouseOffset.y;
- var x = self.currentMouseCoords.x - self.dragObject.initMouseOffset.x;
-
- if (y != self.oldY) {
- self.rowObject.direction = y > self.oldY ? 'down' : 'up';
- self.oldY = y;
-
- var scrollAmount = self.checkScroll(self.currentMouseCoords.y);
-
- clearInterval(self.scrollInterval);
-
- if (scrollAmount > 0 && self.rowObject.direction == 'down' || scrollAmount < 0 && self.rowObject.direction == 'up') {
- self.setScroll(scrollAmount);
- }
-
- var 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);
- }
- self.restripeTable();
- }
- }
-
- if (self.indentEnabled) {
- var xDiff = self.currentMouseCoords.x - self.dragObject.indentMousePos.x;
-
- var indentDiff = Math.round(xDiff / self.indentAmount * self.rtl);
-
-
- var indentChange = self.rowObject.indent(indentDiff);
-
- self.dragObject.indentMousePos.x += self.indentAmount * indentChange * self.rtl;
- self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
- }
- return false;
- }
- };
- Drupal.tableDrag.prototype.dropRow = function (event, self) {
-
- if (self.rowObject != null) {
- var droppedRow = self.rowObject.element;
-
- if (self.rowObject.changed == true) {
-
- self.updateFields(droppedRow);
-
-
- for (var group in self.tableSettings) {
- var rowSettings = self.rowSettings(group, droppedRow);
- if (rowSettings.relationship == 'group') {
- for (var n in self.rowObject.children) {
- 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) {
- $('.tabledrag-handle', droppedRow).removeClass('tabledrag-handle-hover');
- self.dragObject = null;
- $('body').removeClass('drag');
- clearInterval(self.scrollInterval);
-
- if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
- $('select', this.table).css('display', 'block');
- }
- }
- };
- Drupal.tableDrag.prototype.mouseCoords = 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.getMouseOffset = function (target, event) {
- var docPos = $(target).offset();
- var mousePos = this.mouseCoords(event);
- return { x: mousePos.x - docPos.left, y: mousePos.y - docPos.top };
- };
- Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) {
- var rows = $(this.table.tBodies[0].rows).not(':hidden');
- for (var n = 0; n < rows.length; n++) {
- var row = rows[n];
- var indentDiff = 0;
- var rowY = $(row).offset().top;
-
-
-
- if (row.offsetHeight == 0) {
- var rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2;
- }
-
- else {
- var rowHeight = parseInt(row.offsetHeight, 10) / 2;
- }
-
- if ((y > (rowY - rowHeight)) && (y < (rowY + rowHeight))) {
- if (this.indentEnabled) {
-
- for (var 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').get(0);
- }
- return row;
- }
- }
- return null;
- };
- Drupal.tableDrag.prototype.updateFields = function (changedRow) {
- for (var group in this.tableSettings) {
-
-
- this.updateField(changedRow, group);
- }
- };
- Drupal.tableDrag.prototype.updateField = function (changedRow, group) {
- var rowSettings = this.rowSettings(group, changedRow);
-
- if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') {
- var sourceRow = changedRow;
- }
-
- else if (rowSettings.relationship == 'sibling') {
- var previousRow = $(changedRow).prev('tr').get(0);
- var nextRow = $(changedRow).next('tr').get(0);
- var sourceRow = changedRow;
- if ($(previousRow).is('.draggable') && $('.' + group, previousRow).length) {
- if (this.indentEnabled) {
- if ($('.indentations', previousRow).length == $('.indentations', changedRow)) {
- sourceRow = previousRow;
- }
- }
- else {
- sourceRow = previousRow;
- }
- }
- else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) {
- if (this.indentEnabled) {
- if ($('.indentations', nextRow).length == $('.indentations', changedRow)) {
- sourceRow = nextRow;
- }
- }
- else {
- sourceRow = nextRow;
- }
- }
- }
-
-
- else if (rowSettings.relationship == 'parent') {
- var previousRow = $(changedRow).prev('tr');
- while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) {
- previousRow = previousRow.prev('tr');
- }
-
- if (previousRow.length) {
- sourceRow = previousRow[0];
- }
-
-
- else {
-
-
-
- sourceRow = $(this.table).find('tr.draggable:first').get(0);
- if (sourceRow == this.rowObject.element) {
- sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
- }
- var useSibling = true;
- }
- }
-
-
- this.copyDragClasses(sourceRow, changedRow, group);
- rowSettings = this.rowSettings(group, changedRow);
-
-
- if (useSibling) {
- rowSettings.relationship = 'sibling';
- rowSettings.source = rowSettings.target;
- }
- var targetClass = '.' + rowSettings.target;
- var targetElement = $(targetClass, changedRow).get(0);
-
- if (targetElement) {
- var sourceClass = '.' + rowSettings.source;
- var sourceElement = $(sourceClass, sourceRow).get(0);
- switch (rowSettings.action) {
- case 'depth':
-
- targetElement.value = $('.indentation', $(sourceElement).closest('tr')).length;
- break;
- case 'match':
-
- targetElement.value = sourceElement.value;
- break;
- case 'order':
- var siblings = this.rowObject.findSiblings(rowSettings);
- if ($(targetElement).is('select')) {
-
- var values = [];
- $('option', targetElement).each(function () {
- values.push(this.value);
- });
- var maxVal = values[values.length - 1];
-
- $(targetClass, siblings).each(function () {
-
- if (values.length > 0) {
- this.value = values.shift();
- }
- else {
- this.value = maxVal;
- }
- });
- }
- else {
-
- var weight = parseInt($(targetClass, siblings[0]).val(), 10) || 0;
- $(targetClass, siblings).each(function () {
- this.value = weight;
- weight++;
- });
- }
- break;
- }
- }
- };
- Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) {
- var sourceElement = $('.' + group, sourceRow);
- var targetElement = $('.' + group, targetRow);
- if (sourceElement.length && targetElement.length) {
- targetElement[0].className = sourceElement[0].className;
- }
- };
- Drupal.tableDrag.prototype.checkScroll = function (cursorY) {
- var de = document.documentElement;
- var b = document.body;
- var windowHeight = this.windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth != 0 ? de.clientHeight : b.offsetHeight);
- var scrollY = this.scrollY = (document.all ? (!de.scrollTop ? b.scrollTop : de.scrollTop) : (window.pageYOffset ? window.pageYOffset : window.scrollY));
- var trigger = this.scrollSettings.trigger;
- var 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) {
- var self = this;
- this.scrollInterval = setInterval(function () {
-
- self.checkScroll(self.currentMouseCoords.y);
- var aboveTable = self.scrollY > self.table.topY;
- var 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 () {
-
-
-
- $('> tbody > tr.draggable:visible, > tr.draggable:visible', this.table)
- .removeClass('odd even')
- .filter(':odd').addClass('even').end()
- .filter(':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) {
- this.element = tableRow;
- this.method = method;
- this.group = [tableRow];
- this.groupDepth = $('.indentation', tableRow).length;
- this.changed = false;
- this.table = $(tableRow).closest('table').get(0);
- this.indentEnabled = indentEnabled;
- this.maxDepth = maxDepth;
- this.direction = '';
- if (this.indentEnabled) {
- this.indents = $('.indentation', tableRow).length;
- this.children = this.findChildren(addClasses);
- this.group = $.merge(this.group, this.children);
-
- for (var n = 0; n < this.group.length; n++) {
- this.groupDepth = Math.max($('.indentation', this.group[n]).length, this.groupDepth);
- }
- }
- };
- Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) {
- var parentIndentation = this.indents;
- var currentRow = $(this.element, this.table).next('tr.draggable');
- var rows = [];
- var child = 0;
- while (currentRow.length) {
- var rowIndentation = $('.indentation', currentRow).length;
-
- if (rowIndentation > parentIndentation) {
- child++;
- rows.push(currentRow[0]);
- if (addClasses) {
- $('.indentation', currentRow).each(function (indentNum) {
- if (child == 1 && (indentNum == parentIndentation)) {
- $(this).addClass('tree-child-first');
- }
- if (indentNum == parentIndentation) {
- $(this).addClass('tree-child');
- }
- else if (indentNum > parentIndentation) {
- $(this).addClass('tree-child-horizontal');
- }
- });
- }
- }
- else {
- break;
- }
- currentRow = currentRow.next('tr.draggable');
- }
- if (addClasses && rows.length) {
- $('.indentation:nth-child(' + (parentIndentation + 1) + ')', rows[rows.length - 1]).addClass('tree-child-last');
- }
- return rows;
- };
- Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) {
- if (this.indentEnabled) {
- var prevRow, 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) {
- Drupal.detachBehaviors(this.group, Drupal.settings, 'move');
- $(row)[position](this.group);
- Drupal.attachBehaviors(this.group, Drupal.settings);
- this.changed = true;
- this.onSwap(row);
- };
- Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
- var minIndent, maxIndent;
-
-
- minIndent = nextRow ? $('.indentation', nextRow).length : 0;
-
- if (!prevRow || $(prevRow).is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) {
-
-
-
-
- maxIndent = 0;
- }
- else {
-
- maxIndent = $('.indentation', prevRow).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) {
-
- if (!this.interval) {
- var prevRow = $(this.element).prev('tr').get(0);
- var nextRow = $(this.group).filter(':last').next('tr').get(0);
- this.interval = this.validIndentInterval(prevRow, nextRow);
- }
-
- var indent = this.indents + indentDiff;
- indent = Math.max(indent, this.interval.min);
- indent = Math.min(indent, this.interval.max);
- indentDiff = indent - this.indents;
- for (var n = 1; n <= Math.abs(indentDiff); n++) {
-
- if (indentDiff < 0) {
- $('.indentation:first', this.group).remove();
- this.indents--;
- }
- else {
- $('td:first', this.group).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) {
- var siblings = [];
- var directions = ['prev', 'next'];
- var rowIndentation = this.indents;
- for (var d = 0; d < directions.length; d++) {
- var checkRow = $(this.element)[directions[d]]();
- while (checkRow.length) {
-
- if ($('.' + rowSettings.target, checkRow)) {
-
-
- if (this.indentEnabled) {
- var checkRowIndentation = $('.indentation', checkRow).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 () {
- for (var n in this.children) {
- $('.indentation', this.children[n])
- .removeClass('tree-child')
- .removeClass('tree-child-first')
- .removeClass('tree-child-last')
- .removeClass('tree-child-horizontal');
- }
- };
- Drupal.tableDrag.prototype.row.prototype.markChanged = function () {
- var marker = Drupal.theme('tableDragChangedMarker');
- var cell = $('td:first', this.element);
- if ($('span.tabledrag-changed', cell).length == 0) {
- cell.append(marker);
- }
- };
- Drupal.tableDrag.prototype.row.prototype.onIndent = function () {
- return null;
- };
- Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) {
- return null;
- };
- Drupal.theme.prototype.tableDragChangedMarker = function () {
- return '<span class="warning tabledrag-changed">*</span>';
- };
- Drupal.theme.prototype.tableDragIndentation = function () {
- return '<div class="indentation"> </div>';
- };
- Drupal.theme.prototype.tableDragChangedWarning = function () {
- return '<div class="tabledrag-changed-warning messages warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('Changes made in this table will not be saved until the form is submitted.') + '</div>';
- };
- })(jQuery);
|