block.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, window, Drupal) {
  8. Drupal.behaviors.blockSettingsSummary = {
  9. attach: function attach() {
  10. if (typeof $.fn.drupalSetSummary === 'undefined') {
  11. return;
  12. }
  13. function checkboxesSummary(context) {
  14. var vals = [];
  15. var $checkboxes = $(context).find('input[type="checkbox"]:checked + label');
  16. var il = $checkboxes.length;
  17. for (var i = 0; i < il; i++) {
  18. vals.push($($checkboxes[i]).html());
  19. }
  20. if (!vals.length) {
  21. vals.push(Drupal.t('Not restricted'));
  22. }
  23. return vals.join(', ');
  24. }
  25. $('[data-drupal-selector="edit-visibility-node-type"], [data-drupal-selector="edit-visibility-language"], [data-drupal-selector="edit-visibility-user-role"]').drupalSetSummary(checkboxesSummary);
  26. $('[data-drupal-selector="edit-visibility-request-path"]').drupalSetSummary(function (context) {
  27. var $pages = $(context).find('textarea[name="visibility[request_path][pages]"]');
  28. if (!$pages.val()) {
  29. return Drupal.t('Not restricted');
  30. }
  31. return Drupal.t('Restricted to certain pages');
  32. });
  33. }
  34. };
  35. Drupal.behaviors.blockDrag = {
  36. attach: function attach(context, settings) {
  37. if (typeof Drupal.tableDrag === 'undefined' || typeof Drupal.tableDrag.blocks === 'undefined') {
  38. return;
  39. }
  40. function checkEmptyRegions(table, rowObject) {
  41. table.find('tr.region-message').each(function () {
  42. var $this = $(this);
  43. if ($this.prev('tr').get(0) === rowObject.element) {
  44. if (rowObject.method !== 'keyboard' || rowObject.direction === 'down') {
  45. rowObject.swap('after', this);
  46. }
  47. }
  48. if ($this.next('tr').is(':not(.draggable)') || $this.next('tr').length === 0) {
  49. $this.removeClass('region-populated').addClass('region-empty');
  50. } else if ($this.is('.region-empty')) {
  51. $this.removeClass('region-empty').addClass('region-populated');
  52. }
  53. });
  54. }
  55. function updateLastPlaced(table, rowObject) {
  56. table.find('.color-success').removeClass('color-success');
  57. var $rowObject = $(rowObject);
  58. if (!$rowObject.is('.drag-previous')) {
  59. table.find('.drag-previous').removeClass('drag-previous');
  60. $rowObject.addClass('drag-previous');
  61. }
  62. }
  63. function updateBlockWeights(table, region) {
  64. var weight = -Math.round(table.find('.draggable').length / 2);
  65. table.find('.region-' + region + '-message').nextUntil('.region-title').find('select.block-weight').val(function () {
  66. return ++weight;
  67. });
  68. }
  69. var table = $('#blocks');
  70. var tableDrag = Drupal.tableDrag.blocks;
  71. tableDrag.row.prototype.onSwap = function (swappedRow) {
  72. checkEmptyRegions(table, this);
  73. updateLastPlaced(table, this);
  74. };
  75. tableDrag.onDrop = function () {
  76. var dragObject = this;
  77. var $rowElement = $(dragObject.rowObject.element);
  78. var regionRow = $rowElement.prevAll('tr.region-message').get(0);
  79. var regionName = regionRow.className.replace(/([^ ]+[ ]+)*region-([^ ]+)-message([ ]+[^ ]+)*/, '$2');
  80. var regionField = $rowElement.find('select.block-region-select');
  81. if (regionField.find('option[value=' + regionName + ']').length === 0) {
  82. window.alert(Drupal.t('The block cannot be placed in this region.'));
  83. regionField.trigger('change');
  84. }
  85. if (!regionField.is('.block-region-' + regionName)) {
  86. var weightField = $rowElement.find('select.block-weight');
  87. var oldRegionName = weightField[0].className.replace(/([^ ]+[ ]+)*block-weight-([^ ]+)([ ]+[^ ]+)*/, '$2');
  88. regionField.removeClass('block-region-' + oldRegionName).addClass('block-region-' + regionName);
  89. weightField.removeClass('block-weight-' + oldRegionName).addClass('block-weight-' + regionName);
  90. regionField.val(regionName);
  91. }
  92. updateBlockWeights(table, regionName);
  93. };
  94. $(context).find('select.block-region-select').once('block-region-select').on('change', function (event) {
  95. var row = $(this).closest('tr');
  96. var select = $(this);
  97. tableDrag.rowObject = new tableDrag.row(row[0]);
  98. var regionMessage = table.find('.region-' + select[0].value + '-message');
  99. var regionItems = regionMessage.nextUntil('.region-message, .region-title');
  100. if (regionItems.length) {
  101. regionItems.last().after(row);
  102. } else {
  103. regionMessage.after(row);
  104. }
  105. updateBlockWeights(table, select[0].value);
  106. checkEmptyRegions(table, tableDrag.rowObject);
  107. updateLastPlaced(table, row);
  108. if (!tableDrag.changed) {
  109. $(Drupal.theme('tableDragChangedWarning')).insertBefore(tableDrag.table).hide().fadeIn('slow');
  110. tableDrag.changed = true;
  111. }
  112. select.trigger('blur');
  113. });
  114. }
  115. };
  116. })(jQuery, window, Drupal);