3 * Block admin behaviors.
6 (function ($, Drupal) {
11 * Filters the block list by a text input search string.
13 * The text input will have the selector `input.block-filter-text`.
15 * The target element to do searching in will be in the selector
16 * `input.block-filter-text[data-element]`
18 * The text source where the text should be found will have the selector
19 * `.block-filter-text-source`
21 * @type {Drupal~behavior}
23 * @prop {Drupal~behaviorAttach} attach
24 * Attaches the behavior for the block filtering.
26 Drupal.behaviors.blockFilterByText = {
27 attach: function (context, settings) {
28 var $input = $('input.block-filter-text').once('block-filter-text');
29 var $table = $($input.attr('data-element'));
33 * Filters the block list.
35 * @param {jQuery.Event} e
36 * The jQuery event for the keyup event that triggered the filter.
38 function filterBlockList(e) {
39 var query = $(e.target).val().toLowerCase();
42 * Shows or hides the block entry based on the query.
44 * @param {number} index
45 * The index in the loop, as provided by `jQuery.each`
46 * @param {HTMLElement} label
47 * The label of the block.
49 function toggleBlockEntry(index, label) {
50 var $label = $(label);
51 var $row = $label.parent().parent();
52 var textMatch = $label.text().toLowerCase().indexOf(query) !== -1;
53 $row.toggle(textMatch);
56 // Filter if the length of the query is at least 2 characters.
57 if (query.length >= 2) {
58 $filter_rows.each(toggleBlockEntry);
61 $filter_rows.each(function (index) {
62 $(this).parent().parent().show();
68 $filter_rows = $table.find('div.block-filter-text-source');
69 $input.on('keyup', filterBlockList);
75 * Highlights the block that was just placed into the block listing.
77 * @type {Drupal~behavior}
79 * @prop {Drupal~behaviorAttach} attach
80 * Attaches the behavior for the block placement highlighting.
82 Drupal.behaviors.blockHighlightPlacement = {
83 attach: function (context, settings) {
84 if (settings.blockPlacement) {
85 $(context).find('[data-drupal-selector="edit-blocks"]').once('block-highlight').each(function () {
86 var $container = $(this);
87 // Just scrolling the document.body will not work in Firefox. The html
88 // element is needed as well.
89 $('html, body').animate({
90 scrollTop: $('.js-block-placed').offset().top - $container.offset().top + $container.scrollTop()