diff options
author | Gábor Hojtsy <gabor@hojtsy.hu> | 2007-11-26 19:46:52 +0000 |
---|---|---|
committer | Gábor Hojtsy <gabor@hojtsy.hu> | 2007-11-26 19:46:52 +0000 |
commit | e6a88be5d127f41837137a0d00980c8d8115ab1e (patch) | |
tree | a32f57bcd84cc290d0203c850d5b1c5ae676a3a4 /misc/tabledrag.js | |
parent | 9a96837b89a0f6b2b5b2c99063e5faeda53d88c6 (diff) | |
download | brdo-e6a88be5d127f41837137a0d00980c8d8115ab1e.tar.gz brdo-e6a88be5d127f41837137a0d00980c8d8115ab1e.tar.bz2 |
#193333 by quicksketch et al: taxonomy drag and drop support
Diffstat (limited to 'misc/tabledrag.js')
-rw-r--r-- | misc/tabledrag.js | 216 |
1 files changed, 121 insertions, 95 deletions
diff --git a/misc/tabledrag.js b/misc/tabledrag.js index 1d80b2020..797113332 100644 --- a/misc/tabledrag.js +++ b/misc/tabledrag.js @@ -413,7 +413,20 @@ Drupal.tableDrag.prototype.dropRow = function(event) { var droppedRow = self.rowObject.element; // The row is already in the right place so we just release it. if (self.rowObject.changed == true) { + // Update the fields in the dropped row. self.updateFields(droppedRow); + + // If a setting exists for affecting the entire group, update all the + // fields in the entire dragged group. + for (var group in self.tableSettings) { + var rowSettings = self.rowSettings(group, droppedRow); + if (rowSettings.relationship == 'group') { + for (n in self.rowObject.children) { + self.updateField(self.rowObject.children[n], group); + } + } + } + self.rowObject.markChanged(); if (self.changed == false) { $(Drupal.theme('tableDragChangedWarning')).insertAfter(self.table).hide().fadeIn('slow'); @@ -562,114 +575,127 @@ Drupal.tableDrag.prototype.updateFields = function(changedRow) { for (var group in this.tableSettings) { // Each group may have a different setting for relationship, so we find // the source rows for each seperately. - var rowSettings = this.rowSettings(group, changedRow); + this.updateField(changedRow, group); + } +} - // Set the row as it's own target. - if (rowSettings.relationship == 'self') { - var sourceRow = changedRow; - } - // Siblings are easy, check previous and next rows. - 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).size() == $('.indentations', changedRow)) { - sourceRow = previousRow; - } - } - else { +/** + * After the row is dropped, update a single table field according to specific + * settings. + * + * @param changedRow + * DOM object for the row that was just dropped. + * @param group + * The settings group on which field updates will occur. + */ +Drupal.tableDrag.prototype.updateField = function(changedRow, group) { + var rowSettings = this.rowSettings(group, changedRow); + + // Set the row as it's own target. + if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') { + var sourceRow = changedRow; + } + // Siblings are easy, check previous and next rows. + 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).size() == $('.indentations', changedRow)) { sourceRow = previousRow; } } - else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) { - if (this.indentEnabled) { - if ($('.indentations', nextRow).size() == $('.indentations', changedRow)) { - sourceRow = nextRow; - } - } - else { - sourceRow = nextRow; - } + else { + sourceRow = previousRow; } } - // Parents, look up the tree until we find a field not in this group. - // Go up as many parents as indentations in the changed row. - else if (rowSettings.relationship == 'parent') { - var previousRow = $(changedRow).prev('tr'); - while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) { - previousRow = previousRow.prev('tr'); - } - // If we found a row. - if (previousRow.length) { - sourceRow = previousRow[0]; + else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) { + if (this.indentEnabled) { + if ($('.indentations', nextRow).size() == $('.indentations', changedRow)) { + sourceRow = nextRow; + } } - // Otherwise we went all the way to the left of the table without finding - // a parent, meaning this item has been placed at the root level. else { - // Use the first row in the table as source, because it's garanteed to - // be at the root level. Find the first item, then compare this row - // against it as a sibling. - sourceRow = $('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; + sourceRow = nextRow; } } + } + // Parents, look up the tree until we find a field not in this group. + // Go up as many parents as indentations in the changed row. + else if (rowSettings.relationship == 'parent') { + var previousRow = $(changedRow).prev('tr'); + while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) { + previousRow = previousRow.prev('tr'); + } + // If we found a row. + if (previousRow.length) { + sourceRow = previousRow[0]; + } + // Otherwise we went all the way to the left of the table without finding + // a parent, meaning this item has been placed at the root level. + else { + // Use the first row in the table as source, because it's garanteed to + // be at the root level. Find the first item, then compare this row + // against it as a sibling. + sourceRow = $('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; + } + } - // Because we may have moved the row from one category to another, - // take a look at our sibling and borrow its sources and targets. - this.copyDragClasses(sourceRow, changedRow, group); - rowSettings = this.rowSettings(group, changedRow); + // Because we may have moved the row from one category to another, + // take a look at our sibling and borrow its sources and targets. + this.copyDragClasses(sourceRow, changedRow, group); + rowSettings = this.rowSettings(group, changedRow); - // In the case that we're looking for a parent, but the row is at the top - // of the tree, copy our sibling's values. - if (useSibling) { - rowSettings.relationship = 'sibling'; - rowSettings.source = rowSettings.target; - } + // In the case that we're looking for a parent, but the row is at the top + // of the tree, copy our sibling's values. + if (useSibling) { + rowSettings.relationship = 'sibling'; + rowSettings.source = rowSettings.target; + } - var targetClass = '.' + rowSettings.target; - var targetElement = $(targetClass, changedRow).get(0); - - // Check if a target element exists in this row. - if (targetElement) { - var sourceClass = '.' + rowSettings.source; - var sourceElement = $(sourceClass, sourceRow).get(0); - switch (rowSettings.action) { - case 'depth': - // Get the depth of the target row. - targetElement.value = $('.indentation', $(sourceElement).parents('tr:first')).size(); - break; - case 'match': - // Update the value. - targetElement.value = sourceElement.value; - break; - case 'order': - var siblings = this.rowObject.findSiblings(rowSettings); - if ($(targetElement).is('select')) { - // Get a list of acceptable values. - var values = new Array(); - $('option', targetElement).each(function() { - values.push(this.value); - }); - // Populate the values in the siblings. - $(targetClass, siblings).each(function() { - this.value = values.shift(); - }); - } - else { - // Assume a numeric input field. - var weight = parseInt($(targetClass, siblings[0]).val()) || 0; - $(targetClass, siblings).each(function() { - this.value = weight; - weight++; - }); - } - break; - } + var targetClass = '.' + rowSettings.target; + var targetElement = $(targetClass, changedRow).get(0); + + // Check if a target element exists in this row. + if (targetElement) { + var sourceClass = '.' + rowSettings.source; + var sourceElement = $(sourceClass, sourceRow).get(0); + switch (rowSettings.action) { + case 'depth': + // Get the depth of the target row. + targetElement.value = $('.indentation', $(sourceElement).parents('tr:first')).size(); + break; + case 'match': + // Update the value. + targetElement.value = sourceElement.value; + break; + case 'order': + var siblings = this.rowObject.findSiblings(rowSettings); + if ($(targetElement).is('select')) { + // Get a list of acceptable values. + var values = new Array(); + $('option', targetElement).each(function() { + values.push(this.value); + }); + // Populate the values in the siblings. + $(targetClass, siblings).each(function() { + this.value = values.shift(); + }); + } + else { + // Assume a numeric input field. + var weight = parseInt($(targetClass, siblings[0]).val()) || 0; + $(targetClass, siblings).each(function() { + this.value = weight; + weight++; + }); + } + break; } } }; |