web/drupal/modules/profile/profile.js
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 // $Id: profile.js,v 1.2 2007/12/08 14:06:22 goba Exp $
       
     2 
       
     3 /**
       
     4  * Add functionality to the profile drag and drop table.
       
     5  *
       
     6  * This behavior is dependent on the tableDrag behavior, since it uses the
       
     7  * objects initialized in that behavior to update the row. It shows and hides
       
     8  * a warning message when removing the last field from a profile category.
       
     9  */
       
    10 Drupal.behaviors.profileDrag = function(context) {
       
    11   var table = $('#profile-fields');
       
    12   var tableDrag = Drupal.tableDrag['profile-fields']; // Get the profile tableDrag object.
       
    13 
       
    14   // Add a handler for when a row is swapped, update empty categories.
       
    15   tableDrag.row.prototype.onSwap = function(swappedRow) {
       
    16     var rowObject = this;
       
    17     $('tr.category-message', table).each(function() {
       
    18       // If the dragged row is in this category, but above the message row, swap it down one space.
       
    19       if ($(this).prev('tr').get(0) == rowObject.element) {
       
    20         // Prevent a recursion problem when using the keyboard to move rows up.
       
    21         if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
       
    22           rowObject.swap('after', this);
       
    23         }
       
    24       }
       
    25       // This category has become empty
       
    26       if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').size() == 0) {
       
    27         $(this).removeClass('category-populated').addClass('category-empty');
       
    28       }
       
    29       // This category has become populated.
       
    30       else if ($(this).is('.category-empty')) {
       
    31         $(this).removeClass('category-empty').addClass('category-populated');
       
    32       }
       
    33     });
       
    34   };
       
    35 
       
    36   // Add a handler so when a row is dropped, update fields dropped into new categories.
       
    37   tableDrag.onDrop = function() {
       
    38     dragObject = this;
       
    39     if ($(dragObject.rowObject.element).prev('tr').is('.category-message')) {
       
    40       var categoryRow = $(dragObject.rowObject.element).prev('tr').get(0);
       
    41       var categoryNum = categoryRow.className.replace(/([^ ]+[ ]+)*category-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
       
    42       var categoryField = $('select.profile-category', dragObject.rowObject.element);
       
    43       var weightField = $('select.profile-weight', dragObject.rowObject.element);
       
    44       var oldcategoryNum = weightField[0].className.replace(/([^ ]+[ ]+)*profile-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
       
    45 
       
    46       if (!categoryField.is('.profile-category-'+ categoryNum)) {
       
    47         categoryField.removeClass('profile-category-' + oldcategoryNum).addClass('profile-category-' + categoryNum);
       
    48         weightField.removeClass('profile-weight-' + oldcategoryNum).addClass('profile-weight-' + categoryNum);
       
    49 
       
    50         categoryField.val(categoryField[0].options[categoryNum].value);
       
    51       }
       
    52     }
       
    53   };
       
    54 };