1/* global jQuery:false */ 2(function() { 3 "use strict"; 4 5 var $window = jQuery( window ), 6 $document = jQuery( document ), 7 $body = jQuery( 'body' ); 8 9 $document.on('action.before_init_trx_addons', function() { 10 // Remove theme-animations inside sliders with WooCommerce products 11 jQuery( '.slides.products .slider-slide' ).each( function() { 12 var $self = jQuery( this ); 13 $self.find( '>[data-animation]' ).removeAttr( 'data-animation' ); 14 $self.find( '>[data-post-animation]' ).removeAttr( 'data-post-animation' ); 15 } ); 16 } ); 17 18 $document.on('action.ready_trx_addons', function() { 19 20 // WooCommerce categories on homepages 21 //---------------------------------------------------------- 22 23 // Add arrows to the WooCommerce categories on homepages 24 if ( ! $body.hasClass( 'woocommerce' ) ) { 25 $body.find( '.widget_area:not(.footer_wrap) .widget_product_categories:not(.inited)' ).each( function() { 26 var widget = jQuery(this).addClass('inited'); 27 widget.find('ul.product-categories .has_children > a').append('<span class="open_child_menu"></span>'); 28 widget.on('click', 'ul.product-categories.plain li a .open_child_menu', function(e) { 29 var $a = jQuery(this).parent(); 30 if ($a.siblings('ul:visible').length > 0) 31 $a.siblings('ul').slideUp().parent().removeClass('opened'); 32 else { 33 jQuery(this).parents('li').siblings('li').find('ul:visible').slideUp().parent().removeClass('opened'); 34 $a.siblings('ul').slideDown().parent().addClass('opened'); 35 } 36 e.preventDefault(); 37 return false; 38 } ); 39 40 // Resize handlers 41 jQuery(document).on('action.resize_trx_addons', function() { 42 trx_addons_woocommerce_resize_actions(); 43 }); 44 trx_addons_woocommerce_resize_actions(); 45 46 // Switch popup menu / hierarchical list on product categories list placed in sidebar 47 function trx_addons_woocommerce_resize_actions() { 48 var cat_menu = widget.find('ul.product-categories'); 49 var sb = cat_menu.parents('.widget_area'); 50 if ( sb.length > 0 && cat_menu.length > 0 ) { 51 if ( sb.width() == sb.parents('.content_wrap').width() ) { 52 if ( cat_menu.hasClass('inited') ) { 53 cat_menu.removeClass('inited').addClass('plain').superfish('destroy'); 54 cat_menu.find('ul.animated').removeClass('animated').addClass('no_animated'); 55 } 56 } else { 57 if ( ! cat_menu.hasClass('inited') ) { 58 cat_menu.removeClass('plain').addClass('inited'); 59 cat_menu.find('ul.no_animated').removeClass('no_animated').addClass('animated'); 60 trx_addons_init_sfmenu('body:not(.woocommerce) .widget_area:not(.footer_wrap) .widget_product_categories ul.product-categories'); 61 } 62 } 63 } 64 } 65 }); 66 } 67 68 69 // Extended attributes in single product 70 //---------------------------------------------------------- 71 72 // Change a behaviour to AJAX for the button 'Add to Cart' placed inside the popup 'Quick View' 73 function trx_addons_woocommerce_add_to_cart_ajax( type ) { 74 var $bt = $body.find( ( type == 'popup_yith' ? '>#yith-quick-view-modal' : '>.mfp-wrap' ) +' .single_add_to_cart_button' ); 75 if ( $bt.length ) { 76 $bt.addClass( 'add_to_cart_button ajax_add_to_cart' ) 77 .attr( 'data-product_id', $bt.attr( 'value' ) ); 78 } 79 } 80 jQuery( document.body ).on( 'woosq_loaded', function() { 81 trx_addons_woocommerce_add_to_cart_ajax( 'popup_wpc' ); 82 } ); 83 $document.on( 'qv_loader_stop', function() { 84 trx_addons_woocommerce_add_to_cart_ajax( 'popup_yith' ); 85 } ); 86 87 // Init product attributes with variations 88 jQuery( document.body ).on( 'woosq_loaded', function( e ) { 89 trx_addons_woocommerce_init_variations( 'popup_wpc', $body ); 90 } ); 91 $body.on( 'wcpt_product_modal_ready', function( e ) { 92 $document.trigger( 'action.init_hidden_elements', [$body] ); 93 } ); 94 95 $document.on( 'action.init_hidden_elements qv_loader_stop', function( e, cont ) { 96 if ( ! cont ) cont = $body; 97 trx_addons_woocommerce_init_variations( e.type == 'qv_loader_stop' ? 'popup_yith' : 'init_hidden', cont ); 98 } ); 99 100 // Check available product variations 101 function trx_addons_woocommerce_init_variations( type, cont ) { 102 cont.find( '.variations_form.cart:not(.inited)' ).each( function() { 103 var form = jQuery(this).addClass('inited'); 104 var trx_addons_attribs = form.find('.trx_addons_attrib_item'); 105 if ( trx_addons_attribs.length === 0 ) return; 106 107 // First check after variation form inited 108 form.on( 'wc_variation_form', function( variation_form ) { 109 form.on( 'check_variations', function() { 110 trx_addons_woocommerce_check_variations( form ); 111 } ); 112 trx_addons_woocommerce_check_variations( form ); 113 } ); 114 115 // Click on our variations attribs 116 trx_addons_attribs.on( 'click', function(e) { 117 var $attrib = jQuery(this); 118 e.preventDefault(); 119 if ( ! $attrib.hasClass('trx_addons_attrib_disabled') ) { 120 $attrib.toggleClass('trx_addons_attrib_selected').siblings().removeClass('trx_addons_attrib_selected'); 121 var term = $attrib.hasClass('trx_addons_attrib_selected') ? $attrib.data('value') : ''; 122 if ( term === '' ) {
123 $attrib.siblings('.trx_addons_attrib_item[data-value=""]').addClass('trx_addons_attrib_selected'); 124 } 125 var attrib_name = $attrib.parents('.trx_addons_attrib_extended').data('attrib'); 126 $attrib.parents('.trx_addons_attrib_extended').parent().find('#'+attrib_name).val(term).trigger('change'); 127 } 128 return false; 129 } ); 130 } ); 131 } 132 133 function trx_addons_woocommerce_check_variations( form ) { 134 // Refresh attributes on selects are changed 135 form.find( '.variations select' ).each( function() { 136 var select_box = jQuery(this), 137 select_val = select_box.val(), 138 attrib_box = select_box.siblings('.trx_addons_attrib_extended').length == 1 139 ? select_box.siblings('.trx_addons_attrib_extended') 140 : select_box.parent().siblings('.trx_addons_attrib_extended'); 141 attrib_box.find('.trx_addons_attrib_item').removeClass('trx_addons_attrib_selected').addClass('trx_addons_attrib_disabled'); 142 select_box.find('option').each( function() { 143 var opt = jQuery( this ); 144 attrib_box.find( '.trx_addons_attrib_item[data-value="' + opt.val() + '"]' ) 145 .removeClass( 'trx_addons_attrib_disabled' ) 146 .toggleClass( 'trx_addons_attrib_selected', opt.val() == select_val );//opt.get(0).selected 147 } ); 148 } ); 149 } 150 151 152 // Extended attributes in the products list (shop page or category/tag list) 153 //---------------------------------------------------------- 154 if ( true || $body.hasClass( 'woocommerce' ) && $body.hasClass( 'archive' ) ) { 155 $document.on( 'action.init_hidden_elements', function( e, cont ) { 156 // If products with attributes are not present on this page - exit 157 if ( ! jQuery( '.trx_addons_product_attributes' ).length ) return; 158 // Click on the attribute - add filter 159 cont 160 .find( '.trx_addons_product_attribute_item_action_filter:not(.attribute_action_filter_inited)' ) 161 .addClass( 'attribute_action_filter_inited' ) 162 .on( 'click', function( e ) { 163 var flt = jQuery( this ), 164 item = flt.parent(), 165 type = item.data( 'type' ), 166 attr = item.data( 'attribute' ), 167 value = item.data( 'value' ), 168 widget = jQuery( '.trx_addons_woocommerce_search_type_filter' ).eq(0); 169 if ( widget.length ) { 170 var widget_fld = widget.find( '.sc_form_field_' + attr ); 171 if ( widget_fld.length ) { 172 var widget_item = widget_fld.find( '.sc_form_field_item[data-value="' + value + '"]' ).eq( 0 ); 173 if ( widget_item.length && ! widget_item.hasClass( 'sc_form_field_item_checked' ) ) { 174 widget_item.trigger( 'click' ); 175 var apply = widget_fld.find( '.trx_addons_search_apply' ); 176 if ( apply.length ) { 177 apply.trigger( 'click' ); 178 } 179 } 180 } 181 } 182 e.preventDefault(); 183 return false; 184 } ); 185 // Click on the attribute - swap image 186 var $swap_atts = cont 187 .find( '.trx_addons_product_attribute_item_action_swap:not(.attribute_action_swap_inited)' ) 188 .addClass( 'attribute_action_swap_inited' ); 189 if ( $swap_atts.length ) { 190 $swap_atts.on( 'click', function( e ) { 191 var $link = jQuery( this ), 192 $item = $link.parent(), 193 active = $item.hasClass( 'trx_addons_product_attribute_item_active' ), 194 disabled = $item.hasClass( 'trx_addons_product_attribute_item_disabled' ); 195 if ( ! disabled ) { 196 $item.parents( '.trx_addons_product_attribute' ) 197 .find( '.trx_addons_product_attribute_item_active' ) 198 .removeClass( 'trx_addons_product_attribute_item_active' ); 199 swap_product_image( $item.toggleClass( 'trx_addons_product_attribute_item_active', ! active ) ); 200 check_available_variations( $item.parents( '.trx_addons_product_attributes' ) ); 201 } 202 e.preventDefault(); 203 return false; 204 } ); 205 // Swap image and display corresponding price on first run or after new products are loaded 206 swap_product_image( $swap_atts.eq(0) ); 207 // Check variations on first run or after new products are loaded 208 $swap_atts.parents( '.trx_addons_product_attributes' ).each( function() { 209 check_available_variations( jQuery( this ) ); 210 } ); 211 } 212 // Hover on the attribute - change the product image 213 cont 214 .find( '.trx_addons_product_attributes[data-product-variations] .trx_addons_product_attribute_item:not(.attribute_action_swap_inited)' ) 215 .addClass( 'attribute_action_swap_inited' ) 216 .on( 'focus mouseover', function( e ) { 217 var $self = jQuery( this ); 218 if ( ! $self.find( '>.trx_addons_product_attribute_item_action_swap' ).length ) { 219 swap_product_image( $self ); 220 } 221 } ); 222 223 // Check available variations for product in the archive 224 function check_available_variations( $wrap ) { 225 var variations = $wrap.data( 'product-variations' ), 226 variation_sel = {}, 227 $active_items = $wrap.find( '.trx_addons_product_attribute_item_active' ); 228 // Collect active items to the current variation 229 $active_items.each( function( idx ) { 230 variation_sel[ $active_items.eq( idx ).data( 'attribute' ) ] = $active_items.eq( idx ).data( 'value' ); 231 } ); 232 $wrap.find( '.trx_addons_product_attribute_item' ).each( function() { 233 var $attr = jQuery( this ), 234 attr_name = $attr.data( 'attribute' ), 235 attr_value = $attr.data( 'value' ), 236 attr_variation = trx_addons_object_clone( variation_sel ); 237 attr_variation[ attr_name ] = attr_value; 238 $attr.toggleClass( 'trx_addons_product_attribute_item_disabled', get_closest_variation( variations, attr_variation ) === false ); 239 } ); 240 } 241 242 // Get a closest variation 243 function get_closest_variation( variations, variation_sel ) { 244 var variation = false, 245 variation_max = 0, 246 variation_cur = 0; 247 for ( var i = 0; i < variations.length; i++ ) { 248 if ( ! variations[i].variation_is_active || ! variations[i].variation_is_visible ) { 249 continue; 250 } 251 variation_cur = 0; 252 for ( var attr in variation_sel ) { 253 if ( ! variations[i].attributes['attribute_'+attr] ) { 254 variation_cur += 1; 255 } else if ( variations[i].attributes['attribute_' + attr] == variation_sel[attr] ) { 256 variation_cur += 10; 257 } else { 258 variation_cur = 0; 259 break; 260 } 261 } 262 if ( variation_max < variation_cur ) { 263 variation_max = variation_cur; 264 variation = variations[i]; 265 } 266 } 267 return variation; 268 } 269 270 // Return true if all attributes from variation are selected 271 function is_full_variation_present( variations, variation_sel_count ) { 272 var variation_total = 0; 273 for ( var i = 0; i < variations.length; i++ ) { 274 if ( ! variations[i].variation_is_active || ! variations[i].variation_is_visible ) { 275 continue; 276 } 277 for ( var p in variations[i].attributes ) { 278 if ( variations[i].attributes.hasOwnProperty( p ) ) { 279 variation_total++; 280 } 281 } 282 break; 283 } 284 return variation_total == variation_sel_count; 285 } 286 287 // Swap a product image on an attribute hover or click 288 function swap_product_image( $flt ) { 289 var $wrap = $flt.parents( '.trx_addons_product_attributes' ), 290 $product = $flt.parents( '.product' ), 291 variations = $wrap.data( 'product-variations' ), 292 variation = false, 293 variation_sel = {}, 294 variation_sel_count = 0; 295 if ( variations ) { 296 // Collect active items to the current variation 297 var $active_items = $wrap.find( '.trx_addons_product_attribute_item_action_swap' ).length 298 ? $wrap.find( '.trx_addons_product_attribute_item_active' ) 299 : $flt; 300 $active_items.each( function( idx ) { 301 variation_sel[ $active_items.eq( idx ).data( 'attribute' ) ] = $active_items.eq( idx ).data( 'value' ); 302 variation_sel_count++; 303 } ); 304 // Get a closest variation 305 if ( variation_sel_count ) { 306 variation = get_closest_variation( variations, variation_sel ); 307 } 308 // Swap image if a variation found 309 // or change back the original image if a variation with selected atributes is not found
310 var $img = $product.find( 'img[class*="attachment-woocommerce"],img[class*="woocommerce-placeholder"]' ); 311 if ( $img.length ) { 312 if ( ! $img.data( 'src-old' ) ) { 313 $img.data( { 314 'src-old': $img.attr( 'src' ), 315 'srcset-old': $img.attr( 'srcset' ), 316 'sizes-old': $img.attr( 'sizes' ) 317 } ); 318 } 319 if ( variation ) { 320 $img.attr( { 321 'src': variation.image.src, 322 'srcset': variation.image.srcset ? variation.image.srcset : '', 323 'sizes': variation.image.sizes ? variation.image.sizes : '' 324 } ); 325 } else { 326 $img.attr( { 327 'src': $img.data( 'src-old' ), 328 'srcset': $img.data( 'srcset-old' ), 329 'sizes': $img.data( 'sizes-old' ) 330 } ); 331 } 332 } 333 // Swap price if a full variation found (all attributes present in the item and selected) 334 // or change back the original price if a variation with selected atributes is not found 335 if ( trx_addons_apply_filters( 'trx_addons_filter_swap_price_for_variable_products', is_full_variation_present( variations, variation_sel_count ) ) ) { 336 var $price = $product.find( '.price_wrap' ); 337 if ( ! $price.length ) { 338 $price = $product.find( '.price' ); 339 } 340 if ( $price.length ) { 341 if ( ! $price.data( 'price-old' ) ) { 342 $price.data( { 343 'price-old': $price.html() 344 } ); 345 } 346 if ( variation ) { 347 // A 'price_html' can be empty if all variations have an equal price. 348 // In this case leave a price unchanged. 349 if ( variation.price_html != '' ) { 350 $price.html( $price.hasClass( 'price_wrap' ) ? variation.price_html : jQuery( variation.price_html ).html() ); 351 } 352 } else { 353 $price.html( $price.data( 'price-old' ) ); 354 } 355 } 356 } 357 } 358 } 359 } ); 360 } 361 362 363 // WooCommerce Search Widget 364 //---------------------------------------------------------- 365 366 var reopen_after_reload = trx_addons_apply_filters( 'trx_addons_filter_reopen_filter_after_reload_products', false ), 367 reopened = false, 368 reload_is_busy = false, 369 reload_is_allowed = true, 370 last_clicked_item = '', 371 last_opened_filter = '', 372 inline_css_selector = trx_addons_apply_filters( 'trx_addons_filter_reload_inline_css_selector', '#trx_addons-inline-styles-inline-css' ), 373 inline_css_wrap = jQuery( inline_css_selector ), 374 inline_css_start = trx_addons_apply_filters( 'trx_addons_filter_reload_inline_css_start', '#woocommerce_output_start{}' ), 375 inline_css_end = trx_addons_apply_filters( 'trx_addons_filter_reload_inline_css_end', '#woocommerce_output_end{}' ), 376 list_products_selector = trx_addons_apply_filters( 'trx_addons_filter_reload_products_selector', '.list_products' ), 377 list_products_wrap = jQuery( list_products_selector ), 378 list_products_loading = 0, 379 list_products_loading_class = '.trx_addons_loading', 380 list_products_loading_selector = trx_addons_apply_filters( 'trx_addons_filter_reload_products_loading_wrap_selector', 381 '.list_products .products,.list_products .woocommerce-info' 382 ), 383 list_products_loading_html = trx_addons_apply_filters( 'trx_addons_filter_reload_products_loading_html', 384 '<div class="trx_addons_loading"></div>' 385 ), 386 single_product_selector = trx_addons_apply_filters( 'trx_addons_filter_single_product_selector', '.post_item_single.post_type_product' ), 387 mask_fields_wrap_on_loading = true; 388 389 $document.on( 'action.init_hidden_elements', function() { 390 391 // Reinit search form after page reloaded 392 var search_forms = jQuery('.trx_addons_woocommerce_search_form:not(.inited)'); 393 if ( search_forms.length ) { 394 search_forms.each( function() { 395 var form = jQuery(this).addClass('inited'), 396 widget = form.parents('.trx_addons_woocommerce_search'), 397 type = widget.hasClass( 'trx_addons_woocommerce_search_type_filter') 398 ? 'filter' 399 : ( widget.hasClass( 'trx_addons_woocommerce_search_type_form') 400 ? 'form' 401 : 'inline' 402 ), 403 apply = widget.hasClass( 'trx_addons_woocommerce_search_apply' ),
404 ajax = widget.hasClass( 'trx_addons_woocommerce_search_ajax' ), 405 number = widget.data('number') || 1, 406 expanded = widget.data('expanded') || 0; 407 408 form 409 // Submit form ('inline' or 'form' styles) 410 .on('submit', function(e) { 411 var shop_url = form.attr('action'); 412 var params = trx_addons_woocommerce_search_form_get_params(form); 413 if (params !== false) { 414 window.location.href = trx_addons_add_to_url(shop_url, params); 415 } else { 416 e.preventDefault(); 417 } 418 return false; 419 }) 420 // Enable/Disable submit button ('inline' or 'form' styles) 421 .on('change', 'select,input', function(e) { 422 var button = form.find('.trx_addons_woocommerce_search_button'); 423 if ( button.length ) { 424 var params = trx_addons_woocommerce_search_form_get_params(form); 425 if (params === false) { 426 button.attr('disable', 'disable'); 427 } else { 428 button.removeAttr('disable'); 429 } 430 } 431 }) 432 // Open/Close dropdown with items ('inline' style) 433 .on('click', '.trx_addons_woocommerce_search_form_field_label', function(e) { 434 jQuery(this) 435 .parent('.trx_addons_woocommerce_search_form_field') 436 .siblings('.trx_addons_woocommerce_search_form_field') 437 .find('.trx_addons_woocommerce_search_form_field_list').slideUp(); 438 jQuery(this).siblings('.trx_addons_woocommerce_search_form_field_list').slideToggle(); 439 e.preventDefault(); 440 return false; 441 }) 442 // Select item in the 'inline' form 443 .on('click', '.trx_addons_woocommerce_search_form_field_list li', function(e) { 444 var list = jQuery(this).parent(); 445 list.siblings('.trx_addons_woocommerce_search_form_field_label').html(jQuery(this).html()); 446 list.siblings('input[type="hidden"]').val(jQuery(this).data('value')); 447 list.slideUp(); 448 e.preventDefault(); 449 return false; 450 }) 451 // Keypress on the field's title 452 .on('keyup', '.sc_form_field_title', function(e) { 453 if ( type == 'filter' ) { 454 if ( e.keyCode == 13 ) { 455 jQuery(this).trigger('click'); 456 } 457 } 458 }) 459 // Open/Close dropdown with items ('filter' style) 460 .on('click', '.sc_form_field_title', function(e) { 461 if ( type == 'filter' ) { 462 var $self = jQuery(this), 463 field = $self.parent(), 464 opened = field.hasClass('sc_form_field_opened'); 465 // Close all opened fields 466 if ( $self.parents('.trx_addons_woocommerce_tools').length !== 0 467 && $self.parents('.trx_addons_woocommerce_search_form_fields_wrap_opened').length === 0 468 ) { 469 $self 470 .parents('.trx_addons_woocommerce_search_form_fields_wrap') 471 .find('.sc_form_field_opened').each( function() { 472 var cur_field = jQuery( this ), 473 cur_param = cur_field.find( '.sc_form_field_param' ), 474 cur_apply = cur_field.find( '.trx_addons_search_apply' ), 475 need_apply = apply && cur_apply.length && ! cur_apply.attr('disabled') && cur_param.data('changed'); 476 if ( need_apply ) { 477 cur_apply.trigger( 'click' ); 478 } else { 479 trx_addons_woocommerce_search_form_close_field( cur_field ); // , apply 480 } 481 } ); 482 } else if ( opened ) { 483 var cur_apply = field.find( '.trx_addons_search_apply' ), 484 cur_param = field.find( '.sc_form_field_param' ), 485 need_apply = apply && cur_apply.length && ! cur_apply.attr('disabled') && cur_param.data('changed'); 486 if ( false && need_apply ) { // Don't apply on click a title in sidebar or mobile 487 cur_apply.trigger( 'click' ); 488 } else { 489 trx_addons_woocommerce_search_form_close_field( field ); // , apply 490 } 491 } 492 // Open clicked field 493 if ( ! opened ) { 494 trx_addons_woocommerce_search_form_open_field( field ); 495 } 496 e.preventDefault(); 497 return false; 498 } 499 }) 500 // Keypress on the field's title 501 .on('keyup', '.sc_form_field_item', function(e) { 502 if ( type == 'filter' ) { 503 if ( e.keyCode == 13 ) { 504 jQuery(this).trigger('click'); 505 } else if ( e.keyCode == 27 ) { 506 jQuery(this).parents('.sc_form_field').find('.sc_form_field_title').focus().trigger('click'); 507 } 508 } 509 }) 510 // Check item in the select ('filter' style) 511 .on('click', '.sc_form_field .sc_form_field_item', function(e) { 512 if ( type == 'filter' ) { 513 var $self = jQuery(this), 514 wrap = $self.parents('.sc_form_field_wrap'), 515 multi = $self.parents('.sc_form_field').data('multiple') == '1'; 516 last_clicked_item = $self.data('value'); 517 if ( ! multi ) { 518 wrap.find('.sc_form_field_item_checked').removeClass('sc_form_field_item_checked'); 519 } 520 $self.toggleClass('sc_form_field_item_checked'); 521 var value = trx_addons_woocommerce_search_form_get_multiple_field_value( wrap ); 522 wrap.find( '.sc_form_field_param' ).val( value ).trigger( 'change' ); 523 e.preventDefault(); 524 return false;
525 } 526 }) 527 // All fields: Keypress on the 'Clear all' button 528 .on('keyup', '.trx_addons_woocommerce_search_clear_all', function(e) { 529 if ( type == 'filter' ) { 530 if ( e.keyCode == 27 ) { 531 jQuery(this).siblings('.trx_addons_woocommerce_search_close').trigger('click'); 532 } 533 } 534 }) 535 // All fields: Clear all selected items in the multiselect fields 536 .on('click', '.trx_addons_woocommerce_search_clear_all', function(e) { 537 e.preventDefault(); 538 if ( type == 'filter' ) { 539 var need_reload = false; 540 reload_is_allowed = false; 541 form.find('.sc_form_field').each( function() { 542 var $self = jQuery(this), 543 param = $self.find( '.sc_form_field_param' ); 544 if ( param.attr('name') != 'product_cat' && param.val() !== '' ) { 545 var field = param.parents('.sc_form_field'), 546 value = ''; 547 if ( field.hasClass( 'sc_form_field_slider' ) ) { 548 value = param.next().data('min'); 549 } else if ( field.hasClass( 'sc_form_field_range' ) ) { 550 value = param.next().data('min') + ',' + param.next().data('max'); 551 } 552 need_reload = true; 553 param.val( value ).trigger( 'change' ); 554 trx_addons_woocommerce_search_form_restore_field_view( $self ); 555 } 556 } ); 557 reload_is_allowed = true; 558 if ( need_reload ) { 559 trx_addons_woocommerce_search_form_reload_products( form ); 560 } 561 } 562 return false; 563 }) 564 // Keypress on the 'Clear all' button 565 .on('keyup', '.sc_form_field_items_selected_clear', function(e) { 566 if ( type == 'filter' ) { 567 if ( e.keyCode == 27 ) { 568 jQuery(this).parents('.sc_form_field').find('.sc_form_field_title').focus().trigger('click'); 569 } 570 } 571 }) 572 // Clear all selected items in the multiselect fields 573 .on('click', '.sc_form_field_items_selected_clear', function(e) { 574 e.preventDefault(); 575 if ( type == 'filter' ) { 576 var $self = jQuery(this), 577 wrap = $self.parents('.sc_form_field_wrap'); 578 wrap.find( '.sc_form_field_param' ).val( '' ).trigger( 'change' ); 579 trx_addons_woocommerce_search_form_restore_field_view( wrap.parents( '.sc_form_field' ) ); 580 } 581 return false; 582 }) 583 // Keypress on the 'Select all' button 584 .on('keyup', '.sc_form_field_items_selected_select_all', function(e) { 585 if ( type == 'filter' ) { 586 if ( e.keyCode == 27 ) { 587 jQuery(this).parents('.sc_form_field').find('.sc_form_field_title').focus().trigger('click'); 588 } 589 } 590 }) 591 // Select all items in the multiselect fields 592 .on('click', '.sc_form_field_items_selected_select_all', function(e) { 593 e.preventDefault(); 594 if ( type == 'filter' ) { 595 var $self = jQuery(this), 596 wrap = $self.parents('.sc_form_field_wrap'); 597 wrap.find('.sc_form_field_item').toggleClass('sc_form_field_item_checked', true); 598 var value = trx_addons_woocommerce_search_form_get_multiple_field_value( wrap ); 599 wrap.find( '.sc_form_field_param' ).val( value ).trigger( 'change' ); 600 } 601 return false; 602 }) 603 // Update 'param' field on change text ('filter' style) 604 .on('change', '.sc_form_field_text .sc_form_field_input', function(e) { 605 if ( type == 'filter' ) { 606 var $self = jQuery(this), 607 wrap = $self.parents('.sc_form_field_wrap'); 608 wrap.find( '.sc_form_field_param' ).val( $self.val() ).trigger( 'change' ); 609 } 610 }) 611 // Prevent submit form on Enter in the text field 612 .on('keydown', '.sc_form_field_text .sc_form_field_input', function(e) { 613 if ( type == 'filter' ) { 614 var $buttons = jQuery(this).parents('.sc_form_field_wrap').find('.sc_form_field_buttons'); 615 if ( $buttons.length ) { 616 if ( e.keyCode == 13 ) { 617 var $apply = $buttons.find('.trx_addons_search_apply'); 618 if ( $apply.length ) { 619 e.preventDefault(); 620 jQuery(this).trigger('change'); 621 $apply.trigger('click'); 622 } 623 } else if ( e.keyCode == 27 ) { 624 var $cancel = $buttons.find('.trx_addons_search_cancel'); 625 if ( $cancel.length ) { 626 e.preventDefault(); 627 $cancel.trigger('click'); 628 } 629 } 630 } 631 } 632 }) 633 // Update result in the price range ('filter' style) 634 .on('change', '.sc_form_field_price .sc_form_field_param', function(e) { 635 if ( type == 'filter' ) { 636 var $self = jQuery(this), 637 value = $self.val().split(','), 638 slider = $self.next('.trx_addons_range_slider '), 639 result = slider.next('.trx_addons_range_result'); 640 if ( result.length ) { 641 result.find('.trx_addons_range_result_value').html( value[0] + ( value.length > 1 ? ' - ' + value[1] : '' ) ); 642 } 643 } 644 }) 645 // Mark title as filled and update selected total value on param changed ('filter' style) 646 .on('change', '.sc_form_field_param', function(e) { 647 if ( type == 'filter' ) { 648 var $self = jQuery(this).data('changed', 1); 649 $self.parents('.trx_addons_woocommerce_search_form_fields_wrap').toggleClass('trx_addons_woocommerce_search_form_fields_changed', true); 650 trx_addons_woocommerce_search_form_param_changed( $self ); 651 } 652 } ) 653 // Remove attr 'disabled' from buttons 'Apply' and 'Cancel' on param changed ('filter' style) 654 .on('change', '.sc_form_field_param', function(e) { 655 if ( type == 'filter' ) { 656 var field_wrap = jQuery(this).parents('.sc_form_field_wrap'); 657 field_wrap.find('.trx_addons_search_apply').removeAttr('disabled'); 658 field_wrap.find('.trx_addons_search_cancel').removeAttr('disabled'); 659 } 660 } ) 661 // Update available (filtered) products counter on param changed ('filter' style) 662 .on('change', '.sc_form_field_param', function(e) { 663 var $self = jQuery(this), 664 fields_wrap = $self.parents('.trx_addons_woocommerce_search_form_fields_wrap').data('param-changed', 1); 665 if ( type == 'filter' 666 && $self.attr('name') != 'product_cat' 667 && ( fields_wrap.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 668 || 669 ( fields_wrap.parents('.sidebar').length && ( apply || ajax ) ) 670 ) 671 ) { 672 trx_addons_woocommerce_search_form_update_available_products_counter( form ); 673 } 674 } ) 675 // Reload page on any param are changed, except 's' and 'price' ('filter' style) 676 .on('change', '.sc_form_field_param', function(e) { 677 var $self = jQuery(this); 678 if ( type == 'filter' && reload_is_allowed ) { 679 if ( $self.attr('name') == 'product_cat' ) { 680 var $fld = $self.parents('.sc_form_field_opened'), 681 fld_idx = $fld.index(); 682 if ( fld_idx > expanded ) { 683 trx_addons_woocommerce_search_form_close_field( $fld ); 684 } 685 trx_addons_woocommerce_search_form_reload_products( form, $self, '.sc_form_field_param[name="product_cat"]', true ); 686 } else if ( ! apply 687 && ! $self.parents('.trx_addons_woocommerce_search_form_fields_wrap').hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 688 && ( ['price', 's'].indexOf($self.attr('name')) == -1 || $self.parents('.sidebar').length ) 689 ) { 690 trx_addons_woocommerce_search_form_reload_products_after_timeout( form, $self ); 691 } 692 } 693 }) 694 // Reload page on click button 'Apply' ('filter' style) 695 .on('click', '.trx_addons_search_apply', function(e) { 696 if ( type == 'filter' ) { 697 var $self = jQuery(this); 698 if ( ! $self.attr('disabled') ) { 699 if ( ! $self.parents('.trx_addons_woocommerce_search_form_fields_wrap').hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 700 && $self.parents('.sidebar').length === 0 701 ) { 702 trx_addons_woocommerce_search_form_close_field( $self.parents('.sc_form_field_opened') ); 703 } 704 trx_addons_woocommerce_search_form_reload_products( form, $self.parents('.sc_form_field').find('.sc_form_field_param') ); 705 } 706 e.preventDefault(); 707 return false;
708 } 709 }) 710 // Restore values on click button 'Cancel' ('filter' style) 711 .on('click', '.trx_addons_search_cancel', function(e) { 712 if ( type == 'filter' ) { 713 var $self = jQuery(this); 714 if ( ! $self.attr('disabled') ) { 715 trx_addons_woocommerce_search_form_close_field( $self.parents('.sc_form_field_opened'), true ); 716 } 717 e.preventDefault(); 718 return false; 719 } 720 }) 721 // Open/Close panel with filters 722 .on('click', '.trx_addons_woocommerce_search_button_filters,' 723 +'.trx_addons_woocommerce_search_button_show,' 724 +'.trx_addons_woocommerce_search_close', 725 function(e) { 726 var $self = jQuery(this), 727 applied = false, 728 wrap = form.find('.trx_addons_woocommerce_search_form_fields_wrap'); 729 if ( $self.hasClass('trx_addons_woocommerce_search_button_filters') && ! wrap.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') ) { 730 wrap.addClass('trx_addons_woocommerce_search_form_fields_wrap_opened'); 731 } else { 732 if ( wrap.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 733 || 734 ( $self.parents('.sidebar').length 735 && 736 $self.parents('.trx_addons_woocommerce_search').hasClass('trx_addons_woocommerce_search_apply') 737 ) 738 ) { 739 if ( wrap.data('param-changed') > 0 ) { 740 var last_apply = last_opened_filter 741 ? form.find('input[name="'+last_opened_filter+'"]') 742 .parents('.sc_form_field') 743 .find('.trx_addons_search_apply:not([disabled])') 744 : false; 745 if ( ! last_apply || ! last_apply.length || last_apply.attr('disabled') == 'disabled' ) { 746 last_apply = form.find('.trx_addons_search_apply:not([disabled])').eq(0); 747 } 748 applied = true; 749 if ( last_apply && last_apply.length && last_apply.attr('disabled') != 'disabled' ) { 750 last_apply.trigger( 'click' ); 751 } else { 752 trx_addons_woocommerce_search_form_reload_products( form ); 753 } 754 } 755 } 756 if ( true || ! applied ) { // Always hide fields wrap on mobile 757 wrap.removeClass('trx_addons_woocommerce_search_form_fields_wrap_opened'); 758 } 759 } 760 e.preventDefault(); 761 return false; 762 } 763 ); 764 765 // Open initially toggled fields 766 if ( form.parents('.sidebar').length > 0 ) { 767 form.find('.sc_form_field_expanded').each( function() { 768 trx_addons_woocommerce_search_form_open_field( jQuery(this).removeClass('sc_form_field_expanded'), 'show', false ); 769 } ); 770 } 771 772 // Show/hide 'Clear all' on fields changed (exclude the field "Product category") 773 form.find('.trx_addons_woocommerce_search_form_fields_wrap') 774 .toggleClass( 'trx_addons_woocommerce_search_form_fields_filled', 775 form.find('.sc_form_field:not(.sc_form_field_product_cat) .sc_form_field_title_filled').length > 0 776 ); 777 778 // Reopen filter after reload products page ('filter' style) 779 if ( type == 'filter' && ! reopened ) { 780 reopened = true; 781 last_opened_filter = trx_addons_get_value_gp( 'last_filter' ); 782 if ( last_opened_filter ) { 783 var last_stop = trx_addons_get_value_gp( 'last_stop' ); 784 if ( last_stop > 0 ) { 785 trx_addons_document_animate_to( last_stop ); 786 } 787 if ( reopen_after_reload && ! apply ) { 788 // Reopen fields wrap on mobile 789 /* 790 var fields_wrap = form.find('.trx_addons_woocommerce_search_form_fields_wrap'); 791 if ( fields_wrap.length && ! fields_wrap.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') && fields_wrap.css('position') == 'fixed' ) { 792 fields_wrap.addClass('trx_addons_woocommerce_search_form_fields_wrap_opened trx_addons_woocommerce_search_form_fields_wrap_show'); 793 setTimeout( function() { 794 fields_wrap.removeClass('trx_addons_woocommerce_search_form_fields_wrap_show'); 795 }, 500 ); 796 } 797 */ 798 // Reopen last active field 799 var field = form.find('input[name="'+last_opened_filter+'"]').parents('.sc_form_field'); 800 trx_addons_woocommerce_search_form_open_field( field ); 801 last_clicked_item = trx_addons_get_value_gp( 'last_item' ); 802 if ( last_clicked_item ) { 803 field.find('.sc_form_field_item[data-value="'+last_clicked_item+'"]').focus(); 804 } 805 } 806 } 807 } 808 809 // Reload products ('filter' style) 810 function trx_addons_woocommerce_search_form_reload_products( form, filter, selector, force_reload_page ) { 811 // Get all filters to the params array 812 if ( selector === undefined ) { 813 selector = '.sc_form_field_param'; 814 } 815 var params = trx_addons_woocommerce_search_form_get_params( form, selector ); 816 // If any filter selected 817 if ( params !== false ) { 818 // Close last filter field 819 if ( filter 820 && ( ! ajax || apply || force_reload_page ) 821 && filter.parents('.trx_addons_woocommerce_tools').length !== 0 822 && filter.parents('.trx_addons_woocommerce_search_form_fields_wrap_opened').length === 0 823 ) { 824 trx_addons_woocommerce_search_form_close_field( filter.parents('.sc_form_field_opened') ); 825 }
826 // Add filter's name and window scroll position to the parameters 827 if ( last_opened_filter ) { 828 params['last_filter'] = last_opened_filter; 829 } 830 if ( last_clicked_item ) { 831 params['last_item'] = last_clicked_item; 832 } 833 // Add scroll top position to the query 834 params['last_stop'] = jQuery(window).scrollTop(); 835 // Make query url 836 var shop_url = trx_addons_add_to_url(form.attr('action'), params), 837 fields_wrap = form.find('.trx_addons_woocommerce_search_form_fields_wrap').data('param-changed', 0); 838 // Reload products by AJAX 839 if ( ajax && ! force_reload_page ) { 840 trx_addons_woocommerce_search_form_get_products_from_url( shop_url, fields_wrap ); 841 // Redirect page 842 } else { 843 jQuery( list_products_loading_selector ) 844 .addClass( 'trx_addons_woocommerce_search_loading' ) 845 .append( list_products_loading_html ); 846 window.location.href = shop_url; 847 } 848 } 849 } 850 851 // Reload products after timeout ('filter' style) 852 var trx_addons_woocommerce_search_form_reload_products_after_timeout = trx_addons_throttle( 853 trx_addons_woocommerce_search_form_reload_products, 854 trx_addons_apply_filters( 'trx_addons_filter_reload_products_timeout', ajax ? 500 : 0 ), 855 true 856 ); 857 858 // Update available products counter ('filter' style) 859 var trx_addons_woocommerce_search_form_update_available_products_counter = trx_addons_throttle( 860 function( form ) { 861 // Get all filters to the params array 862 var params = trx_addons_woocommerce_search_form_get_params( form, '.sc_form_field_param' ); 863 // If any filter selected 864 if ( params !== false ) { 865 var shop_url = trx_addons_add_to_url(form.attr('action'), params), 866 counter = form.find('.trx_addons_woocommerce_search_button_show_total'); 867 if ( counter.length ) { 868 jQuery.get( shop_url ).done( function( response ) { 869 var value = jQuery( response ).find('.trx_addons_woocommerce_search_button_show_total').html(); 870 if ( value !== '') { 871 counter.html( value ); 872 } 873 } ); 874 } 875 } 876 }, 877 trx_addons_apply_filters( 'trx_addons_filter_update_products_counter_timeout', 500 ), 878 true 879 ); 880 881 }); 882 } 883 884 // WooCommerce NavFilters: AJAX reload products on filter removed (if woocommerce_search_filter is present near this widget) 885 jQuery( '.widget_layered_nav_filters:not(.trx_addons_woocommerce_search_compatibility_inited)' ).each( function() { 886 var $self = jQuery(this).addClass('trx_addons_woocommerce_search_compatibility_inited'), 887 widget_search = $self.siblings('.widget_woocommerce_search'); 888 // Allow AJAX reload on filter removed only if widget 'Product Filter' is present on same sidebar 889 // and ajax search is turned on for the widget 890 // and field 'rating' is not present in the fields set (because WooCommerce don't mark this field in the widget Active filters) 891 if ( widget_search.length 892 && widget_search.find('.trx_addons_woocommerce_search_ajax').length 893 && widget_search.find('.sc_form_field_rating').length === 0 894 ) { 895 $self.find('a').on('click', function(e) { 896 var $link = jQuery(this), 897 list = $link.parents('ul').eq(0), 898 url = location.href; //$link.attr('href'); 899 e.preventDefault(); 900 // Remove current filter from active filters 901 $link.parents('li').eq(0).fadeOut( 200, function() { 902 jQuery(this).remove(); 903 // Prepare url - remove not exists filters 904 var parts = url.split('?'), 905 query = trx_addons_parse_query_string(url), 906 query_new = {}, 907 query_val = [], 908 query_val_new = []; 909 for (var i in query) { 910 if ( i == 'min_price' || i == 'max_price' ) { 911 list.find('.chosen .amount').each( function() { 912 if ( jQuery(this).text().replace(/[\$\.\,]/g, '') == query[i].replace(/[\$\.\,]/g, '') ) { 913 query_new[i] = query[i]; 914 } 915 } ); 916 } else if ( i.substring(0, 7) == 'filter_' ) { 917 query_val = query[i].split(','); 918 query_val_new = []; 919 for (var j = 0; j < query_val.length; j++ ) { 920 if ( list.find('.chosen-' + i.substring(7) + '-' +query_val[j] ).length ) { 921 query_val_new.push( query_val[j] ); 922 } 923 } 924 if ( query_val_new.length > 0 ) { 925 query_new[i] = query_val_new.join(','); 926 } 927 } else { 928 query_new[i] = query[i]; 929 } 930 } 931 trx_addons_woocommerce_search_form_get_products_from_url( trx_addons_add_to_url( parts[0], trx_addons_woocommerce_search_form_add_orderby_to_query_params( query_new ) ) ); 932 } ); 933 return false;
934 } ); 935 } 936 } ); 937 938 // WooCommerce Ordering: Reload products on 'orderby' is changed 939 jQuery('.woocommerce-ordering:not(.trx_addons_woocommerce_search_compatibility_inited)').each( function() { 940 var form = jQuery( this ).addClass( 'trx_addons_woocommerce_search_compatibility_inited' ), 941 select = form.find( '.orderby' ), 942 widget_search = list_products_wrap.find( '.widget_woocommerce_search' ), 943 url = location.href.split( '?' ); 944 if ( widget_search.length === 0 ) { 945 widget_search = jQuery( '.trx_addons_woocommerce_search_type_filter' ).eq(0).parents( '.widget_woocommerce_search' ); 946 } 947 if ( widget_search.length ) { 948 select.on( 'change', function(e) { 949 var params = trx_addons_woocommerce_search_form_get_params( widget_search, '.sc_form_field_param' ); 950 /* 951 var page = form.find('input[name="paged"]').val() || 1; 952 if ( page > 1 ) { 953 params['page'] = page; 954 } 955 */ 956 // Prevent to execute other scripts 957 e.preventDefault(); 958 e.stopImmediatePropagation(); 959 // If a widget with filters present and its mode is AJAX - get products via AJAX 960 if ( widget_search.find('.trx_addons_woocommerce_search_ajax').length ) { 961 trx_addons_woocommerce_search_form_get_products_from_url( trx_addons_add_to_url( url[0], params ) ); 962 // Else - reload a current page with new params 963 } else { 964 jQuery( list_products_loading_selector ) 965 .addClass( 'trx_addons_woocommerce_search_loading' ) 966 .append( list_products_loading_html ); 967 window.location.href = trx_addons_add_to_url( url[0], params ); 968 } 969 return false; 970 } ); 971 } 972 } ); 973 974 // Open field ('filter' style) 975 function trx_addons_woocommerce_search_form_open_field( field, open_style, need_focus ) { 976 if ( open_style === undefined ) { 977 open_style = 'slideDown'; 978 } 979 if ( need_focus === undefined ) { 980 need_focus = true; 981 } 982 var open_time = open_style == 'show' 983 ? 0 984 : trx_addons_apply_filters( 'trx_addons_filter_woocommerce_filter_show_time', field.parents('.trx_addons_woocommerce_tools').length === 0 || field.parents('.trx_addons_woocommerce_search_form_fields_wrap_opened').length > 0 ? 300 : 0 ); 985 // Save current value 986 var param = field.find( '.sc_form_field_param' ); 987 param.data( 'old-value', param.val() ); 988 // Mark field unchanged 989 param.data( 'changed', 0 ); 990 // Save last opened filter 991 last_opened_filter = param.attr('name'); 992 // Set buttons 'Apply' and 'Cancel' to disabled 993 if ( false && field.parents('.trx_addons_woocommerce_search').hasClass('trx_addons_woocommerce_search_apply') ) { 994 field.find('.trx_addons_search_apply').attr('disabled', 'disabled'); 995 field.find('.trx_addons_search_cancel').attr('disabled', 'disabled'); 996 } 997 // Open field wrap 998 var wrap = field.addClass('sc_form_field_opened').find('.sc_form_field_wrap'); 999 if ( open_style == 'slideDown' && open_time > 0 ) { 1000 wrap.slideDown( open_time, function() { 1001 if ( need_focus && field.hasClass('sc_form_field_text') ) { 1002 field.find( 'input[type="text"]').focus(); 1003 } 1004 } ); 1005 } else { 1006 wrap.show(); 1007 if ( need_focus && field.hasClass('sc_form_field_text') ) { 1008 field.find( 'input[type="text"]').focus(); 1009 } 1010 } 1011 } 1012 1013 // Close field ('filter' style) 1014 function trx_addons_woocommerce_search_form_close_field( field, restore_value, close_style ) { 1015 // Restore last value 1016 if ( restore_value ) { 1017 trx_addons_woocommerce_search_form_restore_field_value( field ); 1018 } 1019 last_opened_filter = ''; 1020 // Close field wrap 1021 field.removeClass('sc_form_field_opened'); 1022 if ( close_style === undefined ) { 1023 close_style = 'slideUp'; 1024 } 1025 var close_time = close_style == 'hide' 1026 ? 0 1027 : trx_addons_apply_filters( 'trx_addons_filter_woocommerce_filter_show_time', field.parents('.trx_addons_woocommerce_tools').length === 0 || field.parents('.trx_addons_woocommerce_search_form_fields_wrap_opened').length > 0 ? 300 : 0 ); 1028 if ( close_style == 'slideUp' && close_time > 0 ) {
1029 field.find('.sc_form_field_wrap').slideUp( close_time ); 1030 } else { 1031 field.find('.sc_form_field_wrap').hide(); 1032 } 1033 } 1034 1035 // Restore field's value ('filter' style) 1036 function trx_addons_woocommerce_search_form_restore_field_value( field ) { 1037 var param = field.find('.sc_form_field_param'), 1038 value = param.data('old-value'); 1039 // Restore old value in the param 1040 param.val( value ); 1041 // Mark title as filled and update selected items and restore field's view (mark items as checked, restore search string, etc.) 1042 trx_addons_woocommerce_search_form_restore_field_view( field ); 1043 } 1044 1045 // Restore field's view ('filter' style) 1046 function trx_addons_woocommerce_search_form_restore_field_view( field ) { 1047 var param = field.find('.sc_form_field_param'), 1048 value = param.val(); 1049 // Mark title as filled and update selected items 1050 trx_addons_woocommerce_search_form_param_changed( param ); 1051 // Restore field's view (mark items as checked, restore search string, etc.) 1052 if ( field.hasClass('sc_form_field_select') 1053 || field.hasClass('sc_form_field_image') 1054 || field.hasClass('sc_form_field_color') 1055 || field.hasClass('sc_form_field_button') 1056 ) { 1057 field.find('.sc_form_field_item_checked').removeClass('sc_form_field_item_checked'); 1058 var parts = ( '' + value ).split( ',' ); 1059 for (var i = 0; i < parts.length; i++ ) { 1060 field.find('[data-value="'+parts[i]+'"]').addClass('sc_form_field_item_checked'); 1061 } 1062 } else if ( field.hasClass('sc_form_field_text') ) { 1063 field.find('.sc_form_field_input').val( value ); 1064 } else if ( field.hasClass('sc_form_field_slider') ) { 1065 field.find('.ui-slider').slider( "value", value ); 1066 } else if ( field.hasClass('sc_form_field_range') ) { 1067 field.find('.ui-slider').slider( "values", ( '' + value ).split( ',' ) ); 1068 } 1069 } 1070 1071 // Mark title as filled and update selected total value on param changed ('filter' style) 1072 function trx_addons_woocommerce_search_form_param_changed(param) { 1073 var value = param.val(), 1074 empty = value === '', 1075 title_filled = !empty, 1076 wrap = param.parents('.sc_form_field_wrap'), 1077 title = wrap.siblings('.sc_form_field_title'), 1078 selected = title.find('.sc_form_field_selected_items'), //wrap.siblings('.sc_form_field_selected_items'), 1079 slider = param.next('.trx_addons_range_slider '), 1080 fields_wrap = param.parents('.trx_addons_woocommerce_search_form_fields_wrap'); 1081 // Mark title as 'filled' 1082 if ( slider.length == 1 ) { 1083 var min = slider.data('min'), 1084 max = slider.data('max'), 1085 val = value.split(','); 1086 title_filled = Number( val[0] ) !== Number( min ) || ( val.length > 1 && Number( val[1] ) !== Number( max ) ); 1087 title.toggleClass( 'sc_form_field_title_filled', title_filled ); 1088 } else { 1089 title.toggleClass( 'sc_form_field_title_filled', !empty ); 1090 } 1091 // Update selected items counter 1092 wrap.find('.sc_form_field_items_selected_value').html( value ? value.split(',').length : 0 ); 1093 // Update info with selected items 1094 if ( selected.length 1095 && ( 1096 wrap.parents('.trx_addons_woocommerce_tools').length === 0 1097 || 1098 fields_wrap.hasClass( 'trx_addons_woocommerce_search_form_fields_wrap_opened' ) 1099 ) 1100 ) { 1101 // Get value to display below title 1102 //var value_to_show = decodeURIComponent(''+value).split(',').map(trx_addons_proper).join(', '); 1103 var value_to_show = '', 1104 parts = (''+value).split(','), 1105 $item = null; 1106 for ( var i=0; i<parts.length; i++ ) { 1107 // Try find a text label (name) of the selected item 1108 $item = wrap.find('.sc_form_field_item[data-value="'+parts[i]+'"] .sc_form_field_item_text'); 1109 value_to_show += ( value_to_show ? ', ' : '' ) 1110 + ( $item.length && $item.text() == $item.html() 1111 ? $item.text() // Use text label (name) 1112 : trx_addons_proper( decodeURIComponent( parts[i] ) ) // Use selected slug (value) 1113 ); 1114 } 1115 // Display selected items below title 1116 if ( selected.html() === '' && title_filled ) { 1117 selected.hide().html( value_to_show ).slideDown(); 1118 } else if ( selected.html() !== '' && ! title_filled ) {
1119 selected.slideUp( function() { 1120 selected.html( '' ).show(); 1121 } ); 1122 } else { 1123 selected.html( title_filled ? value_to_show : '' ); 1124 } 1125 } 1126 // Update filters total counters 1127 var filters_total = 0; 1128 fields_wrap.find('.sc_form_field_title_filled + .sc_form_field_wrap .sc_form_field_param').each( function() { 1129 var $self = jQuery( this ), 1130 field = $self.parents('.sc_form_field'); 1131 // Exclude the field "Category" from totals and prevent 'Clear all' to be displayed on category selected 1132 if ( ! field.hasClass('sc_form_field_product_cat') ) { 1133 filters_total += field.hasClass('sc_form_field_text') || field.hasClass('sc_form_field_range') ? 1 : $self.val().split(',').length; 1134 } 1135 }); 1136 fields_wrap.parents('form').find( '.trx_addons_woocommerce_search_button_filters_total' ) 1137 .html( filters_total ) 1138 .toggleClass( 'trx_addons_woocommerce_search_button_filters_total_empty', filters_total === 0 ); 1139 // Show/hide 'Clear all' 1140 fields_wrap.toggleClass( 'trx_addons_woocommerce_search_form_fields_filled', filters_total > 0 ); 1141 } 1142 1143 // Return comma-separated values from the field with multiple select allowed ('filter' style) 1144 function trx_addons_woocommerce_search_form_get_multiple_field_value( $wrap ) { 1145 var value = ''; 1146 $wrap.find('.sc_form_field_item_checked').each( function() { 1147 value += ( value !== '' ? ',' : '' ) + jQuery(this).data('value'); 1148 } ); 1149 return value; 1150 } 1151 1152 // Get products page from specified url and replace products on the page 1153 function trx_addons_woocommerce_search_form_get_products_from_url( shop_url, fields_wrap ) { 1154 reload_is_busy = true; 1155 var need_open_fields_wrap = false, 1156 widget_number = 0; 1157 if ( fields_wrap && fields_wrap.length ) { 1158 if ( mask_fields_wrap_on_loading ) { 1159 if ( ! list_products_loading ) { 1160 fields_wrap.append( list_products_loading_html ); 1161 } 1162 need_open_fields_wrap = false; //fields_wrap.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 1163 // && ! fields_wrap.parents('.trx_addons_woocommerce_search').hasClass('trx_addons_woocommerce_search_apply'); 1164 widget_number = fields_wrap.parents('.trx_addons_woocommerce_search_type_filter').data('number') || 1; 1165 } 1166 fields_wrap.removeClass('trx_addons_woocommerce_search_form_fields_changed'); 1167 } 1168 // Reset a current page number 1169 jQuery( 'input[name="paged"]' ).val( 1 ); 1170 jQuery( '.woocommerce-load-more' ).data( 'page', 1 ).parent().show(); 1171 // Mark products wrapper with 'loading' 1172 if ( ! list_products_loading ) { 1173 jQuery( list_products_loading_selector ) 1174 .addClass( 'trx_addons_woocommerce_search_loading' ) 1175 .append( list_products_loading_html ) 1176 .find('>' + list_products_loading_class) 1177 .addClass('trx_addons_hidden') 1178 .fadeIn(200); 1179 } 1180 list_products_loading++; 1181 // Make GET query to the shop 1182 jQuery.get( shop_url ).done( function( response ) { 1183 list_products_loading--; 1184 if ( list_products_loading ) { 1185 list_products_replace(); 1186 } else { 1187 jQuery( list_products_loading_selector ) 1188 .find('>' + list_products_loading_class) 1189 .fadeOut( 200, function() { 1190 list_products_replace( true ); 1191 } ); 1192 } 1193 function list_products_replace( remove_loading ) { 1194 var $response = jQuery( response ), 1195 new_products = $response.find( list_products_selector ).html(), 1196 new_inline_css = $response.find( inline_css_selector ).html(), 1197 new_total = $response.find('.trx_addons_woocommerce_search_button_show_total').eq(0).text() || 0; 1198 // If products found 1199 if ( new_products ) { 1200 // Replace document url in the browser's address bar 1201 trx_addons_document_set_location( shop_url ); 1202 // Replace products with new items 1203 list_products_wrap.html( new_products ); 1204 // Replace inline css with new styles 1205 if ( inline_css_wrap.length ) { 1206 // If inline css is not found via jQuery - try search with regular expression 1207 if ( ! new_inline_css ) { 1208 var mask = '<style[^>]*id="' + inline_css_selector.substr(1).replace( /\-/g, '\\-' ) + '"[^>]*>([^<]+)</style>'; 1209 var re = new RegExp( mask ); 1210 var matches = response.match( re ); 1211 if ( matches && matches[1] ) { 1212 new_inline_css = matches[1]; 1213 } 1214 } 1215 // If found inline css for list products - replace it 1216 if ( new_inline_css ) { 1217 var new_inline_block_start = new_inline_css.indexOf( inline_css_start ), 1218 new_inline_block_end = new_inline_css.indexOf( inline_css_end ), 1219 new_inline_block = new_inline_block_start >= 0 1220 ? new_inline_css.substring( new_inline_block_start, new_inline_block_end + inline_css_end.length ) 1221 : ''; 1222 if ( new_inline_block ) { 1223 var old_inline_css = inline_css_wrap.html(), 1224 old_inline_block_start = old_inline_css.indexOf( inline_css_start ), 1225 old_inline_block_end = old_inline_css.indexOf( inline_css_end ); 1226 if ( old_inline_block_start ) { 1227 old_inline_css = old_inline_css.substring( 0, old_inline_block_start ) 1228 + new_inline_block 1229 + old_inline_css.substring( old_inline_block_end + inline_css_end.length ); 1230 inline_css_wrap.html( old_inline_css ); 1231 } 1232 } 1233 } 1234 } 1235 // Place back a 'loading' icon 1236 if ( ! remove_loading ) { 1237 jQuery( list_products_loading_selector ) 1238 .toggleClass('trx_addons_woocommerce_search_loading', true) 1239 .append( list_products_loading_html ); 1240 } 1241 // Replace total products counters 1242 jQuery('.trx_addons_woocommerce_search_button_show_total').text(new_total); 1243 // Got new elements 1244 jQuery( document ).trigger( 'action.got_ajax_response', { 1245 action: 'woocommerce_ajax_get_posts', 1246 result: response, 1247 products: list_products_wrap.hasClass('products') 1248 ? list_products_wrap 1249 : list_products_wrap.find('ul.products') 1250 }); 1251 // Init new elements 1252 $document.trigger( 'action.init_hidden_elements', [list_products_wrap] ); 1253 } else { 1254 // If a result is a redirect to the single product - commit it 1255 if ( $response.find( single_product_selector ).length ) { 1256 var url = $response.filter( 'link[rel="canonical"]' ).attr( 'href' ); 1257 if ( url ) { 1258 window.location = url; 1259 return; 1260 } 1261 } 1262 alert( TRX_ADDONS_STORAGE['msg_no_products_found'] ); 1263 } 1264 // Remove 'loading' from products container 1265 if ( remove_loading ) { 1266 jQuery( list_products_loading_selector ) 1267 .removeClass('trx_addons_woocommerce_search_loading') 1268 .find('>' + list_products_loading_class).remove(); 1269 } 1270 // If fields_wrap not empty 1271 if ( widget_number ) { 1272 // Get new object for fields_wrap after products replaced (old fields wrap may be removed) 1273 var widget = jQuery('.trx_addons_woocommerce_search_type_filter[data-number="' + widget_number + '"]').eq(0), 1274 fields_wrap = widget.find('.trx_addons_woocommerce_search_form_fields_wrap').eq(0); 1275 if ( fields_wrap.length ) { 1276 // Remove 'loading' 1277 if ( remove_loading && mask_fields_wrap_on_loading ) { 1278 fields_wrap.find('>' + list_products_loading_class).remove(); 1279 } 1280 // Reopen fields wrap 1281 if ( need_open_fields_wrap ) { 1282 widget.find('.trx_addons_woocommerce_search_form_fields_wrap').addClass('trx_addons_woocommerce_search_form_fields_wrap_opened trx_addons_woocommerce_search_form_fields_wrap_show'); 1283 setTimeout( function() { 1284 widget.find('.trx_addons_woocommerce_search_form_fields_wrap_show').removeClass('trx_addons_woocommerce_search_form_fields_wrap_show'); 1285 }, 500 ); 1286 } 1287 // Reopen last active field 1288 if ( last_opened_filter ) {// && ! fields_wrap.parents('.trx_addons_woocommerce_search ').hasClass('trx_addons_woocommerce_search_apply') ) { 1289 var field = widget.find('input[name="'+last_opened_filter+'"]').parents('.sc_form_field'); 1290 trx_addons_woocommerce_search_form_open_field( field, 'show' ); 1291 if ( last_clicked_item ) { 1292 field.find('.sc_form_field_item[data-value="'+last_clicked_item+'"]').focus(); 1293 } 1294 } 1295 // Clear 'param-changed' data parameter 1296 fields_wrap.data('param-changed', 0); 1297 } 1298 } 1299 reload_is_busy = false;
1300 } 1301 } ); 1302 } 1303 1304 } ); 1305 1306 // Close opened field on click outside the field wrap 1307 var trx_addons_woocommerce_search_form_click_outside = trx_addons_throttle( 1308 function() { 1309 var opened = jQuery('.trx_addons_woocommerce_search_form .sc_form_field_opened').eq(0); 1310 if ( opened.length ) { 1311 var fields_wrap = opened.parents('.trx_addons_woocommerce_search_form_fields_wrap'); 1312 if ( fields_wrap.parents('.trx_addons_woocommerce_tools').length 1313 && ! fields_wrap.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 1314 ) { 1315 opened.find('.sc_form_field_title').trigger('click'); 1316 } else { 1317 fields_wrap.find('.trx_addons_woocommerce_search_button_show').trigger('click'); 1318 } 1319 } 1320 }, 1321 trx_addons_apply_filters( 'trx_addons_filter_woocommerce_search_click_outside_timeout', 10 ), 1322 true 1323 ); 1324 1325 $document.on( 'click', function(e) { 1326 var $self = jQuery( e.target ); 1327 if ( ! $self.hasClass('sc_form_field_wrap') 1328 && $self.parents('.sc_form_field_wrap').length === 0 1329 && $self.parents('.trx_addons_woocommerce_search_form_fields_wrap').length === 0 1330 && ! $self.hasClass(list_products_loading_class) 1331 ) { 1332 trx_addons_woocommerce_search_form_click_outside(); 1333 } 1334 // Close opened fields wrap 1335 /* 1336 if ( ! $self.hasClass('trx_addons_woocommerce_search_form_fields_wrap_opened') 1337 && $self.parents('.trx_addons_woocommerce_search_form_fields_wrap_opened').length === 0 1338 ) { 1339 jQuery('.trx_addons_woocommerce_search_form_fields_wrap_opened').removeClass('trx_addons_woocommerce_search_form_fields_wrap_opened'); 1340 } 1341 */ 1342 } ); 1343 1344 // Collect all form params to array (any style) 1345 function trx_addons_woocommerce_search_form_get_params( form, selector ) { 1346 var params = {}, not_empty = false; 1347 // Collect all filters 1348 form.find( selector ? selector : 'select,input' ).each( function() { 1349 var $self = jQuery(this), 1350 val = $self.val(), 1351 name = $self.attr('name'), 1352 type = ''; 1353 if ( name && ( ( val !== '' && ( '' + val ) !== '0' ) || name == 'product_cat' ) ) { 1354 if ( name == 'price' ) { 1355 var values = val.split(','), 1356 $slider = $self.next('.trx_addons_range_slider '), 1357 min = $slider.data('min'), 1358 max = $slider.data('max'); 1359 if ( values.length == 2 && $slider.length == 1 ) { 1360 if ( Number( values[0] ) > min ) { 1361 params['min_price'] = values[0]; 1362 } 1363 if ( Number( values[1] ) < max ) { 1364 params['max_price'] = values[1]; 1365 } 1366 } 1367 name = ''; 1368 } else if ( name.substring(0, 3) == 'pa_' ) { 1369 type = 'query_type_' + name.substring(3); 1370 name = 'filter_' + name.substring(3); 1371 } else if ( name == 'rating' ) { 1372 name = name + '_filter'; 1373 } 1374 if ( name !== '' ) { 1375 params[name] = val; 1376 } 1377 if ( type !== '' && val.indexOf(',') > 0 ) { 1378 params[type] = 'or'; 1379 } 1380 not_empty = true; 1381 } 1382 } ); 1383 // Add sort order 1384 if ( not_empty || selector ) { 1385 params = trx_addons_woocommerce_search_form_add_orderby_to_query_params( params ); 1386 } 1387 return not_empty || typeof params['orderby'] != 'undefined' ? params : false; 1388 } 1389 1390 // Add sort order to the query params 1391 function trx_addons_woocommerce_search_form_add_orderby_to_query_params( params ) { 1392 var orderby = jQuery('.woocommerce-ordering .orderby').val(); 1393 if ( orderby && orderby != 'menu_order' ) { 1394 params['orderby'] = orderby; 1395 } 1396 return params; 1397 } 1398 1399 }); 1400 1401})();
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.