https://falconi.com/core/modules/a3885e7885/public/js/beautiful-taxonomy-filters-public.js?rnd=50728
1(function( $ ) { 2 'use strict'; 3 4 /** 5 * Variables used by the AJAX call 6 */ 7 var xhr; 8 var active = false; 9 var timer; 10 11 /** 12 * Lets select2 all night long 13 */ 14 function create_select2_dropdown( select_el ){ 15 16 var args = { 17 allowClear: btf_localization.allow_clear, 18 syncCssClasses: true, 19 minimumResultsForSearch: 1 // Temporarily set to 1 because select2 misbehaves without the search. 20 }; 21 22 if( btf_localization.show_description == '1' ){ 23 args.templateResult = formatResult; 24 args.templateSelection = formatSelection; 25 } 26 27 /** 28 * Support language 29 */ 30 if( btf_localization.language !== '' ) { 31 args.language = btf_localization.language; 32 } 33 34 /** 35 * Support RTL 36 */ 37 if( btf_localization.rtl == '1' ) { 38 args.dir = 'rtl'; 39 } 40 41 var select2; 42 43 if ( typeof select_el !== 'undefined' ) { 44 if ( ! $(select_el).hasClass('select2-hidden-accessible') ) { 45 select2 = select_el.select2(args); 46 } 47 }else{ 48 select2 = $('.beautiful-taxonomy-filters-select').select2(args); 49 } 50 } 51 52 53 /** 54 * Format the results of select2 55 * 56 */ 57 function formatResult (term) { 58 if (!term.id ){ return term.text; } 59 60 //var new_term = term.text; 61 var new_term = term.element.innerHTML; 62 if( term.text.indexOf(":.:") !== -1) { 63 new_term = new_term.replace(':.:', ' <br><span class="term-description">'); 64 new_term = new_term.replace(':-:', '</span>'); 65 66 } 67 68 var $term = $( 69 '<span class="' + term.element.className + '">' + new_term + '</span>' 70 ); 71 return $term; 72 73 } 74 75 76 /** 77 * Format the selection of select2 78 * 79 */ 80 function formatSelection (term) { 81 if (!term.id ) { 82 return term.text; 83 } 84 85 if ( term.text.indexOf(":.:") === -1 ) { 86 return term.element.innerHTML; 87 } 88 89 //run a regexp on the text to find :.:<any characters:-: and then replace it 90 //var new_term = term.text; 91 var new_term = term.text; 92 var re = /(:\.:[\s\S]*?:-:)/; 93 var reg_results = re.exec(new_term); 94 new_term = new_term.replace(reg_results[0], ''); 95 var $term = $( 96 '<span>' + new_term + '</span>' 97 ); 98 return $term; 99 } 100 101 /** 102 * Run the AJAX update function to make terms conditional. 103 * 104 * @param el jQuery object of the select that changed. 105 */ 106 function conditional_terms_ajax_new( el ){ 107 /** 108 * If there's already an active AJAX request kill it. 109 */ 110 if( active ) { 111 xhr.abort(); 112 } 113 114 /** 115 * Show loaders and disable selects if the response takes more than 1 second. 116 */ 117 if( timer ){ 118 clearTimeout(timer); 119 } 120 timer = setTimeout(function(){ 121 form.find('.beautiful-taxonomy-filters-loader').addClass('active'); 122 form.find('select.beautiful-taxonomy-filters-select').prop('disabled', true); 123 form.find('.beautiful-taxonomy-filters-button').prop('disabled', true); 124 }, 800); 125 126 127 /** 128 * Get general options 129 */ 130 var form = el.closest('#beautiful-taxonomy-filters-form'), 131 nonce = el.data('nonce'), 132 posttype = $('input[name="post_type"]').val(), 133 selects = [], 134 taxonomies = []; 135 136 /** 137 * Get values from all selects 138 */ 139 var filtered_taxonomies = []; 140 form.find('select.beautiful-taxonomy-filters-select').each(function(index){ 141 var sel = $(this), 142 taxonomy = sel.data('taxonomy'), 143 val = sel.val(); 144 145 if( val === '' ){ 146 val = 0; 147 } 148 149 selects.push({ 150 taxonomy: sel.data('taxonomy'), 151 term: val, 152 }); 153 154 taxonomies.push( sel.data('taxonomy') ); 155 156 if ( val) { 157 filtered_taxonomies.push( sel.data('taxonomy') ); 158 } 159 160 }); 161 162 /** 163 * Run our AJAX 164 */ 165 active = true; 166 xhr = $.ajax({ 167 type: 'post', 168 dataType: 'json', 169 url: btf_localization.ajaxurl, 170 data: { 171 action: 'update_filters_callback', 172 selects: selects, 173 posttype: posttype, 174 nonce: nonce, 175 taxonomies: taxonomies 176 }, 177 success: function( response ){ 178 179 /** 180 * Make sure all dropdowns are enabled and loaders are hidden 181 */ 182 if( timer ){ 183 clearTimeout(timer); 184 } 185 form.find('select.beautiful-taxonomy-filters-select').prop( 'disabled', false ); 186 form.find('.beautiful-taxonomy-filters-loader').removeClass('active'); 187 form.find('.beautiful-taxonomy-filters-button').prop('disabled', false); 188 189 /** 190 * Lets get cracking on hiding options 191 */ 192 if( Object.keys(response).length > 0 ){ 193 $.each(response, function(taxonomy, terms) { 194 var select_element = form.find('select.beautiful-taxonomy-filters-select[data-taxonomy="' + taxonomy + '"]'); 195 196 // Let's first check if we've only filtered one select. If so, we should not disabled any of that's dropdowns so it's easier to switch. 197 if ( 1 === filtered_taxonomies.length && taxonomy === filtered_taxonomies[0] ) { 198 return; 199 } 200 201 // First we disable all options. 202 var options = select_element.find('option').prop('disabled', true); 203 204 // Not for the default value.. that dude wants to live.
205 select_element.find('option[value="0"], option[value=""]').prop('disabled', false); 206 207 // Then we loop through all fetched terms and enable them. 208 for ( var i = 0; i < terms.length; i++ ) { 209 var term = terms[i]; 210 var text = terms[i].term_name; 211 if ( btf_localization.show_count == 1 ) { 212 text += ' (' + terms[i].term_count + ')'; 213 } 214 var option = select_element.find('option[value="' + terms[i].term_id + '"]').prop('disabled', false).text( text ); 215 } 216 217 // If select2 is being used we need to destroy the instance and run a new one. 218 if( btf_localization.disable_select2 != '1' ){ 219 select_element.trigger('change.select2'); 220 } 221 }); 222 } 223 224 }, 225 error: function(){ 226 /** 227 * Make sure all dropdowns are enabled and loaders are hidden 228 */ 229 if( timer ){ 230 clearTimeout(timer); 231 } 232 form.find('select.beautiful-taxonomy-filters-select').prop( 'disabled', false ); 233 form.find('.beautiful-taxonomy-filters-loader').removeClass('active'); 234 form.find('.beautiful-taxonomy-filters-button').prop('disabled', false); 235 236 }, 237 complete: function(){ 238 /** 239 * Regardless of success/error we are done. Set active to false. 240 */ 241 active = false; 242 } 243 }); 244 245 } 246 247 248 /** 249 * Run the AJAX update function to make terms conditional. 250 * 251 * @param el jQuery object of the select that changed. 252 */ 253 function conditional_terms_ajax( el ){ 254 255 /** 256 * If there's already an active AJAX request kill it. 257 */ 258 if( active ) { 259 xhr.abort(); 260 } 261 262 /** 263 * Show loaders and disable selects if the response takes more than 1 second. 264 */ 265 if( timer ){ 266 clearTimeout(timer); 267 } 268 timer = setTimeout(function(){ 269 form.find('.beautiful-taxonomy-filters-loader').addClass('active'); 270 form.find('select.beautiful-taxonomy-filters-select').prop('disabled', true); 271 form.find('.beautiful-taxonomy-filters-button').prop('disabled', true); 272 }, 800); 273 274 275 /** 276 * Get general options 277 */ 278 var form = el.closest('#beautiful-taxonomy-filters-form'), 279 nonce = el.data('nonce'), 280 posttype = $('input[name="post_type"]').val(), 281 current_taxonomy = el.data('taxonomy'), 282 selects = []; 283 284 /** 285 * Get values from all selects 286 */ 287 form.find('select.beautiful-taxonomy-filters-select').each(function(index){ 288 var sel = $(this), 289 taxonomy = sel.data('taxonomy'), 290 val = sel.val(); 291 292 if( val === '' ){ 293 val = 0; 294 } 295 296 selects.push({ 297 taxonomy: sel.data('taxonomy'), 298 term: val, 299 }); 300 301 }); 302 303 /** 304 * Run our AJAX 305 */ 306 active = true; 307 xhr = $.ajax({ 308 type: 'post', 309 dataType: 'json', 310 url: btf_localization.ajaxurl, 311 data: { 312 action: 'update_filters_callback', 313 selects: selects, 314 posttype: posttype, 315 current_taxonomy: current_taxonomy, 316 nonce: nonce, 317 }, 318 success: function( response ){ 319 320 /** 321 * Make sure all dropdowns are enabled and loaders are hidden 322 */ 323 if( timer ){ 324 clearTimeout(timer); 325 } 326 form.find('select.beautiful-taxonomy-filters-select').prop( 'disabled', false ); 327 form.find('.beautiful-taxonomy-filters-loader').removeClass('active'); 328 form.find('.beautiful-taxonomy-filters-button').prop('disabled', false); 329 330 /** 331 * Lets get cracking on hiding options 332 */ 333 if( Object.keys(response.taxonomies).length > 0 ){ 334 335 $.each(response.taxonomies, function(taxonomy, terms){ 336 var select_element = form.find('select.beautiful-taxonomy-filters-select[data-taxonomy="' + taxonomy + '"]'); 337 select_element.find('option').each(function(){ 338 var option = $(this), 339 val = option.val(), 340 option_text = option.text(); 341 342 /** 343 * empty or 0 is probably an "all" option and we should leave these alone! 344 * also.. leave britney alone! 345 */ 346 if( val === '' || val === 0 || val == '0' ){ 347 return true; 348 349 } 350 351 if( $.inArray( val, terms ) === -1 ){ 352 option.prop('disabled', true); 353 354 } else { 355 option.prop('disabled', false); 356 357 } 358 }); 359 360 /** 361 * If select2 is being used we need to destroy the instance and run a new one. 362 */ 363 if( btf_localization.disable_select2 != '1' ){ 364 var select_el = form.find('select.beautiful-taxonomy-filters-select[data-taxonomy="' + taxonomy + '"]'); 365 select_el.trigger('change.select2'); 366 } 367 }); 368 369 } 370 }, 371 error: function(){ 372 /** 373 * Make sure all dropdowns are enabled and loaders are hidden 374 */ 375 if( timer ){ 376 clearTimeout(timer); 377 } 378 form.find('select.beautiful-taxonomy-filters-select').prop( 'disabled', false ); 379 form.find('.beautiful-taxonomy-filters-loader').removeClass('active'); 380 form.find('.beautiful-taxonomy-filters-button').prop('disabled', false); 381 382 },
383 complete: function(){ 384 /** 385 * Regardless of success/error we are done. Set active to false. 386 */ 387 active = false; 388 } 389 }); 390 391 } 392 393 394 //Document is ready for some JS magic! 395 $(document).ready(function(){ 396 397 /** 398 * Trigger select2 399 * 400 */ 401 if( btf_localization.disable_select2 != 1 ){ 402 create_select2_dropdown(); 403 } 404 405 406 /** 407 * Update the terms of each taxonomy on the fly 408 * This allows us to only show relevant terms whenever a selection has been made. 409 * 410 */ 411 if( btf_localization.conditional_dropdowns == 1 ){ 412 413 /** 414 * Trigger on the first select with a value on page load. 415 * This will find all forms and look in each of them. By doing this we make sure all forms that should be updated will be. 416 */ 417 var forms = $('.beautiful-taxonomy-filters form, .beautiful-taxonomy-filters-widget form'); 418 for( var i = 0; i < forms.length; i++ ){ 419 var selects = $(forms[i]).find('.beautiful-taxonomy-filters-select'); 420 for( var j = 0; j < selects.length; j++ ){ 421 if( $(selects[j]).val() != 0 ){ 422 conditional_terms_ajax_new( $(selects[j]) ); 423 break; 424 } 425 } 426 } 427 428 /** 429 * Trigger whenever select is changed 430 */ 431 $('.beautiful-taxonomy-filters, .beautiful-taxonomy-filters-widget').on('change', '.beautiful-taxonomy-filters-select', function(){ 432 var el = $(this); 433 conditional_terms_ajax_new( el ); 434 }); 435 } 436 }); 437 438})( 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.