| 1 |
// $Id: block.js,v 1.10 2009/08/04 06:26:52 webchick Exp $
|
| 2 |
(function ($) {
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Move a block in the blocks table from one region to another via select list.
|
| 6 |
*
|
| 7 |
* This behavior is dependent on the tableDrag behavior, since it uses the
|
| 8 |
* objects initialized in that behavior to update the row.
|
| 9 |
*/
|
| 10 |
Drupal.behaviors.blockDrag = {
|
| 11 |
attach: function (context, settings) {
|
| 12 |
var table = $('table#blocks');
|
| 13 |
var tableDrag = Drupal.tableDrag.blocks; // Get the blocks tableDrag object.
|
| 14 |
|
| 15 |
// Add a handler for when a row is swapped, update empty regions.
|
| 16 |
tableDrag.row.prototype.onSwap = function (swappedRow) {
|
| 17 |
checkEmptyRegions(table, this);
|
| 18 |
};
|
| 19 |
|
| 20 |
// A custom message for the blocks page specifically.
|
| 21 |
Drupal.theme.tableDragChangedWarning = function () {
|
| 22 |
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>';
|
| 23 |
};
|
| 24 |
|
| 25 |
// Add a handler so when a row is dropped, update fields dropped into new regions.
|
| 26 |
tableDrag.onDrop = function () {
|
| 27 |
dragObject = this;
|
| 28 |
// Use "region-message" row instead of "region" row because
|
| 29 |
// "region-{region_name}-message" is less prone to regexp match errors.
|
| 30 |
var regionRow = $(dragObject.rowObject.element).prevAll('tr.region-message').get(0);
|
| 31 |
var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
|
| 32 |
var regionField = $('select.block-region-select', dragObject.rowObject.element);
|
| 33 |
// Check whether the newly picked region is available for this block.
|
| 34 |
if ($('option[value=' + regionName + ']', regionField).length == 0) {
|
| 35 |
// If not, alert the user and keep the block in its old region setting.
|
| 36 |
alert(Drupal.t('The block cannot be placed in this region.'));
|
| 37 |
// Simulate that there was a selected element change, so the row is put
|
| 38 |
// back to from where the user tried to drag it.
|
| 39 |
regionField.change();
|
| 40 |
}
|
| 41 |
else if ($(dragObject.rowObject.element).prev('tr').is('.region-message')) {
|
| 42 |
var weightField = $('select.block-weight', dragObject.rowObject.element);
|
| 43 |
var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*block-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
|
| 44 |
|
| 45 |
if (!regionField.is('.block-region-' + regionName)) {
|
| 46 |
regionField.removeClass('block-region-' + oldRegionName).addClass('block-region-' + regionName);
|
| 47 |
weightField.removeClass('block-weight-' + oldRegionName).addClass('block-weight-' + regionName);
|
| 48 |
regionField.val(regionName);
|
| 49 |
}
|
| 50 |
}
|
| 51 |
};
|
| 52 |
|
| 53 |
// Add the behavior to each region select list.
|
| 54 |
$('select.block-region-select', context).once('block-region-select', function () {
|
| 55 |
$(this).change(function (event) {
|
| 56 |
// Make our new row and select field.
|
| 57 |
var row = $(this).parents('tr:first');
|
| 58 |
var select = $(this);
|
| 59 |
tableDrag.rowObject = new tableDrag.row(row);
|
| 60 |
|
| 61 |
// Find the correct region and insert the row as the first in the region.
|
| 62 |
$('tr.region-message', table).each(function () {
|
| 63 |
if ($(this).is('.region-' + select[0].value + '-message')) {
|
| 64 |
// Add the new row and remove the old one.
|
| 65 |
$(this).after(row);
|
| 66 |
// Manually update weights and restripe.
|
| 67 |
tableDrag.updateFields(row.get(0));
|
| 68 |
tableDrag.rowObject.changed = true;
|
| 69 |
if (tableDrag.oldRowElement) {
|
| 70 |
$(tableDrag.oldRowElement).removeClass('drag-previous');
|
| 71 |
}
|
| 72 |
tableDrag.oldRowElement = row.get(0);
|
| 73 |
tableDrag.restripeTable();
|
| 74 |
tableDrag.rowObject.markChanged();
|
| 75 |
tableDrag.oldRowElement = row;
|
| 76 |
$(row).addClass('drag-previous');
|
| 77 |
}
|
| 78 |
});
|
| 79 |
|
| 80 |
// Modify empty regions with added or removed fields.
|
| 81 |
checkEmptyRegions(table, row);
|
| 82 |
// Remove focus from selectbox.
|
| 83 |
select.get(0).blur();
|
| 84 |
});
|
| 85 |
});
|
| 86 |
|
| 87 |
var checkEmptyRegions = function (table, rowObject) {
|
| 88 |
$('tr.region-message', table).each(function () {
|
| 89 |
// If the dragged row is in this region, but above the message row, swap it down one space.
|
| 90 |
if ($(this).prev('tr').get(0) == rowObject.element) {
|
| 91 |
// Prevent a recursion problem when using the keyboard to move rows up.
|
| 92 |
if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
|
| 93 |
rowObject.swap('after', this);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
// This region has become empty.
|
| 97 |
if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').size() == 0) {
|
| 98 |
$(this).removeClass('region-populated').addClass('region-empty');
|
| 99 |
}
|
| 100 |
// This region has become populated.
|
| 101 |
else if ($(this).is('.region-empty')) {
|
| 102 |
$(this).removeClass('region-empty').addClass('region-populated');
|
| 103 |
}
|
| 104 |
});
|
| 105 |
};
|
| 106 |
}
|
| 107 |
};
|
| 108 |
|
| 109 |
})(jQuery);
|