1'use strict'; 2 3(function($) { 4 var woosb_timeout = null; 5 6 $(function() { 7 if (!$('.woosb-wrap').length) { 8 return; 9 } 10 11 $('.woosb-wrap').each(function() { 12 woosb_init($(this), 'ready'); 13 }); 14 }); 15 16 $(document).on('woosq_loaded', function() { 17 // product bundles in quick view popup 18 woosb_init($('#woosq-popup .woosb-wrap'), 'woosq'); 19 }); 20 21 $(document).on('woovr_selected', function(e, selected, variations) { 22 var $wrap = variations.closest('.woosb-wrap'); 23 var $product = variations.closest('.woosb-product'); 24 25 if ($product.length) { 26 var _id = selected.attr('data-id'); 27 var _price = selected.attr('data-price'); 28 var _regular_price = selected.attr('data-regular-price'); 29 var _price_html = selected.attr('data-pricehtml'); 30 var _image_src = selected.attr('data-imagesrc'); 31 var _purchasable = selected.attr('data-purchasable'); 32 var _attrs = selected.attr('data-attrs'); 33 34 if (_purchasable === 'yes' && _id >= 0) { 35 // change data 36 $product.attr('data-id', _id); 37 $product.attr('data-price', _price); 38 39 // change price 40 woosb_change_price($product, _price, _regular_price, _price_html); 41 42 // change image 43 if (_image_src && _image_src !== '') { 44 $product.find('.woosb-thumb-ori').hide(); 45 $product.find('.woosb-thumb-new'). 46 html('<img src="' + _image_src + '"/>'). 47 show(); 48 } else { 49 $product.find('.woosb-thumb-ori').show(); 50 $product.find('.woosb-thumb-new').html('').hide(); 51 } 52 53 // change attributes 54 $product.attr('data-attrs', _attrs.replace(/\/$/, '')); 55 } else { 56 // reset data 57 $product.attr('data-id', 0); 58 $product.attr('data-price', 0); 59 $product.attr('data-attrs', ''); 60 61 // reset price 62 $product.find('.woosb-price-ori').show(); 63 $product.find('.woosb-price-new').html('').hide(); 64 65 // reset image 66 $product.find('.woosb-thumb-ori').show(); 67 $product.find('.woosb-thumb-new').html('').hide(); 68 } 69 70 // reset sku, weight & dimensions 71 $('.product_meta .sku').wc_reset_content(); 72 $('.product_weight, .woocommerce-product-attributes-item--weight .woocommerce-product-attributes-item__value'). 73 wc_reset_content(); 74 $('.product_dimensions, .woocommerce-product-attributes-item--dimensions .woocommerce-product-attributes-item__value'). 75 wc_reset_content(); 76 } 77 78 woosb_init($wrap, 'woovr_selected'); 79 }); 80 81 $(document).on('found_variation', function(e, t) { 82 var $wrap = $(e['target']).closest('.woosb-wrap'); 83 var $product = $(e['target']).closest('.woosb-product'); 84 85 if ($product.length) { 86 if (t['image']['url'] && t['image']['url'] !== '') { 87 var image_src = t['image']['url']; 88 89 if (t['image']['thumb_src'] && t['image']['thumb_src'] !== '') { 90 image_src = t['image']['thumb_src']; 91 } 92 93 // change image 94 $product.find('.woosb-thumb-ori').hide(); 95 96 if (t['image']['srcset'] && t['image']['srcset'] !== '') { 97 $product.find('.woosb-thumb-new'). 98 html('<img src="' + image_src + '" srcset="' + 99 t['image']['srcset'] + '"/>'). 100 show(); 101 } else { 102 $product.find('.woosb-thumb-new'). 103 html('<img src="' + image_src + '"/>'). 104 show(); 105 } 106 } else { 107 $product.find('.woosb-thumb-ori').show(); 108 $product.find('.woosb-thumb-new').html('').hide(); 109 } 110 111 if (t['price_html'] !== undefined && t['price_html'] !== '' && 112 t['display_price'] !== undefined && t['display_price'] !== '') { 113 woosb_change_price($product, t['display_price'], 114 t['display_regular_price'], t['price_html']); 115 } 116 117 if (t['variation_description'] !== undefined) { 118 $product.find('.woosb-variation-description'). 119 html(t['variation_description']). 120 show(); 121 } else { 122 $product.find('.woosb-variation-description').html('').hide(); 123 } 124 125 if (t['is_purchasable']) { 126 // change the price 127 if (woosb_vars.bundled_price_from === 'regular_price' && 128 t['display_regular_price'] !== undefined) { 129 $product.attr('data-price', t['display_regular_price']); 130 } else { 131 $product.attr('data-price', t['display_price']); 132 } 133 134 // change stock notice 135 if (t['is_in_stock']) { 136 $wrap.next('p.stock').show(); 137 $product.attr('data-id', t['variation_id']); 138 } else { 139 $wrap.next('p.stock').hide(); 140 $product.attr('data-id', 0); 141 } 142 143 // change availability 144 if (t['availability_html'] && t['availability_html'] !== '') { 145 $product.find('.woosb-availability'). 146 html(t['availability_html']). 147 show(); 148 } else { 149 $product.find('.woosb-availability').html('').hide(); 150 } 151 152 // change attributes 153 var attrs = {}; 154 155 $product.find('select[name^="attribute_"]').each(function() { 156 var attr_name = $(this).attr('name'); 157 158 attrs[attr_name] = $(this).val(); 159 }); 160 161 $product.attr('data-attrs', JSON.stringify(attrs)); 162 } 163 164 if (woosb_vars.change_image === 'no') { 165 // prevent changing the main image 166 $(e['target']).closest('.variations_form').trigger('reset_image'); 167 } 168 169 // reset sku, weight & dimensions 170 $('.product_meta .sku').wc_reset_content(); 171 $('.product_weight, .woocommerce-product-attributes-item--weight .woocommerce-product-attributes-item__value'). 172 wc_reset_content(); 173 $('.product_dimensions, .woocommerce-product-attributes-item--dimensions .woocommerce-product-attributes-item__value'). 174 wc_reset_content(); 175 176 $(document).trigger('woosb_found_variation', [$product, t]); 177 178 woosb_init($wrap, 'found_variation'); 179 } 180 }); 181 182 $(document).on('reset_data', function(e) { 183 var $wrap = $(e['target']).closest('.woosb-wrap'); 184 var $product = $(e['target']).closest('.woosb-product'); 185 186 if ($product.length) { 187 // reset thumb 188 $product.find('.woosb-thumb-new').hide(); 189 $product.find('.woosb-thumb-ori').show(); 190 191 // reset price 192 $product.find('.woosb-price-new').hide(); 193 $product.find('.woosb-price-ori').show(); 194 195 // reset availability 196 $product.find('.woosb-availability').html('').hide(); 197 198 // reset desc 199 $product.find('.woosb-variation-description').html('').hide(); 200 201 // reset data 202 $product.attr('data-id', 0); 203 $product.attr('data-price', 0); 204 $product.attr('data-attrs', ''); 205 206 $(document).trigger('woosb_reset_data', [$product]); 207 208 woosb_init($wrap, 'reset_data'); 209 } 210 }); 211 212 $(document). 213 on('click touch', 214 '.woosb-quantity-input-plus, .woosb-quantity-input-minus', 215 function() { 216 // get values 217 var $qty = $(this). 218 closest('.woosb-quantity-input'). 219 find('.woosb-qty'), 220 val = parseFloat($qty.val()), 221 max = parseFloat($qty.attr('max')), 222 min = parseFloat($qty.attr('min')), 223 step = $qty.attr('step'); 224 225 // format values 226 if (!val || val === '' || val === 'NaN') { 227 val = 0; 228 } 229 230 if (max === '' || max === 'NaN') { 231 max = ''; 232 } 233 234 if (min === '' || min === 'NaN') { 235 min = 0; 236 } 237 238 if (step === 'any' || step === '' || step === undefined || 239 parseFloat(step) === 'NaN') { 240 step = 1; 241 } else { 242 step = parseFloat(step); 243 } 244 245 // change the value 246 if ($(this).is('.woosb-quantity-input-plus')) { 247 if (max && ( 248 max == val || val > max 249 )) { 250 $qty.val(max); 251 } else { 252 $qty.val((val + step).toFixed(woosb_decimal_places(step))); 253 } 254 } else { 255 if (min && ( 256 min == val || val < min 257 )) { 258 $qty.val(min); 259 } else if (val > 0) { 260 $qty.val((val - step).toFixed(woosb_decimal_places(step))); 261 } 262 } 263 264 // trigger change event 265 $qty.trigger('change'); 266 }); 267 268 $(document).on('click touch', '.single_add_to_cart_button', function(e) { 269 var $this = $(this); 270 271 if ($this.hasClass('woosb-disabled')) { 272 e.preventDefault(); 273 } 274 }); 275
276 $(document).on('change', '.woosb-qty', function() { 277 var $this = $(this); 278 279 woosb_check_qty($this); 280 }); 281 282 $(document).on('keyup', '.woosb-qty', function() { 283 var $this = $(this); 284 285 if (woosb_timeout != null) clearTimeout(woosb_timeout); 286 woosb_timeout = setTimeout(woosb_check_qty, 1000, $this); 287 }); 288})(jQuery); 289 290function woosb_init($wrap, context = null) { 291 woosb_check_ready($wrap, context); 292 woosb_calc_price($wrap, context); 293 woosb_save_ids($wrap, context); 294 295 jQuery(document).trigger('woosb_init', [$wrap, context]); 296} 297 298function woosb_check_ready($wrap) { 299 var total = 0; 300 var selection_name = ''; 301 var is_selection = false; 302 var is_empty = true; 303 var is_min = false; 304 var is_max = false; 305 var wid = $wrap.attr('data-id'); 306 var $products = $wrap.find('.woosb-products'); 307 var $alert = $wrap.find('.woosb-alert'); 308 var $ids = jQuery('.woosb-ids-' + wid); 309 var $btn = $ids.closest('form.cart').find('.single_add_to_cart_button'); 310 311 // remove ajax add to cart 312 $btn.removeClass('ajax_add_to_cart'); 313 314 if (!$products.length || 315 (($products.attr('data-variables') === 'no') && 316 ($products.attr('data-optional') === 'no'))) { 317 // don't need to do anything 318 return; 319 } 320 321 $products.find('.woosb-product').each(function() { 322 var $this = jQuery(this); 323 324 if (( 325 parseFloat($this.attr('data-qty')) > 0 326 ) && ( 327 parseInt($this.attr('data-id')) === 0 328 )) { 329 is_selection = true; 330 331 if (selection_name === '') { 332 selection_name = $this.attr('data-name'); 333 } 334 } 335 336 if (parseFloat($this.attr('data-qty')) > 0) { 337 is_empty = false; 338 total += parseFloat($this.attr('data-qty')); 339 } 340 }); 341 342 // check min 343 if (( 344 $products.attr('data-optional') === 'yes' 345 ) && $products.attr('data-min') && ( 346 total < parseFloat($products.attr('data-min')) 347 )) { 348 is_min = true; 349 } 350 351 // check max 352 if (( 353 $products.attr('data-optional') === 'yes' 354 ) && $products.attr('data-max') && ( 355 total > parseFloat($products.attr('data-max')) 356 )) { 357 is_max = true; 358 } 359 360 if (is_selection || is_empty || is_min || is_max) { 361 $btn.addClass('woosb-disabled'); 362 363 if (is_selection) { 364 $alert. 365 html(woosb_vars.alert_selection.replace('[name]', 366 '<strong>' + selection_name + '</strong>')).slideDown(); 367 } else if (is_empty) { 368 $alert.html(woosb_vars.alert_empty).slideDown(); 369 } else if (is_min) { 370 $alert.html( 371 woosb_vars.alert_min.replace('[min]', $products.attr('data-min')). 372 replace('[selected]', total)).slideDown(); 373 } else if (is_max) { 374 $alert.html( 375 woosb_vars.alert_max.replace('[max]', $products.attr('data-max')). 376 replace('[selected]', total)).slideDown(); 377 } 378 379 jQuery(document). 380 trigger('woosb_check_ready', 381 [false, is_selection, is_empty, is_min, is_max, $wrap]); 382 } else { 383 $alert.html('').slideUp(); 384 $btn.removeClass('woosb-disabled'); 385 386 // ready 387 jQuery(document). 388 trigger('woosb_check_ready', 389 [true, is_selection, is_empty, is_min, is_max, $wrap]); 390 } 391} 392 393function woosb_calc_price($wrap) { 394 var total = 0; 395 var total_sale = 0; 396 var wid = $wrap.attr('data-id'); 397 var $products = $wrap.find('.woosb-products'); 398 var price_suffix = $products.attr('data-price-suffix'); 399 var $total = $wrap.find('.woosb-total'); 400 var $price = jQuery('.woosb-price-' + wid); 401 var $woobt = jQuery('.woobt-wrap-' + wid); 402 var total_woobt = parseFloat($woobt.length ? $woobt.attr('data-total') : 0); 403 var discount = parseFloat($products.attr('data-discount')); 404 var discount_amount = parseFloat($products.attr('data-discount-amount')); 405 var fixed_price = $products.attr('data-fixed-price'); 406 var saved = ''; 407 var fix = Math.pow(10, Number(woosb_vars.price_decimals) + 1); 408 var is_discount = discount > 0 && discount < 100; 409 var is_discount_amount = discount_amount > 0; 410 411 $products.find('.woosb-product').each(function() { 412 var $this = jQuery(this); 413 if (parseFloat($this.attr('data-price')) > 0) { 414 var this_price = parseFloat($this.attr('data-price')) * 415 parseFloat($this.attr('data-qty')); 416 total += this_price; 417 if (!is_discount_amount && is_discount) { 418 this_price *= (100 - discount) / 100; 419 this_price = Math.round(this_price * fix) / fix; 420 } 421 total_sale += this_price; 422 } 423 }); 424 425 // fix js number https://www.w3schools.com/js/js_numbers.asp 426 total = woosb_round(total, woosb_vars.price_decimals); 427 428 if (is_discount_amount && discount_amount < total) { 429 total_sale = total - discount_amount; 430 saved = woosb_format_price(discount_amount); 431 } else if (is_discount) { 432 saved = woosb_round(discount, 2) + '%'; 433 } else { 434 total_sale = total; 435 } 436 437 if (fixed_price === 'yes') { 438 total_sale = parseFloat($products.attr('data-price')); 439 } 440 441 var total_html = woosb_price_html(total, total_sale); 442 var total_all_html = woosb_price_html(total + total_woobt, 443 total_sale + total_woobt); 444 445 if (saved !== '') { 446 total_html += ' <small class="woocommerce-price-suffix">' + 447 woosb_vars.saved_text.replace('[d]', saved) + '</small>'; 448 } 449 450 // change the bundle total 451 $total.html(woosb_vars.price_text + ' ' + total_html + price_suffix
451). 452 slideDown(); 453 454 if (( 455 woosb_vars.change_price !== 'no' 456 ) && ( 457 $products.attr('data-fixed-price') === 'no' 458 ) && ( 459 ( 460 $products.attr('data-variables') === 'yes' 461 ) || ( 462 $products.attr('data-optional') === 'yes' 463 ) 464 )) { 465 if ((woosb_vars.change_price === 'yes_custom') && 466 (woosb_vars.price_selector != null) && 467 (woosb_vars.price_selector !== '')) { 468 $price = jQuery(woosb_vars.price_selector); 469 } 470 471 // change the main price 472 if ($woobt.length) { 473 // woobt 474 $price.html(total_all_html + price_suffix); 475 } else { 476 if (typeof ($price.attr('data-o_price')) === 'undefined') { 477 $price.attr('data-o_price', woosb_encode_entities($price.html())); 478 } 479 480 $price.html(total_html + price_suffix); 481 } 482 } 483 484 if ($woobt.length) { 485 // woobt 486 $woobt.find('.woobt-products').attr('data-product-price-html', total_html); 487 $woobt.find('.woobt-product-this'). 488 attr('data-price', total_sale). 489 attr('data-regular-price', total); 490 491 woobt_init($woobt); 492 } 493 494 jQuery(document). 495 trigger('woosb_calc_price', 496 [total_sale, total, total_html, price_suffix, $wrap]); 497} 498 499function woosb_save_ids($wrap) { 500 var ids = Array(); 501 var wid = $wrap.attr('data-id'); 502 var $ids = jQuery('.woosb-ids-' + wid); 503 var $products = $wrap.find('.woosb-products'); 504 505 $products.find('.woosb-product').each(function() { 506 var $this = jQuery(this); 507 var id = parseInt($this.attr('data-id')); 508 var qty = parseFloat($this.attr('data-qty')); 509 var attrs = $this.attr('data-attrs'); 510 511 if ((id > 0) && (qty > 0)) { 512 if (attrs != undefined) { 513 attrs = encodeURIComponent(attrs); 514 } else { 515 attrs = ''; 516 } 517 518 ids.push(id + '/' + qty + '/' + attrs); 519 } 520 }); 521 522 $ids.val(ids.join(',')); 523 524 jQuery(document).trigger('woosb_save_ids', [ids, $wrap]); 525} 526 527function woosb_check_qty($qty) { 528 var $wrap = $qty.closest('.woosb-wrap'); 529 var qty = parseFloat($qty.val()); 530 var min = parseFloat($qty.attr('min')); 531 var max = parseFloat($qty.attr('max')); 532 533 if ((qty === '') || isNaN(qty)) { 534 qty = 0; 535 } 536 537 if (!isNaN(min) && ( 538 qty < min 539 )) { 540 qty = min; 541 } 542 543 if (!isNaN(max) && ( 544 qty > max 545 )) { 546 qty = max; 547 } 548 549 $qty.val(qty); 550 $qty.closest('.woosb-product').attr('data-qty', qty); 551 552 // change subtotal 553 if (woosb_vars.bundled_price === 'subtotal') { 554 var $products = $wrap.find('.woosb-products'); 555 var $product = $qty.closest('.woosb-product'); 556 var price_suffix = $product.attr('data-price-suffix'); 557 var ori_price = parseFloat($product.attr('data-price')) * 558 parseFloat($product.attr('data-qty')); 559 560 $product.find('.woosb-price-ori').hide(); 561 562 if (parseFloat($products.attr('data-discount')) > 0 && 563 $products.attr('data-fixed-price') === 'no') { 564 var new_price = ori_price * 565 (100 - parseFloat($products.attr('data-discount'))) / 100; 566 567 $product.find('.woosb-price-new'). 568 html(woosb_price_html(ori_price, new_price) + price_suffix).show(); 569 } else { 570 $product.find('.woosb-price-new'). 571 html(woosb_price_html(ori_price) + price_suffix). 572 show(); 573 } 574 } 575 576 jQuery(document).trigger('woosb_check_qty', [qty, $qty]); 577 578 woosb_init($wrap, 'woosb_check_qty'); 579} 580 581function woosb_change_price($product, price, regular_price, price_html) { 582 var $products = $product.closest('.woosb-products'); 583 var price_suffix = $product.attr('data-price-suffix'); 584 585 // hide ori price 586 $product.find('.woosb-price-ori').hide(); 587 588 // calculate new price 589 if (woosb_vars.bundled_price === 'subtotal') { 590 var ori_price = parseFloat(price) * 591 parseFloat($product.attr('data-qty')); 592 593 if (woosb_vars.bundled_price_from === 'regular_price' && 594 regular_price !== undefined) { 595 ori_price = parseFloat(regular_price) * 596 parseFloat($product.attr('data-qty')); 597 } 598 599 var new_price = ori_price; 600 601 if (parseFloat($products.attr('data-discount')) > 0) { 602 new_price = ori_price * 603 (100 - parseFloat($products.attr('data-discount'))) / 100; 604 } 605 606 $product.find('.woosb-price-new'). 607 html(woosb_price_html(ori_price, new_price) + price_suffix
607).show(); 608 } else { 609 if (parseFloat($products.attr('data-discount')) > 0) { 610 var ori_price = parseFloat(price); 611 612 if (woosb_vars.bundled_price_from === 'regular_price' && 613 regular_price !== undefined) { 614 ori_price = parseFloat(regular_price); 615 } 616 617 var new_price = ori_price * 618 (100 - parseFloat($products.attr('data-discount'))) / 100; 619 $product.find('.woosb-price-new'). 620 html(woosb_price_html(ori_price, new_price) + price_suffix).show(); 621 } else { 622 if (woosb_vars.bundled_price_from === 'regular_price' && 623 regular_price !== undefined) { 624 $product.find('.woosb-price-new'). 625 html(woosb_price_html(regular_price) + price_suffix). 626 show(); 627 } else if (price_html !== '') { 628 $product.find('.woosb-price-new'). 629 html(price_html). 630 show(); 631 } 632 } 633 } 634} 635 636function woosb_round(value, decimals) { 637 return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals); 638} 639 640function woosb_format_money(number, places, symbol, thousand, decimal) { 641 number = number || 0; 642 places = !isNaN(places = Math.abs(places)) ? places : 2; 643 symbol = symbol !== undefined ? symbol : '$'; 644 thousand = thousand || ','; 645 decimal = decimal || '.'; 646 647 var negative = number < 0 ? '-' : '', 648 i = parseInt( 649 number = woosb_round(Math.abs(+number || 0), places).toFixed(places), 650 10) + '', 651 j = 0; 652 653 if (i.length > 3) { 654 j = i.length % 3; 655 } 656 657 return symbol + negative + ( 658 j ? i.substr(0, j) + thousand : '' 659 ) + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) + ( 660 places ? 661 decimal + 662 woosb_round(Math.abs(number - i), places).toFixed(places).slice(2) : 663 '' 664 ); 665} 666 667function woosb_format_price(price) { 668 var price_html = '<span class="woocommerce-Price-amount amount">'; 669 var price_formatted = woosb_format_money(price, woosb_vars.price_decimals, '', 670 woosb_vars.price_thousand_separator, woosb_vars.price_decimal_separator); 671 672 switch (woosb_vars.price_format) { 673 case '%1$s%2$s': 674 //left 675 price_html += '<span class="woocommerce-Price-currencySymbol">' + 676 woosb_vars.currency_symbol + '</span>' + price_formatted; 677 break; 678 case '%1$s %2$s': 679 //left with space 680 price_html += '<span class="woocommerce-Price-currencySymbol">' + 681 woosb_vars.currency_symbol + '</span> ' + price_formatted; 682 break; 683 case '%2$s%1$s': 684 //right 685 price_html += price_formatted + 686 '<span class="woocommerce-Price-currencySymbol">' + 687 woosb_vars.currency_symbol + '</span>'; 688 break; 689 case '%2$s %1$s': 690 //right with space 691 price_html += price_formatted + 692 ' <span class="woocommerce-Price-currencySymbol">' + 693 woosb_vars.currency_symbol + '</span>'; 694 break; 695 default: 696 //default 697 price_html += '<span class="woocommerce-Price-currencySymbol">' + 698 woosb_vars.currency_symbol + '</span> ' + price_formatted; 699 } 700 701 price_html += '</span>'; 702 703 return price_html; 704} 705 706function woosb_price_html(regular_price, sale_price) { 707 var price_html = ''; 708 709 if (sale_price < regular_price) { 710 price_html = '<del>' + woosb_format_price(regular_price) + '</del> <ins>' + 711 woosb_format_price(sale_price) + '</ins>'; 712 } else { 713 price_html = woosb_format_price(regular_price); 714 } 715 716 return price_html; 717} 718 719function woosb_decimal_places(num) { 720 var match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); 721 722 if (!match) { 723 return 0; 724 } 725 726 return Math.max( 727 0, 728 // Number of digits right of decimal point. 729 (match[1] ? match[1].length : 0) 730 // Adjust for scientific notation. 731 - (match[2] ? +match[2] : 0)); 732} 733 734function woosb_encode_entities(str) { 735 return String(str). 736 replace(/&/g, '&'). 737 replace(/</g, '<'). 738 replace(/>/g, '>'). 739 replace(/"/g, '"'); 740} 741 742function woosb_decode_entities(str) { 743 var textArea = document.createElement('textarea'); 744 textArea.innerHTML = str; 745 return textArea.value; 746}
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.