1/** 2 * 2007-2020 PrestaShop and Contributors 3 * 4 * NOTICE OF LICENSE 5 * 6 * This source file is subject to the Academic Free License 3.0 (AFL-3.0) 7 * that is bundled with this package in the file LICENSE.txt. 8 * It is also available through the world-wide-web at this URL: 9 * https://opensource.org/licenses/AFL-3.0 10 * If you did not receive a copy of the license and are unable to 11 * obtain it through the world-wide-web, please send an email 12 * to [email protected] so we can send you a copy immediately. 13 * 14 * @author PrestaShop SA <[email protected]> 15 * @copyright 2007-2020 PrestaShop SA and Contributors 16 * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) 17 * International Registered Trademark & Property of PrestaShop SA 18 */ 19 20/** 21 * This module exposes an extension point through `showModal` function. 22 * 23 * If you want to customize the way the modal window is displayed, you need to do: 24 * 25 * prestashop.blockcart = prestashop.blockcart || {}; 26 * prestashop.blockcart.showModal = function myOwnShowModal (modalHTML) { 27 * // your own code 28 * // please not that it is your responsibility to handle the modal "close" behavior 29 * }; 30 * 31 * Warning: your custom JavaScript needs to be included **before** this file. 32 * The safest way to do so is to place your "override" inside the theme main JavaScript file. 33 * 34 */ 35 36$(document).ready(function () { 37 prestashop.blockcart = prestashop.blockcart || {}; 38 var cart = $('.blockcart'); 39 if($('.blockcart').hasClass('cart-sidebar')){ 40 $('.blockcart > a').click(function(e){ 41 e.preventDefault(); 42 showCartSidebar(); 43 }) 44 } 45 var showCartSidebar = function(){ 46 $('.blockcart').find('.popup-sidebar').addClass('sidebar-opened'); 47 } 48 $('.close-cart').on('click', function(){ 49 $(this).parents('.popup-sidebar').removeClass('sidebar-opened'); 50 }) 51 var showModal = prestashop.blockcart.showModal || function (modal) { 52 var $body = $('body'); 53 $body.append(modal); 54 $body.on('click', '#blockcart-modal', function (event) { 55 if (event.target.id === 'blockcart-modal') { 56 $(event.target).remove(); 57 } 58 }); 59 }; 60 prestashop.on( 61 'updateCart', 62 function (event) { 63 var refreshURL = $('.blockcart').data('refresh-url'); 64 var requestData = {}; 65 if (event && event.reason && typeof event.resp !== 'undefined' && !event.resp.hasError) { 66 requestData = { 67 id_customization: event.reason.idCustomization, 68 id_product_attribute: event.reason.idProductAttribute, 69 id_product: event.reason.idProduct, 70 action: event.reason.linkAction 71 }; 72 } 73 if (event && event.resp && event.resp.hasError) { 74 prestashop.emit('showErrorNextToAddtoCartButton', { errorMessage: event.resp.errors.join('<br/>')}); 75 } 76 $.post(refreshURL, requestData).then(function (resp) { 77 var html = $('<div />').append($.parseHTML(resp.preview)); 78 $('.cart-products-count').replaceWith($(resp.preview).find('.cart-products-count')); 79 $('#qmcart-count').replaceWith($(resp.preview).find('.cart-products-count')); 80 $('.cart-products-total').replaceWith($(resp.preview).find('.cart-products-total')); 81 $('.popup_cart').replaceWith($(resp.preview).find('.popup_cart')); 82 83 $('button.add-to-cart').removeClass('loading'); 84 if (resp.modal && !cart.hasClass('cart-sidebar')) { 85 showModal(resp.modal); 86 }else{ 87 if($('body').hasClass('page-cart') || $('body').hasClass('page-order')) return; 88 if(resp.modal){ 89 setTimeout(function(){ 90 $('.blockcart').find('.popup-sidebar').addClass('sidebar-opened'); 91 }, 1); 92 }else{ 93 $('.blockcart').find('.popup-sidebar').addClass('sidebar-opened'); 94 } 95 $('.blockcart > a').click(function(e){ 96 e.preventDefault(); 97 showCartSidebar(); 98 }) 99 $('.close-cart').on('click', function(){ 100 $(this).parents('.popup-sidebar').removeClass('sidebar-opened'); 101 }) 102 } 103 }).fail(function (resp) { 104 prestashop.emit('handleError', { eventType: 'updateShoppingCart', resp: resp }); 105 }); 106 } 107 ); 108 109 $('body').on( 110 'click', 111 '[data-button-action="buy-now"]', 112 (event) => { 113 event.preventDefault(); 114 var cart_url = $('.blockcart a').attr('href'); 115 if ($('#quantity_wanted').val() > $('[data-stock]').data('stock') && $('[data-allow-oosp]').data('allow-oosp').length === 0) { 116 $('[data-button-action="add-to-cart"]').attr('disabled', 'disabled'); 117 } else { 118 let $form = $(event.target).closest('form'); 119 let query = $form.serialize() + '&add=1&action=add'; 120 let actionURL = $form.attr('action'); 121 const buyNowButton = $(event.target); 122 buyNowButton.addClass('loading'); 123 let isQuantityInputValid = ($input) => {
124 var validInput = true; 125 126 $input.each((index, input) => { 127 let $input = $(input); 128 let minimalValue = parseInt($input.attr('min'), 10); 129 if (minimalValue && $input.val() < minimalValue) { 130 onInvalidQuantity($input); 131 validInput = false; 132 } 133 }); 134 135 return validInput; 136 }; 137 138 let onInvalidQuantity = ($input) => { 139 $input.parents('.product-add-to-cart').first().find('.product-minimal-quantity').addClass('error'); 140 $input.parent().find('label').addClass('error'); 141 }; 142 143 let $quantityInput = $form.find('input[min]' ); 144 if (!isQuantityInputValid($quantityInput)) { 145 onInvalidQuantity($quantityInput); 146 147 return; 148 } 149 150 $.post(actionURL, query, null, 'json') 151 .then((resp) => { 152 prestashop.emit('updateCart', { 153 reason: { 154 idProduct: resp.id_product, 155 idProductAttribute: resp.id_product_attribute, 156 idCustomization: resp.id_customization, 157 linkAction: 'add-to-cart', 158 cart: resp.cart, 159 }, 160 resp, 161 }); 162 163 }) 164 .fail((resp) => { 165 prestashop.emit('handleError', { 166 eventType: 'addProductToCart', 167 resp, 168 }); 169 }) 170 .always(() => { 171 buyNowButton.removeClass('loading'); 172 window.location.href = cart_url; 173 }); 174 175 176 } 177 } 178 ); 179});
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.