1/** 2 * @file input-search.js 3 * 4 * JS File to clear the search input field. 5 */ 6(function (Drupal, once) { 7 /** 8 * @Behavior 9 * 10 * Clear the search input field when the clear button is clicked. 11 */ 12 Drupal.behaviors.clearSearchInput = { 13 onceName: 'clear-search-input', 14 selector: '[data-clear]', 15 attach(context, settings) { 16 // Use 'once' to ensure this behavior only applies once per element 17 once(this.onceName, this.selector, context).forEach( 18 function (button) { 19 // Find the search input within the same context 20 const searchWrapper = button.closest('.form-item'); 21 if (!searchWrapper) { 22 return; 23 } 24 25 const searchInput = searchWrapper.querySelector('input[type="search"]'); 26 if (!searchInput) { 27 return; 28 } 29 30 /** 31 * Toggle the clear-hidden data attribute. 32 */ 33 const toggleClear = function () { 34 let searchNotEmpty = searchInput && searchInput.value.length; 35 button.setAttribute("data-clear", searchNotEmpty ? 'true' : 'false'); 36 }; 37 38 toggleClear(); 39 40 searchInput.addEventListener("input", function () { 41 toggleClear(); 42 }); 43 44 // Add click event listener to the clear button 45 button.addEventListener("click", function (event) { 46 searchInput.value = ""; // Clear the input field 47 searchInput.focus(); // Refocus the input field 48 button.setAttribute('data-clear', 'false'); 49 50 // We dispatch a search cleared event. 51 // Allow other components to react to this event. 52 const searchCleared = new CustomEvent("searchCleared", { 53 detail: { target: event.target }, 54 }); 55 document.dispatchEvent(searchCleared); 56 event.preventDefault(); 57 event.stopPropagation(); 58 }); 59 }, 60 ); 61 }, 62 detach: function detach(context, settings, trigger) { 63 if (trigger === "unload") { 64 once.remove(this.onceName, context.querySelectorAll(this.selector)); 65 } 66 }, 67 }; 68})(Drupal, once);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.