annotate core/modules/block/js/block.js @ 19:fa3358dc1485 tip

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