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