1'use strict'; 2 3(function($) { 4 $(function() { 5 if (!$('.wooco-wrap').length) { 6 return; 7 } 8 9 $('.wooco-wrap').each(function() { 10 wooco_init_selector(); 11 wooco_init($(this), 'load'); 12 }); 13 }); 14 15 $(document).on('woosq_loaded', function() { 16 // composite products in quick view popup 17 if ($('#woosq-popup .wooco-wrap').length) { 18 wooco_init_selector(); 19 wooco_init($('#woosq-popup .wooco-wrap')); 20 } 21 }); 22 23 $(document).on('click touch', '.single_add_to_cart_button', function(e) { 24 if ($(this).hasClass('wooco-disabled')) { 25 if (wooco_vars.show_alert === 'change') { 26 wooco_show_alert($(this).closest('.wooco-wrap')); 27 } 28 29 e.preventDefault(); 30 } 31 }); 32 33 $(document).on('click touch', '.wooco-plus, .wooco-minus', function() { 34 // get values 35 var $qty = $(this).closest('.wooco-qty').find('.qty'), 36 val = parseFloat($qty.val()), 37 max = parseFloat($qty.attr('max')), 38 min = parseFloat($qty.attr('min')), 39 step = $qty.attr('step'); 40 41 // format values 42 if (!val || val === '' || val === 'NaN') { 43 val = 0; 44 } 45 46 if (max === '' || max === 'NaN') { 47 max = ''; 48 } 49 50 if (min === '' || min === 'NaN') { 51 min = 0; 52 } 53 54 if (step === 'any' || step === '' || step === undefined || 55 parseFloat(step) === 'NaN') { 56 step = 1; 57 } else { 58 step = parseFloat(step); 59 } 60 61 // change the value 62 if ($(this).is('.wooco-plus')) { 63 if (max && ( 64 max == val || val > max 65 )) { 66 $qty.val(max); 67 } else { 68 $qty.val((val + step).toFixed(wooco_decimal_places(step))); 69 } 70 } else { 71 if (min && ( 72 min == val || val < min 73 )) { 74 $qty.val(min); 75 } else if (val > 0) { 76 $qty.val((val - step).toFixed(wooco_decimal_places(step))); 77 } 78 } 79 80 // trigger change event 81 $qty.trigger('change'); 82 }); 83 84 $(document). 85 on('keyup change', '.wooco_component_product_qty_input', function() { 86 var $this = $(this); 87 var $wrap = $this.closest('.wooco-wrap'); 88 var val = parseFloat($this.val()); 89 var min = parseFloat($this.attr('min')); 90 var max = parseFloat($this.attr('max')); 91 92 if (( 93 val < min 94 ) || isNaN(val)) { 95 val = min; 96 $this.val(val); 97 } 98 99 if (val > max) { 100 val = max; 101 $this.val(val); 102 } 103 104 $this.closest('.wooco_component_product').attr('data-qty', val); 105 106 wooco_init($wrap); 107 }); 108 109 $(document).on('change', '.wooco-checkbox', function() { 110 var $wrap = $(this).closest('.wooco-wrap'); 111 112 wooco_init($wrap); 113 }); 114})(jQuery); 115 116function wooco_init($wrap, context = null) { 117 wooco_check_ready($wrap); 118 wooco_calc_price($wrap); 119 wooco_save_ids($wrap); 120 121 if (context === null || context === 'on_select' || context === 122 wooco_vars.show_alert) { 123 wooco_show_alert($wrap); 124 } 125 126 jQuery(document).trigger('wooco_init', [$wrap]); 127} 128 129function wooco_check_ready($wrap) { 130 var wid = $wrap.attr('data-id'); 131 var $components = $wrap.find('.wooco-components'); 132 var $ids = jQuery('.wooco-ids-' + wid); 133 var $btn = $ids.closest('form.cart').find('.single_add_to_cart_button'); 134 var $alert = $wrap.find('.wooco-alert'); 135 var is_selection = false; 136 var selection_name = ''; 137 var is_min = false; 138 var is_max = false; 139 var is_same = false; 140 var selected_products = new Array(); 141 var allow_same = $components.attr('data-same'); 142 var qty = 0; 143 var qty_min = parseFloat($components.attr('data-min')); 144 var qty_max = parseFloat($components.attr('data-max')); 145 146 $components.find('.wooco_component_product').each(function() { 147 var $this = jQuery(this); 148 var $checkbox = $this.find('.wooco-checkbox'); 149 var _id = parseInt($this.attr('data-id')); 150 var _qty = parseFloat($this.attr('data-qty')); 151 var _required = $this.attr('data-required'); 152 153 if ($checkbox.length && !$checkbox.prop('checked')) { 154 return; 155 } 156 157 if (_id > 0) { 158 qty += _qty; 159 } 160 161 if (allow_same === 'no') { 162 if (selected_products.includes(_id)) { 163 is_same = true; 164 } else { 165 if (_id > 0) { 166 selected_products.push(_id); 167 } 168 } 169 } 170 171 if ((_id === 0 && _qty > 0) || (_required === 'yes' && _id <= 0)) { 172 is_selection = true; 173 174 if (selection_name === '') { 175 selection_name = $this.attr('data-name'); 176 } 177 } 178 }); 179 180 if (qty < qty_min) { 181 is_min = true; 182 } 183 184 if (qty > qty_max) { 185 is_max = true; 186 } 187 188 if (is_selection || is_min || is_max || is_same) { 189 $btn.addClass('wooco-disabled'); 190 $alert.addClass('alert-active'); 191 192 if (is_selection) { 193 $alert.addClass('alert-selection'). 194 html(wooco_vars.alert_selection.replace('[name]', 195 '<strong>' + selection_name + '</strong>')); 196 } else if (is_min) { 197 $alert.addClass('alert-min'). 198 html(wooco_vars.alert_min.replace('[min]', qty_min)); 199 } else if (is_max) { 200 $alert.addClass('alert-max'). 201 html(wooco_vars.alert_max.replace('[max]', qty_max)); 202 } else if (is_same) { 203 $alert.addClass('alert-same').html(wooco_vars.alert_same); 204 } 205 206 jQuery(document).trigger('wooco_check_ready', 207 [false, is_selection, is_same, is_min, is_max, $wrap]); 208 } else { 209 $alert.removeClass('alert-active alert-selection alert-min alert-max'). 210 html(''); 211 $btn.removeClass('wooco-disabled'); 212 213 // ready 214 jQuery(document).trigger('wooco_check_ready', 215 [true, is_selection, is_same, is_min, is_max, $wrap]); 216 } 217} 218 219function wooco_calc_price($wrap) { 220 var wid = $wrap.attr('data-id'); 221 var $components = $wrap.find('.wooco-components'); 222 var $total = $wrap.find('.wooco-total'); 223 var $price = jQuery('.wooco-price-' + wid); 224 var $woobt = jQuery('.woobt-wrap-' + wid); 225 var pricing = $components.attr('data-pricing'); 226 var price = wooco_format_number($components.attr('data-price')); 227 var regular_price = wooco_format_number( 228 $components.attr('data-regular-price')); 229 var percent = wooco_format_number($components.attr('data-percent')); 230 var total = 0; 231 var total_regular = 0; 232 233 if ((pricing === 'only') && (price > 0)) { 234 total = price; 235 total_regular = regular_price; 236 } else { 237 // calc price 238 $components.find('.wooco_component_product').each(function() { 239 var $this = jQuery(this); 240 var $checkbox = $this.find('.wooco-checkbox'); 241 var _price = wooco_format_number($this.attr('data-price')); 242 var _regular_price = wooco_format_number( 243 $this.attr('data-regular-price')); 244 var _qty = wooco_format_number($this.attr('data-qty')); 245 246 if ($checkbox.length && !$checkbox.prop('checked')) { 247 return; 248 } 249 250 if ((_price > 0) && (_qty > 0)) { 251 total += _price * _qty; 252 } 253 254 if ((_regular_price > 0) && (_qty > 0)) { 255 total_regular += _regular_price * _qty; 256 } 257 }); 258 259 // discount 260 if ((percent > 0) && (percent < 100)) { 261 total = total * (100 - percent) / 100; 262 } 263 264 if (pricing === 'include') { 265 total += price; 266 total_regular += regular_price; 267 } 268 } 269 270 var total_html = wooco_price_html(total_regular, total); 271 272 if ((pricing !== 'only') && (percent > 0) && (percent < 100)) { 273 total_html += ' <small class="woocommerce-price-suffix">' + 274 wooco_vars.saved_text.replace('[d]', percent + '%') + '</small>'; 275 } 276 277 $total.html(wooco_vars.total_text + ' ' + total_html).slideDown(); 278 279 if ((wooco_vars.change_price !== 'no') && (pricing !== 'only')) { 280 if ((wooco_vars.change_price === 'yes_custom') && 281 (wooco_vars.price_selector !== null) && 282 (wooco_vars.price_selector !== '')) { 283 $price = jQuery(wooco_vars.price_selector); 284 } 285 286 $price.html(total_html); 287 } 288 289 if ($woobt.length) { 290 $woobt.find('.woobt-products').attr('data-product-price-html', total_html); 291 $woobt.find('.woobt-product-this'). 292 attr('data-price', total). 293 attr('data-regular-price', total_regular); 294 295 woobt_init($woobt); 296 } 297 298 jQuery(document). 299 trigger('wooco_calc_price', [total, total_html, $wrap]); 300} 301 302function wooco_save_ids($wrap) { 303 var wid = $wrap.attr('data-id'); 304 var $components = $wrap.find('.wooco-components'); 305 var $ids = jQuery('.wooco-ids-' + wid); 306 var ids = Array(); 307 308 $components.find('.wooco_component_product').each(function() { 309 var $this = jQuery(this); 310 var $checkbox = $this.find('.wooco-checkbox'); 311 312 if ($checkbox.length && !$checkbox.prop('checked')) { 313 return; 314 } 315 316 if (( 317 $this.attr('data-id') > 0 318 ) && ( 319 $this.attr('data-qty') > 0 320 )) { 321 if (wooco_vars.hide_component_name === 'yes') { 322 ids.push($this.attr('data-id') + '/' + $this.attr('data-qty') + '/' + 323 $this.attr('data-new-price')); 324 } else { 325 ids.push($this.attr('data-id') + '/' + $this.attr('data-qty') + '/' + 326 $this.attr('data-new-price') + '/' + 327 encodeURIComponent($this.attr('data-name'))); 328 } 329 } 330 }); 331 332 $ids.val(ids.join(',')); 333 jQuery(document).trigger('wooco_save_ids', [ids, $wrap]); 334} 335 336function wooco_show_alert($wrap) { 337 var $alert = $wrap.find('.wooco-alert'); 338 339 if ($alert.hasClass('alert-active')) { 340 $alert.slideDown(); 341 } else { 342 $alert.slideUp(); 343 } 344 345 jQuery(document).trigger('wooco_show_alert', [$wrap]); 346} 347 348function wooco_init_selector() { 349 if (wooco_vars.selector === 'ddslick') { 350 jQuery('.wooco_component_product_select').each(function() { 351 var $this = jQuery(this); 352 var $selection = $this.closest('.wooco_component_product_selection'); 353 var $component = $this.closest('.wooco_component_product'); 354 var $wrap = $this.closest('.wooco-wrap'); 355 356 $selection.data('select', 0); 357 358 $this.ddslick({ 359 width: '100%', 360 onSelected: function(data) { 361 var _select = $selection.data('select'); 362 var $selected = jQuery(data.original[0].children[data.selectedIndex]); 363 364 if (data.selectedData.value == '-1') { 365 if (!$selection.find('.dd-selected .dd-desc').length) { 366 $selection.find('.dd-selected'). 367 addClass('dd-option-without-desc'); 368 $selection.find('.dd-option-selected'). 369 addClass('dd-option-without-desc'); 370 } 371 } else { 372 $selection.find('.dd-selected'). 373 removeClass('dd-option-without-desc'); 374 $selection.find('.dd-option-selected'). 375 removeClass('dd-option-without-desc'); 376 } 377 378 wooco_selected($selected, $selection, $component); 379 380 if (_select > 0) { 381 wooco_init($wrap, 'on_select'); 382 } else { 383 // selected on init_selector 384 wooco_init($wrap, 'selected'); 385 } 386 387 $selection.data('select', _select + 1); 388 }, 389 }); 390 }); 391 } else if (wooco_vars.selector === 'select2') { 392 jQuery('.wooco_component_product_select').each(function() { 393 var $this = jQuery(this); 394 var $selection = $this.closest('.wooco_component_product_selection'); 395 var $component = $this.closest('.wooco_component_product'); 396 var $wrap = $this.closest('.wooco-wrap'); 397 398 if ($this.val() !== '') { 399 var $default = jQuery('option:selected', this); 400 401 wooco_selected($default, $selection, $component); 402 wooco_init($wrap, 'selected'); 403 } 404 405 $this.select2({ 406 templateResult: wooco_select2_state, 407 width: '100%', 408 containerCssClass: 'wpc-select2-container', 409 dropdownCssClass: 'wpc-select2-dropdown', 410 }); 411 }); 412 413 jQuery('.wooco_component_product_select').on('select2:select', function(e) { 414 var $this = jQuery(this); 415 var $selection = $this.closest('.wooco_component_product_selection'); 416 var $component = $this.closest('.wooco_component_product'); 417 var $wrap = $this.closest('.wooco-wrap'); 418 var $selected = jQuery(e.params.data.element); 419 420 wooco_selected($selected, $selection, $component); 421 wooco_init($wrap, 'on_select'); 422 }); 423 } else { 424 jQuery('.wooco_component_product_select').each(function() { 425 //check on start 426 var $this = jQuery(this); 427 var $selection = $this.closest('.wooco_component_product_selection'); 428 var $component = $this.closest('.wooco_component_product'); 429 var $wrap = $this.closest('.wooco-wrap'); 430 var $selected = jQuery('option:selected', this); 431 432 wooco_selected($selected, $selection, $component); 433 wooco_init($wrap, 'selected'); 434 }); 435
436 jQuery('body').on('change', '.wooco_component_product_select', function() { 437 //check on select 438 var $this = jQuery(this); 439 var $selection = $this.closest('.wooco_component_product_selection'); 440 var $component = $this.closest('.wooco_component_product'); 441 var $wrap = $this.closest('.wooco-wrap'); 442 var $selected = jQuery('option:selected', this); 443 444 wooco_selected($selected, $selection, $component); 445 wooco_init($wrap, 'on_select'); 446 }); 447 } 448} 449 450function wooco_selected($selected, $selection, $component) { 451 var id = $selected.attr('value'); 452 var pid = $selected.attr('data-pid'); 453 var price = $selected.attr('data-price'); 454 var regular_price = $selected.attr('data-regular-price'); 455 var link = $selected.attr('data-link'); 456 var image = '<img src="' + $selected.attr('data-imagesrc') + '"/>'; 457 var price_html = $selected.attr('data-price-html'); 458 var availability = $selected.attr('data-availability'); 459 460 $component.attr('data-id', id); 461 $component.attr('data-price', price); 462 $component.attr('data-price-html', price_html); 463 $component.attr('data-regular-price', regular_price); 464 465 if (pid === '0') { 466 // get parent ID for quick view 467 pid = id; 468 } 469 470 if (wooco_vars.product_link !== 'no') { 471 $selection.find('.wooco_component_product_link').remove(); 472 if (link !== '') { 473 if (wooco_vars.product_link === 'yes_popup') { 474 $selection.append( 475 '<a class="wooco_component_product_link woosq-link" data-id="' + 476 pid + '" data-context="wooco" href="' + link + 477 '" target="_blank"> </a>'); 478 } else { 479 $selection.append( 480 '<a class="wooco_component_product_link" href="' + link + 481 '" target="_blank"> </a>'); 482 } 483 } 484 } 485 486 $component.find('.wooco_component_product_image').html(image); 487 $component.find('.wooco_component_product_price').html(price_html); 488 $component.find('.wooco_component_product_availability').html(availability); 489 490 jQuery(document). 491 trigger('wooco_selected', [$selected, $selection, $component]); 492} 493 494function wooco_select2_state(state) { 495 if (!state.id) { 496 return state.text; 497 } 498 499 var $state = new Object(); 500 501 if (jQuery(state.element).attr('data-imagesrc') !== '') { 502 $state = jQuery( 503 '<span class="image"><img src="' + 504 jQuery(state.element).attr('data-imagesrc') + 505 '"/></span><span class="info"><span class="name">' + state.text + 506 '</span> <span class="desc">' + 507 jQuery(state.element).attr('data-description') + '</span></span>', 508 ); 509 } else { 510 $state = jQuery( 511 '<span class="info"><span class="name">' + state.text + 512 '</span> <span class="desc">' + 513 jQuery(state.element).attr('data-description') + '</span></span>', 514 ); 515 } 516 517 return $state; 518} 519 520function wooco_round(num) { 521 return +( 522 Math.round(num + 'e+2') + 'e-2' 523 ); 524} 525 526function wooco_decimal_places(num) { 527 var match = ('' + num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); 528 529 if (!match) { 530 return 0; 531 } 532 533 return Math.max( 534 0, 535 // Number of digits right of decimal point. 536 (match[1] ? match[1].length : 0) 537 // Adjust for scientific notation. 538 - (match[2] ? +match[2] : 0)); 539} 540 541function wooco_format_money(number, places, symbol, thousand, decimal) { 542 number = number || 0; 543 places = !isNaN(places = Math.abs(places)) ? places : 2; 544 symbol = symbol !== undefined ? symbol : '$'; 545 thousand = thousand || ','; 546 decimal = decimal || '.'; 547 548 var negative = number < 0 ? '-' : '', 549 i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + '', 550 j = 0; 551 552 if (i.length > 3) { 553 j = i.length % 3; 554 } 555 556 if (wooco_vars.trim_zeros === '1') { 557 return symbol + negative + ( 558 j ? i.substr(0, j) + thousand : '' 559 ) + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) + ( 560 places && (parseFloat(number) > parseFloat(i)) ? 561 decimal + Math.abs(number - i). 562 toFixed(places). 563 slice(2). 564 replace(/(\d*?[1-9])0+$/g, '$1') : 565 '' 566 ); 567 } else { 568 return symbol + negative + ( 569 j ? i.substr(0, j) + thousand : '' 570 ) + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) + ( 571 places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : '' 572 ); 573 } 574} 575 576function wooco_format_number(number) { 577 return parseFloat(number.replace(/[^0-9.,]/g, '').replace(',', '.')); 578} 579 580function wooco_format_price(price) { 581 var price_html = '<span class="woocommerce-Price-amount amount">'; 582 var price_formatted = wooco_format_money(price, wooco_vars.price_decimals, '', 583 wooco_vars.price_thousand_separator, wooco_vars.price_decimal_separator); 584 585 switch (wooco_vars.price_format) { 586 case '%1$s%2$s': 587 //left 588 price_html += '<span class="woocommerce-Price-currencySymbol">' + 589 wooco_vars.currency_symbol + '</span>' + price_formatted; 590 break; 591 case '%1$s %2$s': 592 //left with space 593 price_html += '<span class="woocommerce-Price-currencySymbol">' + 594 wooco_vars.currency_symbol + '</span> ' + price_formatted; 595 break; 596 case '%2$s%1$s': 597 //right 598 price_html += price_formatted + 599 '<span class="woocommerce-Price-currencySymbol">' + 600 wooco_vars.currency_symbol + '</span>'; 601 break; 602 case '%2$s %1$s': 603 //right with space 604 price_html += price_formatted + 605 ' <span class="woocommerce-Price-currencySymbol">' + 606 wooco_vars.currency_symbol + '</span>'; 607 break; 608 default: 609 //default 610 price_html += '<span class="woocommerce-Price-currencySymbol">' + 611 wooco_vars.currency_symbol + '</span> ' + price_formatted; 612 } 613 614 price_html += '</span>'; 615 616 return price_html; 617} 618 619function wooco_price_html(regular_price, sale_price) { 620 var price_html = ''; 621 622 if (sale_price < regular_price) { 623 price_html = '<del>' + wooco_format_price(regular_price) + '</del> <ins>' + 624 wooco_format_price(sale_price) + '</ins>'; 625 } else { 626 price_html = wooco_format_price(regular_price); 627 } 628 629 return price_html; 630}
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.