1jQuery(function ($) { 2 3 let cuw_data = cuw_template.data; 4 let cuw_i18n = cuw_template.i18n; 5 let cuw_is_rtl = cuw_template.is_rtl; 6 7 /* Offer */ 8 window.cuw_offer = { 9 // init 10 init: function () { 11 this.event_listeners(); 12 }, 13 14 // update price when change variant 15 update_price: function (select, section) { 16 let variant = select.find('option:selected'); 17 section.find('.cuw-product-price, .cuw-page-product-price').html(variant.data('price')); 18 select.find('.quantity-input input').attr('max', variant.data('max_qty')); 19 }, 20 21 // update the closest quantity input on click press plus or minus button 22 update_quantity: function (button) { 23 let wrapper = button.parent(); 24 let input = wrapper.find('input'), 25 val = parseFloat(input.val()) || 0, 26 max = parseFloat(input.attr('max')) || '', 27 min = parseFloat(input.attr('min')) || 0, 28 step = parseFloat(input.attr('step')) || 1; 29 30 if (button.is('.cuw-plus')) { 31 if (max && (val >= max)) input.val(max); 32 else input.val(val + step); 33 } else { 34 if (min && (val <= min)) input.val(min); 35 else if (val > 0) input.val(val - step); 36 } 37 38 val = parseFloat(input.val()); // to get current value 39 wrapper.find('.cuw-minus').css('opacity', min === val ? 0.6 : 1); 40 wrapper.find('.cuw-plus').css('opacity', max === val ? 0.6 : 1); 41 input.trigger('change'); 42 }, 43 44 // to load event listeners 45 event_listeners: function () { 46 $(document).on("click", ".cuw-offer .quantity-input .cuw-plus, .cuw-offer .quantity-input .cuw-minus", function () { 47 cuw_offer.update_quantity($(this)); 48 }); 49 50 $(document).on("click", ".cuw-page .quantity-input .cuw-plus, .cuw-page .quantity-input .cuw-minus", function () { 51 cuw_offer.update_quantity($(this)); 52 }); 53 54 $(document).on("change", ".cuw-offer .variant-select", function () { 55 cuw_offer.update_price($(this), $(this).closest('.cuw-offer')); 56 }); 57 58 $(document).on("change", ".cuw-page .variant-select", function () { 59 cuw_offer.update_price($(this), $(this).closest('.cuw-page')); 60 }); 61 }, 62 } 63 64 /* Products */ 65 window.cuw_products = { 66 // init 67 init: function () { 68 this.event_listeners(); 69 this.load_sections(); 70 }, 71 72 // load all available sections 73 load_sections: function () { 74 $(".cuw-products").each(function (index, section) { 75 cuw_products.load_section($(section)); 76 }); 77 }, 78 79 // load specific section 80 load_section: function (section) { 81 cuw_products.update_prices(section); 82 }, 83 84 // get chosen products 85 get_chosen_products: function (section) { 86 let products = []; 87 section.find('.cuw-product').each(function(index, product) { 88 if ($(product).find(".cuw-product-checkbox").prop("checked") || $(product).data('key')) { 89 products.push($(product)); 90 } 91 }); 92 return products; 93 }, 94 95 // update modal product prices 96 update_product_row: function (section, product_row) { 97 let price_column = product_row.find('.cuw-product-price'); 98 let product_id = product_row.data('product_id'), variant_id; 99 if (!product_id) { 100 product_id = product_row.data('id'); 101 } 102 103 let product = section.find('.cuw-product[data-id="' + product_id + '"]').first(); 104 let variant_select = product_row.find('.variant-select').first(); 105 if (variant_select && variant_select.length) { 106 variant_id = variant_select.val(); 107 let variant = variant_select.find('option[value="' + variant_id + '"]').first(); 108 product.data('regular_price', variant.data('regular_price')); 109 product.data('price', variant.data('price')); 110 product.find('.cuw-product-variation-id').val(variant_id); 111 price_column.html(cuw_helper.get_price_html(variant.data('price'), variant.data('regular_price'))); 112 } else if (price_column.html() === '') { 113 price_column.html(cuw_helper.get_price_html(product.data('price'), product.data('regular_price'))); 114 } 115 }, 116 117 // update pricing section 118 update_prices: function(section, data = {}) { 119 if (section.closest('.cuw-modal').length > 0) { 120 section = section.closest('.cuw-modal'); 121 } 122 let regular_price_total = 0, price_total = 0, items = 0, main_price = 0, main_item = 0, addons_price = 0, addon_items = 0; 123 let main_product_id = parseInt(section.find('[name="main_product_id"]').val());
124 this.get_chosen_products(section).forEach(function (product) { 125 let quantity = parseFloat(product.find('.quantity-input .cuw-qty').first().val() || 1); 126 if (main_product_id === product.data('id')) { 127 main_price = parseFloat(product.data('price')) * quantity; 128 main_item = 1; 129 } else { 130 addons_price += parseFloat(product.data('price')) * quantity; 131 addon_items++; 132 } 133 regular_price_total += parseFloat(product.data('regular_price')) * quantity; 134 price_total += parseFloat(product.data('price')) * quantity; 135 items++; 136 }); 137 138 let total_savings = regular_price_total - price_total; 139 let at_least_items = parseInt(section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('at_least_items')) || 1; 140 if (items >= at_least_items) { 141 section.find(".cuw-buy-section .cuw-message").hide(); 142 if (section.find(".cuw-buy-section .cuw-actions").first().data('inactive') === 'disable') { 143 section.find(".cuw-buy-section .cuw-actions").show(); 144 section.find(".cuw-buy-section .cuw-actions").find(":input").prop('disabled', false); 145 } else { 146 section.find(".cuw-buy-section .cuw-actions").show(); 147 } 148 section.find(".cuw-buy-section .cuw-prices").show(); 149 150 section.find(".cuw-main-item").html(main_item); 151 section.find(".cuw-addon-items").html(addon_items); 152 section.find(".cuw-total-items").html(items); 153 section.find(".cuw-main-price").html(cuw_helper.get_price_html(main_price)); 154 section.find(".cuw-addons-price").html(cuw_helper.get_price_html(addons_price)); 155 156 if (section.data('main_product_price')) { 157 price_total += parseFloat(section.data('main_product_price')); 158 } 159 160 section.find(".cuw-total-price").html(cuw_helper.get_price_html(price_total, regular_price_total)); 161 } else { 162 if (section.find(".cuw-buy-section .cuw-actions").first().data('inactive') === 'disable') { 163 section.find(".cuw-buy-section .cuw-actions").find(":input").prop('disabled', true); 164 } else { 165 section.find(".cuw-buy-section .cuw-actions").hide(); 166 } 167 section.find(".cuw-buy-section .cuw-prices").hide(); 168 section.find(".cuw-buy-section .cuw-message").show(); 169 } 170 if (total_savings > 0) { 171 section.find('.cuw-total-savings').show(); 172 section.find('.cuw-total-savings .cuw-saved-amount').html(cuw_helper.get_price_html(total_savings)); 173 } else { 174 section.find('.cuw-total-savings').hide(); 175 section.find('.cuw-total-savings .cuw-saved-amount').html(''); 176 } 177 if (data.cart_subtotal) { 178 section.find('.cuw-cart-subtotal').html(data.cart_subtotal); 179 } 180 this.update_add_to_cart_text(section, items); 181 }, 182 183 // update inputs 184 update_inputs: function (product, show = true) { 185 let inputs = product.find(':input').not('input[type="checkbox"]'); 186 let product_id = product.data('id'); 187 if (show) { 188 inputs.prop("disabled", false); 189 product.css('opacity', '1'); 190 if (product_id) { 191 product.closest('.cuw-products').find('.cuw-product[data-id="' + product_id + '"]').css('opacity', '1'); 192 } 193 product.find('.cuw-plus, .cuw-minus').css('pointer-events', 'all'); 194 195 product.find('.cuw-remove-item-from-cart, .cuw-added-text, .cuw-added-icon').hide(); 196 product.find('.cuw-add-product-to-cart').show(); 197 } else { 198 inputs.prop("disabled", true); 199 product.css('opacity', '0.8'); 200 if (product_id) { 201 product.closest('.cuw-products').find('.cuw-product[data-id="' + product_id + '"]').css('opacity', '0.8'); 202 } 203 product.find('.cuw-plus, .cuw-minus').css('pointer-events', 'n
203one'); 204 205 product.find('.cuw-add-product-to-cart').hide(); 206 product.find('.cuw-remove-item-from-cart, .cuw-added-text, .cuw-added-icon').show(); 207 } 208 }, 209 210 // update add to cart text 211 update_add_to_cart_text: function (section, count = 1) { 212 if (section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('buy_now')) { 213 return; 214 } 215 let text = ''; 216 if (count > 1) { 217 let items_text = ''; 218 let items_count = count + ' ' + cuw_i18n.add_to_cart.items; 219 if (cuw_i18n.add_to_cart.number_to_text[count]) { 220 items_text = cuw_i18n.add_to_cart.number_to_text[count]; 221 } else { 222 items_text = items_count; 223 } 224 if (section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('text')) { 225 text = section.find(".cuw-buy-section").find(".cuw-add-to-cart").data('text') 226 .replace('{items_text}', items_text) 227 .replace('{items_count}', items_count); 228 } 229 } else { 230 text = cuw_i18n.add_to_cart.text; 231 } 232 section.find(".cuw-add-to-cart").html(text); 233 }, 234 235 // update carousel actions 236 carousel_update_actions: function (section) { 237 let slider = section.find(".cuw-carousel-slider"); 238 if (slider[0].scrollLeft === 0) { 239 section.find(".cuw-previous").css('opacity', 0.5); 240 } else if (Math.abs(slider[0].scrollLeft) + slider[0].clientWidth == slider[0].scrollWidth) { 241 section.find(".cuw-next").css('opacity', 0.5); 242 } else { 243 section.find(".cuw-previous, .cuw-next").css('opacity', 1); 244 } 245 }, 246 247 // carousel scroll items 248 carousel_scroll_items : function (section, action) { 249 let slider = section.find(".cuw-carousel-slider"); 250 let item_gap = parseInt(slider.data('gap')) || 18; 251 let item_width = parseInt(slider.find(':first-child').width()); 252 if (action.hasClass('cuw-next')) { 253 slider.animate({ 254 scrollLeft: (cuw_is_rtl ? '-=' : '+=') + (item_width + item_gap), 255 behaviour: 'smooth' 256 }, 10); 257 } else if (action.hasClass('cuw-previous')) { 258 slider.animate({ 259 scrollLeft: (cuw_is_rtl ? '+=' : '-=') + (item_width + item_gap), 260 behaviour: 'smooth' 261 }, 10); 262 } 263 }, 264 265 // to load event listeners 266 event_listeners: function () { 267 $(document).on("click", ".cuw-products .cuw-product-image", function () { 268 let checkbox = $(this).closest(".cuw-product").find(".cuw-product-checkbox").first(); 269 checkbox.prop("checked", !checkbox.prop("checked")).trigger("change"); 270 }); 271 272 $(document).on('click', '.cuw-carousel .cuw-scroll-action', function () { 273 cuw_products.carousel_scroll_items($(this).closest(".cuw-carousel"), $(this)); 274 }); 275 276 $('.cuw-carousel .cuw-carousel-slider').on('scroll', function() { 277 cuw_products.carousel_update_actions($(this).closest(".cuw-carousel")); 278 }); 279 280 $(document).on("change", ".cuw-products .cuw-product-checkbox", function () { 281 cuw_products.update_inputs($(this).closest(".cuw-product"), $(this).is(":checked")); 282 cuw_products.update_prices($(this).closest(".cuw-products")); 283 }); 284 285 $(document).on("click", ".cuw-products .quantity-input .cuw-plus, .cuw-products .quantity-input .cuw-minus", function () { 286 cuw_offer.update_quantity($(this)); 287 }); 288 289 $(document).on("change", '.cuw-products .quantity-input .cuw-qty', function () { 290 cuw_products.update_prices($(this).closest(".cuw-products")); 291 }); 292 293 $(document).on("change", ".cuw-products .variant-select", function () { 294 cuw_products.update_product_row($(this).closest(".cuw-products"), $(this).closest(".cuw-product-row")); 295 cuw_products.update_prices($(this).closest(".cuw-products")); 296 }); 297 298 $(document).on("cuw_products_load_section", function (event, section) { 299 cuw_products.load_section(section); 300 }); 301 302 $(document).on("click", '.cuw-cart-addon-products .cuw-toggle-ad
302dons', function () { 303 $(this).closest(".cuw-cart-addon-products").find(".cuw-products").slideToggle(); 304 $(this).closest(".cuw-cart-addon-products").find(".cuw-addon-arrow-up").toggle(); 305 $(this).closest(".cuw-cart-addon-products").find(".cuw-addon-arrow-down").toggle(); 306 }); 307 }, 308 } 309 310 /* Helper */ 311 window.cuw_helper = { 312 // format price 313 format_price: function (price, tag = '') { 314 price = parseFloat(price).toFixed(cuw_data.woocommerce.price.decimals); 315 price = price.replace(".", cuw_data.woocommerce.price.decimal_separator); 316 let split = price.split(cuw_data.woocommerce.price.decimal_separator); 317 split[0] = split[0].replace(/\B(?=(\d{3})+(?!\d))/g, cuw_data.woocommerce.price.thousand_separator); 318 price = split.join(cuw_data.woocommerce.price.decimal_separator); 319 price = cuw_data.woocommerce.price.format.replace('%1$s', cuw_data.woocommerce.price.symbol).replace('%2$s', price); 320 return tag ? '<' + tag + '>' + price + '</' + tag + '>' : price; 321 }, 322 323 // get price html 324 get_price_html: function (price, regular_price = null) { 325 if (regular_price && regular_price > price) { 326 return this.format_price(regular_price, 'del') + ' ' + this.format_price(price, 'ins'); 327 } 328 return this.format_price(price, 'ins'); 329 }, 330 331 // get discount text 332 get_discount_text: function (discount_type, discount_value) { 333 let discount_text = ''; 334 if (discount_type === 'percentage' && discount_value !== '') { 335 discount_text = discount_value + '%'; 336 } else if (discount_type === 'fixed_price' && discount_value !== '') { 337 discount_text = cuw_helper.format_price(discount_value); 338 } else if (discount_type === 'free') { 339 discount_text = cuw_i18n.free; 340 } 341 return discount_text; 342 } 343 } 344 345 /* Spinner */ 346 window.cuw_spinner = { 347 // show spinner 348 show: function (section) { 349 if (typeof section === 'string') { 350 section = $(section).first(); 351 } 352 if (section.block) { 353 $(section).block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } }); 354 } 355 }, 356 357 // hide spinner 358 hide: function (section) { 359 if (typeof section === 'string') { 360 section = $(section).first(); 361 } 362 if (section.unblock) { 363 $(section).unblock(); 364 } 365 } 366 } 367 368 /* Model */ 369 window.cuw_modal = { 370 // show modal 371 show: function (modal, temp = false) { 372 if (typeof modal === 'string') { 373 modal = $(modal).first(); 374 } 375 modal.show(); 376 modal.find('.cuw-modal-close').click(function() { temp ? modal.remove() : modal.hide() }); 377 $(window).click(function(event) { 378 if ($(event.target).hasClass('cuw-modal')) { temp ? modal.remove() : modal.hide() } 379 }); 380 $(document).keydown(function(event) { 381 if (event.keyCode === 27) { temp ? modal.remove() : modal.hide() } 382 }); 383 }, 384 385 // hide modal 386 hide: function (modal) { 387 if (typeof modal === 'string') { 388 modal = $(modal).first(); 389 } 390 modal.hide(); 391 } 392 } 393 394 /* Init */ 395 $(document).ready(function () { 396 cuw_offer.init(); 397 cuw_products.init(); 398 }); 399 400});
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.