Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Block admin behaviors.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@0
|
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@0
|
36 const query = $(e.target).val().toLowerCase();
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Shows or hides the block entry based on the query.
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param {number} index
|
Chris@0
|
42 * The index in the loop, as provided by `jQuery.each`
|
Chris@0
|
43 * @param {HTMLElement} label
|
Chris@0
|
44 * The label of the block.
|
Chris@0
|
45 */
|
Chris@0
|
46 function toggleBlockEntry(index, label) {
|
Chris@0
|
47 const $label = $(label);
|
Chris@0
|
48 const $row = $label.parent().parent();
|
Chris@0
|
49 const textMatch = $label.text().toLowerCase().indexOf(query) !== -1;
|
Chris@0
|
50 $row.toggle(textMatch);
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 // Filter if the length of the query is at least 2 characters.
|
Chris@0
|
54 if (query.length >= 2) {
|
Chris@0
|
55 $filterRows.each(toggleBlockEntry);
|
Chris@0
|
56 Drupal.announce(
|
Chris@0
|
57 Drupal.formatPlural(
|
Chris@0
|
58 $table.find('tr:visible').length - 1,
|
Chris@0
|
59 '1 block is available in the modified list.',
|
Chris@0
|
60 '@count blocks are available in the modified list.',
|
Chris@0
|
61 ),
|
Chris@0
|
62 );
|
Chris@0
|
63 }
|
Chris@0
|
64 else {
|
Chris@0
|
65 $filterRows.each(function (index) {
|
Chris@0
|
66 $(this).parent().parent().show();
|
Chris@0
|
67 });
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 if ($table.length) {
|
Chris@0
|
72 $filterRows = $table.find('div.block-filter-text-source');
|
Chris@0
|
73 $input.on('keyup', debounce(filterBlockList, 200));
|
Chris@0
|
74 }
|
Chris@0
|
75 },
|
Chris@0
|
76 };
|
Chris@0
|
77
|
Chris@0
|
78 /**
|
Chris@0
|
79 * Highlights the block that was just placed into the block listing.
|
Chris@0
|
80 *
|
Chris@0
|
81 * @type {Drupal~behavior}
|
Chris@0
|
82 *
|
Chris@0
|
83 * @prop {Drupal~behaviorAttach} attach
|
Chris@0
|
84 * Attaches the behavior for the block placement highlighting.
|
Chris@0
|
85 */
|
Chris@0
|
86 Drupal.behaviors.blockHighlightPlacement = {
|
Chris@0
|
87 attach(context, settings) {
|
Chris@0
|
88 if (settings.blockPlacement) {
|
Chris@0
|
89 $(context).find('[data-drupal-selector="edit-blocks"]').once('block-highlight').each(function () {
|
Chris@0
|
90 const $container = $(this);
|
Chris@0
|
91 // Just scrolling the document.body will not work in Firefox. The html
|
Chris@0
|
92 // element is needed as well.
|
Chris@0
|
93 $('html, body').animate({
|
Chris@0
|
94 scrollTop: ($('.js-block-placed').offset().top - $container.offset().top) + $container.scrollTop(),
|
Chris@0
|
95 }, 500);
|
Chris@0
|
96 });
|
Chris@0
|
97 }
|
Chris@0
|
98 },
|
Chris@0
|
99 };
|
Chris@0
|
100 }(jQuery, Drupal, Drupal.debounce));
|