1(function ($) { 2 // Init fSelect switchers 3 var fselects = $(".facetwp-layout-switcher.type-fselect select"); 4 init_fselects(fselects); 5 6 // Set initial mode 7 set_initialmode(); 8 9 const PRICE_VALUES = ['price-desc', 'price_order']; 10 let removed = []; 11 12 function togglePriceOptions() { 13 const isList = document.querySelector('.facetwp-layout-switcher li.active[data-value="singletarget-mode-list"]'); 14 const select = document.querySelector('.woocommerce-ordering .orderby'); 15 if (!select) return; 16 17 if (isList) { 18 select.querySelectorAll('option').forEach(option => { 19 if (PRICE_VALUES.includes(option.value)) { 20 removed.push(option); 21 option.remove(); 22 } 23 }); 24 } else { 25 removed.forEach(option => select.appendChild(option)); 26 removed = []; 27 } 28 } 29 30 togglePriceOptions(); 31 32 // Trigger switch for switcher type text or icon 33 $().on("click", ".facetwp-layout-switcher li a", function (e) { 34 e.preventDefault(); 35 var trigger = $(this); 36 var target = get_targets( 37 trigger.closest(".facetwp-layout-switcher").nodes[0] 38 ); 39 var layoutmode = trigger 40 .closest("li") 41 .nodes[0].getAttribute("data-value"); 42 change_url_param(layoutmode); 43 sync_and_switch(layoutmode, target); 44 togglePriceOptions(); 45 }); 46 47 // Trigger switch for switcher type dropdown or fselect 48 $().on("change", ".facetwp-layout-switcher select", function () { 49 var trigger = $(this); 50 var target = get_targets( 51 trigger.closest(".facetwp-layout-switcher").nodes[0] 52 ); 53 if ("" !== trigger.val()) { 54 var layoutmode = trigger.val(); 55 change_url_param(layoutmode); 56 sync_and_switch(layoutmode, target); 57 togglePriceOptions(); 58 } 59 }); 60 61 // Change URL parameter based on selected display mode 62 function change_url_param(layoutmode) { 63 var urlParams = new URLSearchParams(window.location.search); 64 urlParams.set("layout", layoutmode.replace("singletarget-mode-", "")); 65 var qs = urlParams.toString(); 66 var newUrl = window.location.pathname + (qs ? "?" + qs : ""); 67 window.history.replaceState({}, "", newUrl); 68 } 69 70 // Sync all switchers with same target, then switch class of target(s) 71 function sync_and_switch(layoutmode, target) { 72 var targetclasses = "." + target.join("."); 73 74 if (target.length === 1) { 75 targetclasses = targetclasses + ":not(.multitarget)"; 76 } 77 78 // Sync active classes for type text and icons 79 $(targetclasses + " li").removeClass("active"); 80 $(targetclasses + ' li[data-value="' + layoutmode + '"]').addClass( 81 "active" 82 ); 83 84 // Sync "selected" for type dropdown and fselect 85 $(targetclasses + " select").each((node) => (node.value = layoutmode)); 86 87 // Reset fSelects and set selected class for bg color 88 init_fselects(fselects); 89 $( 90 targetclasses + ' .fs-option[data-value="' + layoutmode + '"]' 91 ).addClass("selected"); 92 93 if (FWP.extras) { 94 FWP.extras.layoutmode = layoutmode; 95 FWP.refresh(); 96 } 97 $(document).on('facetwp-loaded', function () { 98 switch_target_class(layoutmode, target); 99 }); 100 } 101 102 // Switch class of targeted item(s) 103 function switch_target_class(layoutmode, target) { 104 var is_multitargetswitcher = target.length > 1; 105 106 target.forEach((targetelement) => { 107 targetelement = targetelement.replace("target-", ""); 108 var targetnodes = $("." + targetelement).nodes; 109 110 if (targetnodes.length) { 111 var classnameregex = is_multitargetswitcher 112 ? /(^|\s)multitarget-mode-\S+/g 113 : /(^|\s)singletarget-mode-\S+/g; 114 115 for (var i = 0; i < targetnodes.length; i++) { 116 targetnodes[i].className = targetnodes[i].className.replace( 117 classnameregex, 118 "" 119 ); 120 $(targetnodes[i]).addClass(layoutmode); 121 } 122 } 123 }); 124 } 125 126 // Get targeted item(s) 127 function get_targets(switcher) { 128 var switcherclasses = switcher.classList; 129 var target = []; 130 if (switcherclasses.length > 0) { 131 for (var i = 0; i < switcherclasses.length; i++) { 132 var targetclass = switcherclasses[i]; 133 if (targetclass.startsWith("target-")) { 134 target.push(targetclass); 135 } 136 } 137 } 138 return target; 139 } 140 141 // Get initial mode 142 function get_initialmode(switcher) { 143 var switcherclasses = switcher.classList; 144 for (var i = 0; i < switcherclasses.length; i++) { 145 var initialclass = switcherclasses[i]; 146 if (initialclass.startsWith("initial-")) { 147 return initialclass.replace("initial-", ""); 148 } 149 } 150 151 return null; 152 } 153 154 // Find the first switcher and run the switcher sync and target class switch if it is not set to setinitial-false. 155 // Runs separately for singletarget and multitarget switchers so both can be used together and have separate initial settings. 156 function set_initialmode() { 157 var switcherTypes = [ 158 { type: "singletarget", prefix: "singletarget-mode-" }, 159 { type: "multitarget", prefix: "multitarget-mode-" }, 160 ]; 161
162 switcherTypes.forEach(({ type, prefix }) => { 163 var switchers = $(`.facetwp-layout-switcher.${type}`).nodes; 164 if (switchers.length > 0) { 165 if (!switchers[0].classList.contains("setinitial-false")) { 166 var initialmode = get_initialmode(switchers[0]); 167 if (!initialmode) { 168 var urlParams = new URLSearchParams(window.location.search); 169 initialmode = urlParams.get("layout"); 170 } 171 if (initialmode) { 172 var target = get_targets(switchers[0]); 173 change_url_param(prefix + initialmode); 174 sync_and_switch(prefix + initialmode, target, true); 175 } 176 } 177 } 178 }); 179 } 180 181 // (Re)init fSelect switchers 182 function init_fselects(fselects) { 183 fselects.each((node) => fSelect(node, { showSearch: false })); 184 } 185 186 // On start of the facet refresh, but not on first page load 187 $(document).on('facetwp-refresh', function() { 188 if ( FWP.loaded ) { 189 $('.facetwp-template').addClass('is-loading'); 190 } 191 }); 192 193 // On finishing the facet refresh 194 $(document).on('facetwp-loaded', function() { 195 $('.facetwp-template').removeClass('is-loading'); 196 }); 197 198 // Check URL for layout parameter and set initial mode 199 function check_url_for_layout() { 200 var urlParams = new URLSearchParams(window.location.search); 201 var layout = urlParams.get("layout"); 202 var _paged = urlParams.get("_paged"); 203 if (layout || _paged) { 204 var layoutmode = "singletarget-mode-" + layout; 205 var switchers = $(".facetwp-layout-switcher.singletarget").nodes; 206 if (switchers.length > 0) { 207 var target = get_targets(switchers[0]); 208 sync_and_switch(layoutmode, target); 209 } 210 } 211 } 212 213 // Run check_url_for_layout on page load 214 $(document).ready(function () { 215 check_url_for_layout(); 216 }); 217})(fUtil);
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.