[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/jquery_update/replace/ -> tabledrag.js (source)

   1  
   2  /**
   3   * Drag and drop table rows with field manipulation.
   4   *
   5   * Using the drupal_add_tabledrag() function, any table with weights or parent
   6   * relationships may be made into draggable tables. Columns containing a field
   7   * may optionally be hidden, providing a better user experience.
   8   *
   9   * Created tableDrag instances may be modified with custom behaviors by
  10   * overriding the .onDrag, .onDrop, .row.onSwap, and .row.onIndent methods.
  11   * See blocks.js for an example of adding additional functionality to tableDrag.
  12   */
  13  Drupal.behaviors.tableDrag = function(context) {
  14    for (var base in Drupal.settings.tableDrag) {
  15      if (!$('#' + base + '.tabledrag-processed', context).size()) {
  16        var tableSettings = Drupal.settings.tableDrag[base];
  17  
  18        $('#' + base).filter(':not(.tabledrag-processed)').each(function() {
  19          // Create the new tableDrag instance. Save in the Drupal variable
  20          // to allow other scripts access to the object.
  21          Drupal.tableDrag[base] = new Drupal.tableDrag(this, tableSettings);
  22        });
  23  
  24        $('#' + base).addClass('tabledrag-processed');
  25      }
  26    }
  27  };
  28  
  29  /**
  30   * Constructor for the tableDrag object. Provides table and field manipulation.
  31   *
  32   * @param table
  33   *   DOM object for the table to be made draggable.
  34   * @param tableSettings
  35   *   Settings for the table added via drupal_add_dragtable().
  36   */
  37  Drupal.tableDrag = function(table, tableSettings) {
  38    var self = this;
  39  
  40    // Required object variables.
  41    this.table = table;
  42    this.tableSettings = tableSettings;
  43    this.dragObject = null; // Used to hold information about a current drag operation.
  44    this.rowObject = null; // Provides operations for row manipulation.
  45    this.oldRowElement = null; // Remember the previous element.
  46    this.oldY = 0; // Used to determine up or down direction from last mouse move.
  47    this.changed = false; // Whether anything in the entire table has changed.
  48    this.maxDepth = 0; // Maximum amount of allowed parenting.
  49    this.rtl = $(this.table).css('direction') == 'rtl' ? -1 : 1; // Direction of the table.
  50  
  51    // Configure the scroll settings.
  52    this.scrollSettings = { amount: 4, interval: 50, trigger: 70 };
  53    this.scrollInterval = null;
  54    this.scrollY = 0;
  55    this.windowHeight = 0;
  56  
  57    // Check this table's settings to see if there are parent relationships in
  58    // this table. For efficiency, large sections of code can be skipped if we
  59    // don't need to track horizontal movement and indentations.
  60    this.indentEnabled = false;
  61    for (group in tableSettings) {
  62      for (n in tableSettings[group]) {
  63        if (tableSettings[group][n]['relationship'] == 'parent') {
  64          this.indentEnabled = true;
  65        }
  66        if (tableSettings[group][n]['limit'] > 0) {
  67          this.maxDepth = tableSettings[group][n]['limit'];
  68        }
  69      }
  70    }
  71    if (this.indentEnabled) {
  72      this.indentCount = 1; // Total width of indents, set in makeDraggable.
  73      // Find the width of indentations to measure mouse movements against.
  74      // Because the table doesn't need to start with any indentations, we
  75      // manually append 2 indentations in the first draggable row, measure
  76      // the offset, then remove.
  77      var indent = Drupal.theme('tableDragIndentation');
  78      // Match immediate children of the parent element to allow nesting.
  79      var testCell = $('> tbody > tr.draggable:first td:first, > tr.draggable:first td:first', table).prepend(indent).prepend(indent);
  80      this.indentAmount = $('.indentation', testCell).get(1).offsetLeft - $('.indentation', testCell).get(0).offsetLeft;
  81      $('.indentation', testCell).slice(0, 2).remove();
  82    }
  83  
  84    // Make each applicable row draggable.
  85    // Match immediate children of the parent element to allow nesting.
  86    $('> tr.draggable, > tbody > tr.draggable', table).each(function() { self.makeDraggable(this); });
  87  
  88    // Hide columns containing affected form elements.
  89    this.hideColumns();
  90  
  91    // Add mouse bindings to the document. The self variable is passed along
  92    // as event handlers do not have direct access to the tableDrag object.
  93    $(document).bind('mousemove', function(event) { return self.dragRow(event, self); });
  94    $(document).bind('mouseup', function(event) { return self.dropRow(event, self); });
  95  };
  96  
  97  /**
  98   * Hide the columns containing form elements according to the settings for
  99   * this tableDrag instance.
 100   */
 101  Drupal.tableDrag.prototype.hideColumns = function(){
 102    for (var group in this.tableSettings) {
 103      // Find the first field in this group.
 104      for (var d in this.tableSettings[group]) {
 105        var field = $('.' + this.tableSettings[group][d]['target'] + ':first', this.table);
 106        if (field.size() && this.tableSettings[group][d]['hidden']) {
 107          var hidden = this.tableSettings[group][d]['hidden'];
 108          var cell = field.parents('td:first');
 109          break;
 110        }
 111      }
 112  
 113      // Hide the column containing this field.
 114      if (hidden && cell[0] && cell.css('display') != 'none') {
 115        // Add 1 to our indexes. The nth-child selector is 1 based, not 0 based.
 116        // Match immediate children of the parent element to allow nesting.
 117        var columnIndex = $('> td', cell.parent()).index(cell.get(0)) + 1;
 118        var headerIndex = $('> td:not(:hidden)', cell.parent()).index(cell.get(0)) + 1;
 119        $('> thead > tr, > tbody > tr, > tr', this.table).each(function(){
 120          var row = $(this);
 121          var parentTag = row.parent().get(0).tagName.toLowerCase();
 122          var index = (parentTag == 'thead') ? headerIndex : columnIndex;
 123  
 124          // Adjust the index to take into account colspans.
 125          row.children().each(function(n) {
 126            if (n < index) {
 127              index -= (this.colSpan && this.colSpan > 1) ? this.colSpan - 1 : 0;
 128            }
 129          });
 130          if (index > 0) {
 131            cell = row.children(':nth-child(' + index + ')');
 132            if (cell[0].colSpan > 1) {
 133              // If this cell has a colspan, simply reduce it.
 134              cell[0].colSpan = cell[0].colSpan - 1;
 135            }
 136            else {
 137              // Hide table body cells, but remove table header cells entirely
 138              // (Safari doesn't hide properly).
 139              parentTag == 'thead' ? cell.remove() : cell.css('display', 'none');
 140            }
 141          }
 142        });
 143      }
 144    }
 145  };
 146  
 147  /**
 148   * Find the target used within a particular row and group.
 149   */
 150  Drupal.tableDrag.prototype.rowSettings = function(group, row) {
 151    var field = $('.' + group, row);
 152    for (delta in this.tableSettings[group]) {
 153      var targetClass = this.tableSettings[group][delta]['target'];
 154      if (field.is('.' + targetClass)) {
 155        // Return a copy of the row settings.
 156        var rowSettings = new Object();
 157        for (var n in this.tableSettings[group][delta]) {
 158          rowSettings[n] = this.tableSettings[group][delta][n];
 159        }
 160        return rowSettings;
 161      }
 162    }
 163  };
 164  
 165  /**
 166   * Take an item and add event handlers to make it become draggable.
 167   */
 168  Drupal.tableDrag.prototype.makeDraggable = function(item) {
 169    var self = this;
 170  
 171    // Create the handle.
 172    var handle = $('<a href="#" class="tabledrag-handle"><div class="handle">&nbsp;</div></a>').attr('title', Drupal.t('Drag to re-order'));
 173    // Insert the handle after indentations (if any).
 174    if ($('td:first .indentation:last', item).after(handle).size()) {
 175      // Update the total width of indentation in this entire table.
 176      self.indentCount = Math.max($('.indentation', item).size(), self.indentCount);
 177    }
 178    else {
 179      $('td:first', item).prepend(handle);
 180    }
 181  
 182    // Add hover action for the handle.
 183    handle.hover(function() {
 184      self.dragObject == null ? $(this).addClass('tabledrag-handle-hover') : null;
 185    }, function() {
 186      self.dragObject == null ? $(this).removeClass('tabledrag-handle-hover') : null;
 187    });
 188  
 189    // Add the mousedown action for the handle.
 190    handle.mousedown(function(event) {
 191      // Create a new dragObject recording the event information.
 192      self.dragObject = new Object();
 193      self.dragObject.initMouseOffset = self.getMouseOffset(item, event);
 194      self.dragObject.initMouseCoords = self.mouseCoords(event);
 195      if (self.indentEnabled) {
 196        self.dragObject.indentMousePos = self.dragObject.initMouseCoords;
 197      }
 198  
 199      // If there's a lingering row object from the keyboard, remove its focus.
 200      if (self.rowObject) {
 201        $('a.tabledrag-handle', self.rowObject.element).blur();
 202      }
 203  
 204      // Create a new rowObject for manipulation of this row.
 205      self.rowObject = new self.row(item, 'mouse', self.indentEnabled, self.maxDepth, true);
 206  
 207      // Save the position of the table.
 208      self.table.topY = $(self.table).offset().top;
 209      self.table.bottomY = self.table.topY + self.table.offsetHeight;
 210  
 211      // Add classes to the handle and row.
 212      $(this).addClass('tabledrag-handle-hover');
 213      $(item).addClass('drag');
 214  
 215      // Set the document to use the move cursor during drag.
 216      $('body').addClass('drag');
 217      if (self.oldRowElement) {
 218        $(self.oldRowElement).removeClass('drag-previous');
 219      }
 220  
 221      // Hack for IE6 that flickers uncontrollably if select lists are moved.
 222      if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
 223        $('select', this.table).css('display', 'none');
 224      }
 225  
 226      // Hack for Konqueror, prevent the blur handler from firing.
 227      // Konqueror always gives links focus, even after returning false on mousedown.
 228      self.safeBlur = false;
 229  
 230      // Call optional placeholder function.
 231      self.onDrag();
 232      return false;
 233    });
 234  
 235    // Prevent the anchor tag from jumping us to the top of the page.
 236    handle.click(function() {
 237      return false;
 238    });
 239  
 240    // Similar to the hover event, add a class when the handle is focused.
 241    handle.focus(function() {
 242      $(this).addClass('tabledrag-handle-hover');
 243      self.safeBlur = true;
 244    });
 245  
 246    // Remove the handle class on blur and fire the same function as a mouseup.
 247    handle.blur(function(event) {
 248      $(this).removeClass('tabledrag-handle-hover');
 249      if (self.rowObject && self.safeBlur) {
 250        self.dropRow(event, self);
 251      }
 252    });
 253  
 254    // Add arrow-key support to the handle.
 255    handle.keydown(function(event) {
 256      // If a rowObject doesn't yet exist and this isn't the tab key.
 257      if (event.keyCode != 9 && !self.rowObject) {
 258        self.rowObject = new self.row(item, 'keyboard', self.indentEnabled, self.maxDepth, true);
 259      }
 260  
 261      var keyChange = false;
 262      switch (event.keyCode) {
 263        case 37: // Left arrow.
 264        case 63234: // Safari left arrow.
 265          keyChange = true;
 266          self.rowObject.indent(-1 * self.rtl);
 267          break;
 268        case 38: // Up arrow.
 269        case 63232: // Safari up arrow.
 270          var previousRow = $(self.rowObject.element).prev('tr').get(0);
 271          while (previousRow && $(previousRow).is(':hidden')) {
 272            previousRow = $(previousRow).prev('tr').get(0);
 273          }
 274          if (previousRow) {
 275            self.safeBlur = false; // Do not allow the onBlur cleanup.
 276            self.rowObject.direction = 'up';
 277            keyChange = true;
 278  
 279            if ($(item).is('.tabledrag-root')) {
 280              // Swap with the previous top-level row..
 281              var groupHeight = 0;
 282              while (previousRow && $('.indentation', previousRow).size()) {
 283                previousRow = $(previousRow).prev('tr').get(0);
 284                groupHeight += $(previousRow).is(':hidden') ? 0 : previousRow.offsetHeight;
 285              }
 286              if (previousRow) {
 287                self.rowObject.swap('before', previousRow);
 288                // No need to check for indentation, 0 is the only valid one.
 289                window.scrollBy(0, -groupHeight);
 290              }
 291            }
 292            else if (self.table.tBodies[0].rows[0] != previousRow || $(previousRow).is('.draggable')) {
 293              // Swap with the previous row (unless previous row is the first one
 294              // and undraggable).
 295              self.rowObject.swap('before', previousRow);
 296              self.rowObject.interval = null;
 297              self.rowObject.indent(0);
 298              window.scrollBy(0, -parseInt(item.offsetHeight));
 299            }
 300            handle.get(0).focus(); // Regain focus after the DOM manipulation.
 301          }
 302          break;
 303        case 39: // Right arrow.
 304        case 63235: // Safari right arrow.
 305          keyChange = true;
 306          self.rowObject.indent(1 * self.rtl);
 307          break;
 308        case 40: // Down arrow.
 309        case 63233: // Safari down arrow.
 310          var nextRow = $(self.rowObject.group).filter(':last').next('tr').get(0);
 311          while (nextRow && $(nextRow).is(':hidden')) {
 312            nextRow = $(nextRow).next('tr').get(0);
 313          }
 314          if (nextRow) {
 315            self.safeBlur = false; // Do not allow the onBlur cleanup.
 316            self.rowObject.direction = 'down';
 317            keyChange = true;
 318  
 319            if ($(item).is('.tabledrag-root')) {
 320              // Swap with the next group (necessarily a top-level one).
 321              var groupHeight = 0;
 322              nextGroup = new self.row(nextRow, 'keyboard', self.indentEnabled, self.maxDepth, false);
 323              if (nextGroup) {
 324                $(nextGroup.group).each(function () {groupHeight += $(this).is(':hidden') ? 0 : this.offsetHeight});
 325                nextGroupRow = $(nextGroup.group).filter(':last').get(0);
 326                self.rowObject.swap('after', nextGroupRow);
 327                // No need to check for indentation, 0 is the only valid one.
 328                window.scrollBy(0, parseInt(groupHeight));
 329              }
 330            }
 331            else {
 332              // Swap with the next row.
 333              self.rowObject.swap('after', nextRow);
 334              self.rowObject.interval = null;
 335              self.rowObject.indent(0);
 336              window.scrollBy(0, parseInt(item.offsetHeight));
 337            }
 338            handle.get(0).focus(); // Regain focus after the DOM manipulation.
 339          }
 340          break;
 341      }
 342  
 343      if (self.rowObject && self.rowObject.changed == true) {
 344        $(item).addClass('drag');
 345        if (self.oldRowElement) {
 346          $(self.oldRowElement).removeClass('drag-previous');
 347        }
 348        self.oldRowElement = item;
 349        self.restripeTable();
 350        self.onDrag();
 351      }
 352  
 353      // Returning false if we have an arrow key to prevent scrolling.
 354      if (keyChange) {
 355        return false;
 356      }
 357    });
 358  
 359    // Compatibility addition, return false on keypress to prevent unwanted scrolling.
 360    // IE and Safari will supress scrolling on keydown, but all other browsers
 361    // need to return false on keypress. http://www.quirksmode.org/js/keys.html
 362    handle.keypress(function(event) {
 363      switch (event.keyCode) {
 364        case 37: // Left arrow.
 365        case 38: // Up arrow.
 366        case 39: // Right arrow.
 367        case 40: // Down arrow.
 368          return false;
 369      }
 370    });
 371  };
 372  
 373  /**
 374   * Mousemove event handler, bound to document.
 375   */
 376  Drupal.tableDrag.prototype.dragRow = function(event, self) {
 377    if (self.dragObject) {
 378      self.currentMouseCoords = self.mouseCoords(event);
 379  
 380      var y = self.currentMouseCoords.y - self.dragObject.initMouseOffset.y;
 381      var x = self.currentMouseCoords.x - self.dragObject.initMouseOffset.x;
 382  
 383      // Check for row swapping and vertical scrolling.
 384      if (y != self.oldY) {
 385        self.rowObject.direction = y > self.oldY ? 'down' : 'up';
 386        self.oldY = y; // Update the old value.
 387  
 388        // Check if the window should be scrolled (and how fast).
 389        var scrollAmount = self.checkScroll(self.currentMouseCoords.y);
 390        // Stop any current scrolling.
 391        clearInterval(self.scrollInterval);
 392        // Continue scrolling if the mouse has moved in the scroll direction.
 393        if (scrollAmount > 0 && self.rowObject.direction == 'down' || scrollAmount < 0 && self.rowObject.direction == 'up') {
 394          self.setScroll(scrollAmount);
 395        }
 396  
 397        // If we have a valid target, perform the swap and restripe the table.
 398        var currentRow = self.findDropTargetRow(x, y);
 399        if (currentRow) {
 400          if (self.rowObject.direction == 'down') {
 401            self.rowObject.swap('after', currentRow, self);
 402          }
 403          else {
 404            self.rowObject.swap('before', currentRow, self);
 405          }
 406          self.restripeTable();
 407        }
 408      }
 409  
 410      // Similar to row swapping, handle indentations.
 411      if (self.indentEnabled) {
 412        var xDiff = self.currentMouseCoords.x - self.dragObject.indentMousePos.x;
 413        // Set the number of indentations the mouse has been moved left or right.
 414        var indentDiff = Math.round(xDiff / self.indentAmount * self.rtl);
 415        // Indent the row with our estimated diff, which may be further
 416        // restricted according to the rows around this row.
 417        var indentChange = self.rowObject.indent(indentDiff);
 418        // Update table and mouse indentations.
 419        self.dragObject.indentMousePos.x += self.indentAmount * indentChange * self.rtl;
 420        self.indentCount = Math.max(self.indentCount, self.rowObject.indents);
 421      }
 422  
 423      return false;
 424    }
 425  };
 426  
 427  /**
 428   * Mouseup event handler, bound to document.
 429   * Blur event handler, bound to drag handle for keyboard support.
 430   */
 431  Drupal.tableDrag.prototype.dropRow = function(event, self) {
 432    // Drop row functionality shared between mouseup and blur events.
 433    if (self.rowObject != null) {
 434      var droppedRow = self.rowObject.element;
 435      // The row is already in the right place so we just release it.
 436      if (self.rowObject.changed == true) {
 437        // Update the fields in the dropped row.
 438        self.updateFields(droppedRow);
 439  
 440        // If a setting exists for affecting the entire group, update all the
 441        // fields in the entire dragged group.
 442        for (var group in self.tableSettings) {
 443          var rowSettings = self.rowSettings(group, droppedRow);
 444          if (rowSettings.relationship == 'group') {
 445            for (n in self.rowObject.children) {
 446              self.updateField(self.rowObject.children[n], group);
 447            }
 448          }
 449        }
 450  
 451        self.rowObject.markChanged();
 452        if (self.changed == false) {
 453          $(Drupal.theme('tableDragChangedWarning')).insertAfter(self.table).hide().fadeIn('slow');
 454          self.changed = true;
 455        }
 456      }
 457  
 458      if (self.indentEnabled) {
 459        self.rowObject.removeIndentClasses();
 460      }
 461      if (self.oldRowElement) {
 462        $(self.oldRowElement).removeClass('drag-previous');
 463      }
 464      $(droppedRow).removeClass('drag').addClass('drag-previous');
 465      self.oldRowElement = droppedRow;
 466      self.onDrop();
 467      self.rowObject = null;
 468    }
 469  
 470    // Functionality specific only to mouseup event.
 471    if (self.dragObject != null) {
 472      $('.tabledrag-handle', droppedRow).removeClass('tabledrag-handle-hover');
 473  
 474      self.dragObject = null;
 475      $('body').removeClass('drag');
 476      clearInterval(self.scrollInterval);
 477  
 478      // Hack for IE6 that flickers uncontrollably if select lists are moved.
 479      if (navigator.userAgent.indexOf('MSIE 6.') != -1) {
 480        $('select', this.table).css('display', 'block');
 481      }
 482    }
 483  };
 484  
 485  /**
 486   * Get the mouse coordinates from the event (allowing for browser differences).
 487   */
 488  Drupal.tableDrag.prototype.mouseCoords = function(event){
 489    if (event.pageX || event.pageY) {
 490      return {x:event.pageX, y:event.pageY};
 491    }
 492    return {
 493      x:event.clientX + document.body.scrollLeft - document.body.clientLeft,
 494      y:event.clientY + document.body.scrollTop  - document.body.clientTop
 495    };
 496  };
 497  
 498  /**
 499   * Given a target element and a mouse event, get the mouse offset from that
 500   * element. To do this we need the element's position and the mouse position.
 501   */
 502  Drupal.tableDrag.prototype.getMouseOffset = function(target, event) {
 503    var docPos   = $(target).offset();
 504    var mousePos = this.mouseCoords(event);
 505    return { x: mousePos.x - docPos.left, y: mousePos.y - docPos.top };
 506  };
 507  
 508  /**
 509   * Find the row the mouse is currently over. This row is then taken and swapped
 510   * with the one being dragged.
 511   *
 512   * @param x
 513   *   The x coordinate of the mouse on the page (not the screen).
 514   * @param y
 515   *   The y coordinate of the mouse on the page (not the screen).
 516   */
 517  Drupal.tableDrag.prototype.findDropTargetRow = function(x, y) {
 518    var rows = this.table.tBodies[0].rows;
 519    for (var n=0; n<rows.length; n++) {
 520      var row = rows[n];
 521      var indentDiff = 0;
 522      var rowY = $(row).offset().top;
 523      // Because Safari does not report offsetHeight on table rows, but does on table
 524      // cells, grab the firstChild of the row and use that instead.
 525      // http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari
 526      if (row.offsetHeight == 0) {
 527        var rowHeight = parseInt(row.firstChild.offsetHeight)/2;
 528      }
 529      // Other browsers.
 530      else {
 531        var rowHeight = parseInt(row.offsetHeight)/2;
 532      }
 533  
 534      // Because we always insert before, we need to offset the height a bit.
 535      if ((y > (rowY - rowHeight)) && (y < (rowY + rowHeight))) {
 536        if (this.indentEnabled) {
 537          // Check that this row is not a child of the row being dragged.
 538          for (n in this.rowObject.group) {
 539            if (this.rowObject.group[n] == row) {
 540              return null;
 541            }
 542          }
 543        }
 544        else {
 545          // Do not allow a row to be swapped with itself.
 546          if (row == this.rowObject.element) {
 547            return null;
 548          }
 549        }
 550  
 551        // Check that swapping with this row is allowed.
 552        if (!this.rowObject.isValidSwap(row)) {
 553          return null;
 554        }
 555  
 556        // We may have found the row the mouse just passed over, but it doesn't
 557        // take into account hidden rows. Skip backwards until we find a draggable
 558        // row.
 559        while ($(row).is(':hidden') && $(row).prev('tr').is(':hidden')) {
 560          row = $(row).prev('tr').get(0);
 561        }
 562        return row;
 563      }
 564    }
 565    return null;
 566  };
 567  
 568  /**
 569   * After the row is dropped, update the table fields according to the settings
 570   * set for this table.
 571   *
 572   * @param changedRow
 573   *   DOM object for the row that was just dropped.
 574   */
 575  Drupal.tableDrag.prototype.updateFields = function(changedRow) {
 576    for (var group in this.tableSettings) {
 577      // Each group may have a different setting for relationship, so we find
 578      // the source rows for each seperately.
 579      this.updateField(changedRow, group);
 580    }
 581  };
 582  
 583  /**
 584   * After the row is dropped, update a single table field according to specific
 585   * settings.
 586   *
 587   * @param changedRow
 588   *   DOM object for the row that was just dropped.
 589   * @param group
 590   *   The settings group on which field updates will occur.
 591   */
 592  Drupal.tableDrag.prototype.updateField = function(changedRow, group) {
 593    var rowSettings = this.rowSettings(group, changedRow);
 594  
 595    // Set the row as it's own target.
 596    if (rowSettings.relationship == 'self' || rowSettings.relationship == 'group') {
 597      var sourceRow = changedRow;
 598    }
 599    // Siblings are easy, check previous and next rows.
 600    else if (rowSettings.relationship == 'sibling') {
 601      var previousRow = $(changedRow).prev('tr').get(0);
 602      var nextRow = $(changedRow).next('tr').get(0);
 603      var sourceRow = changedRow;
 604      if ($(previousRow).is('.draggable') && $('.' + group, previousRow).length) {
 605        if (this.indentEnabled) {
 606          if ($('.indentations', previousRow).size() == $('.indentations', changedRow)) {
 607            sourceRow = previousRow;
 608          }
 609        }
 610        else {
 611          sourceRow = previousRow;
 612        }
 613      }
 614      else if ($(nextRow).is('.draggable') && $('.' + group, nextRow).length) {
 615        if (this.indentEnabled) {
 616          if ($('.indentations', nextRow).size() == $('.indentations', changedRow)) {
 617            sourceRow = nextRow;
 618          }
 619        }
 620        else {
 621          sourceRow = nextRow;
 622        }
 623      }
 624    }
 625    // Parents, look up the tree until we find a field not in this group.
 626    // Go up as many parents as indentations in the changed row.
 627    else if (rowSettings.relationship == 'parent') {
 628      var previousRow = $(changedRow).prev('tr');
 629      while (previousRow.length && $('.indentation', previousRow).length >= this.rowObject.indents) {
 630        previousRow = previousRow.prev('tr');
 631      }
 632      // If we found a row.
 633      if (previousRow.length) {
 634        sourceRow = previousRow[0];
 635      }
 636      // Otherwise we went all the way to the left of the table without finding
 637      // a parent, meaning this item has been placed at the root level.
 638      else {
 639        // Use the first row in the table as source, because it's garanteed to
 640        // be at the root level. Find the first item, then compare this row
 641        // against it as a sibling.
 642        sourceRow = $('tr.draggable:first').get(0);
 643        if (sourceRow == this.rowObject.element) {
 644          sourceRow = $(this.rowObject.group[this.rowObject.group.length - 1]).next('tr.draggable').get(0);
 645        }
 646        var useSibling = true;
 647      }
 648    }
 649  
 650    // Because we may have moved the row from one category to another,
 651    // take a look at our sibling and borrow its sources and targets.
 652    this.copyDragClasses(sourceRow, changedRow, group);
 653    rowSettings = this.rowSettings(group, changedRow);
 654  
 655    // In the case that we're looking for a parent, but the row is at the top
 656    // of the tree, copy our sibling's values.
 657    if (useSibling) {
 658      rowSettings.relationship = 'sibling';
 659      rowSettings.source = rowSettings.target;
 660    }
 661  
 662    var targetClass = '.' + rowSettings.target;
 663    var targetElement = $(targetClass, changedRow).get(0);
 664  
 665    // Check if a target element exists in this row.
 666    if (targetElement) {
 667      var sourceClass = '.' + rowSettings.source;
 668      var sourceElement = $(sourceClass, sourceRow).get(0);
 669      switch (rowSettings.action) {
 670        case 'depth':
 671          // Get the depth of the target row.
 672          targetElement.value = $('.indentation', $(sourceElement).parents('tr:first')).size();
 673          break;
 674        case 'match':
 675          // Update the value.
 676          targetElement.value = sourceElement.value;
 677          break;
 678        case 'order':
 679          var siblings = this.rowObject.findSiblings(rowSettings);
 680          if ($(targetElement).is('select')) {
 681            // Get a list of acceptable values.
 682            var values = new Array();
 683            $('option', targetElement).each(function() {
 684              values.push(this.value);
 685            });
 686            var maxVal = values[values.length - 1];
 687            // Populate the values in the siblings.
 688            $(targetClass, siblings).each(function() {
 689              // If there are more items than possible values, assign the maximum value to the row. 
 690              if (values.length > 0) {
 691                this.value = values.shift();
 692              }
 693              else {
 694                this.value = maxVal;
 695              }
 696            });
 697          }
 698          else {
 699            // Assume a numeric input field.
 700            var weight = parseInt($(targetClass, siblings[0]).val()) || 0;
 701            $(targetClass, siblings).each(function() {
 702              this.value = weight;
 703              weight++;
 704            });
 705          }
 706          break;
 707      }
 708    }
 709  };
 710  
 711  /**
 712   * Copy all special tableDrag classes from one row's form elements to a
 713   * different one, removing any special classes that the destination row
 714   * may have had.
 715   */
 716  Drupal.tableDrag.prototype.copyDragClasses = function(sourceRow, targetRow, group) {
 717    var sourceElement = $('.' + group, sourceRow);
 718    var targetElement = $('.' + group, targetRow);
 719    if (sourceElement.length && targetElement.length) {
 720      targetElement[0].className = sourceElement[0].className;
 721    }
 722  };
 723  
 724  Drupal.tableDrag.prototype.checkScroll = function(cursorY) {
 725    var de  = document.documentElement;
 726    var b  = document.body;
 727  
 728    var windowHeight = this.windowHeight = window.innerHeight || (de.clientHeight && de.clientWidth != 0 ? de.clientHeight : b.offsetHeight);
 729    var scrollY = this.scrollY = (document.all ? (!de.scrollTop ? b.scrollTop : de.scrollTop) : (window.pageYOffset ? window.pageYOffset : window.scrollY));
 730    var trigger = this.scrollSettings.trigger;
 731    var delta = 0;
 732  
 733    // Return a scroll speed relative to the edge of the screen.
 734    if (cursorY - scrollY > windowHeight - trigger) {
 735      delta = trigger / (windowHeight + scrollY - cursorY);
 736      delta = (delta > 0 && delta < trigger) ? delta : trigger;
 737      return delta * this.scrollSettings.amount;
 738    }
 739    else if (cursorY - scrollY < trigger) {
 740      delta = trigger / (cursorY - scrollY);
 741      delta = (delta > 0 && delta < trigger) ? delta : trigger;
 742      return -delta * this.scrollSettings.amount;
 743    }
 744  };
 745  
 746  Drupal.tableDrag.prototype.setScroll = function(scrollAmount) {
 747    var self = this;
 748  
 749    this.scrollInterval = setInterval(function() {
 750      // Update the scroll values stored in the object.
 751      self.checkScroll(self.currentMouseCoords.y);
 752      var aboveTable = self.scrollY > self.table.topY;
 753      var belowTable = self.scrollY + self.windowHeight < self.table.bottomY;
 754      if (scrollAmount > 0 && belowTable || scrollAmount < 0 && aboveTable) {
 755        window.scrollBy(0, scrollAmount);
 756      }
 757    }, this.scrollSettings.interval);
 758  };
 759  
 760  Drupal.tableDrag.prototype.restripeTable = function() {
 761    // :even and :odd are reversed because jquery counts from 0 and
 762    // we count from 1, so we're out of sync.
 763    // Match immediate children of the parent element to allow nesting.
 764    $('> tbody > tr.draggable, > tr.draggable', this.table)
 765      .filter(':odd').filter('.odd')
 766        .removeClass('odd').addClass('even')
 767      .end().end()
 768      .filter(':even').filter('.even')
 769        .removeClass('even').addClass('odd');
 770  };
 771  
 772  /**
 773   * Stub function. Allows a custom handler when a row begins dragging.
 774   */
 775  Drupal.tableDrag.prototype.onDrag = function() {
 776    return null;
 777  };
 778  
 779  /**
 780   * Stub function. Allows a custom handler when a row is dropped.
 781   */
 782  Drupal.tableDrag.prototype.onDrop = function() {
 783    return null;
 784  };
 785  
 786  /**
 787   * Constructor to make a new object to manipulate a table row.
 788   *
 789   * @param tableRow
 790   *   The DOM element for the table row we will be manipulating.
 791   * @param method
 792   *   The method in which this row is being moved. Either 'keyboard' or 'mouse'.
 793   * @param indentEnabled
 794   *   Whether the containing table uses indentations. Used for optimizations.
 795   * @param maxDepth
 796   *   The maximum amount of indentations this row may contain.
 797   * @param addClasses
 798   *   Whether we want to add classes to this row to indicate child relationships.
 799   */
 800  Drupal.tableDrag.prototype.row = function(tableRow, method, indentEnabled, maxDepth, addClasses) {
 801    this.element = tableRow;
 802    this.method = method;
 803    this.group = new Array(tableRow);
 804    this.groupDepth = $('.indentation', tableRow).size();
 805    this.changed = false;
 806    this.table = $(tableRow).parents('table:first').get(0);
 807    this.indentEnabled = indentEnabled;
 808    this.maxDepth = maxDepth;
 809    this.direction = ''; // Direction the row is being moved.
 810  
 811    if (this.indentEnabled) {
 812      this.indents = $('.indentation', tableRow).size();
 813      this.children = this.findChildren(addClasses);
 814      this.group = $.merge(this.group, this.children);
 815      // Find the depth of this entire group.
 816      for (var n = 0; n < this.group.length; n++) {
 817        this.groupDepth = Math.max($('.indentation', this.group[n]).size(), this.groupDepth);
 818      }
 819    }
 820  };
 821  
 822  /**
 823   * Find all children of rowObject by indentation.
 824   *
 825   * @param addClasses
 826   *   Whether we want to add classes to this row to indicate child relationships.
 827   */
 828  Drupal.tableDrag.prototype.row.prototype.findChildren = function(addClasses) {
 829    var parentIndentation = this.indents;
 830    var currentRow = $(this.element, this.table).next('tr.draggable');
 831    var rows = new Array();
 832    var child = 0;
 833    while (currentRow.length) {
 834      var rowIndentation = $('.indentation', currentRow).length;
 835      // A greater indentation indicates this is a child.
 836      if (rowIndentation > parentIndentation) {
 837        child++;
 838        rows.push(currentRow[0]);
 839        if (addClasses) {
 840          $('.indentation', currentRow).each(function(indentNum) {
 841            if (child == 1 && (indentNum == parentIndentation)) {
 842              $(this).addClass('tree-child-first');
 843            }
 844            if (indentNum == parentIndentation) {
 845              $(this).addClass('tree-child');
 846            }
 847            else if (indentNum > parentIndentation) {
 848              $(this).addClass('tree-child-horizontal');
 849            }
 850          });
 851        }
 852      }
 853      else {
 854        break;
 855      }
 856      currentRow = currentRow.next('tr.draggable');
 857    }
 858    if (addClasses && rows.length) {
 859      $('.indentation:nth-child(' + (parentIndentation + 1) + ')', rows[rows.length - 1]).addClass('tree-child-last');
 860    }
 861    return rows;
 862  };
 863  
 864  /**
 865   * Ensure that two rows are allowed to be swapped.
 866   *
 867   * @param row
 868   *   DOM object for the row being considered for swapping.
 869   */
 870  Drupal.tableDrag.prototype.row.prototype.isValidSwap = function(row) {
 871    if (this.indentEnabled) {
 872      var prevRow, nextRow;
 873      if (this.direction == 'down') {
 874        prevRow = row;
 875        nextRow = $(row).next('tr').get(0);
 876      }
 877      else {
 878        prevRow = $(row).prev('tr').get(0);
 879        nextRow = row;
 880      }
 881      this.interval = this.validIndentInterval(prevRow, nextRow);
 882  
 883      // We have an invalid swap if the valid indentations interval is empty.
 884      if (this.interval.min > this.interval.max) {
 885        return false;
 886      }
 887    }
 888  
 889    // Do not let an un-draggable first row have anything put before it.
 890    if (this.table.tBodies[0].rows[0] == row && $(row).is(':not(.draggable)')) {
 891      return false;
 892    }
 893  
 894    return true;
 895  };
 896  
 897  /**
 898   * Perform the swap between two rows.
 899   *
 900   * @param position
 901   *   Whether the swap will occur 'before' or 'after' the given row.
 902   * @param row
 903   *   DOM element what will be swapped with the row group.
 904   */
 905  Drupal.tableDrag.prototype.row.prototype.swap = function(position, row) {
 906    $(row)[position](this.group);
 907    this.changed = true;
 908    this.onSwap(row);
 909  };
 910  
 911  /**
 912   * Determine the valid indentations interval for the row at a given position
 913   * in the table.
 914   *
 915   * @param prevRow
 916   *   DOM object for the row before the tested position
 917   *   (or null for first position in the table).
 918   * @param nextRow
 919   *   DOM object for the row after the tested position
 920   *   (or null for last position in the table).
 921   */
 922  Drupal.tableDrag.prototype.row.prototype.validIndentInterval = function (prevRow, nextRow) {
 923    var minIndent, maxIndent;
 924  
 925    // Minimum indentation:
 926    // Do not orphan the next row.
 927    minIndent = nextRow ? $('.indentation', nextRow).size() : 0;
 928  
 929    // Maximum indentation:
 930    if (!prevRow || $(this.element).is('.tabledrag-root')) {
 931      // Do not indent the first row in the table or 'root' rows..
 932      maxIndent = 0;
 933    }
 934    else {
 935      // Do not go deeper than as a child of the previous row.
 936      maxIndent = $('.indentation', prevRow).size() + ($(prevRow).is('.tabledrag-leaf') ? 0 : 1);
 937      // Limit by the maximum allowed depth for the table.
 938      if (this.maxDepth) {
 939        maxIndent = Math.min(maxIndent, this.maxDepth - (this.groupDepth - this.indents));
 940      }
 941    }
 942  
 943    return {'min':minIndent, 'max':maxIndent};
 944  }
 945  
 946  /**
 947   * Indent a row within the legal bounds of the table.
 948   *
 949   * @param indentDiff
 950   *   The number of additional indentations proposed for the row (can be
 951   *   positive or negative). This number will be adjusted to nearest valid
 952   *   indentation level for the row.
 953   */
 954  Drupal.tableDrag.prototype.row.prototype.indent = function(indentDiff) {
 955    // Determine the valid indentations interval if not available yet.
 956    if (!this.interval) {
 957      prevRow = $(this.element).prev('tr').get(0);
 958      nextRow = $(this.group).filter(':last').next('tr').get(0);
 959      this.interval = this.validIndentInterval(prevRow, nextRow);
 960    }
 961  
 962    // Adjust to the nearest valid indentation.
 963    var indent = this.indents + indentDiff;
 964    indent = Math.max(indent, this.interval.min);
 965    indent = Math.min(indent, this.interval.max);
 966    indentDiff = indent - this.indents;
 967  
 968    for (var n = 1; n <= Math.abs(indentDiff); n++) {
 969      // Add or remove indentations.
 970      if (indentDiff < 0) {
 971        $('.indentation:first', this.group).remove();
 972        this.indents--;
 973      }
 974      else {
 975        $('td:first', this.group).prepend(Drupal.theme('tableDragIndentation'));
 976        this.indents++;
 977      }
 978    }
 979    if (indentDiff) {
 980      // Update indentation for this row.
 981      this.changed = true;
 982      this.groupDepth += indentDiff;
 983      this.onIndent();
 984    }
 985  
 986    return indentDiff;
 987  };
 988  
 989  /**
 990   * Find all siblings for a row, either according to its subgroup or indentation.
 991   * Note that the passed in row is included in the list of siblings.
 992   *
 993   * @param settings
 994   *   The field settings we're using to identify what constitutes a sibling.
 995   */
 996  Drupal.tableDrag.prototype.row.prototype.findSiblings = function(rowSettings) {
 997    var siblings = new Array();
 998    var directions = new Array('prev', 'next');
 999    var rowIndentation = this.indents;
1000    for (var d in directions) {
1001      var checkRow = $(this.element)[directions[d]]();
1002      while (checkRow.length) {
1003        // Check that the sibling contains a similar target field.
1004        if ($('.' + rowSettings.target, checkRow)) {
1005          // Either add immediately if this is a flat table, or check to ensure
1006          // that this row has the same level of indentaiton.
1007          if (this.indentEnabled) {
1008            var checkRowIndentation = $('.indentation', checkRow).length
1009          }
1010  
1011          if (!(this.indentEnabled) || (checkRowIndentation == rowIndentation)) {
1012            siblings.push(checkRow[0]);
1013          }
1014          else if (checkRowIndentation < rowIndentation) {
1015            // No need to keep looking for siblings when we get to a parent.
1016            break;
1017          }
1018        }
1019        else {
1020          break;
1021        }
1022        checkRow = $(checkRow)[directions[d]]();
1023      }
1024      // Since siblings are added in reverse order for previous, reverse the
1025      // completed list of previous siblings. Add the current row and continue.
1026      if (directions[d] == 'prev') {
1027        siblings.reverse();
1028        siblings.push(this.element);
1029      }
1030    }
1031    return siblings;
1032  };
1033  
1034  /**
1035   * Remove indentation helper classes from the current row group.
1036   */
1037  Drupal.tableDrag.prototype.row.prototype.removeIndentClasses = function() {
1038    for (n in this.children) {
1039      $('.indentation', this.children[n])
1040        .removeClass('tree-child')
1041        .removeClass('tree-child-first')
1042        .removeClass('tree-child-last')
1043        .removeClass('tree-child-horizontal');
1044    }
1045  };
1046  
1047  /**
1048   * Add an asterisk or other marker to the changed row.
1049   */
1050  Drupal.tableDrag.prototype.row.prototype.markChanged = function() {
1051    var marker = Drupal.theme('tableDragChangedMarker');
1052    var cell = $('td:first', this.element);
1053    if ($('span.tabledrag-changed', cell).length == 0) {
1054      cell.append(marker);
1055    }
1056  };
1057  
1058  /**
1059   * Stub function. Allows a custom handler when a row is indented.
1060   */
1061  Drupal.tableDrag.prototype.row.prototype.onIndent = function() {
1062    return null;
1063  };
1064  
1065  /**
1066   * Stub function. Allows a custom handler when a row is swapped.
1067   */
1068  Drupal.tableDrag.prototype.row.prototype.onSwap = function(swappedRow) {
1069    return null;
1070  };
1071  
1072  Drupal.theme.prototype.tableDragChangedMarker = function () {
1073    return '<span class="warning tabledrag-changed">*</span>';
1074  };
1075  
1076  Drupal.theme.prototype.tableDragIndentation = function () {
1077    return '<div class="indentation">&nbsp;</div>';
1078  };
1079  
1080  Drupal.theme.prototype.tableDragChangedWarning = function () {
1081    return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("Changes made in this table will not be saved until the form is submitted.") + '</div>';
1082  };


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7