1import { 2 Cart, 3 init_Cart, 4 openCart 5} from "./chunk-BY72GGKB.js"; 6import { 7 init_cf_utils, 8 range 9} from "./chunk-KIZY5IRN.js"; 10import { 11 __esm, 12 init_define_process 13} from "./chunk-RFSPGZ3L.js"; 14 15// projects/lib/packages/yggdrasil-blueprints/src/Elements/Order/AddToCart/addToCart.ts 16function getSelectedVariant(component, selectedPropertyIndex, newSelectedValue) { 17 const { product, variantValues, valuesPositions } = component; 18 const { sorted_property_values: propertyValues, property_values_variant_mapping: valuesVariants } = product; 19 let selectedValuePath = newSelectedValue.join(","); 20 if (Object.keys(variantValues ?? {}).length <= 1) return product.default_variant?.id; 21 if (!valuesVariants[selectedValuePath]) { 22 const distances = []; 23 const rightPart = range(selectedPropertyIndex + 1, propertyValues.length - 1, 1); 24 const leftPart = range(0, selectedPropertyIndex - 1, 1); 25 const orderedIndexes = rightPart.concat(leftPart); 26 Object.values(variantValues).forEach((values) => { 27 if (values[selectedPropertyIndex] == newSelectedValue[selectedPropertyIndex]) { 28 let distance = 0; 29 let changedProperties = 0; 30 orderedIndexes.forEach((position, count) => { 31 if (values[position] != newSelectedValue[position]) { 32 changedProperties += 1; 33 distance = 1e4 * count + valuesPositions[values[position]] - valuesPositions[newSelectedValue[position]]; 34 } 35 }); 36 distances.push({ 37 distance, 38 changedProperties, 39 variantPath: values.join(",") 40 }); 41 } 42 }); 43 distances.sort((d1, d2) => { 44 const diffChangedProperties = d1.changedProperties - d2.changedProperties; 45 if (diffChangedProperties != 0) return diffChangedProperties; 46 return d1.distance - d2.distance; 47 }); 48 selectedValuePath = distances[0].variantPath; 49 } 50 return valuesVariants[selectedValuePath]; 51} 52var mountComponent, renderAndMount, registerVariantEventListeners; 53var init_addToCart = __esm({ 54 "projects/lib/packages/yggdrasil-blueprints/src/Elements/Order/AddToCart/addToCart.ts"() { 55 init_define_process(); 56 init_Cart(); 57 init_cf_utils(); 58 mountComponent = (component) => { 59 component.variantValues = Object.entries(component.product.property_values_variant_mapping ?? {}).reduce((acc, entries) => { 60 const values = entries[0].split(",").map((value) => parseInt(value)); 61 acc[entries[1]] = values; 62 return acc; 63 }, {}) ?? {}; 64 component.valuesPositions = component.product.sorted_property_values?.reduce((acc, property) => { 65 property.value_ids.forEach((value, index) => { 66 acc[value] = index; 67 }); 68 return acc; 69 }, {}) ?? {}; 70 const variantSelects = component.element.querySelectorAll(".elVariantSelector"); 71 const newValues = [...variantSelects].map((e) => e.value); 72 const selectedVariantId = getSelectedVariant(component, 0, newValues); 73 document.dispatchEvent( 74 new CustomEvent("ProductCarousel:Update", { 75 detail: { 76 productId: component.product.id, 77 variantId: selectedVariantId 78 } 79 }) 80 ); 81 registerVariantEventListeners(component); 82 }; 83 renderAndMount = (component) => { 84 component.render(); 85 registerVariantEventListeners(component); 86 }; 87 registerVariantEventListeners = (component) => { 88 const variantSelects = component.element.querySelectorAll(".elVariantSelector"); 89 const variantPriceSelector = component.element.querySelector('[name="variant_price"]'); 90 component.element.querySelector('[href="#add-to-cart"').addEventListener("click", () => { 91 const product = component.product; 92 const variant = component.currentVariant ?? product.variants[0]; 93 const price = component.selected_price ?? variant.prices[0]; 94 const cartItem = Cart.findItemByProductId(product.id); 95 if (cartItem && Cart.isItemRecurring(cartItem) && price.is_recurring && price.id != cartItem.price_id) { 96 Cart.removeOperation(cartItem, true); 97 } 98 Cart.incrementOperation({ 99 product_id: product.id, 100 variant_id: Number(variant.id), 101 price_id: Number(price.id) 102 }); 103 openCart(); 104 }); 105 variantPriceSelector?.addEventListener("click", (evt) => { 106 evt.preventDefault(); 107 evt.stopImmediatePropagation(); 108 evt.stopPropagation(); 109 }); 110 variantPriceSelector?.addEventListener("change", (evt) => { 111 evt.stopImmediatePropagation(); 112 evt.stopPropagation(); 113 evt.preventDefault(); 114 const product = component.product; 115 const variant = component.currentVariant ?? product.variants[0]; 116 const newPriceId = evt.target.value; 117 const newPrice = variant.prices.find((p) => p.id === newPriceId); 118 component.selected_price = newPrice; 119 });
120 variantSelects.forEach((select, index) => { 121 select.addEventListener("click", (evt) => { 122 evt.stopImmediatePropagation(); 123 evt.stopPropagation(); 124 evt.preventDefault(); 125 }); 126 select.addEventListener("change", (evt) => { 127 evt.stopImmediatePropagation(); 128 evt.stopPropagation(); 129 evt.preventDefault(); 130 const newValues = [...variantSelects].map((e) => e.value); 131 const selectedVariantId = getSelectedVariant(component, index, newValues); 132 const newVariant = component.product.variants.find((v) => v.id == String(selectedVariantId)); 133 component.currentVariant = newVariant; 134 component.selected_price = component.currentVariant.prices[0]; 135 document.dispatchEvent( 136 new CustomEvent("ProductCarousel:Update", { 137 detail: { 138 productId: component.product.id, 139 variantId: newVariant.id 140 } 141 }) 142 ); 143 renderAndMount(component); 144 }); 145 }); 146 }; 147 } 148}); 149 150export { 151 mountComponent, 152 getSelectedVariant, 153 init_addToCart 154}; 155//# sourceMappingURL=chunk-Q6RFXGRD.js.map
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.