danielebarchiesi@0: (function ($) { danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Drag and drop table rows with field manipulation. danielebarchiesi@0: * danielebarchiesi@0: * Using the drupal_add_tabledrag() function, any table with weights or parent danielebarchiesi@0: * relationships may be made into draggable tables. Columns containing a field danielebarchiesi@0: * may optionally be hidden, providing a better user experience. danielebarchiesi@0: * danielebarchiesi@0: * Created tableDrag instances may be modified with custom behaviors by danielebarchiesi@0: * overriding the .onDrag, .onDrop, .row.onSwap, and .row.onIndent methods. danielebarchiesi@0: * See blocks.js for an example of adding additional functionality to tableDrag. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.behaviors.tableDrag = { danielebarchiesi@0: attach: function (context, settings) { danielebarchiesi@0: for (var base in settings.tableDrag) { danielebarchiesi@0: $('#' + base, context).once('tabledrag', function () { danielebarchiesi@0: // Create the new tableDrag instance. Save in the Drupal variable danielebarchiesi@0: // to allow other scripts access to the object. danielebarchiesi@0: Drupal.tableDrag[base] = new Drupal.tableDrag(this, settings.tableDrag[base]); danielebarchiesi@0: }); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Constructor for the tableDrag object. Provides table and field manipulation. danielebarchiesi@0: * danielebarchiesi@0: * @param table danielebarchiesi@0: * DOM object for the table to be made draggable. danielebarchiesi@0: * @param tableSettings danielebarchiesi@0: * Settings for the table added via drupal_add_dragtable(). danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag = function (table, tableSettings) { danielebarchiesi@0: var self = this; danielebarchiesi@0: danielebarchiesi@0: // Required object variables. danielebarchiesi@0: this.table = table; danielebarchiesi@0: this.tableSettings = tableSettings; danielebarchiesi@0: this.dragObject = null; // Used to hold information about a current drag operation. danielebarchiesi@0: this.rowObject = null; // Provides operations for row manipulation. danielebarchiesi@0: this.oldRowElement = null; // Remember the previous element. danielebarchiesi@0: this.oldY = 0; // Used to determine up or down direction from last mouse move. danielebarchiesi@0: this.changed = false; // Whether anything in the entire table has changed. danielebarchiesi@0: this.maxDepth = 0; // Maximum amount of allowed parenting. danielebarchiesi@0: this.rtl = $(this.table).css('direction') == 'rtl' ? -1 : 1; // Direction of the table. danielebarchiesi@0: danielebarchiesi@0: // Configure the scroll settings. danielebarchiesi@0: this.scrollSettings = { amount: 4, interval: 50, trigger: 70 }; danielebarchiesi@0: this.scrollInterval = null; danielebarchiesi@0: this.scrollY = 0; danielebarchiesi@0: this.windowHeight = 0; danielebarchiesi@0: danielebarchiesi@0: // Check this table's settings to see if there are parent relationships in danielebarchiesi@0: // this table. For efficiency, large sections of code can be skipped if we danielebarchiesi@0: // don't need to track horizontal movement and indentations. danielebarchiesi@0: this.indentEnabled = false; danielebarchiesi@0: for (var group in tableSettings) { danielebarchiesi@0: for (var n in tableSettings[group]) { danielebarchiesi@0: if (tableSettings[group][n].relationship == 'parent') { danielebarchiesi@0: this.indentEnabled = true; danielebarchiesi@0: } danielebarchiesi@0: if (tableSettings[group][n].limit > 0) { danielebarchiesi@0: this.maxDepth = tableSettings[group][n].limit; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: this.indentCount = 1; // Total width of indents, set in makeDraggable. danielebarchiesi@0: // Find the width of indentations to measure mouse movements against. danielebarchiesi@0: // Because the table doesn't need to start with any indentations, we danielebarchiesi@0: // manually append 2 indentations in the first draggable row, measure danielebarchiesi@0: // the offset, then remove. danielebarchiesi@0: var indent = Drupal.theme('tableDragIndentation'); danielebarchiesi@0: var testRow = $('').addClass('draggable').appendTo(table); danielebarchiesi@0: var testCell = $('').appendTo(testRow).prepend(indent).prepend(indent); danielebarchiesi@0: this.indentAmount = $('.indentation', testCell).get(1).offsetLeft - $('.indentation', testCell).get(0).offsetLeft; danielebarchiesi@0: testRow.remove(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Make each applicable row draggable. danielebarchiesi@0: // Match immediate children of the parent element to allow nesting. danielebarchiesi@0: $('> tr.draggable, > tbody > tr.draggable', table).each(function () { self.makeDraggable(this); }); danielebarchiesi@0: danielebarchiesi@0: // Add a link before the table for users to show or hide weight columns. danielebarchiesi@0: $(table).before($('') danielebarchiesi@0: .attr('title', Drupal.t('Re-order rows by numerical weight instead of dragging.')) danielebarchiesi@0: .click(function () { danielebarchiesi@0: if ($.cookie('Drupal.tableDrag.showWeight') == 1) { danielebarchiesi@0: self.hideColumns(); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: self.showColumns(); danielebarchiesi@0: } danielebarchiesi@0: return false; danielebarchiesi@0: }) danielebarchiesi@0: .wrap('
') danielebarchiesi@0: .parent() danielebarchiesi@0: ); danielebarchiesi@0: danielebarchiesi@0: // Initialize the specified columns (for example, weight or parent columns) danielebarchiesi@0: // to show or hide according to user preference. This aids accessibility danielebarchiesi@0: // so that, e.g., screen reader users can choose to enter weight values and danielebarchiesi@0: // manipulate form elements directly, rather than using drag-and-drop.. danielebarchiesi@0: self.initColumns(); danielebarchiesi@0: danielebarchiesi@0: // Add mouse bindings to the document. The self variable is passed along danielebarchiesi@0: // as event handlers do not have direct access to the tableDrag object. danielebarchiesi@0: $(document).bind('mousemove', function (event) { return self.dragRow(event, self); }); danielebarchiesi@0: $(document).bind('mouseup', function (event) { return self.dropRow(event, self); }); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Initialize columns containing form elements to be hidden by default, danielebarchiesi@0: * according to the settings for this tableDrag instance. danielebarchiesi@0: * danielebarchiesi@0: * Identify and mark each cell with a CSS class so we can easily toggle danielebarchiesi@0: * show/hide it. Finally, hide columns if user does not have a danielebarchiesi@0: * 'Drupal.tableDrag.showWeight' cookie. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.initColumns = function () { danielebarchiesi@0: for (var group in this.tableSettings) { danielebarchiesi@0: // Find the first field in this group. danielebarchiesi@0: for (var d in this.tableSettings[group]) { danielebarchiesi@0: var field = $('.' + this.tableSettings[group][d].target + ':first', this.table); danielebarchiesi@0: if (field.length && this.tableSettings[group][d].hidden) { danielebarchiesi@0: var hidden = this.tableSettings[group][d].hidden; danielebarchiesi@0: var cell = field.closest('td'); danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Mark the column containing this field so it can be hidden. danielebarchiesi@0: if (hidden && cell[0]) { danielebarchiesi@0: // Add 1 to our indexes. The nth-child selector is 1 based, not 0 based. danielebarchiesi@0: // Match immediate children of the parent element to allow nesting. danielebarchiesi@0: var columnIndex = $('> td', cell.parent()).index(cell.get(0)) + 1; danielebarchiesi@0: $('> thead > tr, > tbody > tr, > tr', this.table).each(function () { danielebarchiesi@0: // Get the columnIndex and adjust for any colspans in this row. danielebarchiesi@0: var index = columnIndex; danielebarchiesi@0: var cells = $(this).children(); danielebarchiesi@0: cells.each(function (n) { danielebarchiesi@0: if (n < index && this.colSpan && this.colSpan > 1) { danielebarchiesi@0: index -= this.colSpan - 1; danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: if (index > 0) { danielebarchiesi@0: cell = cells.filter(':nth-child(' + index + ')'); danielebarchiesi@0: if (cell[0].colSpan && cell[0].colSpan > 1) { danielebarchiesi@0: // If this cell has a colspan, mark it so we can reduce the colspan. danielebarchiesi@0: cell.addClass('tabledrag-has-colspan'); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // Mark this cell so we can hide it. danielebarchiesi@0: cell.addClass('tabledrag-hide'); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Now hide cells and reduce colspans unless cookie indicates previous choice. danielebarchiesi@0: // Set a cookie if it is not already present. danielebarchiesi@0: if ($.cookie('Drupal.tableDrag.showWeight') === null) { danielebarchiesi@0: $.cookie('Drupal.tableDrag.showWeight', 0, { danielebarchiesi@0: path: Drupal.settings.basePath, danielebarchiesi@0: // The cookie expires in one year. danielebarchiesi@0: expires: 365 danielebarchiesi@0: }); danielebarchiesi@0: this.hideColumns(); danielebarchiesi@0: } danielebarchiesi@0: // Check cookie value and show/hide weight columns accordingly. danielebarchiesi@0: else { danielebarchiesi@0: if ($.cookie('Drupal.tableDrag.showWeight') == 1) { danielebarchiesi@0: this.showColumns(); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: this.hideColumns(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Hide the columns containing weight/parent form elements. danielebarchiesi@0: * Undo showColumns(). danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.hideColumns = function () { danielebarchiesi@0: // Hide weight/parent cells and headers. danielebarchiesi@0: $('.tabledrag-hide', 'table.tabledrag-processed').css('display', 'none'); danielebarchiesi@0: // Show TableDrag handles. danielebarchiesi@0: $('.tabledrag-handle', 'table.tabledrag-processed').css('display', ''); danielebarchiesi@0: // Reduce the colspan of any effected multi-span columns. danielebarchiesi@0: $('.tabledrag-has-colspan', 'table.tabledrag-processed').each(function () { danielebarchiesi@0: this.colSpan = this.colSpan - 1; danielebarchiesi@0: }); danielebarchiesi@0: // Change link text. danielebarchiesi@0: $('.tabledrag-toggle-weight').text(Drupal.t('Show row weights')); danielebarchiesi@0: // Change cookie. danielebarchiesi@0: $.cookie('Drupal.tableDrag.showWeight', 0, { danielebarchiesi@0: path: Drupal.settings.basePath, danielebarchiesi@0: // The cookie expires in one year. danielebarchiesi@0: expires: 365 danielebarchiesi@0: }); danielebarchiesi@0: // Trigger an event to allow other scripts to react to this display change. danielebarchiesi@0: $('table.tabledrag-processed').trigger('columnschange', 'hide'); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Show the columns containing weight/parent form elements danielebarchiesi@0: * Undo hideColumns(). danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.showColumns = function () { danielebarchiesi@0: // Show weight/parent cells and headers. danielebarchiesi@0: $('.tabledrag-hide', 'table.tabledrag-processed').css('display', ''); danielebarchiesi@0: // Hide TableDrag handles. danielebarchiesi@0: $('.tabledrag-handle', 'table.tabledrag-processed').css('display', 'none'); danielebarchiesi@0: // Increase the colspan for any columns where it was previously reduced. danielebarchiesi@0: $('.tabledrag-has-colspan', 'table.tabledrag-processed').each(function () { danielebarchiesi@0: this.colSpan = this.colSpan + 1; danielebarchiesi@0: }); danielebarchiesi@0: // Change link text. danielebarchiesi@0: $('.tabledrag-toggle-weight').text(Drupal.t('Hide row weights')); danielebarchiesi@0: // Change cookie. danielebarchiesi@0: $.cookie('Drupal.tableDrag.showWeight', 1, { danielebarchiesi@0: path: Drupal.settings.basePath, danielebarchiesi@0: // The cookie expires in one year. danielebarchiesi@0: expires: 365 danielebarchiesi@0: }); danielebarchiesi@0: // Trigger an event to allow other scripts to react to this display change. danielebarchiesi@0: $('table.tabledrag-processed').trigger('columnschange', 'show'); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Find the target used within a particular row and group. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.rowSettings = function (group, row) { danielebarchiesi@0: var field = $('.' + group, row); danielebarchiesi@0: for (var delta in this.tableSettings[group]) { danielebarchiesi@0: var targetClass = this.tableSettings[group][delta].target; danielebarchiesi@0: if (field.is('.' + targetClass)) { danielebarchiesi@0: // Return a copy of the row settings. danielebarchiesi@0: var rowSettings = {}; danielebarchiesi@0: for (var n in this.tableSettings[group][delta]) { danielebarchiesi@0: rowSettings[n] = this.tableSettings[group][delta][n]; danielebarchiesi@0: } danielebarchiesi@0: return rowSettings; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Take an item and add event handlers to make it become draggable. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.makeDraggable = function (item) { danielebarchiesi@0: var self = this; danielebarchiesi@0: danielebarchiesi@0: // Create the handle. danielebarchiesi@0: var handle = $('
 
').attr('title', Drupal.t('Drag to re-order')); danielebarchiesi@0: // Insert the handle after indentations (if any). danielebarchiesi@0: if ($('td:first .indentation:last', item).length) { danielebarchiesi@0: $('td:first .indentation:last', item).after(handle); danielebarchiesi@0: // Update the total width of indentation in this entire table. danielebarchiesi@0: self.indentCount = Math.max($('.indentation', item).length, self.indentCount); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $('td:first', item).prepend(handle); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Add hover action for the handle. danielebarchiesi@0: handle.hover(function () { danielebarchiesi@0: self.dragObject == null ? $(this).addClass('tabledrag-handle-hover') : null; danielebarchiesi@0: }, function () { danielebarchiesi@0: self.dragObject == null ? $(this).removeClass('tabledrag-handle-hover') : null; danielebarchiesi@0: }); danielebarchiesi@0: danielebarchiesi@0: // Add the mousedown action for the handle. danielebarchiesi@0: handle.mousedown(function (event) { danielebarchiesi@0: // Create a new dragObject recording the event information. danielebarchiesi@0: self.dragObject = {}; danielebarchiesi@0: self.dragObject.initMouseOffset = self.getMouseOffset(item, event); danielebarchiesi@0: self.dragObject.initMouseCoords = self.mouseCoords(event); danielebarchiesi@0: if (self.indentEnabled) { danielebarchiesi@0: self.dragObject.indentMousePos = self.dragObject.initMouseCoords; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // If there's a lingering row object from the keyboard, remove its focus. danielebarchiesi@0: if (self.rowObject) { danielebarchiesi@0: $('a.tabledrag-handle', self.rowObject.element).blur(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Create a new rowObject for manipulation of this row. danielebarchiesi@0: self.rowObject = new self.row(item, 'mouse', self.indentEnabled, self.maxDepth, true); danielebarchiesi@0: danielebarchiesi@0: // Save the position of the table. danielebarchiesi@0: self.table.topY = $(self.table).offset().top; danielebarchiesi@0: self.table.bottomY = self.table.topY + self.table.offsetHeight; danielebarchiesi@0: danielebarchiesi@0: // Add classes to the handle and row. danielebarchiesi@0: $(this).addClass('tabledrag-handle-hover'); danielebarchiesi@0: $(item).addClass('drag'); danielebarchiesi@0: danielebarchiesi@0: // Set the document to use the move cursor during drag. danielebarchiesi@0: $('body').addClass('drag'); danielebarchiesi@0: if (self.oldRowElement) { danielebarchiesi@0: $(self.oldRowElement).removeClass('drag-previous'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Hack for IE6 that flickers uncontrollably if select lists are moved. danielebarchiesi@0: if (navigator.userAgent.indexOf('MSIE 6.') != -1) { danielebarchiesi@0: $('select', this.table).css('display', 'none'); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Hack for Konqueror, prevent the blur handler from firing. danielebarchiesi@0: // Konqueror always gives links focus, even after returning false on mousedown. danielebarchiesi@0: self.safeBlur = false; danielebarchiesi@0: danielebarchiesi@0: // Call optional placeholder function. danielebarchiesi@0: self.onDrag(); danielebarchiesi@0: return false; danielebarchiesi@0: }); danielebarchiesi@0: danielebarchiesi@0: // Prevent the anchor tag from jumping us to the top of the page. danielebarchiesi@0: handle.click(function () { danielebarchiesi@0: return false; danielebarchiesi@0: }); danielebarchiesi@0: danielebarchiesi@0: // Similar to the hover event, add a class when the handle is focused. danielebarchiesi@0: handle.focus(function () { danielebarchiesi@0: $(this).addClass('tabledrag-handle-hover'); danielebarchiesi@0: self.safeBlur = true; danielebarchiesi@0: }); danielebarchiesi@0: danielebarchiesi@0: // Remove the handle class on blur and fire the same function as a mouseup. danielebarchiesi@0: handle.blur(function (event) { danielebarchiesi@0: $(this).removeClass('tabledrag-handle-hover'); danielebarchiesi@0: if (self.rowObject && self.safeBlur) { danielebarchiesi@0: self.dropRow(event, self); danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: danielebarchiesi@0: // Add arrow-key support to the handle. danielebarchiesi@0: handle.keydown(function (event) { danielebarchiesi@0: // If a rowObject doesn't yet exist and this isn't the tab key. danielebarchiesi@0: if (event.keyCode != 9 && !self.rowObject) { danielebarchiesi@0: self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: var keyChange = false; danielebarchiesi@0: switch (event.keyCode) { danielebarchiesi@0: case 37: // Left arrow. danielebarchiesi@0: case 63234: // Safari left arrow. danielebarchiesi@0: keyChange = true; danielebarchiesi@0: self.rowObject.indent(-1 * self.rtl); danielebarchiesi@0: break; danielebarchiesi@0: case 38: // Up arrow. danielebarchiesi@0: case 63232: // Safari up arrow. danielebarchiesi@0: var previousRow = $(self.rowObject.element).prev('tr').get(0); danielebarchiesi@0: while (previousRow && $(previousRow).is(':hidden')) { danielebarchiesi@0: previousRow = $(previousRow).prev('tr').get(0); danielebarchiesi@0: } danielebarchiesi@0: if (previousRow) { danielebarchiesi@0: self.safeBlur = false; // Do not allow the onBlur cleanup. danielebarchiesi@0: self.rowObject.direction = 'up'; danielebarchiesi@0: keyChange = true; danielebarchiesi@0: danielebarchiesi@0: if ($(item).is('.tabledrag-root')) { danielebarchiesi@0: // Swap with the previous top-level row. danielebarchiesi@0: var groupHeight = 0; danielebarchiesi@0: while (previousRow && $('.indentation', previousRow).length) { danielebarchiesi@0: previousRow = $(previousRow).prev('tr').get(0); danielebarchiesi@0: groupHeight += $(previousRow).is(':hidden') ? 0 : previousRow.offsetHeight; danielebarchiesi@0: } danielebarchiesi@0: if (previousRow) { danielebarchiesi@0: self.rowObject.swap('before', previousRow); danielebarchiesi@0: // No need to check for indentation, 0 is the only valid one. danielebarchiesi@0: window.scrollBy(0, -groupHeight); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else if (self.table.tBodies[0].rows[0] != previousRow || $(previousRow).is('.draggable')) { danielebarchiesi@0: // Swap with the previous row (unless previous row is the first one danielebarchiesi@0: // and undraggable). danielebarchiesi@0: self.rowObject.swap('before', previousRow); danielebarchiesi@0: self.rowObject.interval = null; danielebarchiesi@0: self.rowObject.indent(0); danielebarchiesi@0: window.scrollBy(0, -parseInt(item.offsetHeight, 10)); danielebarchiesi@0: } danielebarchiesi@0: handle.get(0).focus(); // Regain focus after the DOM manipulation. danielebarchiesi@0: } danielebarchiesi@0: break; danielebarchiesi@0: case 39: // Right arrow. danielebarchiesi@0: case 63235: // Safari right arrow. danielebarchiesi@0: keyChange = true; danielebarchiesi@0: self.rowObject.indent(1 * self.rtl); danielebarchiesi@0: break; danielebarchiesi@0: case 40: // Down arrow. danielebarchiesi@0: case 63233: // Safari down arrow. danielebarchiesi@0: var nextRow = $(self.rowObject.group).filter(':last').next('tr').get(0); danielebarchiesi@0: while (nextRow && $(nextRow).is(':hidden')) { danielebarchiesi@0: nextRow = $(nextRow).next('tr').get(0); danielebarchiesi@0: } danielebarchiesi@0: if (nextRow) { danielebarchiesi@0: self.safeBlur = false; // Do not allow the onBlur cleanup. danielebarchiesi@0: self.rowObject.direction = 'down'; danielebarchiesi@0: keyChange = true; danielebarchiesi@0: danielebarchiesi@0: if ($(item).is('.tabledrag-root')) { danielebarchiesi@0: // Swap with the next group (necessarily a top-level one). danielebarchiesi@0: var groupHeight = 0; danielebarchiesi@0: var nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false); danielebarchiesi@0: if (nextGroup) { danielebarchiesi@0: $(nextGroup.group).each(function () { danielebarchiesi@0: groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight; danielebarchiesi@0: }); danielebarchiesi@0: var nextGroupRow = $(nextGroup.group).filter(':last').get(0); danielebarchiesi@0: self.rowObject.swap('after', nextGroupRow); danielebarchiesi@0: // No need to check for indentation, 0 is the only valid one. danielebarchiesi@0: window.scrollBy(0, parseInt(groupHeight, 10)); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // Swap with the next row. danielebarchiesi@0: self.rowObject.swap('after', nextRow); danielebarchiesi@0: self.rowObject.interval = null; danielebarchiesi@0: self.rowObject.indent(0); danielebarchiesi@0: window.scrollBy(0, parseInt(item.offsetHeight, 10)); danielebarchiesi@0: } danielebarchiesi@0: handle.get(0).focus(); // Regain focus after the DOM manipulation. danielebarchiesi@0: } danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (self.rowObject && self.rowObject.changed == true) { danielebarchiesi@0: $(item).addClass('drag'); danielebarchiesi@0: if (self.oldRowElement) { danielebarchiesi@0: $(self.oldRowElement).removeClass('drag-previous'); danielebarchiesi@0: } danielebarchiesi@0: self.oldRowElement = item; danielebarchiesi@0: self.restripeTable(); danielebarchiesi@0: self.onDrag(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Returning false if we have an arrow key to prevent scrolling. danielebarchiesi@0: if (keyChange) { danielebarchiesi@0: return false; danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: danielebarchiesi@0: // Compatibility addition, return false on keypress to prevent unwanted scrolling. danielebarchiesi@0: // IE and Safari will suppress scrolling on keydown, but all other browsers danielebarchiesi@0: // need to return false on keypress. http://www.quirksmode.org/js/keys.html danielebarchiesi@0: handle.keypress(function (event) { danielebarchiesi@0: switch (event.keyCode) { danielebarchiesi@0: case 37: // Left arrow. danielebarchiesi@0: case 38: // Up arrow. danielebarchiesi@0: case 39: // Right arrow. danielebarchiesi@0: case 40: // Down arrow. danielebarchiesi@0: return false; danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Mousemove event handler, bound to document. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.dragRow = function (event, self) { danielebarchiesi@0: if (self.dragObject) { danielebarchiesi@0: self.currentMouseCoords = self.mouseCoords(event); danielebarchiesi@0: danielebarchiesi@0: var y = self.currentMouseCoords.y - self.dragObject.initMouseOffset.y; danielebarchiesi@0: var x = self.currentMouseCoords.x - self.dragObject.initMouseOffset.x; danielebarchiesi@0: danielebarchiesi@0: // Check for row swapping and vertical scrolling. danielebarchiesi@0: if (y != self.oldY) { danielebarchiesi@0: self.rowObject.direction = y > self.oldY ? 'down' : 'up'; danielebarchiesi@0: self.oldY = y; // Update the old value. danielebarchiesi@0: danielebarchiesi@0: // Check if the window should be scrolled (and how fast). danielebarchiesi@0: var scrollAmount = self.checkScroll(self.currentMouseCoords.y); danielebarchiesi@0: // Stop any current scrolling. danielebarchiesi@0: clearInterval(self.scrollInterval); danielebarchiesi@0: // Continue scrolling if the mouse has moved in the scroll direction. danielebarchiesi@0: if (scrollAmount > 0 && self.rowObject.direction == 'down' || scrollAmount < 0 && self.rowObject.direction == 'up') { danielebarchiesi@0: self.setScroll(scrollAmount); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // If we have a valid target, perform the swap and restripe the table. danielebarchiesi@0: var currentRow = self.findDropTargetRow(x, y); danielebarchiesi@0: if (currentRow) { danielebarchiesi@0: if (self.rowObject.direction == 'down') { danielebarchiesi@0: self.rowObject.swap('after', currentRow, self); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: self.rowObject.swap('before', currentRow, self); danielebarchiesi@0: } danielebarchiesi@0: self.restripeTable(); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Similar to row swapping, handle indentations. danielebarchiesi@0: if (self.indentEnabled) { danielebarchiesi@0: var xDiff = self.currentMouseCoords.x - self.dragObject.indentMousePos.x; danielebarchiesi@0: // Set the number of indentations the mouse has been moved left or right. danielebarchiesi@0: var indentDiff = Math.round(xDiff / self.indentAmount * self.rtl); danielebarchiesi@0: // Indent the row with our estimated diff, which may be further danielebarchiesi@0: // restricted according to the rows around this row. danielebarchiesi@0: var indentChange = self.rowObject.indent(indentDiff); danielebarchiesi@0: // Update table and mouse indentations. danielebarchiesi@0: self.dragObject.indentMousePos.x += self.indentAmount * indentChange * self.rtl; danielebarchiesi@0: self.indentCount = Math.max(self.indentCount, self.rowObject.indents); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return false; danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Mouseup event handler, bound to document. danielebarchiesi@0: * Blur event handler, bound to drag handle for keyboard support. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.dropRow = function (event, self) { danielebarchiesi@0: // Drop row functionality shared between mouseup and blur events. danielebarchiesi@0: if (self.rowObject != null) { danielebarchiesi@0: var droppedRow = self.rowObject.element; danielebarchiesi@0: // The row is already in the right place so we just release it. danielebarchiesi@0: if (self.rowObject.changed == true) { danielebarchiesi@0: // Update the fields in the dropped row. danielebarchiesi@0: self.updateFields(droppedRow); danielebarchiesi@0: danielebarchiesi@0: // If a setting exists for affecting the entire group, update all the danielebarchiesi@0: // fields in the entire dragged group. danielebarchiesi@0: for (var group in self.tableSettings) { danielebarchiesi@0: var rowSettings = self.rowSettings(group, droppedRow); danielebarchiesi@0: if (rowSettings.relationship == 'group') { danielebarchiesi@0: for (var n in self.rowObject.children) { danielebarchiesi@0: self.updateField(self.rowObject.children[n], group); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: self.rowObject.markChanged(); danielebarchiesi@0: if (self.changed == false) { danielebarchiesi@0: $(Drupal.theme('tableDragChangedWarning')).insertBefore(self.table).hide().fadeIn('slow'); danielebarchiesi@0: self.changed = true; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (self.indentEnabled) { danielebarchiesi@0: self.rowObject.removeIndentClasses(); danielebarchiesi@0: } danielebarchiesi@0: if (self.oldRowElement) { danielebarchiesi@0: $(self.oldRowElement).removeClass('drag-previous'); danielebarchiesi@0: } danielebarchiesi@0: $(droppedRow).removeClass('drag').addClass('drag-previous'); danielebarchiesi@0: self.oldRowElement = droppedRow; danielebarchiesi@0: self.onDrop(); danielebarchiesi@0: self.rowObject = null; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Functionality specific only to mouseup event. danielebarchiesi@0: if (self.dragObject != null) { danielebarchiesi@0: $('.tabledrag-handle', droppedRow).removeClass('tabledrag-handle-hover'); danielebarchiesi@0: danielebarchiesi@0: self.dragObject = null; danielebarchiesi@0: $('body').removeClass('drag'); danielebarchiesi@0: clearInterval(self.scrollInterval); danielebarchiesi@0: danielebarchiesi@0: // Hack for IE6 that flickers uncontrollably if select lists are moved. danielebarchiesi@0: if (navigator.userAgent.indexOf('MSIE 6.') != -1) { danielebarchiesi@0: $('select', this.table).css('display', 'block'); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Get the mouse coordinates from the event (allowing for browser differences). danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.mouseCoords = function (event) { danielebarchiesi@0: if (event.pageX || event.pageY) { danielebarchiesi@0: return { x: event.pageX, y: event.pageY }; danielebarchiesi@0: } danielebarchiesi@0: return { danielebarchiesi@0: x: event.clientX + document.body.scrollLeft - document.body.clientLeft, danielebarchiesi@0: y: event.clientY + document.body.scrollTop - document.body.clientTop danielebarchiesi@0: }; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Given a target element and a mouse event, get the mouse offset from that danielebarchiesi@0: * element. To do this we need the element's position and the mouse position. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.getMouseOffset = function (target, event) { danielebarchiesi@0: var docPos = $(target).offset(); danielebarchiesi@0: var mousePos = this.mouseCoords(event); danielebarchiesi@0: return { x: mousePos.x - docPos.left, y: mousePos.y - docPos.top }; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Find the row the mouse is currently over. This row is then taken and swapped danielebarchiesi@0: * with the one being dragged. danielebarchiesi@0: * danielebarchiesi@0: * @param x danielebarchiesi@0: * The x coordinate of the mouse on the page (not the screen). danielebarchiesi@0: * @param y danielebarchiesi@0: * The y coordinate of the mouse on the page (not the screen). danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.findDropTargetRow = function (x, y) { danielebarchiesi@0: var rows = $(this.table.tBodies[0].rows).not(':hidden'); danielebarchiesi@0: for (var n = 0; n < rows.length; n++) { danielebarchiesi@0: var row = rows[n]; danielebarchiesi@0: var indentDiff = 0; danielebarchiesi@0: var rowY = $(row).offset().top; danielebarchiesi@0: // Because Safari does not report offsetHeight on table rows, but does on danielebarchiesi@0: // table cells, grab the firstChild of the row and use that instead. danielebarchiesi@0: // http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari. danielebarchiesi@0: if (row.offsetHeight == 0) { danielebarchiesi@0: var rowHeight = parseInt(row.firstChild.offsetHeight, 10) / 2; danielebarchiesi@0: } danielebarchiesi@0: // Other browsers. danielebarchiesi@0: else { danielebarchiesi@0: var rowHeight = parseInt(row.offsetHeight, 10) / 2; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Because we always insert before, we need to offset the height a bit. danielebarchiesi@0: if ((y > (rowY - rowHeight)) && (y < (rowY + rowHeight))) { danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: // Check that this row is not a child of the row being dragged. danielebarchiesi@0: for (var n in this.rowObject.group) { danielebarchiesi@0: if (this.rowObject.group[n] == row) { danielebarchiesi@0: return null; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // Do not allow a row to be swapped with itself. danielebarchiesi@0: if (row == this.rowObject.element) { danielebarchiesi@0: return null; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Check that swapping with this row is allowed. danielebarchiesi@0: if (!this.rowObject.isValidSwap(row)) { danielebarchiesi@0: return null; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // We may have found the row the mouse just passed over, but it doesn't danielebarchiesi@0: // take into account hidden rows. Skip backwards until we find a draggable danielebarchiesi@0: // row. danielebarchiesi@0: while ($(row).is(':hidden') && $(row).prev('tr').is(':hidden')) { danielebarchiesi@0: row = $(row).prev('tr').get(0); danielebarchiesi@0: } danielebarchiesi@0: return row; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: return null; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * After the row is dropped, update the table fields according to the settings danielebarchiesi@0: * set for this table. danielebarchiesi@0: * danielebarchiesi@0: * @param changedRow danielebarchiesi@0: * DOM object for the row that was just dropped. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.updateFields = function (changedRow) { danielebarchiesi@0: for (var group in this.tableSettings) { danielebarchiesi@0: // Each group may have a different setting for relationship, so we find danielebarchiesi@0: // the source rows for each separately. danielebarchiesi@0: this.updateField(changedRow, group); danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * After the row is dropped, update a single table field according to specific danielebarchiesi@0: * settings. danielebarchiesi@0: * danielebarchiesi@0: * @param changedRow danielebarchiesi@0: * DOM object for the row that was just dropped. danielebarchiesi@0: * @param group danielebarchiesi@0: * The settings group on which field updates will occur. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.updateField = function (changedRow, group) { danielebarchiesi@0: var rowSettings = this.rowSettings(group, changedRow); danielebarchiesi@0: danielebarchiesi@0: // Set the row as its own target. danielebarchiesi@0: if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') { danielebarchiesi@0: var sourceRow = changedRow; danielebarchiesi@0: } danielebarchiesi@0: // Siblings are easy, check previous and next rows. danielebarchiesi@0: else if (rowSettings.relationship == 'sibling') { danielebarchiesi@0: var previousRow = $(changedRow).prev('tr').get(0); danielebarchiesi@0: var nextRow = $(changedRow).next('tr').get(0); danielebarchiesi@0: var sourceRow = changedRow; danielebarchiesi@0: if ($(previousRow).is('.draggable') && $('.' + group, previousRow).length) { danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: if ($('.indentations', previousRow).length == $('.indentations', changedRow)) { danielebarchiesi@0: sourceRow = previousRow; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: sourceRow = previousRow; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) { danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: if ($('.indentations', nextRow).length == $('.indentations', changedRow)) { danielebarchiesi@0: sourceRow = nextRow; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: sourceRow = nextRow; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: // Parents, look up the tree until we find a field not in this group. danielebarchiesi@0: // Go up as many parents as indentations in the changed row. danielebarchiesi@0: else if (rowSettings.relationship == 'parent') { danielebarchiesi@0: var previousRow = $(changedRow).prev('tr'); danielebarchiesi@0: while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) { danielebarchiesi@0: previousRow = previousRow.prev('tr'); danielebarchiesi@0: } danielebarchiesi@0: // If we found a row. danielebarchiesi@0: if (previousRow.length) { danielebarchiesi@0: sourceRow = previousRow[0]; danielebarchiesi@0: } danielebarchiesi@0: // Otherwise we went all the way to the left of the table without finding danielebarchiesi@0: // a parent, meaning this item has been placed at the root level. danielebarchiesi@0: else { danielebarchiesi@0: // Use the first row in the table as source, because it's guaranteed to danielebarchiesi@0: // be at the root level. Find the first item, then compare this row danielebarchiesi@0: // against it as a sibling. danielebarchiesi@0: sourceRow = $(this.table).find('tr.draggable:first').get(0); danielebarchiesi@0: if (sourceRow == this.rowObject.element) { danielebarchiesi@0: sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0); danielebarchiesi@0: } danielebarchiesi@0: var useSibling = true; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Because we may have moved the row from one category to another, danielebarchiesi@0: // take a look at our sibling and borrow its sources and targets. danielebarchiesi@0: this.copyDragClasses(sourceRow, changedRow, group); danielebarchiesi@0: rowSettings = this.rowSettings(group, changedRow); danielebarchiesi@0: danielebarchiesi@0: // In the case that we're looking for a parent, but the row is at the top danielebarchiesi@0: // of the tree, copy our sibling's values. danielebarchiesi@0: if (useSibling) { danielebarchiesi@0: rowSettings.relationship = 'sibling'; danielebarchiesi@0: rowSettings.source = rowSettings.target; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: var targetClass = '.' + rowSettings.target; danielebarchiesi@0: var targetElement = $(targetClass, changedRow).get(0); danielebarchiesi@0: danielebarchiesi@0: // Check if a target element exists in this row. danielebarchiesi@0: if (targetElement) { danielebarchiesi@0: var sourceClass = '.' + rowSettings.source; danielebarchiesi@0: var sourceElement = $(sourceClass, sourceRow).get(0); danielebarchiesi@0: switch (rowSettings.action) { danielebarchiesi@0: case 'depth': danielebarchiesi@0: // Get the depth of the target row. danielebarchiesi@0: targetElement.value = $('.indentation', $(sourceElement).closest('tr')).length; danielebarchiesi@0: break; danielebarchiesi@0: case 'match': danielebarchiesi@0: // Update the value. danielebarchiesi@0: targetElement.value = sourceElement.value; danielebarchiesi@0: break; danielebarchiesi@0: case 'order': danielebarchiesi@0: var siblings = this.rowObject.findSiblings(rowSettings); danielebarchiesi@0: if ($(targetElement).is('select')) { danielebarchiesi@0: // Get a list of acceptable values. danielebarchiesi@0: var values = []; danielebarchiesi@0: $('option', targetElement).each(function () { danielebarchiesi@0: values.push(this.value); danielebarchiesi@0: }); danielebarchiesi@0: var maxVal = values[values.length - 1]; danielebarchiesi@0: // Populate the values in the siblings. danielebarchiesi@0: $(targetClass, siblings).each(function () { danielebarchiesi@0: // If there are more items than possible values, assign the maximum value to the row. danielebarchiesi@0: if (values.length > 0) { danielebarchiesi@0: this.value = values.shift(); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: this.value = maxVal; danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // Assume a numeric input field. danielebarchiesi@0: var weight = parseInt($(targetClass, siblings[0]).val(), 10) || 0; danielebarchiesi@0: $(targetClass, siblings).each(function () { danielebarchiesi@0: this.value = weight; danielebarchiesi@0: weight++; danielebarchiesi@0: }); danielebarchiesi@0: } danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Copy all special tableDrag classes from one row's form elements to a danielebarchiesi@0: * different one, removing any special classes that the destination row danielebarchiesi@0: * may have had. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.copyDragClasses = function (sourceRow, targetRow, group) { danielebarchiesi@0: var sourceElement = $('.' + group, sourceRow); danielebarchiesi@0: var targetElement = $('.' + group, targetRow); danielebarchiesi@0: if (sourceElement.length && targetElement.length) { danielebarchiesi@0: targetElement[0].className = sourceElement[0].className; danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: Drupal.tableDrag.prototype.checkScroll = function (cursorY) { danielebarchiesi@0: var de = document.documentElement; danielebarchiesi@0: var b = document.body; danielebarchiesi@0: danielebarchiesi@0: var windowHeight = this.windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth != 0 ? de.clientHeight : b.offsetHeight); danielebarchiesi@0: var scrollY = this.scrollY = (document.all ? (!de.scrollTop ? b.scrollTop : de.scrollTop) : (window.pageYOffset ? window.pageYOffset : window.scrollY)); danielebarchiesi@0: var trigger = this.scrollSettings.trigger; danielebarchiesi@0: var delta = 0; danielebarchiesi@0: danielebarchiesi@0: // Return a scroll speed relative to the edge of the screen. danielebarchiesi@0: if (cursorY - scrollY > windowHeight - trigger) { danielebarchiesi@0: delta = trigger / (windowHeight + scrollY - cursorY); danielebarchiesi@0: delta = (delta > 0 && delta < trigger) ? delta : trigger; danielebarchiesi@0: return delta * this.scrollSettings.amount; danielebarchiesi@0: } danielebarchiesi@0: else if (cursorY - scrollY < trigger) { danielebarchiesi@0: delta = trigger / (cursorY - scrollY); danielebarchiesi@0: delta = (delta > 0 && delta < trigger) ? delta : trigger; danielebarchiesi@0: return -delta * this.scrollSettings.amount; danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: Drupal.tableDrag.prototype.setScroll = function (scrollAmount) { danielebarchiesi@0: var self = this; danielebarchiesi@0: danielebarchiesi@0: this.scrollInterval = setInterval(function () { danielebarchiesi@0: // Update the scroll values stored in the object. danielebarchiesi@0: self.checkScroll(self.currentMouseCoords.y); danielebarchiesi@0: var aboveTable = self.scrollY > self.table.topY; danielebarchiesi@0: var belowTable = self.scrollY + self.windowHeight < self.table.bottomY; danielebarchiesi@0: if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) { danielebarchiesi@0: window.scrollBy(0, scrollAmount); danielebarchiesi@0: } danielebarchiesi@0: }, this.scrollSettings.interval); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: Drupal.tableDrag.prototype.restripeTable = function () { danielebarchiesi@0: // :even and :odd are reversed because jQuery counts from 0 and danielebarchiesi@0: // we count from 1, so we're out of sync. danielebarchiesi@0: // Match immediate children of the parent element to allow nesting. danielebarchiesi@0: $('> tbody > tr.draggable:visible, > tr.draggable:visible', this.table) danielebarchiesi@0: .removeClass('odd even') danielebarchiesi@0: .filter(':odd').addClass('even').end() danielebarchiesi@0: .filter(':even').addClass('odd'); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Stub function. Allows a custom handler when a row begins dragging. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.onDrag = function () { danielebarchiesi@0: return null; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Stub function. Allows a custom handler when a row is dropped. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.onDrop = function () { danielebarchiesi@0: return null; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Constructor to make a new object to manipulate a table row. danielebarchiesi@0: * danielebarchiesi@0: * @param tableRow danielebarchiesi@0: * The DOM element for the table row we will be manipulating. danielebarchiesi@0: * @param method danielebarchiesi@0: * The method in which this row is being moved. Either 'keyboard' or 'mouse'. danielebarchiesi@0: * @param indentEnabled danielebarchiesi@0: * Whether the containing table uses indentations. Used for optimizations. danielebarchiesi@0: * @param maxDepth danielebarchiesi@0: * The maximum amount of indentations this row may contain. danielebarchiesi@0: * @param addClasses danielebarchiesi@0: * Whether we want to add classes to this row to indicate child relationships. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row = function (tableRow, method, indentEnabled, maxDepth, addClasses) { danielebarchiesi@0: this.element = tableRow; danielebarchiesi@0: this.method = method; danielebarchiesi@0: this.group = [tableRow]; danielebarchiesi@0: this.groupDepth = $('.indentation', tableRow).length; danielebarchiesi@0: this.changed = false; danielebarchiesi@0: this.table = $(tableRow).closest('table').get(0); danielebarchiesi@0: this.indentEnabled = indentEnabled; danielebarchiesi@0: this.maxDepth = maxDepth; danielebarchiesi@0: this.direction = ''; // Direction the row is being moved. danielebarchiesi@0: danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: this.indents = $('.indentation', tableRow).length; danielebarchiesi@0: this.children = this.findChildren(addClasses); danielebarchiesi@0: this.group = $.merge(this.group, this.children); danielebarchiesi@0: // Find the depth of this entire group. danielebarchiesi@0: for (var n = 0; n < this.group.length; n++) { danielebarchiesi@0: this.groupDepth = Math.max($('.indentation', this.group[n]).length, this.groupDepth); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Find all children of rowObject by indentation. danielebarchiesi@0: * danielebarchiesi@0: * @param addClasses danielebarchiesi@0: * Whether we want to add classes to this row to indicate child relationships. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.findChildren = function (addClasses) { danielebarchiesi@0: var parentIndentation = this.indents; danielebarchiesi@0: var currentRow = $(this.element, this.table).next('tr.draggable'); danielebarchiesi@0: var rows = []; danielebarchiesi@0: var child = 0; danielebarchiesi@0: while (currentRow.length) { danielebarchiesi@0: var rowIndentation = $('.indentation', currentRow).length; danielebarchiesi@0: // A greater indentation indicates this is a child. danielebarchiesi@0: if (rowIndentation > parentIndentation) { danielebarchiesi@0: child++; danielebarchiesi@0: rows.push(currentRow[0]); danielebarchiesi@0: if (addClasses) { danielebarchiesi@0: $('.indentation', currentRow).each(function (indentNum) { danielebarchiesi@0: if (child == 1 && (indentNum == parentIndentation)) { danielebarchiesi@0: $(this).addClass('tree-child-first'); danielebarchiesi@0: } danielebarchiesi@0: if (indentNum == parentIndentation) { danielebarchiesi@0: $(this).addClass('tree-child'); danielebarchiesi@0: } danielebarchiesi@0: else if (indentNum > parentIndentation) { danielebarchiesi@0: $(this).addClass('tree-child-horizontal'); danielebarchiesi@0: } danielebarchiesi@0: }); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: currentRow = currentRow.next('tr.draggable'); danielebarchiesi@0: } danielebarchiesi@0: if (addClasses && rows.length) { danielebarchiesi@0: $('.indentation:nth-child(' + (parentIndentation + 1) + ')', rows[rows.length - 1]).addClass('tree-child-last'); danielebarchiesi@0: } danielebarchiesi@0: return rows; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Ensure that two rows are allowed to be swapped. danielebarchiesi@0: * danielebarchiesi@0: * @param row danielebarchiesi@0: * DOM object for the row being considered for swapping. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.isValidSwap = function (row) { danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: var prevRow, nextRow; danielebarchiesi@0: if (this.direction == 'down') { danielebarchiesi@0: prevRow = row; danielebarchiesi@0: nextRow = $(row).next('tr').get(0); danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: prevRow = $(row).prev('tr').get(0); danielebarchiesi@0: nextRow = row; danielebarchiesi@0: } danielebarchiesi@0: this.interval = this.validIndentInterval(prevRow, nextRow); danielebarchiesi@0: danielebarchiesi@0: // We have an invalid swap if the valid indentations interval is empty. danielebarchiesi@0: if (this.interval.min > this.interval.max) { danielebarchiesi@0: return false; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Do not let an un-draggable first row have anything put before it. danielebarchiesi@0: if (this.table.tBodies[0].rows[0] == row && $(row).is(':not(.draggable)')) { danielebarchiesi@0: return false; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return true; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Perform the swap between two rows. danielebarchiesi@0: * danielebarchiesi@0: * @param position danielebarchiesi@0: * Whether the swap will occur 'before' or 'after' the given row. danielebarchiesi@0: * @param row danielebarchiesi@0: * DOM element what will be swapped with the row group. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.swap = function (position, row) { danielebarchiesi@0: Drupal.detachBehaviors(this.group, Drupal.settings, 'move'); danielebarchiesi@0: $(row)[position](this.group); danielebarchiesi@0: Drupal.attachBehaviors(this.group, Drupal.settings); danielebarchiesi@0: this.changed = true; danielebarchiesi@0: this.onSwap(row); danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Determine the valid indentations interval for the row at a given position danielebarchiesi@0: * in the table. danielebarchiesi@0: * danielebarchiesi@0: * @param prevRow danielebarchiesi@0: * DOM object for the row before the tested position danielebarchiesi@0: * (or null for first position in the table). danielebarchiesi@0: * @param nextRow danielebarchiesi@0: * DOM object for the row after the tested position danielebarchiesi@0: * (or null for last position in the table). danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) { danielebarchiesi@0: var minIndent, maxIndent; danielebarchiesi@0: danielebarchiesi@0: // Minimum indentation: danielebarchiesi@0: // Do not orphan the next row. danielebarchiesi@0: minIndent = nextRow ? $('.indentation', nextRow).length : 0; danielebarchiesi@0: danielebarchiesi@0: // Maximum indentation: danielebarchiesi@0: if (!prevRow || $(prevRow).is(':not(.draggable)') || $(this.element).is('.tabledrag-root')) { danielebarchiesi@0: // Do not indent: danielebarchiesi@0: // - the first row in the table, danielebarchiesi@0: // - rows dragged below a non-draggable row, danielebarchiesi@0: // - 'root' rows. danielebarchiesi@0: maxIndent = 0; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: // Do not go deeper than as a child of the previous row. danielebarchiesi@0: maxIndent = $('.indentation', prevRow).length + ($(prevRow).is('.tabledrag-leaf') ? 0 : 1); danielebarchiesi@0: // Limit by the maximum allowed depth for the table. danielebarchiesi@0: if (this.maxDepth) { danielebarchiesi@0: maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents)); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return { 'min': minIndent, 'max': maxIndent }; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Indent a row within the legal bounds of the table. danielebarchiesi@0: * danielebarchiesi@0: * @param indentDiff danielebarchiesi@0: * The number of additional indentations proposed for the row (can be danielebarchiesi@0: * positive or negative). This number will be adjusted to nearest valid danielebarchiesi@0: * indentation level for the row. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.indent = function (indentDiff) { danielebarchiesi@0: // Determine the valid indentations interval if not available yet. danielebarchiesi@0: if (!this.interval) { danielebarchiesi@0: var prevRow = $(this.element).prev('tr').get(0); danielebarchiesi@0: var nextRow = $(this.group).filter(':last').next('tr').get(0); danielebarchiesi@0: this.interval = this.validIndentInterval(prevRow, nextRow); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: // Adjust to the nearest valid indentation. danielebarchiesi@0: var indent = this.indents + indentDiff; danielebarchiesi@0: indent = Math.max(indent, this.interval.min); danielebarchiesi@0: indent = Math.min(indent, this.interval.max); danielebarchiesi@0: indentDiff = indent - this.indents; danielebarchiesi@0: danielebarchiesi@0: for (var n = 1; n <= Math.abs(indentDiff); n++) { danielebarchiesi@0: // Add or remove indentations. danielebarchiesi@0: if (indentDiff < 0) { danielebarchiesi@0: $('.indentation:first', this.group).remove(); danielebarchiesi@0: this.indents--; danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: $('td:first', this.group).prepend(Drupal.theme('tableDragIndentation')); danielebarchiesi@0: this.indents++; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: if (indentDiff) { danielebarchiesi@0: // Update indentation for this row. danielebarchiesi@0: this.changed = true; danielebarchiesi@0: this.groupDepth += indentDiff; danielebarchiesi@0: this.onIndent(); danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: return indentDiff; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Find all siblings for a row, either according to its subgroup or indentation. danielebarchiesi@0: * Note that the passed-in row is included in the list of siblings. danielebarchiesi@0: * danielebarchiesi@0: * @param settings danielebarchiesi@0: * The field settings we're using to identify what constitutes a sibling. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.findSiblings = function (rowSettings) { danielebarchiesi@0: var siblings = []; danielebarchiesi@0: var directions = ['prev', 'next']; danielebarchiesi@0: var rowIndentation = this.indents; danielebarchiesi@0: for (var d = 0; d < directions.length; d++) { danielebarchiesi@0: var checkRow = $(this.element)[directions[d]](); danielebarchiesi@0: while (checkRow.length) { danielebarchiesi@0: // Check that the sibling contains a similar target field. danielebarchiesi@0: if ($('.' + rowSettings.target, checkRow)) { danielebarchiesi@0: // Either add immediately if this is a flat table, or check to ensure danielebarchiesi@0: // that this row has the same level of indentation. danielebarchiesi@0: if (this.indentEnabled) { danielebarchiesi@0: var checkRowIndentation = $('.indentation', checkRow).length; danielebarchiesi@0: } danielebarchiesi@0: danielebarchiesi@0: if (!(this.indentEnabled) || (checkRowIndentation == rowIndentation)) { danielebarchiesi@0: siblings.push(checkRow[0]); danielebarchiesi@0: } danielebarchiesi@0: else if (checkRowIndentation < rowIndentation) { danielebarchiesi@0: // No need to keep looking for siblings when we get to a parent. danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: else { danielebarchiesi@0: break; danielebarchiesi@0: } danielebarchiesi@0: checkRow = $(checkRow)[directions[d]](); danielebarchiesi@0: } danielebarchiesi@0: // Since siblings are added in reverse order for previous, reverse the danielebarchiesi@0: // completed list of previous siblings. Add the current row and continue. danielebarchiesi@0: if (directions[d] == 'prev') { danielebarchiesi@0: siblings.reverse(); danielebarchiesi@0: siblings.push(this.element); danielebarchiesi@0: } danielebarchiesi@0: } danielebarchiesi@0: return siblings; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Remove indentation helper classes from the current row group. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function () { danielebarchiesi@0: for (var n in this.children) { danielebarchiesi@0: $('.indentation', this.children[n]) danielebarchiesi@0: .removeClass('tree-child') danielebarchiesi@0: .removeClass('tree-child-first') danielebarchiesi@0: .removeClass('tree-child-last') danielebarchiesi@0: .removeClass('tree-child-horizontal'); danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Add an asterisk or other marker to the changed row. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.markChanged = function () { danielebarchiesi@0: var marker = Drupal.theme('tableDragChangedMarker'); danielebarchiesi@0: var cell = $('td:first', this.element); danielebarchiesi@0: if ($('span.tabledrag-changed', cell).length == 0) { danielebarchiesi@0: cell.append(marker); danielebarchiesi@0: } danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Stub function. Allows a custom handler when a row is indented. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.onIndent = function () { danielebarchiesi@0: return null; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: /** danielebarchiesi@0: * Stub function. Allows a custom handler when a row is swapped. danielebarchiesi@0: */ danielebarchiesi@0: Drupal.tableDrag.prototype.row.prototype.onSwap = function (swappedRow) { danielebarchiesi@0: return null; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: Drupal.theme.prototype.tableDragChangedMarker = function () { danielebarchiesi@0: return '*'; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: Drupal.theme.prototype.tableDragIndentation = function () { danielebarchiesi@0: return '
 
'; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: Drupal.theme.prototype.tableDragChangedWarning = function () { danielebarchiesi@0: return '
' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t('Changes made in this table will not be saved until the form is submitted.') + '
'; danielebarchiesi@0: }; danielebarchiesi@0: danielebarchiesi@0: })(jQuery);