PageSourceSearch

https://www.toonly.com/assets/projects/user_pages/chunk-372FPXG6.js

js toonly.com collected 2026-09-28 08:18:22 UTC 6,273 bytes, 153 lines download raw bytes

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 drivesProductCarousel, updateProductCarousel, 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    drivesProductCarousel = (component) => !component.element.closest('[data-page-element="ProductsCollection/V1"]');
59    updateProductCarousel = (component, variantId) => {
60      if (!drivesProductCarousel(component)) return;
61      document.dispatchEvent(
62        new CustomEvent("ProductCarousel:Update", {
63          detail: {
64            productId: component.product.id,
65            variantId
66          }
67        })
68      );
69    };
70    mountComponent = (component) => {
71      component.variantValues = Object.entries(component.product.property_values_variant_mapping ?? {}).reduce((acc, entries) => {
72        const values = entries[0].split(",").map((value) => parseInt(value));
73        acc[entries[1]] = values;
74        return acc;
75      }, {}) ?? {};
76      component.valuesPositions = component.product.sorted_property_values?.reduce((acc, property) => {
77        property.value_ids.forEach((value, index) => {
78          acc[value] = index;
79        });
80        return acc;
81      }, {}) ?? {};
82      const variantSelects = component.element.querySelectorAll(".elVariantSelector");
83      const newValues = [...variantSelects].map((e) => e.value);
84      const selectedVariantId = getSelectedVariant(component, 0, newValues);
85      updateProductCarousel(component, selectedVariantId);
86      registerVariantEventListeners(component);
87    };
88    renderAndMount = (component) => {
89      component.render();
90      registerVariantEventListeners(component);
91    };
92    registerVariantEventListeners = (component) => {
93      const variantSelects = component.element.querySelectorAll(".elVariantSelector");
94      const variantPriceSelector = component.element.querySelector('[name="variant_price"]');
95      component.element.querySelector('[href="#add-to-cart"').addEventListener("click", () => {
96        const product = component.product;
97        const variant = component.currentVariant ?? product.variants[0];
98        const price = component.selected_price ?? variant.prices[0];
99        const cartItem = Cart.findItemByProductId(product.id);
100        if (cartItem && Cart.isItemRecurring(cartItem) && price.is_recurring && price.id != cartItem.price_id) {
101          Cart.removeOperation(cartItem, true);
102        }
103        Cart.incrementOperation({
104          product_id: product.id,
105          variant_id: Number(variant.id),
106          price_id: Number(price.id)
107        });
108        openCart();
109      });
110      variantPriceSelector?.addEventListener("click", (evt) => {
111        evt.preventDefault();
112        evt.stopImmediatePropagation();
113        evt.stopPropagation();
114      });
115      variantPriceSelector?.addEventListener("change", (evt) => {
116        evt.stopImmediatePropagation();
117        evt.stopPropagation();
118        evt.preventDefault();
119        const product = component.product;
120        const variant = component.currentVariant ?? product.variants[0];
121        const newPriceId = evt.target.value;
122        const newPrice = variant.prices.find((p) => p.id === newPriceId);
123        component.selected_price = newPrice;
124      });
125      variantSelects.forEach((select, index) => {
126        select.addEventListener("click", (evt) => {
127          evt.stopImmediatePropagation();
128          evt.stopPropagation();
129          evt.preventDefault();
130        });
131        select.addEventListener("change", (evt) => {
132          evt.stopImmediatePropagation();
133          evt.stopPropagation();
134          evt.preventDefault();
135          const newValues = [...variantSelects].map((e) => e.value);
136          const selectedVariantId = getSelectedVariant(component, index, newValues);
137          const newVariant = component.product.variants.find((v) => v.id == String(selectedVariantId));
138          component.currentVariant = newVariant;
139          component.selected_price = component.currentVariant.prices[0];
140          updateProductCarousel(component, newVariant.id);
141          renderAndMount(component);
142        });
143      });
144    };
145  }
146});
147
148export {
149  mountComponent,
150  getSelectedVariant,
151  init_addToCart
152};
153//# sourceMappingURL=chunk-372FPXG6.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.