Chris@0
|
1 /**
|
Chris@0
|
2 * @file
|
Chris@0
|
3 * Block admin behaviors.
|
Chris@0
|
4 */
|
Chris@0
|
5
|
Chris@17
|
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@14
|
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@17
|
36 const query = $(e.target)
|
Chris@17
|
37 .val()
|
Chris@17
|
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@17
|
51 const textMatch =
|
Chris@17
|
52 $label
|
Chris@17
|
53 .text()
|
Chris@17
|
54 .toLowerCase()
|
Chris@17
|
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@14
|
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@14
|
66 '@count blocks are available in the modified list.',
|
Chris@14
|
67 ),
|
Chris@0
|
68 );
|
Chris@17
|
69 } else {
|
Chris@17
|
70 $filterRows.each(function(index) {
|
Chris@17
|
71 $(this)
|
Chris@17
|
72 .parent()
|
Chris@17
|
73 .parent()
|
Chris@17
|
74 .show();
|
Chris@0
|
75 });
|
Chris@0
|
76 }
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 if ($table.length) {
|
Chris@14
|
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@17
|
96 // Ensure that the block we are attempting to scroll to actually exists.
|
Chris@17
|
97 if (settings.blockPlacement && $('.js-block-placed').length) {
|
Chris@17
|
98 $(context)
|
Chris@17
|
99 .find('[data-drupal-selector="edit-blocks"]')
|
Chris@17
|
100 .once('block-highlight')
|
Chris@17
|
101 .each(function() {
|
Chris@17
|
102 const $container = $(this);
|
Chris@17
|
103 // Just scrolling the document.body will not work in Firefox. The html
|
Chris@17
|
104 // element is needed as well.
|
Chris@17
|
105 $('html, body').animate(
|
Chris@17
|
106 {
|
Chris@17
|
107 scrollTop:
|
Chris@17
|
108 $('.js-block-placed').offset().top -
|
Chris@17
|
109 $container.offset().top +
|
Chris@17
|
110 $container.scrollTop(),
|
Chris@17
|
111 },
|
Chris@17
|
112 500,
|
Chris@17
|
113 );
|
Chris@17
|
114 });
|
Chris@0
|
115 }
|
Chris@0
|
116 },
|
Chris@0
|
117 };
|
Chris@17
|
118 })(jQuery, Drupal, Drupal.debounce);
|