1/* global jQuery, ALEX_STONE_STORAGE */ 2 3jQuery( document ).on( 'action.ready_alex_stone', function() { 4 5 "use strict"; 6 7 var $window = jQuery( window ), 8 $document = jQuery( document ), 9 $body = jQuery( 'body' ); 10 11 var $list_products_infinite, 12 $list_products_infinite_link, 13 $list_products, 14 _list_products_height; 15 16 // Update links after the new post added 17 $document.on( 'action.new_post_added', update_jquery_links ); 18 $document.on( 'action.got_ajax_response', update_jquery_links ); 19 $document.on( 'action.init_hidden_elements', update_jquery_links ); 20 var first_run = true; 21 function update_jquery_links(e) { 22 if ( first_run && e && e.namespace == 'init_hidden_elements' ) { 23 first_run = false; 24 return; 25 } 26 $list_products_infinite = jQuery( '.woocommerce-links-infinite' ); 27 $list_products_infinite_link = $list_products_infinite.find( '>a' ); 28 $list_products = jQuery( '.list_products > .products' ).eq( 0 ); 29 _list_products_height = $list_products.length > 0 ? $list_products.height() : 0; 30 } 31 update_jquery_links(); 32 33 $document.on( 'action.resize_alex_stone', function() { 34 _list_products_height = $list_products.length > 0 ? $list_products.height() : 0; 35 } ); 36 37 38 // Change display mode 39 //-------------------------------- 40 jQuery( '.woocommerce,.woocommerce-page' ).on( 'click', '.alex_stone_shop_mode_buttons a', function(e) { 41 var $self = jQuery(this), 42 mode = $self.hasClass( 'woocommerce_thumbs' ) ? 'thumbs' : 'list'; 43 alex_stone_set_cookie( 'alex_stone_shop_mode', mode, 365 * 24 * 60 * 60 * 1000 ); // 1 year 44 $self.siblings( 'input' ).val( mode ).parents( 'form' ).get( 0 ).submit(); 45 e.preventDefault(); 46 return false; 47 } ); 48 49 50 // Add buttons to quantity 51 //-------------------------------- 52 var woocomerce_inc_dec = '<span class="q_inc"></span><span class="q_dec"></span>'; 53 54 // On first run 55 alex_stone_woocommerce_add_inc_dec(); 56 57 // After the cart is updated 58 jQuery( document.body ).on( 'updated_wc_div woosq_loaded', function() { 59 alex_stone_woocommerce_add_inc_dec(); 60 } ); 61 62 // After products loaded by AJAX - timeout need to wait for fadeOut on old posts 63 $document.on( 'action.new_post_added action.got_ajax_response qv_loader_stop', function() { 64 setTimeout( function() { 65 alex_stone_woocommerce_add_inc_dec(); 66 }, 500 ); 67 } ); 68 69 // After variations are changed 70 $document.on( 'action.init_hidden_elements', function( e, container ) { 71 alex_stone_woocommerce_add_inc_dec(); 72 container.find( '.variations_form.cart:not(.quantity_inited)' ) 73 .addClass( 'quantity_inited' ) 74 .on( 'wc_variation_form check_variations', function() { 75 setTimeout( function() { 76 alex_stone_woocommerce_add_inc_dec(); 77 }, 100 ); 78 } ); 79 } ); 80 81 82 // Add buttons inc/dec 83 function alex_stone_woocommerce_add_inc_dec() { 84 jQuery( '.woocommerce div.quantity:not(.inited),.woocommerce-page div.quantity:not(.inited),.variations_form.cart div.quantity:not(.inited)' ).each( function() { 85 var $self = jQuery(this).addClass('inited'); 86 // If quantity is not hidden - add Inc/Dec buttons. If quantity is hidden - a total number of product is 1 and buttons are not needed. 87 if ( $self.find('>input[name="quantity"][type="hidden"]').length === 0 ) { 88 if ( $self.find('>.q_inc').length === 0 ) { 89 $self.append( woocomerce_inc_dec ); 90 } 91 $self 92 .on( 'click', '>span', function(e) { 93 alex_stone_woocomerce_inc_dec_click( jQuery( this ) ); 94 e.preventDefault(); 95 return false; 96 } ); 97 } else { 98 $self.addClass( 'hidden last_item_in_stock' ); 99 } 100 } ); 101 } 102 103 // Inc/Dec quantity on buttons inc/dec 104 function alex_stone_woocomerce_inc_dec_click(button) { 105 var f = button.siblings( 'input' ); 106 if (button.hasClass( 'q_inc' )) { 107 f.val( ( f.val() === '' ? 0 : parseInt( f.val(), 10 ) ) + 1 ).trigger( 'change' ); 108 } else { 109 f.val( Math.max( 0, ( f.val() === '' ? 0 : parseInt( f.val(), 10 ) ) - 1 ) ).trigger( 'change' ); 110 } 111 } 112 113 114 // Decorate YITH Compare & Add to wishlist 115 //---------------------------------------------- 116 117 // On the single product page 118 //---------------------------- 119 120 // Move 'Compare' before 'Wishlist' and convert 'Wishlist' to the button (if 'Compare' is a button) 121 var wishlist = jQuery( '.woocommerce .product .summary .yith-wcwl-add-to-wis
121hlist' ).eq( 0 ), 122 compare = jQuery( '.woocommerce .product .summary .compare' ).eq( 0 ); 123 if ( wishlist.length > 0 && compare.length > 0 ) { 124 compare.insertBefore( wishlist ); 125 if ( compare.hasClass( 'button' ) ) { 126 // Add class 'button' to the link (if the link 'Compare' have same class) 127 wishlist.find( '.add_to_wishlist' ).addClass( 'button' ); 128 // Restore class 'button' after any AJAX request 129 $document.on( 'ajaxComplete', function(e) { 130 setTimeout( function() { 131 jQuery( '.woocommerce .product .summary .yith-wcwl-add-to-wishlist .add_to_wishlist' ).eq(0).toggleClass( 'button', true ); 132 }, 10 ); 133 } ); 134 } 135 } 136 137 // Remove class 'button' from links 'Compare' and 'Add to Wishlist' in the 'Related products' on the single product page 138 jQuery( '.single-product ul.products li.product .post_data .yith_buttons_wrap a' ).removeClass( 'button' ); 139 140 // Remove class 'button' from links 'Quick view' on the product archive page 141 jQuery( 'ul.products li.product .post_data .yith-wcqv-button' ).removeClass( 'button' ); 142 $document.on( 'action.new_post_added action.got_ajax_response', function() { 143 jQuery( 'ul.products li.product .post_data .yith-wcqv-button' ).removeClass( 'button' ); 144 } ); 145 146 // On the products list (if 'Add to wishlist' placed before image) 147 //---------------------------------------------------------------- 148 $document.on( 'action.init_hidden_elements', function( e, container ) { 149 150 jQuery( 'ul.products li.product:not(.wishlist_decorated)' ).each( function() { 151 152 var $product = jQuery(this).addClass( 'wishlist_decorated' ), 153 $yith_buttons_wrap = $product.find('.yith_buttons_wrap' ).eq( 0 ); 154 155 if ( $yith_buttons_wrap.length > 0 ) { 156 // Move 'Wishlist' and 'Quick View' to the '.yith_buttons_wrap' after the 'Compare' 157 $product.find( '.yith-wcwl-add-to-wishlist,.yith-wcqv-button,.woosq-btn,.compare' ).each( function() { 158 var $self = jQuery( this ).removeClass( 'button' ); 159 if ( ! $self.parent().hasClass( 'yith_buttons_wrap' ) ) { 160 $yith_buttons_wrap.append( $self ); 161 } 162 } ); 163 164 if ( $product.hasClass( 'add-to-wishlist-before_image' ) ) { 165 166 // Wrap inner text in the links 'Compare' and 'Add to Wishlist' to the span 167 $yith_buttons_wrap.find( 'a' ).wrapInner( '<span class="tooltip"></span>' ); 168 169 // Wrap inner text in the link 'Compare' after click 170 $yith_buttons_wrap.find( '.compare' ).on( 'click', function(e) { 171 var bt = jQuery(this), atts = 10; 172 setTimeout( trx_addons_add_tooltip_to_compare, 500 ); 173 function trx_addons_add_tooltip_to_compare() { 174 if ( bt.hasClass('added') && bt.find('.tooltip').length === 0 ) { 175 bt.wrapInner( '<span class="tooltip"></span>' ); 176 } else if (atts-- > 0) { 177 setTimeout( trx_addons_add_tooltip_to_compare, 500 ); 178 } 179 } 180 } ); 181 182 // Wrap inner text in the link 'Add to Wishlist' after click 183 $yith_buttons_wrap.find( '.add_to_wishlist' ).on( 'click', function(e) { 184 var atts = 30; 185 setTimeout( trx_addons_add_tooltip_to_wishlist, 250 ); 186 function trx_addons_add_tooltip_to_wishlist() { 187 var bt = $yith_buttons_wrap.find('.yith-wcwl-add-to-wishlist a'); 188 if ( bt.find('.tooltip').length === 0 ) { 189 bt.wrapInner( '<span class="tooltip"></span>' ); 190 } else if ( atts-- > 0 ) { 191 setTimeout( trx_addons_add_tooltip_to_wishlist, 250 ); 192 } 193 } 194 } ); 195 196 // Change spinner image in the 'Add to Wishlist' 197 var img = $yith_buttons_wrap.find( '.yith-wcwl-add-button > img' ); 198 if ( img.length > 0 ) { 199 img.each( function() { 200 var $self = jQuery(this), 201 src = $self.attr('src'); 202 if ( src != undefined && src.indexOf('wpspin_light') > 0 ) { 203 $self.attr( 'src', src.replace('wpspin_light', 'ajax-loader') ); 204 } 205 } ); 206 } 207 } 208 } 209 210 } ); 211 212 // 'Load more' and 'Infinite scroll' on the products page 213 jQuery( '.woocommerce-load-more:not(.woocommerce-load-more-inited)' ) 214 .addClass( 'woocommerce-load-more-inited' ) 215 .on( 'click', function(e) { 216 217 e.preventDefault(); 218 219 var more = jQuery( this ); 220 if ( more.data( 'load_more_link_busy' ) ) { 221 return false;
222 } 223 224 var page = Number( more.data( 'page' ) ), 225 max_page = Number( more.data( 'max-page' ) ); 226 if (page >= max_page) { 227 more.parent().addClass( 'all_items_loaded' ).hide(); 228 return false; 229 } 230 231 more.data( 'load_more_link_busy', true ) 232 .parent().addClass( 'loading' ); 233 234 var link = more.parent().next( '.woocommerce-pagination' ).find( '.next' ).attr( 'href' ).replace( /\/page\/[0-9]+/, '/page/' + ( page + 1 ) ); 235 var products = more.parent().prev( '.products' ); 236 237 jQuery.get( link ).done( 238 function(response) { 239 // Get inline styles and add to the page styles 240 alex_stone_import_inline_styles( response ); 241 // Get tags 'link' from response and add its to the 'head' 242 alex_stone_import_tags_link( response ); 243 // Get new posts and append to the .products 244 var posts_container = jQuery( response ).find('.list_products > .products'); 245 if ( posts_container.length === 0 ) { 246 posts_container = jQuery( response ).find('ul.products'); 247 } 248 if ( posts_container.length > 0 ) { 249 products.append( posts_container.find( '> li' ) ); 250 more.data( 'page', page + 1 ).parent().removeClass( 'loading' ); 251 // Trigger actions to init new elements 252 ALEX_STONE_STORAGE['init_all_mediaelements'] = true; 253 $document 254 .trigger( 'action.new_post_added', [products] ) 255 .trigger( 'action.init_hidden_elements', [products] ); 256 } 257 if (page + 1 >= max_page) { 258 more.parent().addClass( 'all_items_loaded' ).hide(); 259 } 260 more.data( 'load_more_link_busy', false ); 261 // Fire 'window.scroll' after clearing busy state 262 $window.trigger( 'scroll' ); 263 264 $document.trigger( 'action.got_ajax_response', { 265 action: 'woocommerce_ajax_get_posts', 266 result: response, 267 products: products 268 }); 269 } 270 ); 271 272 return false; 273 274 } ); 275 276 } ); 277 278 // Infinite scroll on the products page 279 $document.on( 'action.scroll_alex_stone', function(e) { 280 if ( $list_products_infinite.length === 0 || $list_products_infinite_link.length === 0 || $list_products_infinite_link.data( 'load_more_link_busy' ) ) { 281 return; 282 } 283 if ( $list_products.length > 0 && $list_products.offset().top + _list_products_height < alex_stone_window_scroll_top() + alex_stone_window_height() * 1.5 ) { 284 $list_products_infinite_link.trigger( 'click' ); 285 } 286 } ); 287 288 289 // Wrap new select (created dynamically) with .select_container 290 //------------------------------------------------------------------------- 291 jQuery( 'select#calc_shipping_country:not(.inited)' ) 292 .addClass( 'inited' ) 293 .on( 'change', function() { 294 setTimeout( 295 function() { 296 var state = jQuery( 'select#calc_shipping_state' ); 297 if (state.length == 1 && ! state.parent().hasClass( 'select_container' )) { 298 state.wrap( '<div class="select_container"></div>' ); 299 } 300 }, 10 301 ); 302 } ); 303 jQuery( document.body ).on( 'wc_fragments_refreshed wc_fragments_loaded updated_shipping_method update_checkout', function() { 304 jQuery( 'div.cart_totals select' ).each( function() { 305 var $self = jQuery( this ); 306 if ( ! $self.parent().hasClass( 'select_container' )) { 307 $self.wrap( '<div class="select_container"></div>' ); 308 } 309 } ); 310 } ); 311 312 313 // Add hover classes to the buttons created dynamically 314 //------------------------------------------------------------------------- 315 jQuery( document.body ).on( 'wc_fragments_refreshed wc_fragments_loaded update_checkout update_cart added_to_cart removed_from_cart', function() { 316 $document.trigger( 'action.init_hidden_elements', [ $body ] ); 317 } ); 318 319 320 // Update cart 321 //------------------------------------------------------------------------- 322 var table_cart = jQuery( 'table.cart' ); 323 324 jQuery( document.body ).on( 'updated_wc_div', function() { 325 326 alex_stone_create_observer( 'update_woocommerce_cart', table_cart.parents('form').eq(0), function( mutationsList ) { 327 for ( var mutation of mutationsList ) { 328 if ( mutation.type == 'childList' || mutation.type == 'subtree' ) { 329 alex_stone_remove_observer( 'update_woocommerce_cart' );
330 table_cart = jQuery( 'table.cart' ); 331 alex_stone_woocommerce_update_cart( 'update' ); 332 break; 333 } 334 } 335 } ); 336 337 338 if ( table_cart.length > 0 ) { 339 alex_stone_woocommerce_update_cart( 'update' ); 340 } 341 } ); 342 343 // Generate 'scroll' event after the cart is filled 344 jQuery( document.body ).on( 'wc_fragment_refresh', function() { 345 $window.trigger( 'scroll' ); 346 } ); 347 348 // Check device and update cart if need 349 if ( table_cart.length > 0 ) { 350 alex_stone_woocommerce_update_cart( 'init' ); 351 } 352 353 // Resize action 354 $window.on( 'resize', function() { 355 if ( table_cart.length > 0 ) { 356 alex_stone_woocommerce_update_cart( 'resize' ); 357 } 358 } ); 359 360 // Update cart display 361 function alex_stone_woocommerce_update_cart(status){ 362 setTimeout( 363 function() { 364 var w = window.innerWidth; 365 if (w == undefined) { 366 w = alex_stone_window_width() + ( alex_stone_window_height() < $document.height() || alex_stone_window_scroll_top() > 0 ? 16 : 0 ); 367 } 368 369 var tbl = table_cart; 370 371 if ( w < ALEX_STONE_STORAGE['mobile_layout_width'] ) { 372 var $mobile_cell = tbl.find( '.mobile_cell' ); 373 if (status == 'resize' && $mobile_cell.length > 0) { 374 return; 375 } else { 376 if ( tbl.length > 0 ) { 377 if ( $mobile_cell.length === 0 ) { 378 tbl.find( 'thead tr .product-quantity, thead tr .product-subtotal, thead tr .product-thumbnail' ).hide(); 379 if ( tbl.hasClass( 'wishlist_table' ) ) { 380 tbl.find( 'thead tr .product-remove, thead tr .product-stock-status' ).hide(); 381 tbl.find( 'tfoot tr td' ).each( function() { 382 var $self = jQuery( this ); 383 $self.data( 'colspan', $self.attr( 'colspan' ) ).attr( 'colspan', 3 ); 384 } ); 385 } 386 } 387 tbl.find( '.cart_item,[id*="yith-wcwl-row-"]' ).each( function() { 388 var $self = jQuery( this ); 389 if ( $self.find( '>.mobile_cell' ).length == 0 ) { 390 $self.prepend( '<td class="mobile_cell" colspan="3"><table width="100%"><tr class="first_row"></tr><tr class="second_row"></tr></table></td>' ); 391 $self.find( '.first_row' ).append( $self.find( '.product-thumbnail, .product-name, .product-price' ) ); 392 $self.find( '.second_row' ).append( $self.find( '.product-remove, .product-quantity, .product-subtotal, .product-stock-status, .product-add-to-cart' ) ); 393 } 394 } ); 395 if ( ! tbl.hasClass( 'inited' ) ) { 396 tbl.addClass( 'inited' ); 397 } 398 } 399 } 400 } 401 402 if ( w >= ALEX_STONE_STORAGE['mobile_layout_width'] && status == 'resize' && jQuery( 'table.cart .mobile_cell' ).length > 0) { 403 if ( tbl.length > 0 ) { 404 tbl.find( 'thead tr .product-quantity, thead tr .product-subtotal, thead tr .product-thumbnail' ).show(); 405 if ( tbl.hasClass( 'wishlist_table' ) ) { 406 tbl.find( 'thead tr .product-remove, thead tr .product-stock-status' ).show(); 407 tbl.find( 'tfoot tr td' ).each( function() { 408 var $self = jQuery( this ); 409 $self.attr( 'colspan', $self.data( 'colspan' ) ); 410 } ); 411 } 412 tbl.find( '.cart_item,[id*="yith-wcwl-row-"]' ).each( function(){ 413 var $self = jQuery( this ); 414 $self.find( '.first_row td, .second_row td' ).prependTo( $self ); 415 $self.find( '.product-remove' ).prependTo( $self ); 416 $self.find( 'td.mobile_cell' ).remove(); 417 } ); 418 } 419 } 420 421 }, 10 422 ); 423 } 424 425} );
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.