1/** 2 * Add products to cart with array 3 * You can set products options using format : 6{1}4 (product n°6 with option n°1 with value n°4) 4 * @param {Array} products [{id:6, qty:5}, {id:5, qty:10}] 5 */ 6async function add_to_cart_from_desc(products) { 7 for (const prd of products) { 8 add_to_cart_from_desc_single(prd['id'], prd['qty'], false); 9 } 10} 11 12async function add_to_cart_from_desc_single(id, qty, drawer_option = true) { 13 endpoint = new URL(document.baseURI + "api/v2/product/" + id); 14 endpoint_params = { 15 'quantity': qty, 16 }; 17 endpoint.search = new URLSearchParams(endpoint_params).toString(); 18 19 response = await fetch(endpoint, { 20 method: 'GET' 21 }); 22 23 if (!response.ok) { 24 return; 25 } 26 27 let json = await response.json(); 28 29 if (drawer_option && (json.is_master || json.has_option_with_multiples_values)) { 30 await openProductDrawer(id); 31 return; 32 } 33 34 await addProductToCart(id, qty, true); 35} 36 37/** 38 * Add product to cart using block info 39 * @param {int} id 40 */ 41async function add_to_cart(id) { 42 let qty = document.getElementById("qty_" + id).value; 43 let full_id = get_full_product_id(id); 44 let form = document.querySelector("#prd_" + id + " form"); 45 46 if (form.checkValidity()) { 47 await addProductToCart(full_id, qty, true); 48 } else { 49 form.reportValidity(); 50 } 51} 52 53/** 54 * Add product to wishlist 55 * @param {int} id 56 */ 57async function add_to_wishlist(id) { 58 await openWishlistDrawer(); 59 await addProductToWishlist(id, true); 60} 61 62/** 63 * Remove product from wishlist 64 * @param {int} id 65 */ 66async function remove_from_wishlist(id) { 67 //await openWishlistDrawer(); 68 await removeProductFromWishlist(id, false); 69} 70 71/** 72 * Return the product id with option using product block 73 * @param {string} id 74 * @returns {string} 75 */ 76function get_full_product_id(id) { 77 // get attributes 78 let attrs = ''; 79 let attributes = document.querySelectorAll('#prd_' + id + ' .bloc_option select'); 80 attributes.forEach((select) => { 81 let attr_id = select.name.substring(3).slice(0, -1); 82 let attr_value = select.options[select.selectedIndex].value; 83 84 if (attr_id != '' && attr_value != '') { 85 attrs += '{' + attr_id + '}' + attr_value; 86 } 87 }); 88 89 let real_id = remove_random_from_id(id); 90 91 return real_id + '' + attrs; 92} 93 94let quantity_product = new Quantity(async function(obj) { 95 // On vérifie si on est actuellement dans le drawer produit 96 let drawer = document.querySelector('#product_drawer'); 97 let in_drawer = drawer ? drawer.contains(obj) : false; 98 99 // Mise à jour des prix produits 100 let id = obj.id.substring(4); 101 await update_product(id, null, in_drawer); 102}); 103 104/** 105 * Remove last part of html id 106 * @param {string} id ex : 1234_d / 1234_p 107 * @returns {int} 1234 108 */ 109function remove_random_from_id(id) { 110 return id.split('_')[0]; 111} 112 113/** 114 * Update product using block info 115 * @param {string} id 116 */ 117async function update_product(id, full_id = null, in_drawer) { 118 let block = document.getElementById("prd_" + id); 119 if (!block) { 120 console.log('Product ' + id + ' does not exists in page'); 121 return; 122 } 123 124 let qty = document.getElementById("qty_" + id).value; 125 126 if (full_id == null) { 127 full_id = get_full_product_id(id); 128 } 129 130 await update_product_with_params(full_id, block, qty, in_drawer); 131} 132 133/** 134 * Update product block 135 * @param {string} id Product ID (simple or with options) 136 * @param {HTMLElement} block Html block to update 137 * @param {int} qty Product quantity 138 * @param {boolean} only_content Only update content (first child of block) 139 * @param {boolean} on_master_page Are we on master product page ? (use to display or hide some info) 140 * @param {boolean} is_drawer Are we in the product drawer ? 141 * @param {boolean} use_new_template Use new template (only price and qty) ? 142 */ 143async function update_product_with_params(id, block, qty, in_drawer) { 144 let endpoint_params = { 145 'qty': qty, 146 'in_drawer': in_drawer 147 }; 148 149 let endpoint = new URL(document.baseURI + "products/" + id + "/container-prix"); 150 151 endpoint.search = new URLSearchParams(endpoint_params).toString(); 152 153 let response = await fetch(endpoint, { 154 method: 'GET' 155 }); 156 if (response.ok) { 157 let html = await response.text();
158 /* Update elements */ 159 block.outerHTML = html; 160 161 /* Update links */ 162 let new_block = document.getElementById(block.id); 163 insertRpdEvents(new_block); 164 } else { 165 let retry_js = `update_product_with_params(${id}, ${block.id}, ${qty})`; 166 block.outerHTML = ` 167 <div class="global_container_prix_produit" id="${block.id}" style="height: ${block.clientHeight}px"> 168 <div style="background: white; display: flex; justify-content: center; align-items: center; flex-direction: column; gap: 5%; height: 100%"> 169 <span>Une erreur s\'est produite pendant le chargement du produit</span> 170 <button onclick="${retry_js}">Réessayer</button> 171 </div> 172 </div> 173 `; 174 } 175} 176 177function centerInParentHorizontal(parent, child) { 178 if (!parent || !child) { 179 return; 180 } 181 182 var parentRect = parent.getBoundingClientRect(); 183 var childRect = child.getBoundingClientRect(); 184 185 let scroll_destination = (parent.clientWidth / 2) - (child.clientWidth / 2); 186 let scroll_left = (childRect.left - parentRect.left) - scroll_destination; 187 188 parent.scrollLeft += scroll_left; 189} 190 191function scrollToSelected(parent, selected_class) {
192 let child = parent.querySelector("." + selected_class); 193 centerInParentHorizontal(parent, child); 194} 195 196function scrollToProductDescription() { 197 to_position('.desc_produit'); 198 document.querySelector('#product-description').checked = true; 199}
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.