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