block.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. (function ($) {
  2. /**
  3. * Provide the summary information for the block settings vertical tabs.
  4. */
  5. Drupal.behaviors.blockSettingsSummary = {
  6. attach: function (context) {
  7. // The drupalSetSummary method required for this behavior is not available
  8. // on the Blocks administration page, so we need to make sure this
  9. // behavior is processed only if drupalSetSummary is defined.
  10. if (typeof jQuery.fn.drupalSetSummary == 'undefined') {
  11. return;
  12. }
  13. $('fieldset#edit-path', context).drupalSetSummary(function (context) {
  14. if (!$('textarea[name="pages"]', context).val()) {
  15. return Drupal.t('Not restricted');
  16. }
  17. else {
  18. return Drupal.t('Restricted to certain pages');
  19. }
  20. });
  21. $('fieldset#edit-node-type', context).drupalSetSummary(function (context) {
  22. var vals = [];
  23. $('input[type="checkbox"]:checked', context).each(function () {
  24. vals.push($.trim($(this).next('label').text()));
  25. });
  26. if (!vals.length) {
  27. vals.push(Drupal.t('Not restricted'));
  28. }
  29. return vals.join(', ');
  30. });
  31. $('fieldset#edit-role', context).drupalSetSummary(function (context) {
  32. var vals = [];
  33. $('input[type="checkbox"]:checked', context).each(function () {
  34. vals.push($.trim($(this).next('label').text()));
  35. });
  36. if (!vals.length) {
  37. vals.push(Drupal.t('Not restricted'));
  38. }
  39. return vals.join(', ');
  40. });
  41. $('fieldset#edit-user', context).drupalSetSummary(function (context) {
  42. var $radio = $('input[name="custom"]:checked', context);
  43. if ($radio.val() == 0) {
  44. return Drupal.t('Not customizable');
  45. }
  46. else {
  47. return $radio.next('label').text();
  48. }
  49. });
  50. }
  51. };
  52. /**
  53. * Move a block in the blocks table from one region to another via select list.
  54. *
  55. * This behavior is dependent on the tableDrag behavior, since it uses the
  56. * objects initialized in that behavior to update the row.
  57. */
  58. Drupal.behaviors.blockDrag = {
  59. attach: function (context, settings) {
  60. // tableDrag is required and we should be on the blocks admin page.
  61. if (typeof Drupal.tableDrag == 'undefined' || typeof Drupal.tableDrag.blocks == 'undefined') {
  62. return;
  63. }
  64. var table = $('table#blocks');
  65. var tableDrag = Drupal.tableDrag.blocks; // Get the blocks tableDrag object.
  66. // Add a handler for when a row is swapped, update empty regions.
  67. tableDrag.row.prototype.onSwap = function (swappedRow) {
  68. checkEmptyRegions(table, this);
  69. };
  70. // A custom message for the blocks page specifically.
  71. Drupal.theme.tableDragChangedWarning = function () {
  72. return '<div class="messages 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>';
  73. };
  74. // Add a handler so when a row is dropped, update fields dropped into new regions.
  75. tableDrag.onDrop = function () {
  76. dragObject = this;
  77. // Use "region-message" row instead of "region" row because
  78. // "region-{region_name}-message" is less prone to regexp match errors.
  79. var regionRow = $(dragObject.rowObject.element).prevAll('tr.region-message').get(0);
  80. var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
  81. var regionField = $('select.block-region-select', dragObject.rowObject.element);
  82. // Check whether the newly picked region is available for this block.
  83. if ($('option[value=' + regionName + ']', regionField).length == 0) {
  84. // If not, alert the user and keep the block in its old region setting.
  85. alert(Drupal.t('The block cannot be placed in this region.'));
  86. // Simulate that there was a selected element change, so the row is put
  87. // back to from where the user tried to drag it.
  88. regionField.change();
  89. }
  90. else if ($(dragObject.rowObject.element).prev('tr').is('.region-message')) {
  91. var weightField = $('select.block-weight', dragObject.rowObject.element);
  92. var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*block-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
  93. if (!regionField.is('.block-region-' + regionName)) {
  94. regionField.removeClass('block-region-' + oldRegionName).addClass('block-region-' + regionName);
  95. weightField.removeClass('block-weight-' + oldRegionName).addClass('block-weight-' + regionName);
  96. regionField.val(regionName);
  97. }
  98. }
  99. };
  100. // Add the behavior to each region select list.
  101. $('select.block-region-select', context).once('block-region-select', function () {
  102. $(this).change(function (event) {
  103. // Make our new row and select field.
  104. var row = $(this).closest('tr');
  105. var select = $(this);
  106. tableDrag.rowObject = new tableDrag.row(row);
  107. // Find the correct region and insert the row as the last in the region.
  108. table.find('.region-' + select[0].value + '-message').nextUntil('.region-message').last().before(row);
  109. // Modify empty regions with added or removed fields.
  110. checkEmptyRegions(table, row);
  111. // Remove focus from selectbox.
  112. select.get(0).blur();
  113. });
  114. });
  115. var checkEmptyRegions = function (table, rowObject) {
  116. $('tr.region-message', table).each(function () {
  117. // If the dragged row is in this region, but above the message row, swap it down one space.
  118. if ($(this).prev('tr').get(0) == rowObject.element) {
  119. // Prevent a recursion problem when using the keyboard to move rows up.
  120. if ((rowObject.method != 'keyboard' || rowObject.direction == 'down')) {
  121. rowObject.swap('after', this);
  122. }
  123. }
  124. // This region has become empty.
  125. if ($(this).next('tr').is(':not(.draggable)') || $(this).next('tr').length == 0) {
  126. $(this).removeClass('region-populated').addClass('region-empty');
  127. }
  128. // This region has become populated.
  129. else if ($(this).is('.region-empty')) {
  130. $(this).removeClass('region-empty').addClass('region-populated');
  131. }
  132. });
  133. };
  134. }
  135. };
  136. })(jQuery);