1(($) => { 2 // Create variable to store AJAX request cache 3 let requestCache = {}; 4 let currentRequest = null; 5 function processPostsListAJAX(url, formId) { 6 // Store element that will have updated content 7 formSubId = formId.substring(8); 8 let $postsListContainer = $("#filter-container-" + formSubId); 9 let postsListContainer = document.getElementById( 10 "filter-container-" + formSubId 11 ); 12 let filterForm = document.getElementById(formId); 13 14 let $postsListLoader = $(".visc-posts-list__loader"); 15 16 function updateContent(content) { 17 // Display the new data in the container 18 $postsListContainer.empty(); 19 $postsListContainer.append(content); 20 21 // Scroll to the top of the list container 22 if (filterForm.hasAttribute("data-results-after-filter")) { 23 postsListContainer.scrollIntoView({ 24 behavior: "smooth", 25 block: "start", 26 inline: "nearest", 27 }); 28 } 29 // Show the user the request is comlete 30 $postsListLoader.removeClass("visc-posts-list__loader--active"); 31 $postsListContainer.removeClass("visc-posts-list--loading"); 32 // Need to re-run this after each complete AJAX too 33 // postsListPaginationAJAXHandler(); 34 history.pushState("", document.title, url); 35 // Create custom JS event to listen to for custom JS after AJAX is complete 36 $(document).trigger("visceralPosts:ajaxSuccess"); 37 } 38 39 // If we've made this request befre, use the cached version 40 if (requestCache[url]) { 41 // Get the content from the cache 42 let cachedContent = requestCache[url]; 43 updateContent(cachedContent); 44 // If we haven't made this request before, make the AJAX request 45 } else { 46 // Get new results using AJAX 47 currentRequest = $.ajax({ 48 url, 49 dataType: "html", 50 beforeSend: function () { 51 if (currentRequest != null) { 52 currentRequest.abort(); 53 } 54 // Show the user something is happening 55 $postsListLoader.addClass("visc-posts-list__loader--active"); 56 $postsListContainer.addClass("visc-posts-list--loading"); 57 }, 58 success: function (data) { 59 // Get all the new content inside of the container 60 let updatedContent = $(data).find( 61 "#filter-container-" + formSubId + " > *" 62 ); 63 // Store the new content in the cache 64 requestCache[url] = updatedContent; 65 updateContent(updatedContent); 66 }, 67 }); 68 } 69 } 70 71 function visceralQueryFilter() { 72 let $filterLinkLists = $(".visc-query-filter__links"); 73 if ($filterLinkLists) { 74 $filterLinkLists.each(function () { 75 let $filterLinks = $(this).find(".visc-query-filter-link > a"); 76 if ($filterLinks) { 77 $filterLinks.each(function () { 78 $(this).on("click", function (e) { 79 e.preventDefault(); 80 let $thisForm = $(this).closest("form"); 81 let $formID = $thisForm.attr("id"); 82 83 let $thisValue = $(this).attr("data-value"); 84 let $thisHiddenInput = $thisForm.find('input[type="hidden"]'); 85 $thisHiddenInput.val($thisValue); 86 if ($thisForm.hasClass("visc-query-filter__form--ajax")) { 87 // Get the form data 88 let formData = $thisForm.serialize(); 89 // Get the form action 90 let formAction = $thisForm.attr("action"); 91 92 let url = formAction + "?" + formData; 93 processPostsListAJAX(url, $formID); 94 } else { 95 $thisForm.submit(); 96 } 97 }); 98 }); 99 } 100 let $filterLinkItems = $(this).find(".visc-query-filter-link"); 101 102 if ($filterLinkItems) { 103 $filterLinkItems.each(function () { 104 $(this).on("click", function (e) { 105 $filterLinkItems.removeClass("visc-query-filter-link--active"); 106 $(this).addClass("visc-query-filter-link--active"); 107 }); 108 }); 109 } 110 }); 111 } 112 113 // AJAX form submission 114 $(".visc-query-filter__form--ajax").on("submit", function (e) { 115 // Prevent form from submitting normally 116 e.preventDefault(); 117 118 // Get the form data 119 let formData = $(this).serialize(); 120 // Get the form action 121 let formAction = $(this).attr("action"); 122 let $formId = $(this).attr("id"); 123 124 let url = formAction + "?" + formData; 125 // Get new results using AJAX 126 processPostsListAJAX(url, $formId); 127 }); 128 129 // Auto-submit form on input change and AJAX load results 130 $( 131 ".visc-query-filter__form.visc-query-filter__form--ajax select.auto-submit, .visc-query-filter__form.visc-query-filter__form--ajax input[type='checkbox'], .visc-query-filter__form.visc-query-filter__form--ajax input[type='radio']" 132 ).on("change", function (e) { 133 // Prevent form from submitting normally 134 e.preventDefault(); 135 let $form = $(this).closest("form"); 136 let $formID = $form.attr("id"); 137 // Get the form data 138 let formData = $form.serialize(); 139 // Get the form action 140 let formAction = $form.attr("action"); 141 142 let url = formAction + "?" + formData; 143 144 // Get new results using AJAX 145 processPostsListAJAX(url, $formID); 146 }); 147 148 // Detect when done typing in search input and auto-submit 149 $.fn.donetyping = function (callback) { 150 var _this = $(this); 151 var x_timer; 152 _this.keyup(function () { 153 clearTimeout(x_timer); 154 x_timer = setTimeout(clear_timer, 1000); 155 }); 156 157 function clear_timer() { 158 clearTimeout(x_timer); 159 callback.call(_this); 160 } 161 }; 162 163 $(".visc-query-filter__search").donetyping(function () { 164 let $form = $(this).closest("form"); 165 let $formID = $form.attr("id"); 166 // Get the form data 167 let formData = $form.serialize(); 168 // Get the form action 169 let formAction = $form.attr("action"); 170 171 let url = formAction + "?" + formData; 172 173 // Get new results using AJAX 174 processPostsListAJAX(url, $formID); 175 }); 176 177 // Make multi-select selects look cooler 178 let filterSelectInputs = document.querySelectorAll( 179 ".visc-query-filter__select" 180 ); 181 if (filterSelectInputs) {
182 filterSelectInputs.forEach(function (input) { 183 if ( 184 !input.classList.contains("input--hidden") && 185 input.classList.contains("visc-query-filter__select--enhanced-select") 186 ) { 187 let selector = ".visc-query-filter__select#" + input.id; 188 let selectBox = new vanillaSelectBox(selector, { 189 placeHolder: input.dataset.placeholder, 190 disableSelectAll: true, 191 }); 192 } 193 }); 194 } 195 } 196 visceralQueryFilter(); 197 198 $(document).on("visceralPage:ajaxSuccess", function () { 199 visceralQueryFilter(); 200 }); 201})(jQuery);
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.