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