annotate core/modules/block/js/block.admin.es6.js @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
rev   line source
Chris@0 1 /**
Chris@0 2 * @file
Chris@0 3 * Block admin behaviors.
Chris@0 4 */
Chris@0 5
Chris@4 6 (function($, Drupal, debounce) {
Chris@0 7 /**
Chris@0 8 * Filters the block list by a text input search string.
Chris@0 9 *
Chris@0 10 * The text input will have the selector `input.block-filter-text`.
Chris@0 11 *
Chris@0 12 * The target element to do searching in will be in the selector
Chris@0 13 * `input.block-filter-text[data-element]`
Chris@0 14 *
Chris@0 15 * The text source where the text should be found will have the selector
Chris@0 16 * `.block-filter-text-source`
Chris@0 17 *
Chris@0 18 * @type {Drupal~behavior}
Chris@0 19 *
Chris@0 20 * @prop {Drupal~behaviorAttach} attach
Chris@0 21 * Attaches the behavior for the block filtering.
Chris@0 22 */
Chris@0 23 Drupal.behaviors.blockFilterByText = {
Chris@0 24 attach(context, settings) {
Chris@0 25 const $input = $('input.block-filter-text').once('block-filter-text');
Chris@0 26 const $table = $($input.attr('data-element'));
Chris@0 27 let $filterRows;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Filters the block list.
Chris@0 31 *
Chris@0 32 * @param {jQuery.Event} e
Chris@0 33 * The jQuery event for the keyup event that triggered the filter.
Chris@0 34 */
Chris@0 35 function filterBlockList(e) {
Chris@4 36 const query = $(e.target)
Chris@4 37 .val()
Chris@4 38 .toLowerCase();
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Shows or hides the block entry based on the query.
Chris@0 42 *
Chris@0 43 * @param {number} index
Chris@0 44 * The index in the loop, as provided by `jQuery.each`
Chris@0 45 * @param {HTMLElement} label
Chris@0 46 * The label of the block.
Chris@0 47 */
Chris@0 48 function toggleBlockEntry(index, label) {
Chris@0 49 const $label = $(label);
Chris@0 50 const $row = $label.parent().parent();
Chris@4 51 const textMatch =
Chris@4 52 $label
Chris@4 53 .text()
Chris@4 54 .toLowerCase()
Chris@4 55 .indexOf(query) !== -1;
Chris@0 56 $row.toggle(textMatch);
Chris@0 57 }
Chris@0 58
Chris@0 59 // Filter if the length of the query is at least 2 characters.
Chris@0 60 if (query.length >= 2) {
Chris@0 61 $filterRows.each(toggleBlockEntry);
Chris@0 62 Drupal.announce(
Chris@0 63 Drupal.formatPlural(
Chris@0 64 $table.find('tr:visible').length - 1,
Chris@0 65 '1 block is available in the modified list.',
Chris@0 66 '@count blocks are available in the modified list.',
Chris@0 67 ),
Chris@0 68 );
Chris@4 69 } else {
Chris@4 70 $filterRows.each(function(index) {
Chris@4 71 $(this)
Chris@4 72 .parent()
Chris@4 73 .parent()
Chris@4 74 .show();
Chris@0 75 });
Chris@0 76 }
Chris@0 77 }
Chris@0 78
Chris@0 79 if ($table.length) {
Chris@0 80 $filterRows = $table.find('div.block-filter-text-source');
Chris@0 81 $input.on('keyup', debounce(filterBlockList, 200));
Chris@0 82 }
Chris@0 83 },
Chris@0 84 };
Chris@0 85
Chris@0 86 /**
Chris@0 87 * Highlights the block that was just placed into the block listing.
Chris@0 88 *
Chris@0 89 * @type {Drupal~behavior}
Chris@0 90 *
Chris@0 91 * @prop {Drupal~behaviorAttach} attach
Chris@0 92 * Attaches the behavior for the block placement highlighting.
Chris@0 93 */
Chris@0 94 Drupal.behaviors.blockHighlightPlacement = {
Chris@0 95 attach(context, settings) {
Chris@4 96 // Ensure that the block we are attempting to scroll to actually exists.
Chris@4 97 if (settings.blockPlacement && $('.js-block-placed').length) {
Chris@4 98 $(context)
Chris@4 99 .find('[data-drupal-selector="edit-blocks"]')
Chris@4 100 .once('block-highlight')
Chris@4 101 .each(function() {
Chris@4 102 const $container = $(this);
Chris@4 103 // Just scrolling the document.body will not work in Firefox. The html
Chris@4 104 // element is needed as well.
Chris@4 105 $('html, body').animate(
Chris@4 106 {
Chris@4 107 scrollTop:
Chris@4 108 $('.js-block-placed').offset().top -
Chris@4 109 $container.offset().top +
Chris@4 110 $container.scrollTop(),
Chris@4 111 },
Chris@4 112 500,
Chris@4 113 );
Chris@4 114 });
Chris@0 115 }
Chris@0 116 },
Chris@0 117 };
Chris@4 118 })(jQuery, Drupal, Drupal.debounce);