web/drupal/modules/block/block.js
branchdrupal
changeset 74 0ff3ba646492
equal deleted inserted replaced
73:fcf75e232c5b 74:0ff3ba646492
       
     1 // $Id: block.js,v 1.2 2007/12/16 10:36:53 goba Exp $
       
     2 
       
     3 /**
       
     4  * Move a block in the blocks table from one region to another via select list.
       
     5  *
       
     6  * This behavior is dependent on the tableDrag behavior, since it uses the
       
     7  * objects initialized in that behavior to update the row.
       
     8  */
       
     9 Drupal.behaviors.blockDrag = function(context) {
       
    10   var table = $('table#blocks');
       
    11   var tableDrag = Drupal.tableDrag.blocks; // Get the blocks tableDrag object.
       
    12 
       
    13   // Add a handler for when a row is swapped, update empty regions.
       
    14   tableDrag.row.prototype.onSwap = function(swappedRow) {
       
    15     checkEmptyRegions(table, this);
       
    16   };
       
    17 
       
    18   // A custom message for the blocks page specifically.
       
    19   Drupal.theme.tableDragChangedWarning = function () {
       
    20     return '<div class="warning">' + Drupal.theme('tableDragChangedMarker') + ' ' + Drupal.t("The changes to these blocks will not be saved until the <em>Save blocks</em> button is clicked.") + '</div>';
       
    21   };
       
    22 
       
    23   // Add a handler so when a row is dropped, update fields dropped into new regions.
       
    24   tableDrag.onDrop = function() {
       
    25     dragObject = this;
       
    26     if ($(dragObject.rowObject.element).prev('tr').is('.region-message')) {
       
    27       var regionRow = $(dragObject.rowObject.element).prev('tr').get(0);
       
    28       var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
       
    29       var regionField = $('select.block-region-select', dragObject.rowObject.element);
       
    30       var weightField = $('select.block-weight', dragObject.rowObject.element);
       
    31       var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*block-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
       
    32 
       
    33       if (!regionField.is('.block-region-'+ regionName)) {
       
    34         regionField.removeClass('block-region-' + oldRegionName).addClass('block-region-' + regionName);
       
    35         weightField.removeClass('block-weight-' + oldRegionName).addClass('block-weight-' + regionName);
       
    36         regionField.val(regionName);
       
    37       }
       
    38     }
       
    39   };
       
    40 
       
    41   // Add the behavior to each region select list.
       
    42   $('select.block-region-select:not(.blockregionselect-processed)', context).each(function() {
       
    43     $(this).change(function(event) {
       
    44       // Make our new row and select field.
       
    45       var row = $(this).parents('tr:first');
       
    46       var select = $(this);
       
    47       tableDrag.rowObject = new tableDrag.row(row);
       
    48 
       
    49       // Find the correct region and insert the row as the first in the region.
       
    50       $('tr.region-message', table).each(function() {
       
    51         if ($(this).is('.region-' + select[0].value + '-message')) {
       
    52           // Add the new row and remove the old one.
       
    53           $(this).after(row);
       
    54           // Manually update weights and restripe.
       
    55           tableDrag.updateFields(row.get(0));
       
    56           tableDrag.rowObject.changed = true;
       
    57           if (tableDrag.oldRowElement) {
       
    58             $(tableDrag.oldRowElement).removeClass('drag-previous');
       
    59           }
       
    60           tableDrag.oldRowElement = row.get(0);
       
    61           tableDrag.restripeTable();
       
    62           tableDrag.rowObject.markChanged();
       
    63           tableDrag.oldRowElement = row;
       
    64           $(row).addClass('drag-previous');
       
    65         }
       
    66       });
       
    67 
       
    68       // Modify empty regions with added or removed fields.
       
    69       checkEmptyRegions(table, row);
       
    70       // Remove focus from selectbox.
       
    71       select.get(0).blur();
       
    72     });
       
    73     $(this).addClass('blockregionselect-processed');
       
    74   });
       
    75 
       
    76   var checkEmptyRegions = function(table, rowObject) {
       
    77     $('tr.region-message', table).each(function() {
       
    78       // If the dragged row is in this region, but above the message row, swap it down one space.
       
    79       if ($(this).prev('tr').get(0) == rowObject.element) {
       
    80         // Prevent a recursion problem when using the keyboard to move rows up.
       
    81         if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
       
    82           rowObject.swap('after', this);
       
    83         }
       
    84       }
       
    85       // This region has become empty
       
    86       if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').size() == 0) {
       
    87         $(this).removeClass('region-populated').addClass('region-empty');
       
    88       }
       
    89       // This region has become populated.
       
    90       else if ($(this).is('.region-empty')) {
       
    91         $(this).removeClass('region-empty').addClass('region-populated');
       
    92       }
       
    93     });
       
    94   };
       
    95 };