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