create-abcart-theme 0.10.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +133 -127
- package/LICENSE +202 -202
- package/NOTICE +2 -2
- package/README.md +65 -65
- package/bin/create-abcart-theme.mjs +4 -4
- package/package.json +49 -49
- package/src/args.mjs +113 -113
- package/src/banner.mjs +21 -21
- package/src/cli.mjs +119 -119
- package/src/create.mjs +85 -82
- package/src/package-manager.mjs +32 -32
- package/src/style.mjs +17 -17
- package/templates/default/.agents/skills/{wocart-docs → abcart-docs}/SKILL.md +146 -146
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/SKILL.md +58 -58
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/inputs-and-rights.md +47 -47
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/layout-rtl-and-phone.md +36 -36
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/measure-the-design.md +60 -60
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/tokens-sections-content.md +103 -103
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/references/verify-and-report.md +102 -102
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/compare-design.cjs +230 -230
- package/templates/default/.agents/skills/{wocart-theme-from-design → abcart-theme-from-design}/scripts/qa-pages.cjs +219 -219
- package/templates/default/.agents/skills/shopify-theme-asset-collector/SKILL.md +125 -125
- package/templates/default/.agents/skills/shopify-theme-asset-collector/scripts/collect_public_assets.py +370 -370
- package/templates/default/.dev.vars.example +12 -12
- package/templates/default/AGENTS.md +240 -240
- package/templates/default/README.md +184 -184
- package/templates/default/_gitignore +11 -11
- package/templates/default/astro.config.mjs +85 -85
- package/templates/default/package.json +36 -36
- package/templates/default/public/fonts/OFL.txt +96 -96
- package/templates/default/src/actions/index.ts +22 -22
- package/templates/default/src/components/cart/AddToCartButton.astro +45 -45
- package/templates/default/src/components/cart/CartDrawer.astro +47 -47
- package/templates/default/src/components/cart/CartSummary.astro +11 -11
- package/templates/default/src/components/cart/FreeShipping.astro +7 -7
- package/templates/default/src/components/checkout/CheckoutMain.astro +29 -29
- package/templates/default/src/components/checkout/OrderForm.astro +280 -280
- package/templates/default/src/components/checkout/ThankYouMain.astro +68 -68
- package/templates/default/src/components/landing/LandingImages.astro +51 -51
- package/templates/default/src/components/landing/LandingOrder.astro +46 -46
- package/templates/default/src/components/landing/LandingProduct.astro +41 -41
- package/templates/default/src/components/listing/ListingMain.astro +93 -93
- package/templates/default/src/components/listing/PageMain.astro +52 -52
- package/templates/default/src/components/listing/Pagination.astro +31 -31
- package/templates/default/src/components/listing/SortControl.astro +35 -35
- package/templates/default/src/components/product/BuyBox.astro +96 -96
- package/templates/default/src/components/product/Description.astro +24 -24
- package/templates/default/src/components/product/Gallery.astro +66 -66
- package/templates/default/src/components/product/MobileBuyBar.astro +29 -29
- package/templates/default/src/components/product/OfferChoice.astro +66 -66
- package/templates/default/src/components/product/ProductMain.astro +40 -40
- package/templates/default/src/components/product/ProductReviews.astro +72 -72
- package/templates/default/src/components/product/RelatedProducts.astro +30 -30
- package/templates/default/src/components/product/blocks/BenefitsBlock.astro +13 -13
- package/templates/default/src/components/product/blocks/CollapsibleBlock.astro +20 -20
- package/templates/default/src/components/product/blocks/DescriptionBlock.astro +19 -19
- package/templates/default/src/components/product/blocks/DetailsBlock.astro +33 -33
- package/templates/default/src/components/product/blocks/GalleryBlock.astro +12 -12
- package/templates/default/src/components/product/blocks/OrderBlock.astro +30 -30
- package/templates/default/src/components/product/blocks/PriceBlock.astro +23 -23
- package/templates/default/src/components/product/blocks/RatingBlock.astro +16 -16
- package/templates/default/src/components/product/blocks/StockBlock.astro +22 -22
- package/templates/default/src/components/product/blocks/TextBlock.astro +11 -11
- package/templates/default/src/components/product/blocks/TitleBlock.astro +20 -20
- package/templates/default/src/components/reviews/ReviewCard.astro +46 -46
- package/templates/default/src/components/reviews/ReviewForm.astro +113 -113
- package/templates/default/src/components/reviews/VoiceNote.astro +60 -60
- package/templates/default/src/components/sections/CollectionCards.astro +65 -65
- package/templates/default/src/components/sections/Countdown.astro +57 -57
- package/templates/default/src/components/sections/Faq.astro +44 -44
- package/templates/default/src/components/sections/FeaturedProduct.astro +71 -71
- package/templates/default/src/components/sections/Hero.astro +99 -99
- package/templates/default/src/components/sections/ImageBanner.astro +46 -46
- package/templates/default/src/components/sections/ProductGrid.astro +47 -47
- package/templates/default/src/components/sections/RichText.astro +35 -35
- package/templates/default/src/components/sections/Steps.astro +46 -46
- package/templates/default/src/components/sections/Testimonials.astro +57 -57
- package/templates/default/src/components/shell/Footer.astro +80 -80
- package/templates/default/src/components/shell/Header.astro +89 -89
- package/templates/default/src/components/shell/LanguageMenu.astro +33 -33
- package/templates/default/src/components/shell/MobileMenu.astro +65 -65
- package/templates/default/src/components/shell/SearchDialog.astro +19 -19
- package/templates/default/src/components/shell/TopBar.astro +54 -54
- package/templates/default/src/components/shell/WhatsAppWidget.astro +111 -111
- package/templates/default/src/components/store/ContactMain.astro +79 -79
- package/templates/default/src/components/store/SearchMain.astro +56 -56
- package/templates/default/src/components/store/TrackOrderMain.astro +161 -161
- package/templates/default/src/components/surface/Area.astro +20 -20
- package/templates/default/src/components/surface/PreviewBridge.astro +24 -24
- package/templates/default/src/components/surface/Sections.astro +35 -35
- package/templates/default/src/components/ui/Breadcrumbs.astro +36 -36
- package/templates/default/src/components/ui/Button.astro +45 -45
- package/templates/default/src/components/ui/EditorPlaceholder.astro +24 -24
- package/templates/default/src/components/ui/Icon.astro +93 -93
- package/templates/default/src/components/ui/Logo.astro +36 -36
- package/templates/default/src/components/ui/Photo.astro +46 -46
- package/templates/default/src/components/ui/Price.astro +24 -24
- package/templates/default/src/components/ui/ProductCard.astro +41 -41
- package/templates/default/src/components/ui/Rating.astro +22 -22
- package/templates/default/src/components/ui/Section.astro +34 -34
- package/templates/default/src/components/ui/SectionHeading.astro +19 -19
- package/templates/default/src/env.d.ts +23 -23
- package/templates/default/src/layouts/StoreLayout.astro +145 -145
- package/templates/default/src/lib/browser-copy.ts +37 -37
- package/templates/default/src/lib/context.ts +89 -89
- package/templates/default/src/lib/coupon-notice.ts +47 -47
- package/templates/default/src/lib/i18n.ts +32 -32
- package/templates/default/src/lib/images.ts +38 -38
- package/templates/default/src/lib/listing-query.ts +41 -41
- package/templates/default/src/lib/money.ts +16 -16
- package/templates/default/src/lib/motion.ts +16 -16
- package/templates/default/src/lib/nav.ts +26 -26
- package/templates/default/src/lib/offer-tiles.ts +105 -105
- package/templates/default/src/lib/order-outcome.ts +66 -66
- package/templates/default/src/lib/otp-message.ts +21 -21
- package/templates/default/src/lib/products.ts +38 -38
- package/templates/default/src/lib/review-refusals.ts +31 -31
- package/templates/default/src/lib/store.ts +31 -31
- package/templates/default/src/lib/whatsapp.ts +29 -29
- package/templates/default/src/middleware.ts +23 -23
- package/templates/default/src/pages/404.astro +22 -22
- package/templates/default/src/pages/{wocart-preview.astro → abcart-preview.astro} +43 -43
- package/templates/default/src/pages/{wocart-revalidate.ts → abcart-revalidate.ts} +10 -10
- package/templates/default/src/pages/{wocart-styles.css.ts → abcart-styles.css.ts} +5 -5
- package/templates/default/src/pages/{wocart-theme.json.ts → abcart-theme.json.ts} +5 -5
- package/templates/default/src/pages/api/abandoned/convert.ts +4 -4
- package/templates/default/src/pages/api/abandoned/upsert.ts +4 -4
- package/templates/default/src/pages/api/cart/validate.ts +4 -4
- package/templates/default/src/pages/api/communes/[wilayaId].ts +4 -4
- package/templates/default/src/pages/api/otp/send.ts +4 -4
- package/templates/default/src/pages/api/otp/verify.ts +4 -4
- package/templates/default/src/pages/api/reviews/[productId].ts +3 -3
- package/templates/default/src/pages/api/reviews/eligibility.ts +3 -3
- package/templates/default/src/pages/api/reviews/submit.ts +3 -3
- package/templates/default/src/pages/api/reviews/upload.ts +3 -3
- package/templates/default/src/pages/category/[slug].astro +20 -20
- package/templates/default/src/pages/checkout.astro +25 -25
- package/templates/default/src/pages/collections/[handle].astro +20 -20
- package/templates/default/src/pages/contact.astro +17 -17
- package/templates/default/src/pages/index.astro +9 -9
- package/templates/default/src/pages/lp/[slug].astro +47 -47
- package/templates/default/src/pages/pages/[slug].astro +16 -16
- package/templates/default/src/pages/products/[handle].astro +37 -37
- package/templates/default/src/pages/products/index.astro +15 -15
- package/templates/default/src/pages/robots.txt.ts +9 -9
- package/templates/default/src/pages/search.astro +19 -19
- package/templates/default/src/pages/sitemap.xml.ts +12 -12
- package/templates/default/src/pages/thank-you.astro +33 -33
- package/templates/default/src/pages/track-order.astro +17 -17
- package/templates/default/src/scripts/cart.ts +371 -371
- package/templates/default/src/scripts/checkout/coupon.ts +96 -96
- package/templates/default/src/scripts/checkout/delivery.ts +86 -86
- package/templates/default/src/scripts/checkout/form.ts +54 -54
- package/templates/default/src/scripts/checkout/otp.ts +106 -106
- package/templates/default/src/scripts/checkout/picker.ts +169 -169
- package/templates/default/src/scripts/checkout/quote.ts +169 -169
- package/templates/default/src/scripts/checkout/submit.ts +113 -113
- package/templates/default/src/scripts/checkout/tracking.ts +74 -74
- package/templates/default/src/scripts/countdown.ts +29 -29
- package/templates/default/src/scripts/events.ts +4 -4
- package/templates/default/src/scripts/hero.ts +49 -49
- package/templates/default/src/scripts/listing.ts +13 -13
- package/templates/default/src/scripts/order-form.ts +100 -100
- package/templates/default/src/scripts/product.ts +306 -306
- package/templates/default/src/scripts/public-config.ts +79 -79
- package/templates/default/src/scripts/reviews.ts +358 -358
- package/templates/default/src/scripts/site.ts +131 -131
- package/templates/default/src/scripts/thank-you.ts +11 -11
- package/templates/default/src/scripts/voice-note.ts +94 -94
- package/templates/default/src/styles/base.css +1030 -1030
- package/templates/default/src/styles/checkout.css +1111 -1111
- package/templates/default/src/styles/global.css +12 -12
- package/templates/default/src/styles/product.css +871 -871
- package/templates/default/src/styles/reviews.css +533 -533
- package/templates/default/src/styles/sections.css +931 -931
- package/templates/default/src/styles/shell.css +970 -970
- package/templates/default/src/styles/tokens.css +583 -583
- package/templates/default/src/theme/api-routes.ts +10 -10
- package/templates/default/src/theme/content/catalog.ts +117 -117
- package/templates/default/src/theme/content/checkout.ts +168 -168
- package/templates/default/src/theme/content/editor.ts +21 -21
- package/templates/default/src/theme/content/index.ts +45 -45
- package/templates/default/src/theme/content/reviews.ts +76 -76
- package/templates/default/src/theme/content/shell.ts +59 -59
- package/templates/default/src/theme/content/types.ts +12 -12
- package/templates/default/src/theme/documents/catalog.json +11 -11
- package/templates/default/src/theme/documents/category.json +11 -11
- package/templates/default/src/theme/documents/checkout.json +11 -11
- package/templates/default/src/theme/documents/collection.json +11 -11
- package/templates/default/src/theme/documents/contact.json +11 -11
- package/templates/default/src/theme/documents/footer.area.json +11 -11
- package/templates/default/src/theme/documents/header.area.json +16 -16
- package/templates/default/src/theme/documents/home.json +313 -313
- package/templates/default/src/theme/documents/landing.json +8 -8
- package/templates/default/src/theme/documents/page.json +11 -11
- package/templates/default/src/theme/documents/product.json +125 -125
- package/templates/default/src/theme/documents/search.json +11 -11
- package/templates/default/src/theme/documents/thank-you.json +11 -11
- package/templates/default/src/theme/documents/track-order.json +11 -11
- package/templates/default/src/theme/fields.ts +61 -61
- package/templates/default/src/theme/key.ts +11 -11
- package/templates/default/src/theme/labels.ts +231 -231
- package/templates/default/src/theme/locales.ts +11 -11
- package/templates/default/src/theme/palette.ts +14 -14
- package/templates/default/src/theme/sections/collection-cards.ts +65 -65
- package/templates/default/src/theme/sections/faq.ts +74 -74
- package/templates/default/src/theme/sections/featured-product.ts +42 -42
- package/templates/default/src/theme/sections/hero.ts +47 -47
- package/templates/default/src/theme/sections/image-banner.ts +25 -25
- package/templates/default/src/theme/sections/index.ts +40 -40
- package/templates/default/src/theme/sections/landing.ts +150 -150
- package/templates/default/src/theme/sections/listing.ts +84 -84
- package/templates/default/src/theme/sections/product-grid.ts +42 -42
- package/templates/default/src/theme/sections/product.ts +297 -297
- package/templates/default/src/theme/sections/rich-text.ts +39 -39
- package/templates/default/src/theme/sections/shared.ts +54 -54
- package/templates/default/src/theme/sections/shell.ts +85 -85
- package/templates/default/src/theme/sections/store-pages.ts +101 -101
- package/templates/default/src/theme/sections/testimonials.ts +61 -61
- package/templates/default/src/theme/sections/thank-you.ts +35 -35
- package/templates/default/src/theme/settings.ts +124 -124
- package/templates/default/src/theme/theme.ts +65 -65
- package/templates/default/tests/fixtures/canvas-tote.json +62 -62
- package/templates/default/tests/fixtures/linen-shirt.json +128 -128
- package/templates/default/tests/unit/checkout.test.ts +241 -241
- package/templates/default/tests/unit/contract.test.ts +170 -169
- package/templates/default/tests/unit/contrast.test.ts +68 -68
- package/templates/default/tests/unit/copy.test.ts +101 -101
- package/templates/default/tests/unit/design-values.test.ts +95 -95
- package/templates/default/tests/unit/editability.test.ts +180 -180
- package/templates/default/tests/unit/fixtures.ts +91 -91
- package/templates/default/tests/unit/footer.test.ts +54 -54
- package/templates/default/tests/unit/guards.test.ts +340 -340
- package/templates/default/tests/unit/images.test.ts +26 -26
- package/templates/default/tests/unit/landing.test.ts +151 -151
- package/templates/default/tests/unit/languages.test.ts +42 -42
- package/templates/default/tests/unit/middleware.test.ts +53 -53
- package/templates/default/tests/unit/offer-tiles.test.ts +75 -75
- package/templates/default/tests/unit/render.ts +38 -38
- package/templates/default/tests/unit/reviews.test.ts +25 -25
- package/templates/default/tests/unit/rules.test.ts +59 -59
- package/templates/default/tests/unit/source.ts +75 -75
- package/templates/default/tests/unit/store-pages.test.ts +147 -147
- package/templates/default/tests/unit/studio.test.ts +202 -202
- package/templates/default/tests/unit/testimonials.test.ts +52 -52
- package/templates/default/tests/unit/thank-you.test.ts +52 -52
- package/templates/default/tests/unit/tracking.test.ts +29 -29
- package/templates/default/tests/unit/voice-note.test.ts +42 -42
- package/templates/default/tsconfig.json +13 -13
- package/templates/default/vitest.config.ts +15 -15
- package/templates/default/wrangler.jsonc +16 -16
|
@@ -1,306 +1,306 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A product on the page: its photos, the options, the offer tiles or the
|
|
3
|
-
* quantity, and a variant for each piece of a multi-piece offer. The choice
|
|
4
|
-
* is written into the fields the order form and the basket read, and the
|
|
5
|
-
* prices follow the chosen variant. The phone's sticky bar shows what the
|
|
6
|
-
* choice costs — the server's quote once the form has it.
|
|
7
|
-
*/
|
|
8
|
-
import {
|
|
9
|
-
formatMoney,
|
|
10
|
-
getOfferTierView,
|
|
11
|
-
initialVariant,
|
|
12
|
-
optionStates,
|
|
13
|
-
pickOption,
|
|
14
|
-
variantFor,
|
|
15
|
-
type OptionSelection,
|
|
16
|
-
storefrontTracking,
|
|
17
|
-
} from "@abcart/storefront";
|
|
18
|
-
import type { BrowserProduct } from "@/lib/products";
|
|
19
|
-
import { ORDER_QUOTE_EVENT, PRODUCT_CHANGE_EVENT } from "./events";
|
|
20
|
-
import { bindEach } from "@abcart/theme/lifecycle";
|
|
21
|
-
import { readPublicConfig, translate } from "./public-config";
|
|
22
|
-
|
|
23
|
-
const config = readPublicConfig();
|
|
24
|
-
const tr = (key: string, values?: Record<string, string | number>) => translate(config, key, values);
|
|
25
|
-
const money = (amount: number) => formatMoney(Math.round(amount), { symbol: config.symbol, locale: config.localeTag, decimals: 0 });
|
|
26
|
-
const labelOf = (variations: Readonly<Record<string, string>> | undefined) => Object.values(variations ?? {}).filter((value) => value !== "Default Title").join(" · ");
|
|
27
|
-
|
|
28
|
-
bindEach<HTMLElement>("[data-product]", (root) => {
|
|
29
|
-
const dataNode = root.querySelector<HTMLScriptElement>("[data-product-data]");
|
|
30
|
-
if (!dataNode?.textContent) return;
|
|
31
|
-
const product = JSON.parse(dataNode.textContent) as BrowserProduct;
|
|
32
|
-
const catalog = { variants: product.variants, trackInventory: product.trackInventory, variantOptions: product.variantOptions };
|
|
33
|
-
let variant = initialVariant(catalog);
|
|
34
|
-
let selection: OptionSelection = { ...(variant?.variations ?? {}) };
|
|
35
|
-
|
|
36
|
-
const all = <T extends Element>(selector: string) => Array.from(root.querySelectorAll<T>(selector));
|
|
37
|
-
const optionButtons = all<HTMLButtonElement>("[data-option]");
|
|
38
|
-
const tiles = all<HTMLLabelElement>("[data-tile]");
|
|
39
|
-
const units = root.querySelector<HTMLElement>("[data-offer-units]");
|
|
40
|
-
const unitRows = root.querySelector<HTMLElement>("[data-offer-unit-rows]");
|
|
41
|
-
const unitTemplate = root.querySelector<HTMLTemplateElement>("[data-offer-unit-template]");
|
|
42
|
-
const qtyInput = root.querySelector<HTMLInputElement>("[data-qty-input]");
|
|
43
|
-
const mobileBar = root.querySelector<HTMLElement>("[data-mobile-buy]");
|
|
44
|
-
const mobileTotal = root.querySelector<HTMLElement>("[data-mobile-total]");
|
|
45
|
-
/** The variant each piece of the chosen tile is ordered in, by its options. */
|
|
46
|
-
let pieces: OptionSelection[] = [];
|
|
47
|
-
/** Pieces the shopper set by hand keep their choice when the main options change. */
|
|
48
|
-
const touched = new Set<number>();
|
|
49
|
-
|
|
50
|
-
const price = () => variant?.price ?? product.price;
|
|
51
|
-
const compare = () => variant?.compareAtPrice ?? product.compareAtPrice ?? null;
|
|
52
|
-
const chosenTile = () => tiles.find((tile) => tile.querySelector<HTMLInputElement>("input")?.checked && !tile.hidden) ?? null;
|
|
53
|
-
const quantity = () => {
|
|
54
|
-
const tile = chosenTile();
|
|
55
|
-
if (tile) return Number(tile.dataset.quantity) || 1;
|
|
56
|
-
return Math.max(1, Math.min(Number(qtyInput?.max) || 100, Number(qtyInput?.value) || 1));
|
|
57
|
-
};
|
|
58
|
-
const setField = (name: string, value: string) => all<HTMLInputElement>(`input[type="hidden"][name="${name}"]`).forEach((input) => (input.value = value));
|
|
59
|
-
const stockOf = (candidate = variant) => (candidate ? candidate.inventory : product.inventory);
|
|
60
|
-
const soldOut = () => product.trackInventory && stockOf() <= 0;
|
|
61
|
-
|
|
62
|
-
function paintOptions() {
|
|
63
|
-
const states = optionStates(catalog, selection);
|
|
64
|
-
for (const button of optionButtons) {
|
|
65
|
-
const name = button.dataset.option ?? "";
|
|
66
|
-
const value = button.dataset.value ?? "";
|
|
67
|
-
const state = states[name]?.[value] ?? "unavailable";
|
|
68
|
-
button.dataset.state = state;
|
|
69
|
-
button.disabled = state === "unavailable";
|
|
70
|
-
button.setAttribute("aria-pressed", selection[name] === value ? "true" : "false");
|
|
71
|
-
}
|
|
72
|
-
all<HTMLElement>("[data-option-chosen]").forEach((node) => (node.textContent = selection[node.dataset.optionChosen ?? ""] ?? ""));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function paintPrice() {
|
|
76
|
-
const now = price();
|
|
77
|
-
const before = compare();
|
|
78
|
-
const discounted = before !== null && before > now;
|
|
79
|
-
all<HTMLElement>("[data-product-price]").forEach((node) => (node.textContent = money(now)));
|
|
80
|
-
all<HTMLElement>("[data-product-compare]").forEach((node) => {
|
|
81
|
-
node.textContent = discounted ? money(before) : "";
|
|
82
|
-
node.hidden = !discounted;
|
|
83
|
-
});
|
|
84
|
-
const percent = discounted ? Math.round((1 - now / before) * 100) : 0;
|
|
85
|
-
all<HTMLElement>("[data-product-save]").forEach((node) => {
|
|
86
|
-
node.textContent = discounted ? tr("product.save", { percent }) : "";
|
|
87
|
-
node.hidden = !discounted;
|
|
88
|
-
});
|
|
89
|
-
all<HTMLElement>("[data-gallery-sticker]").forEach((node) => {
|
|
90
|
-
node.textContent = `−${percent}%`;
|
|
91
|
-
node.hidden = !discounted;
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function paintStock() {
|
|
96
|
-
const stock = root.querySelector<HTMLElement>("[data-stock]");
|
|
97
|
-
const out = soldOut();
|
|
98
|
-
if (stock) {
|
|
99
|
-
const low = !out && product.trackInventory && stock.dataset.lowAlert === "true" && stockOf() <= Math.max(product.lowStockThreshold, 1);
|
|
100
|
-
stock.dataset.out = out ? "true" : "false";
|
|
101
|
-
stock.dataset.low = low ? "true" : "false";
|
|
102
|
-
const text = stock.querySelector<HTMLElement>("[data-stock-text]");
|
|
103
|
-
if (text) text.textContent = out ? tr("product.soldOut") : low ? tr("product.lowStock", { count: stockOf() }) : tr("common.inStock");
|
|
104
|
-
}
|
|
105
|
-
all<HTMLButtonElement>("[data-add-to-cart], [data-checkout-submit]").forEach((button) => {
|
|
106
|
-
button.disabled = out;
|
|
107
|
-
});
|
|
108
|
-
if (qtyInput && product.trackInventory) qtyInput.max = String(Math.max(1, Math.min(100, stockOf())));
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** Tiles tied to another variant hide; a hidden choice falls back to the first tile shown. */
|
|
112
|
-
function showTiles() {
|
|
113
|
-
for (const tile of tiles) tile.hidden = Boolean(tile.dataset.variant) && tile.dataset.variant !== variant?.id;
|
|
114
|
-
if (tiles.length > 0 && !chosenTile()) {
|
|
115
|
-
const first = tiles.find((tile) => !tile.hidden)?.querySelector<HTMLInputElement>("input");
|
|
116
|
-
if (first) first.checked = true;
|
|
117
|
-
}
|
|
118
|
-
tiles.forEach((tile) => tile.classList.toggle("is-chosen", tile === chosenTile()));
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/** What the chosen pieces cost at their own variants' prices; null until each piece is one that exists. */
|
|
122
|
-
function piecesPrice(): number | null {
|
|
123
|
-
if (pieces.length === 0) return null;
|
|
124
|
-
const matched = pieces.map((piece) => variantFor(product.variants, piece));
|
|
125
|
-
return matched.every(Boolean) ? matched.reduce((sum, match) => sum + match!.price, 0) : null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/** Each tile's price at the chosen variant; the chosen tile at the variant of each of its pieces. */
|
|
129
|
-
function priceTiles() {
|
|
130
|
-
for (const tile of tiles) {
|
|
131
|
-
const offer = product.offers.find((candidate) => candidate.id === tile.dataset.offer);
|
|
132
|
-
if (tile.dataset.fixed === "true") continue;
|
|
133
|
-
const view = offer ? getOfferTierView(offer, price(), compare()) : null;
|
|
134
|
-
const ownPieces = tile === chosenTile() ? piecesPrice() : null;
|
|
135
|
-
const amount = ownPieces ?? (view ? view.price : price() * (Number(tile.dataset.quantity) || 1));
|
|
136
|
-
const crossed = view ? view.comparePrice : compare();
|
|
137
|
-
tile.querySelector<HTMLElement>("[data-tile-price]")!.textContent = money(amount);
|
|
138
|
-
const was = tile.querySelector<HTMLElement>("[data-tile-compare]");
|
|
139
|
-
if (was) {
|
|
140
|
-
was.hidden = !(crossed !== null && crossed > amount);
|
|
141
|
-
was.textContent = was.hidden ? "" : money(crossed!);
|
|
142
|
-
}
|
|
143
|
-
const save = tile.querySelector<HTMLElement>("[data-tile-save]");
|
|
144
|
-
if (save && view) {
|
|
145
|
-
save.hidden = view.savingsAmount <= 0;
|
|
146
|
-
save.textContent = save.hidden ? "" : tr("offer.saving", { amount: money(view.savingsAmount) });
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/** A multi-piece tile on a product with options: one row of options per piece. */
|
|
152
|
-
function paintPieces() {
|
|
153
|
-
const tile = chosenTile();
|
|
154
|
-
const count = quantity();
|
|
155
|
-
const perPiece = Boolean(units && unitRows && unitTemplate && tile && count > 1 && !tile.dataset.variant);
|
|
156
|
-
if (units) units.hidden = !perPiece;
|
|
157
|
-
if (!perPiece || !unitRows || !unitTemplate) {
|
|
158
|
-
pieces = [];
|
|
159
|
-
touched.clear();
|
|
160
|
-
unitRows?.replaceChildren();
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
pieces = Array.from({ length: count }, (_, index) => (touched.has(index) && pieces[index] ? pieces[index]! : { ...selection }));
|
|
164
|
-
unitRows.replaceChildren(
|
|
165
|
-
...pieces.map((piece, index) => {
|
|
166
|
-
const row = unitTemplate.content.firstElementChild!.cloneNode(true) as HTMLElement;
|
|
167
|
-
row.querySelector<HTMLElement>("[data-offer-unit-label]")!.textContent = tr("offer.piece", { n: index + 1 });
|
|
168
|
-
const states = optionStates(catalog, piece);
|
|
169
|
-
row.querySelectorAll<HTMLButtonElement>("[data-unit-option]").forEach((button) => {
|
|
170
|
-
const name = button.dataset.unitOption ?? "";
|
|
171
|
-
const value = button.dataset.value ?? "";
|
|
172
|
-
const state = states[name]?.[value] ?? "unavailable";
|
|
173
|
-
button.dataset.state = state;
|
|
174
|
-
button.disabled = state === "unavailable";
|
|
175
|
-
button.setAttribute("aria-pressed", piece[name] === value ? "true" : "false");
|
|
176
|
-
button.addEventListener("click", () => {
|
|
177
|
-
const picked = pickOption(catalog, piece, name, value);
|
|
178
|
-
if (!picked.variant) return;
|
|
179
|
-
pieces[index] = picked.selection;
|
|
180
|
-
touched.add(index);
|
|
181
|
-
update();
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
return row;
|
|
185
|
-
}),
|
|
186
|
-
);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
function writeFields() {
|
|
190
|
-
const tile = chosenTile();
|
|
191
|
-
setField("variantId", variant?.id ?? "");
|
|
192
|
-
setField("variantLabel", labelOf(variant?.variations));
|
|
193
|
-
setField("pricePerUnit", String(price()));
|
|
194
|
-
setField("quantity", String(quantity()));
|
|
195
|
-
setField("offerId", tile?.dataset.offer ?? "");
|
|
196
|
-
const selections = pieces
|
|
197
|
-
.map((piece) => variantFor(product.variants, piece))
|
|
198
|
-
.filter((match): match is NonNullable<typeof match> => match !== null)
|
|
199
|
-
.map((match) => ({ variantId: match.id, variantLabel: labelOf(match.variations) }));
|
|
200
|
-
setField("variantSelections", JSON.stringify(selections.length === pieces.length ? selections : []));
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/** What the choice costs before the server answers: a tier's own price, else the unit price times the pieces. */
|
|
204
|
-
function estimate(): number {
|
|
205
|
-
const tile = chosenTile();
|
|
206
|
-
if (tile?.dataset.fixed === "true") return Number(tile.dataset.price) || price() * quantity();
|
|
207
|
-
const own = piecesPrice();
|
|
208
|
-
if (own !== null) return own;
|
|
209
|
-
const offer = product.offers.find((candidate) => candidate.id === tile?.dataset.offer);
|
|
210
|
-
return offer ? getOfferTierView(offer, price(), compare()).price : price() * quantity();
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
function update() {
|
|
214
|
-
paintOptions();
|
|
215
|
-
paintPrice();
|
|
216
|
-
showTiles();
|
|
217
|
-
paintPieces();
|
|
218
|
-
priceTiles();
|
|
219
|
-
paintStock();
|
|
220
|
-
writeFields();
|
|
221
|
-
if (mobileTotal) mobileTotal.textContent = money(estimate());
|
|
222
|
-
root.dispatchEvent(new CustomEvent(PRODUCT_CHANGE_EVENT, { bubbles: true }));
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
optionButtons.forEach((button) =>
|
|
226
|
-
button.addEventListener("click", () => {
|
|
227
|
-
const picked = pickOption(catalog, selection, button.dataset.option ?? "", button.dataset.value ?? "");
|
|
228
|
-
if (!picked.variant) return;
|
|
229
|
-
selection = { ...picked.selection };
|
|
230
|
-
variant = picked.variant;
|
|
231
|
-
if (variant.imageId) showImageId(variant.imageId);
|
|
232
|
-
update();
|
|
233
|
-
}),
|
|
234
|
-
);
|
|
235
|
-
tiles.forEach((tile) => tile.querySelector("input")?.addEventListener("change", () => {
|
|
236
|
-
touched.clear();
|
|
237
|
-
update();
|
|
238
|
-
}));
|
|
239
|
-
const step = (by: number) => {
|
|
240
|
-
if (!qtyInput) return;
|
|
241
|
-
qtyInput.value = String(Math.max(1, Math.min(Number(qtyInput.max) || 100, (Number(qtyInput.value) || 1) + by)));
|
|
242
|
-
update();
|
|
243
|
-
};
|
|
244
|
-
root.querySelector("[data-qty-minus]")?.addEventListener("click", () => step(-1));
|
|
245
|
-
root.querySelector("[data-qty-plus]")?.addEventListener("click", () => step(1));
|
|
246
|
-
qtyInput?.addEventListener("change", () => step(0));
|
|
247
|
-
|
|
248
|
-
root.addEventListener(ORDER_QUOTE_EVENT, (event) => {
|
|
249
|
-
const subtotal = (event as CustomEvent<{ subtotal: number }>).detail?.subtotal;
|
|
250
|
-
if (mobileTotal && Number.isFinite(subtotal)) mobileTotal.textContent = money(subtotal);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
// ── Photos ──────────────────────────────────────────────────────────────
|
|
254
|
-
const track = root.querySelector<HTMLElement>("[data-gallery-track]");
|
|
255
|
-
const slides = all<HTMLElement>(".gallery__slide");
|
|
256
|
-
const thumbs = all<HTMLButtonElement>("[data-gallery-index]");
|
|
257
|
-
const counter = root.querySelector<HTMLElement>("[data-gallery-current]");
|
|
258
|
-
let current = 0;
|
|
259
|
-
const mark = (index: number) => {
|
|
260
|
-
current = index;
|
|
261
|
-
thumbs.forEach((thumb, at) => thumb.setAttribute("aria-current", at === index ? "true" : "false"));
|
|
262
|
-
if (counter) counter.textContent = String(index + 1);
|
|
263
|
-
};
|
|
264
|
-
const show = (index: number) => {
|
|
265
|
-
const slide = slides[(index + slides.length) % slides.length];
|
|
266
|
-
if (!slide) return;
|
|
267
|
-
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
268
|
-
slide.scrollIntoView({ behavior: reduce ? "auto" : "smooth", block: "nearest", inline: "center" });
|
|
269
|
-
mark(slides.indexOf(slide));
|
|
270
|
-
};
|
|
271
|
-
function showImageId(imageId: string) {
|
|
272
|
-
const index = slides.findIndex((slide) => slide.dataset.imageId === imageId);
|
|
273
|
-
if (index >= 0) show(index);
|
|
274
|
-
}
|
|
275
|
-
if (track && slides.length > 1 && "IntersectionObserver" in window) {
|
|
276
|
-
const seen = new IntersectionObserver(
|
|
277
|
-
(entries) => {
|
|
278
|
-
for (const entry of entries) if (entry.isIntersecting) mark(slides.indexOf(entry.target as HTMLElement));
|
|
279
|
-
},
|
|
280
|
-
{ root: track, threshold: 0.6 },
|
|
281
|
-
);
|
|
282
|
-
slides.forEach((slide) => seen.observe(slide));
|
|
283
|
-
}
|
|
284
|
-
thumbs.forEach((thumb) => thumb.addEventListener("click", () => show(Number(thumb.dataset.galleryIndex) || 0)));
|
|
285
|
-
root.querySelector("[data-gallery-prev]")?.addEventListener("click", () => show(current - 1));
|
|
286
|
-
root.querySelector("[data-gallery-next]")?.addEventListener("click", () => show(current + 1));
|
|
287
|
-
track?.addEventListener("keydown", (event) => {
|
|
288
|
-
const rtl = getComputedStyle(track).direction === "rtl";
|
|
289
|
-
if (event.key === "ArrowRight") show(current + (rtl ? -1 : 1));
|
|
290
|
-
else if (event.key === "ArrowLeft") show(current + (rtl ? 1 : -1));
|
|
291
|
-
else return;
|
|
292
|
-
event.preventDefault();
|
|
293
|
-
});
|
|
294
|
-
|
|
295
|
-
// ── The phone's sticky bar: away while any of the order form is on screen or above it ──
|
|
296
|
-
const form = root.querySelector<HTMLElement>("[data-order-scope]");
|
|
297
|
-
if (mobileBar && form && "IntersectionObserver" in window) {
|
|
298
|
-
new IntersectionObserver(([entry]) => {
|
|
299
|
-
if (!entry) return;
|
|
300
|
-
mobileBar.dataset.visible = entry.isIntersecting || entry.boundingClientRect.top < 0 ? "false" : "true";
|
|
301
|
-
}).observe(form);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
update();
|
|
305
|
-
if (root.dataset.viewContent === "true") storefrontTracking().productViewed({ productId: product.id, price: price() });
|
|
306
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* A product on the page: its photos, the options, the offer tiles or the
|
|
3
|
+
* quantity, and a variant for each piece of a multi-piece offer. The choice
|
|
4
|
+
* is written into the fields the order form and the basket read, and the
|
|
5
|
+
* prices follow the chosen variant. The phone's sticky bar shows what the
|
|
6
|
+
* choice costs — the server's quote once the form has it.
|
|
7
|
+
*/
|
|
8
|
+
import {
|
|
9
|
+
formatMoney,
|
|
10
|
+
getOfferTierView,
|
|
11
|
+
initialVariant,
|
|
12
|
+
optionStates,
|
|
13
|
+
pickOption,
|
|
14
|
+
variantFor,
|
|
15
|
+
type OptionSelection,
|
|
16
|
+
storefrontTracking,
|
|
17
|
+
} from "@abcart/storefront";
|
|
18
|
+
import type { BrowserProduct } from "@/lib/products";
|
|
19
|
+
import { ORDER_QUOTE_EVENT, PRODUCT_CHANGE_EVENT } from "./events";
|
|
20
|
+
import { bindEach } from "@abcart/theme/lifecycle";
|
|
21
|
+
import { readPublicConfig, translate } from "./public-config";
|
|
22
|
+
|
|
23
|
+
const config = readPublicConfig();
|
|
24
|
+
const tr = (key: string, values?: Record<string, string | number>) => translate(config, key, values);
|
|
25
|
+
const money = (amount: number) => formatMoney(Math.round(amount), { symbol: config.symbol, locale: config.localeTag, decimals: 0 });
|
|
26
|
+
const labelOf = (variations: Readonly<Record<string, string>> | undefined) => Object.values(variations ?? {}).filter((value) => value !== "Default Title").join(" · ");
|
|
27
|
+
|
|
28
|
+
bindEach<HTMLElement>("[data-product]", (root) => {
|
|
29
|
+
const dataNode = root.querySelector<HTMLScriptElement>("[data-product-data]");
|
|
30
|
+
if (!dataNode?.textContent) return;
|
|
31
|
+
const product = JSON.parse(dataNode.textContent) as BrowserProduct;
|
|
32
|
+
const catalog = { variants: product.variants, trackInventory: product.trackInventory, variantOptions: product.variantOptions };
|
|
33
|
+
let variant = initialVariant(catalog);
|
|
34
|
+
let selection: OptionSelection = { ...(variant?.variations ?? {}) };
|
|
35
|
+
|
|
36
|
+
const all = <T extends Element>(selector: string) => Array.from(root.querySelectorAll<T>(selector));
|
|
37
|
+
const optionButtons = all<HTMLButtonElement>("[data-option]");
|
|
38
|
+
const tiles = all<HTMLLabelElement>("[data-tile]");
|
|
39
|
+
const units = root.querySelector<HTMLElement>("[data-offer-units]");
|
|
40
|
+
const unitRows = root.querySelector<HTMLElement>("[data-offer-unit-rows]");
|
|
41
|
+
const unitTemplate = root.querySelector<HTMLTemplateElement>("[data-offer-unit-template]");
|
|
42
|
+
const qtyInput = root.querySelector<HTMLInputElement>("[data-qty-input]");
|
|
43
|
+
const mobileBar = root.querySelector<HTMLElement>("[data-mobile-buy]");
|
|
44
|
+
const mobileTotal = root.querySelector<HTMLElement>("[data-mobile-total]");
|
|
45
|
+
/** The variant each piece of the chosen tile is ordered in, by its options. */
|
|
46
|
+
let pieces: OptionSelection[] = [];
|
|
47
|
+
/** Pieces the shopper set by hand keep their choice when the main options change. */
|
|
48
|
+
const touched = new Set<number>();
|
|
49
|
+
|
|
50
|
+
const price = () => variant?.price ?? product.price;
|
|
51
|
+
const compare = () => variant?.compareAtPrice ?? product.compareAtPrice ?? null;
|
|
52
|
+
const chosenTile = () => tiles.find((tile) => tile.querySelector<HTMLInputElement>("input")?.checked && !tile.hidden) ?? null;
|
|
53
|
+
const quantity = () => {
|
|
54
|
+
const tile = chosenTile();
|
|
55
|
+
if (tile) return Number(tile.dataset.quantity) || 1;
|
|
56
|
+
return Math.max(1, Math.min(Number(qtyInput?.max) || 100, Number(qtyInput?.value) || 1));
|
|
57
|
+
};
|
|
58
|
+
const setField = (name: string, value: string) => all<HTMLInputElement>(`input[type="hidden"][name="${name}"]`).forEach((input) => (input.value = value));
|
|
59
|
+
const stockOf = (candidate = variant) => (candidate ? candidate.inventory : product.inventory);
|
|
60
|
+
const soldOut = () => product.trackInventory && stockOf() <= 0;
|
|
61
|
+
|
|
62
|
+
function paintOptions() {
|
|
63
|
+
const states = optionStates(catalog, selection);
|
|
64
|
+
for (const button of optionButtons) {
|
|
65
|
+
const name = button.dataset.option ?? "";
|
|
66
|
+
const value = button.dataset.value ?? "";
|
|
67
|
+
const state = states[name]?.[value] ?? "unavailable";
|
|
68
|
+
button.dataset.state = state;
|
|
69
|
+
button.disabled = state === "unavailable";
|
|
70
|
+
button.setAttribute("aria-pressed", selection[name] === value ? "true" : "false");
|
|
71
|
+
}
|
|
72
|
+
all<HTMLElement>("[data-option-chosen]").forEach((node) => (node.textContent = selection[node.dataset.optionChosen ?? ""] ?? ""));
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function paintPrice() {
|
|
76
|
+
const now = price();
|
|
77
|
+
const before = compare();
|
|
78
|
+
const discounted = before !== null && before > now;
|
|
79
|
+
all<HTMLElement>("[data-product-price]").forEach((node) => (node.textContent = money(now)));
|
|
80
|
+
all<HTMLElement>("[data-product-compare]").forEach((node) => {
|
|
81
|
+
node.textContent = discounted ? money(before) : "";
|
|
82
|
+
node.hidden = !discounted;
|
|
83
|
+
});
|
|
84
|
+
const percent = discounted ? Math.round((1 - now / before) * 100) : 0;
|
|
85
|
+
all<HTMLElement>("[data-product-save]").forEach((node) => {
|
|
86
|
+
node.textContent = discounted ? tr("product.save", { percent }) : "";
|
|
87
|
+
node.hidden = !discounted;
|
|
88
|
+
});
|
|
89
|
+
all<HTMLElement>("[data-gallery-sticker]").forEach((node) => {
|
|
90
|
+
node.textContent = `−${percent}%`;
|
|
91
|
+
node.hidden = !discounted;
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function paintStock() {
|
|
96
|
+
const stock = root.querySelector<HTMLElement>("[data-stock]");
|
|
97
|
+
const out = soldOut();
|
|
98
|
+
if (stock) {
|
|
99
|
+
const low = !out && product.trackInventory && stock.dataset.lowAlert === "true" && stockOf() <= Math.max(product.lowStockThreshold, 1);
|
|
100
|
+
stock.dataset.out = out ? "true" : "false";
|
|
101
|
+
stock.dataset.low = low ? "true" : "false";
|
|
102
|
+
const text = stock.querySelector<HTMLElement>("[data-stock-text]");
|
|
103
|
+
if (text) text.textContent = out ? tr("product.soldOut") : low ? tr("product.lowStock", { count: stockOf() }) : tr("common.inStock");
|
|
104
|
+
}
|
|
105
|
+
all<HTMLButtonElement>("[data-add-to-cart], [data-checkout-submit]").forEach((button) => {
|
|
106
|
+
button.disabled = out;
|
|
107
|
+
});
|
|
108
|
+
if (qtyInput && product.trackInventory) qtyInput.max = String(Math.max(1, Math.min(100, stockOf())));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Tiles tied to another variant hide; a hidden choice falls back to the first tile shown. */
|
|
112
|
+
function showTiles() {
|
|
113
|
+
for (const tile of tiles) tile.hidden = Boolean(tile.dataset.variant) && tile.dataset.variant !== variant?.id;
|
|
114
|
+
if (tiles.length > 0 && !chosenTile()) {
|
|
115
|
+
const first = tiles.find((tile) => !tile.hidden)?.querySelector<HTMLInputElement>("input");
|
|
116
|
+
if (first) first.checked = true;
|
|
117
|
+
}
|
|
118
|
+
tiles.forEach((tile) => tile.classList.toggle("is-chosen", tile === chosenTile()));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** What the chosen pieces cost at their own variants' prices; null until each piece is one that exists. */
|
|
122
|
+
function piecesPrice(): number | null {
|
|
123
|
+
if (pieces.length === 0) return null;
|
|
124
|
+
const matched = pieces.map((piece) => variantFor(product.variants, piece));
|
|
125
|
+
return matched.every(Boolean) ? matched.reduce((sum, match) => sum + match!.price, 0) : null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Each tile's price at the chosen variant; the chosen tile at the variant of each of its pieces. */
|
|
129
|
+
function priceTiles() {
|
|
130
|
+
for (const tile of tiles) {
|
|
131
|
+
const offer = product.offers.find((candidate) => candidate.id === tile.dataset.offer);
|
|
132
|
+
if (tile.dataset.fixed === "true") continue;
|
|
133
|
+
const view = offer ? getOfferTierView(offer, price(), compare()) : null;
|
|
134
|
+
const ownPieces = tile === chosenTile() ? piecesPrice() : null;
|
|
135
|
+
const amount = ownPieces ?? (view ? view.price : price() * (Number(tile.dataset.quantity) || 1));
|
|
136
|
+
const crossed = view ? view.comparePrice : compare();
|
|
137
|
+
tile.querySelector<HTMLElement>("[data-tile-price]")!.textContent = money(amount);
|
|
138
|
+
const was = tile.querySelector<HTMLElement>("[data-tile-compare]");
|
|
139
|
+
if (was) {
|
|
140
|
+
was.hidden = !(crossed !== null && crossed > amount);
|
|
141
|
+
was.textContent = was.hidden ? "" : money(crossed!);
|
|
142
|
+
}
|
|
143
|
+
const save = tile.querySelector<HTMLElement>("[data-tile-save]");
|
|
144
|
+
if (save && view) {
|
|
145
|
+
save.hidden = view.savingsAmount <= 0;
|
|
146
|
+
save.textContent = save.hidden ? "" : tr("offer.saving", { amount: money(view.savingsAmount) });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** A multi-piece tile on a product with options: one row of options per piece. */
|
|
152
|
+
function paintPieces() {
|
|
153
|
+
const tile = chosenTile();
|
|
154
|
+
const count = quantity();
|
|
155
|
+
const perPiece = Boolean(units && unitRows && unitTemplate && tile && count > 1 && !tile.dataset.variant);
|
|
156
|
+
if (units) units.hidden = !perPiece;
|
|
157
|
+
if (!perPiece || !unitRows || !unitTemplate) {
|
|
158
|
+
pieces = [];
|
|
159
|
+
touched.clear();
|
|
160
|
+
unitRows?.replaceChildren();
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
pieces = Array.from({ length: count }, (_, index) => (touched.has(index) && pieces[index] ? pieces[index]! : { ...selection }));
|
|
164
|
+
unitRows.replaceChildren(
|
|
165
|
+
...pieces.map((piece, index) => {
|
|
166
|
+
const row = unitTemplate.content.firstElementChild!.cloneNode(true) as HTMLElement;
|
|
167
|
+
row.querySelector<HTMLElement>("[data-offer-unit-label]")!.textContent = tr("offer.piece", { n: index + 1 });
|
|
168
|
+
const states = optionStates(catalog, piece);
|
|
169
|
+
row.querySelectorAll<HTMLButtonElement>("[data-unit-option]").forEach((button) => {
|
|
170
|
+
const name = button.dataset.unitOption ?? "";
|
|
171
|
+
const value = button.dataset.value ?? "";
|
|
172
|
+
const state = states[name]?.[value] ?? "unavailable";
|
|
173
|
+
button.dataset.state = state;
|
|
174
|
+
button.disabled = state === "unavailable";
|
|
175
|
+
button.setAttribute("aria-pressed", piece[name] === value ? "true" : "false");
|
|
176
|
+
button.addEventListener("click", () => {
|
|
177
|
+
const picked = pickOption(catalog, piece, name, value);
|
|
178
|
+
if (!picked.variant) return;
|
|
179
|
+
pieces[index] = picked.selection;
|
|
180
|
+
touched.add(index);
|
|
181
|
+
update();
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
return row;
|
|
185
|
+
}),
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function writeFields() {
|
|
190
|
+
const tile = chosenTile();
|
|
191
|
+
setField("variantId", variant?.id ?? "");
|
|
192
|
+
setField("variantLabel", labelOf(variant?.variations));
|
|
193
|
+
setField("pricePerUnit", String(price()));
|
|
194
|
+
setField("quantity", String(quantity()));
|
|
195
|
+
setField("offerId", tile?.dataset.offer ?? "");
|
|
196
|
+
const selections = pieces
|
|
197
|
+
.map((piece) => variantFor(product.variants, piece))
|
|
198
|
+
.filter((match): match is NonNullable<typeof match> => match !== null)
|
|
199
|
+
.map((match) => ({ variantId: match.id, variantLabel: labelOf(match.variations) }));
|
|
200
|
+
setField("variantSelections", JSON.stringify(selections.length === pieces.length ? selections : []));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** What the choice costs before the server answers: a tier's own price, else the unit price times the pieces. */
|
|
204
|
+
function estimate(): number {
|
|
205
|
+
const tile = chosenTile();
|
|
206
|
+
if (tile?.dataset.fixed === "true") return Number(tile.dataset.price) || price() * quantity();
|
|
207
|
+
const own = piecesPrice();
|
|
208
|
+
if (own !== null) return own;
|
|
209
|
+
const offer = product.offers.find((candidate) => candidate.id === tile?.dataset.offer);
|
|
210
|
+
return offer ? getOfferTierView(offer, price(), compare()).price : price() * quantity();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function update() {
|
|
214
|
+
paintOptions();
|
|
215
|
+
paintPrice();
|
|
216
|
+
showTiles();
|
|
217
|
+
paintPieces();
|
|
218
|
+
priceTiles();
|
|
219
|
+
paintStock();
|
|
220
|
+
writeFields();
|
|
221
|
+
if (mobileTotal) mobileTotal.textContent = money(estimate());
|
|
222
|
+
root.dispatchEvent(new CustomEvent(PRODUCT_CHANGE_EVENT, { bubbles: true }));
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
optionButtons.forEach((button) =>
|
|
226
|
+
button.addEventListener("click", () => {
|
|
227
|
+
const picked = pickOption(catalog, selection, button.dataset.option ?? "", button.dataset.value ?? "");
|
|
228
|
+
if (!picked.variant) return;
|
|
229
|
+
selection = { ...picked.selection };
|
|
230
|
+
variant = picked.variant;
|
|
231
|
+
if (variant.imageId) showImageId(variant.imageId);
|
|
232
|
+
update();
|
|
233
|
+
}),
|
|
234
|
+
);
|
|
235
|
+
tiles.forEach((tile) => tile.querySelector("input")?.addEventListener("change", () => {
|
|
236
|
+
touched.clear();
|
|
237
|
+
update();
|
|
238
|
+
}));
|
|
239
|
+
const step = (by: number) => {
|
|
240
|
+
if (!qtyInput) return;
|
|
241
|
+
qtyInput.value = String(Math.max(1, Math.min(Number(qtyInput.max) || 100, (Number(qtyInput.value) || 1) + by)));
|
|
242
|
+
update();
|
|
243
|
+
};
|
|
244
|
+
root.querySelector("[data-qty-minus]")?.addEventListener("click", () => step(-1));
|
|
245
|
+
root.querySelector("[data-qty-plus]")?.addEventListener("click", () => step(1));
|
|
246
|
+
qtyInput?.addEventListener("change", () => step(0));
|
|
247
|
+
|
|
248
|
+
root.addEventListener(ORDER_QUOTE_EVENT, (event) => {
|
|
249
|
+
const subtotal = (event as CustomEvent<{ subtotal: number }>).detail?.subtotal;
|
|
250
|
+
if (mobileTotal && Number.isFinite(subtotal)) mobileTotal.textContent = money(subtotal);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// ── Photos ──────────────────────────────────────────────────────────────
|
|
254
|
+
const track = root.querySelector<HTMLElement>("[data-gallery-track]");
|
|
255
|
+
const slides = all<HTMLElement>(".gallery__slide");
|
|
256
|
+
const thumbs = all<HTMLButtonElement>("[data-gallery-index]");
|
|
257
|
+
const counter = root.querySelector<HTMLElement>("[data-gallery-current]");
|
|
258
|
+
let current = 0;
|
|
259
|
+
const mark = (index: number) => {
|
|
260
|
+
current = index;
|
|
261
|
+
thumbs.forEach((thumb, at) => thumb.setAttribute("aria-current", at === index ? "true" : "false"));
|
|
262
|
+
if (counter) counter.textContent = String(index + 1);
|
|
263
|
+
};
|
|
264
|
+
const show = (index: number) => {
|
|
265
|
+
const slide = slides[(index + slides.length) % slides.length];
|
|
266
|
+
if (!slide) return;
|
|
267
|
+
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
268
|
+
slide.scrollIntoView({ behavior: reduce ? "auto" : "smooth", block: "nearest", inline: "center" });
|
|
269
|
+
mark(slides.indexOf(slide));
|
|
270
|
+
};
|
|
271
|
+
function showImageId(imageId: string) {
|
|
272
|
+
const index = slides.findIndex((slide) => slide.dataset.imageId === imageId);
|
|
273
|
+
if (index >= 0) show(index);
|
|
274
|
+
}
|
|
275
|
+
if (track && slides.length > 1 && "IntersectionObserver" in window) {
|
|
276
|
+
const seen = new IntersectionObserver(
|
|
277
|
+
(entries) => {
|
|
278
|
+
for (const entry of entries) if (entry.isIntersecting) mark(slides.indexOf(entry.target as HTMLElement));
|
|
279
|
+
},
|
|
280
|
+
{ root: track, threshold: 0.6 },
|
|
281
|
+
);
|
|
282
|
+
slides.forEach((slide) => seen.observe(slide));
|
|
283
|
+
}
|
|
284
|
+
thumbs.forEach((thumb) => thumb.addEventListener("click", () => show(Number(thumb.dataset.galleryIndex) || 0)));
|
|
285
|
+
root.querySelector("[data-gallery-prev]")?.addEventListener("click", () => show(current - 1));
|
|
286
|
+
root.querySelector("[data-gallery-next]")?.addEventListener("click", () => show(current + 1));
|
|
287
|
+
track?.addEventListener("keydown", (event) => {
|
|
288
|
+
const rtl = getComputedStyle(track).direction === "rtl";
|
|
289
|
+
if (event.key === "ArrowRight") show(current + (rtl ? -1 : 1));
|
|
290
|
+
else if (event.key === "ArrowLeft") show(current + (rtl ? 1 : -1));
|
|
291
|
+
else return;
|
|
292
|
+
event.preventDefault();
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// ── The phone's sticky bar: away while any of the order form is on screen or above it ──
|
|
296
|
+
const form = root.querySelector<HTMLElement>("[data-order-scope]");
|
|
297
|
+
if (mobileBar && form && "IntersectionObserver" in window) {
|
|
298
|
+
new IntersectionObserver(([entry]) => {
|
|
299
|
+
if (!entry) return;
|
|
300
|
+
mobileBar.dataset.visible = entry.isIntersecting || entry.boundingClientRect.top < 0 ? "false" : "true";
|
|
301
|
+
}).observe(form);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
update();
|
|
305
|
+
if (root.dataset.viewContent === "true") storefrontTracking().productViewed({ productId: product.id, price: price() });
|
|
306
|
+
});
|