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