@eldrajs/theme-cli 0.0.0 → 0.1.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 +69 -0
- package/LICENSE +21 -0
- package/README.md +26 -1
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +203 -0
- package/dist/index.d.ts +174 -0
- package/dist/index.js +3 -0
- package/dist/validate-C6jB8_Oc.js +510 -0
- package/package.json +50 -2
- package/template/.eldra/block-types.d.ts +429 -0
- package/template/.eldra/manifest.json +9377 -0
- package/template/.eldra/previews.json +36 -0
- package/template/.github/workflows/eldra-deploy.yml +24 -0
- package/template/.gitlab-ci.yml +16 -0
- package/template/.storybook/eldra.ts +123 -0
- package/template/.storybook/i18n.ts +23 -0
- package/template/.storybook/main.ts +59 -0
- package/template/.storybook/nuxt-link-stub.ts +23 -0
- package/template/.storybook/preview.ts +52 -0
- package/template/.storybook/shims-vue.d.ts +10 -0
- package/template/.storybook/virtual-eldra.d.ts +27 -0
- package/template/README.md +288 -0
- package/template/app/app.vue +126 -0
- package/template/app/assets/main.css +404 -0
- package/template/app/components/EldraIcon.vue +66 -0
- package/template/app/components/EldraRouterLink.vue +62 -0
- package/template/app/components/ui/UiImage.stories.ts +30 -0
- package/template/app/components/ui/UiImage.vue +224 -0
- package/template/app/components/ui/__tests__/UiImage.spec.ts +292 -0
- package/template/app/composables/iconComponent.ts +84 -0
- package/template/app/composables/iconFetcher.ts +14 -0
- package/template/app/composables/useBlockData.ts +30 -0
- package/template/app/composables/useEditing.ts +31 -0
- package/template/app/composables/useEldraIcon.ts +46 -0
- package/template/app/composables/useRevalidating.ts +69 -0
- package/template/app/composables/useRichTextScrollRegions.ts +80 -0
- package/template/app/composables/useStorefront.ts +18 -0
- package/template/app/composables/useStorefrontFeedback.ts +49 -0
- package/template/app/composables/useUiId.ts +18 -0
- package/template/app/composables/useWishlist.ts +42 -0
- package/template/app/i18n.d.ts +28 -0
- package/template/app/icons.ts +185 -0
- package/template/app/pages/[...slug].vue +134 -0
- package/template/app/plugins/eldra-i18n.ts +98 -0
- package/template/app/plugins/eldra-storefront.ts +288 -0
- package/template/app/plugins/eldra-ui-messages.ts +112 -0
- package/template/app/storefront/cart.ts +245 -0
- package/template/app/storefront/categories.ts +283 -0
- package/template/app/storefront/collectionSelector.ts +60 -0
- package/template/app/storefront/commerce.ts +58 -0
- package/template/app/storefront/demo.ts +1313 -0
- package/template/app/storefront/facets.ts +476 -0
- package/template/app/storefront/feedback.ts +122 -0
- package/template/app/storefront/gateway.ts +2256 -0
- package/template/app/storefront/history.ts +233 -0
- package/template/app/storefront/money.ts +247 -0
- package/template/app/storefront/options.ts +71 -0
- package/template/app/storefront/prerender.ts +77 -0
- package/template/app/storefront/refresh.ts +222 -0
- package/template/app/storefront/toProductCard.ts +154 -0
- package/template/app/storefront/types.ts +815 -0
- package/template/app/storefront/volatile.ts +211 -0
- package/template/app/templates.ts +544 -0
- package/template/app/utils/classes.ts +40 -0
- package/template/app/utils/links.ts +70 -0
- package/template/app/utils/pageStructure.ts +77 -0
- package/template/app/utils/pageTitle.ts +37 -0
- package/template/app/virtual-eldra.d.ts +12 -0
- package/template/bitbucket-pipelines.yml +19 -0
- package/template/blocks/announcement-bar/Block.vue +203 -0
- package/template/blocks/announcement-bar/__tests__/Block.spec.ts +227 -0
- package/template/blocks/announcement-bar/block.json +45 -0
- package/template/blocks/announcement-bar/mock.json +7 -0
- package/template/blocks/announcement-bar/preview.png +0 -0
- package/template/blocks/article/Block.vue +317 -0
- package/template/blocks/article/__tests__/Block.spec.ts +229 -0
- package/template/blocks/article/block.json +148 -0
- package/template/blocks/article/mock.json +512 -0
- package/template/blocks/article/preview.json +16 -0
- package/template/blocks/article/preview.png +0 -0
- package/template/blocks/article-list/Block.vue +343 -0
- package/template/blocks/article-list/__tests__/Block.spec.ts +279 -0
- package/template/blocks/article-list/block.json +181 -0
- package/template/blocks/article-list/mock.json +68 -0
- package/template/blocks/article-list/preview.json +84 -0
- package/template/blocks/article-list/preview.png +0 -0
- package/template/blocks/breadcrumbs/Block.vue +278 -0
- package/template/blocks/breadcrumbs/__tests__/Block.spec.ts +461 -0
- package/template/blocks/breadcrumbs/block.json +89 -0
- package/template/blocks/breadcrumbs/mock.json +11 -0
- package/template/blocks/breadcrumbs/preview.json +3 -0
- package/template/blocks/breadcrumbs/preview.png +0 -0
- package/template/blocks/cart/Block.vue +372 -0
- package/template/blocks/cart/__tests__/Block.spec.ts +561 -0
- package/template/blocks/cart/__tests__/ShippingBar.spec.ts +78 -0
- package/template/blocks/cart/__tests__/Summary.spec.ts +245 -0
- package/template/blocks/cart/block.json +74 -0
- package/template/blocks/cart/mock.json +11 -0
- package/template/blocks/cart/parts/CartLines.vue +257 -0
- package/template/blocks/cart/parts/ShippingBar.vue +105 -0
- package/template/blocks/cart/parts/Summary.vue +291 -0
- package/template/blocks/cart/preview.png +0 -0
- package/template/blocks/collection-grid/Block.vue +1832 -0
- package/template/blocks/collection-grid/__tests__/Block.spec.ts +3037 -0
- package/template/blocks/collection-grid/__tests__/groups.spec.ts +731 -0
- package/template/blocks/collection-grid/block.json +187 -0
- package/template/blocks/collection-grid/mock.json +23 -0
- package/template/blocks/collection-grid/parts/groups.ts +861 -0
- package/template/blocks/collection-grid/preview.json +7 -0
- package/template/blocks/collection-grid/preview.png +0 -0
- package/template/blocks/collection-header/Block.vue +533 -0
- package/template/blocks/collection-header/__tests__/Block.spec.ts +381 -0
- package/template/blocks/collection-header/block.json +140 -0
- package/template/blocks/collection-header/mock.json +28 -0
- package/template/blocks/collection-header/preview.json +14 -0
- package/template/blocks/collection-header/preview.png +0 -0
- package/template/blocks/contact/Block.vue +621 -0
- package/template/blocks/contact/__tests__/Block.spec.ts +347 -0
- package/template/blocks/contact/block.json +174 -0
- package/template/blocks/contact/mock.json +28 -0
- package/template/blocks/contact/preview.json +9 -0
- package/template/blocks/contact/preview.png +0 -0
- package/template/blocks/cta/Block.vue +383 -0
- package/template/blocks/cta/__tests__/Block.spec.ts +309 -0
- package/template/blocks/cta/block.json +86 -0
- package/template/blocks/cta/mock.json +10 -0
- package/template/blocks/cta/preview.json +9 -0
- package/template/blocks/cta/preview.png +0 -0
- package/template/blocks/faq/Block.vue +254 -0
- package/template/blocks/faq/__tests__/Block.spec.ts +312 -0
- package/template/blocks/faq/block.json +113 -0
- package/template/blocks/faq/mock.json +115 -0
- package/template/blocks/faq/preview.png +0 -0
- package/template/blocks/feature-grid/Block.vue +241 -0
- package/template/blocks/feature-grid/__tests__/Block.spec.ts +311 -0
- package/template/blocks/feature-grid/block.json +106 -0
- package/template/blocks/feature-grid/mock.json +29 -0
- package/template/blocks/feature-grid/preview.json +45 -0
- package/template/blocks/feature-grid/preview.png +0 -0
- package/template/blocks/footer/Block.vue +652 -0
- package/template/blocks/footer/__tests__/Block.spec.ts +700 -0
- package/template/blocks/footer/block.json +240 -0
- package/template/blocks/footer/mock.json +161 -0
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/gallery/Block.vue +416 -0
- package/template/blocks/gallery/__tests__/Block.spec.ts +289 -0
- package/template/blocks/gallery/block.json +87 -0
- package/template/blocks/gallery/mock.json +16 -0
- package/template/blocks/gallery/preview.json +64 -0
- package/template/blocks/gallery/preview.png +0 -0
- package/template/blocks/hero/Block.vue +378 -0
- package/template/blocks/hero/__tests__/Block.spec.ts +324 -0
- package/template/blocks/hero/block.json +146 -0
- package/template/blocks/hero/mock.json +11 -0
- package/template/blocks/hero/preview.json +55 -0
- package/template/blocks/hero/preview.png +0 -0
- package/template/blocks/image/Block.vue +232 -0
- package/template/blocks/image/__tests__/Block.spec.ts +224 -0
- package/template/blocks/image/block.json +67 -0
- package/template/blocks/image/mock.json +7 -0
- package/template/blocks/image/preview.json +9 -0
- package/template/blocks/image/preview.png +0 -0
- package/template/blocks/logo-cloud/Block.vue +206 -0
- package/template/blocks/logo-cloud/__tests__/Block.spec.ts +210 -0
- package/template/blocks/logo-cloud/block.json +79 -0
- package/template/blocks/logo-cloud/mock.json +15 -0
- package/template/blocks/logo-cloud/preview.json +92 -0
- package/template/blocks/logo-cloud/preview.png +0 -0
- package/template/blocks/navigation/Block.vue +1348 -0
- package/template/blocks/navigation/__tests__/Block.spec.ts +2030 -0
- package/template/blocks/navigation/block.json +167 -0
- package/template/blocks/navigation/mock.json +191 -0
- package/template/blocks/navigation/preview.png +0 -0
- package/template/blocks/navigation/useMegaMenuKeys.ts +276 -0
- package/template/blocks/newsletter/Block.vue +399 -0
- package/template/blocks/newsletter/__tests__/Block.spec.ts +365 -0
- package/template/blocks/newsletter/block.json +105 -0
- package/template/blocks/newsletter/mock.json +31 -0
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/order-status/Block.vue +754 -0
- package/template/blocks/order-status/__tests__/Block.spec.ts +515 -0
- package/template/blocks/order-status/block.json +138 -0
- package/template/blocks/order-status/mock.json +19 -0
- package/template/blocks/order-status/preview.png +0 -0
- package/template/blocks/pricing-table/Block.vue +297 -0
- package/template/blocks/pricing-table/__tests__/Block.spec.ts +357 -0
- package/template/blocks/pricing-table/block.json +155 -0
- package/template/blocks/pricing-table/mock.json +47 -0
- package/template/blocks/pricing-table/preview.png +0 -0
- package/template/blocks/product-carousel/Block.vue +459 -0
- package/template/blocks/product-carousel/__tests__/Block.spec.ts +957 -0
- package/template/blocks/product-carousel/block.json +71 -0
- package/template/blocks/product-carousel/mock.json +9 -0
- package/template/blocks/product-carousel/preview.json +7 -0
- package/template/blocks/product-carousel/preview.png +0 -0
- package/template/blocks/product-detail/Block.vue +886 -0
- package/template/blocks/product-detail/__tests__/Block.spec.ts +1495 -0
- package/template/blocks/product-detail/__tests__/stock.spec.ts +119 -0
- package/template/blocks/product-detail/block.json +155 -0
- package/template/blocks/product-detail/mock.json +113 -0
- package/template/blocks/product-detail/parts/Gallery.vue +181 -0
- package/template/blocks/product-detail/parts/StickyBuyBar.vue +99 -0
- package/template/blocks/product-detail/preview.png +0 -0
- package/template/blocks/product-detail/stock.ts +150 -0
- package/template/blocks/quote/Block.vue +206 -0
- package/template/blocks/quote/__tests__/Block.spec.ts +265 -0
- package/template/blocks/quote/block.json +76 -0
- package/template/blocks/quote/mock.json +9 -0
- package/template/blocks/quote/preview.json +16 -0
- package/template/blocks/quote/preview.png +0 -0
- package/template/blocks/rich-text/Block.vue +143 -0
- package/template/blocks/rich-text/__tests__/Block.spec.ts +191 -0
- package/template/blocks/rich-text/block.json +57 -0
- package/template/blocks/rich-text/mock.json +87 -0
- package/template/blocks/rich-text/preview.png +0 -0
- package/template/blocks/search/Block.vue +589 -0
- package/template/blocks/search/PopularChips.vue +40 -0
- package/template/blocks/search/TypeSection.vue +148 -0
- package/template/blocks/search/__tests__/Block.spec.ts +718 -0
- package/template/blocks/search/__tests__/results.spec.ts +166 -0
- package/template/blocks/search/block.json +102 -0
- package/template/blocks/search/mock.json +18 -0
- package/template/blocks/search/preview.png +0 -0
- package/template/blocks/search/results.ts +85 -0
- package/template/blocks/split-content/Block.vue +195 -0
- package/template/blocks/split-content/__tests__/Block.spec.ts +225 -0
- package/template/blocks/split-content/block.json +70 -0
- package/template/blocks/split-content/mock.json +45 -0
- package/template/blocks/split-content/preview.json +58 -0
- package/template/blocks/split-content/preview.png +0 -0
- package/template/blocks/stats/Block.vue +159 -0
- package/template/blocks/stats/__tests__/Block.spec.ts +194 -0
- package/template/blocks/stats/block.json +73 -0
- package/template/blocks/stats/mock.json +12 -0
- package/template/blocks/stats/preview.png +0 -0
- package/template/blocks/tabs/Block.vue +278 -0
- package/template/blocks/tabs/__tests__/Block.spec.ts +303 -0
- package/template/blocks/tabs/block.json +97 -0
- package/template/blocks/tabs/mock.json +129 -0
- package/template/blocks/tabs/preview.json +153 -0
- package/template/blocks/tabs/preview.png +0 -0
- package/template/blocks/team/Block.vue +288 -0
- package/template/blocks/team/__tests__/Block.spec.ts +221 -0
- package/template/blocks/team/block.json +125 -0
- package/template/blocks/team/mock.json +39 -0
- package/template/blocks/team/preview.json +62 -0
- package/template/blocks/team/preview.png +0 -0
- package/template/blocks/testimonials/Block.vue +408 -0
- package/template/blocks/testimonials/__tests__/Block.spec.ts +319 -0
- package/template/blocks/testimonials/block.json +121 -0
- package/template/blocks/testimonials/mock.json +46 -0
- package/template/blocks/testimonials/preview.json +68 -0
- package/template/blocks/testimonials/preview.png +0 -0
- package/template/blocks/timeline/Block.vue +274 -0
- package/template/blocks/timeline/__tests__/Block.spec.ts +310 -0
- package/template/blocks/timeline/block.json +98 -0
- package/template/blocks/timeline/mock.json +27 -0
- package/template/blocks/timeline/preview.png +0 -0
- package/template/blocks/trust-strip/Block.vue +330 -0
- package/template/blocks/trust-strip/__tests__/Block.spec.ts +290 -0
- package/template/blocks/trust-strip/block.json +117 -0
- package/template/blocks/trust-strip/mock.json +37 -0
- package/template/blocks/trust-strip/preview.png +0 -0
- package/template/blocks/video-embed/Block.vue +354 -0
- package/template/blocks/video-embed/__tests__/Block.spec.ts +219 -0
- package/template/blocks/video-embed/__tests__/embed.spec.ts +76 -0
- package/template/blocks/video-embed/block.json +94 -0
- package/template/blocks/video-embed/embed.ts +84 -0
- package/template/blocks/video-embed/mock.json +13 -0
- package/template/blocks/video-embed/preview.json +9 -0
- package/template/blocks/video-embed/preview.png +0 -0
- package/template/blocks/wishlist/Block.vue +367 -0
- package/template/blocks/wishlist/__tests__/Block.spec.ts +508 -0
- package/template/blocks/wishlist/block.json +42 -0
- package/template/blocks/wishlist/mock.json +9 -0
- package/template/blocks/wishlist/preview.png +0 -0
- package/template/i18n/en-US.json +602 -0
- package/template/i18n/is-IS.json +602 -0
- package/template/nuxt.config.ts +148 -0
- package/template/package.json +55 -0
- package/template/pages/article.page.json +731 -0
- package/template/pages/cart.page.json +85 -0
- package/template/pages/category.page.json +133 -0
- package/template/pages/collection.page.json +194 -0
- package/template/pages/home.page.json +353 -0
- package/template/pages/product.page.json +323 -0
- package/template/pages/products.page.json +144 -0
- package/template/pages/search.page.json +83 -0
- package/template/pages/wishlist.page.json +88 -0
- package/template/public/demo/article-cover.svg +1 -0
- package/template/public/demo/article-figure.svg +1 -0
- package/template/public/demo/avatar-1.svg +3 -0
- package/template/public/demo/avatar-2.svg +3 -0
- package/template/public/demo/avatar-3.svg +3 -0
- package/template/public/demo/avatar-4.svg +3 -0
- package/template/public/demo/avatar-5.svg +3 -0
- package/template/public/demo/avatar-6.svg +3 -0
- package/template/public/demo/avatar-7.svg +3 -0
- package/template/public/demo/avatar-8.svg +3 -0
- package/template/public/demo/collection-1.svg +1 -0
- package/template/public/demo/collection-2.svg +1 -0
- package/template/public/demo/cta-split.svg +1 -0
- package/template/public/demo/feature-1.svg +1 -0
- package/template/public/demo/feature-2.svg +1 -0
- package/template/public/demo/feature-3.svg +1 -0
- package/template/public/demo/feature-4.svg +1 -0
- package/template/public/demo/gallery-1.svg +1 -0
- package/template/public/demo/gallery-2.svg +1 -0
- package/template/public/demo/gallery-3.svg +1 -0
- package/template/public/demo/gallery-4.svg +1 -0
- package/template/public/demo/gallery-5.svg +1 -0
- package/template/public/demo/gallery-6.svg +1 -0
- package/template/public/demo/gallery-7.svg +1 -0
- package/template/public/demo/gallery-8.svg +1 -0
- package/template/public/demo/hero-slide-1.svg +1 -0
- package/template/public/demo/hero-slide-2.svg +1 -0
- package/template/public/demo/hero-slide-3.svg +1 -0
- package/template/public/demo/hero-slide-4.svg +1 -0
- package/template/public/demo/hero-wide.svg +1 -0
- package/template/public/demo/hero.svg +1 -0
- package/template/public/demo/logo-1.svg +4 -0
- package/template/public/demo/logo-2.svg +4 -0
- package/template/public/demo/logo-3.svg +4 -0
- package/template/public/demo/logo-4.svg +4 -0
- package/template/public/demo/logo-5.svg +4 -0
- package/template/public/demo/logo-6.svg +4 -0
- package/template/public/demo/logo-7.svg +4 -0
- package/template/public/demo/logo-8.svg +4 -0
- package/template/public/demo/logo.svg +3 -0
- package/template/public/demo/map.svg +1 -0
- package/template/public/demo/poster.svg +1 -0
- package/template/public/demo/product-1.svg +4 -0
- package/template/public/demo/product-10.svg +4 -0
- package/template/public/demo/product-11.svg +4 -0
- package/template/public/demo/product-12.svg +4 -0
- package/template/public/demo/product-2.svg +4 -0
- package/template/public/demo/product-3.svg +4 -0
- package/template/public/demo/product-4.svg +4 -0
- package/template/public/demo/product-5.svg +4 -0
- package/template/public/demo/product-6.svg +4 -0
- package/template/public/demo/product-7.svg +4 -0
- package/template/public/demo/product-8.svg +4 -0
- package/template/public/demo/product-9.svg +4 -0
- package/template/public/demo/product-square-1.svg +4 -0
- package/template/public/demo/product-square-2.svg +4 -0
- package/template/public/demo/product-square-3.svg +4 -0
- package/template/public/demo/product-square-4.svg +4 -0
- package/template/public/demo/product-square-5.svg +4 -0
- package/template/public/demo/product-square-6.svg +4 -0
- package/template/public/demo/quote-portrait.svg +4 -0
- package/template/public/demo/split-1.svg +1 -0
- package/template/public/demo/split-2.svg +1 -0
- package/template/public/demo/split-3.svg +1 -0
- package/template/public/demo/split-4.svg +1 -0
- package/template/public/demo/team-1.svg +4 -0
- package/template/public/demo/team-2.svg +4 -0
- package/template/public/demo/team-3.svg +4 -0
- package/template/public/demo/team-4.svg +4 -0
- package/template/scripts/demo-images.mjs +393 -0
- package/template/scripts/generate-stories.mjs +156 -0
- package/template/scripts/previewHash.mjs +125 -0
- package/template/scripts/previews.mjs +149 -0
- package/template/stories/generated/announcement-bar.stories.ts +44 -0
- package/template/stories/generated/article-list.stories.ts +87 -0
- package/template/stories/generated/article.stories.ts +27 -0
- package/template/stories/generated/breadcrumbs.stories.ts +39 -0
- package/template/stories/generated/cart.stories.ts +38 -0
- package/template/stories/generated/collection-grid.stories.ts +117 -0
- package/template/stories/generated/collection-header.stories.ts +51 -0
- package/template/stories/generated/contact.stories.ts +45 -0
- package/template/stories/generated/cta.stories.ts +51 -0
- package/template/stories/generated/faq.stories.ts +56 -0
- package/template/stories/generated/feature-grid.stories.ts +69 -0
- package/template/stories/generated/footer.stories.ts +50 -0
- package/template/stories/generated/gallery.stories.ts +81 -0
- package/template/stories/generated/hero.stories.ts +75 -0
- package/template/stories/generated/image.stories.ts +99 -0
- package/template/stories/generated/logo-cloud.stories.ts +57 -0
- package/template/stories/generated/navigation.stories.ts +62 -0
- package/template/stories/generated/newsletter.stories.ts +68 -0
- package/template/stories/generated/order-status.stories.ts +32 -0
- package/template/stories/generated/pricing-table.stories.ts +44 -0
- package/template/stories/generated/product-carousel.stories.ts +75 -0
- package/template/stories/generated/product-detail.stories.ts +62 -0
- package/template/stories/generated/quote.stories.ts +69 -0
- package/template/stories/generated/rich-text.stories.ts +50 -0
- package/template/stories/generated/search.stories.ts +56 -0
- package/template/stories/generated/split-content.stories.ts +39 -0
- package/template/stories/generated/stats.stories.ts +68 -0
- package/template/stories/generated/tabs.stories.ts +45 -0
- package/template/stories/generated/team.stories.ts +45 -0
- package/template/stories/generated/testimonials.stories.ts +69 -0
- package/template/stories/generated/timeline.stories.ts +80 -0
- package/template/stories/generated/trust-strip.stories.ts +62 -0
- package/template/stories/generated/video-embed.stories.ts +63 -0
- package/template/stories/generated/wishlist.stories.ts +26 -0
- package/template/stories/pages/article.stories.ts +51 -0
- package/template/stories/pages/cart.stories.ts +56 -0
- package/template/stories/pages/category.stories.ts +56 -0
- package/template/stories/pages/collection.stories.ts +51 -0
- package/template/stories/pages/home.stories.ts +51 -0
- package/template/stories/pages/product.stories.ts +47 -0
- package/template/stories/pages/products.stories.ts +58 -0
- package/template/stories/pages/search.stories.ts +54 -0
- package/template/stories/pages/wishlist.stories.ts +53 -0
- package/template/stories/support/pageBlocks.ts +115 -0
- package/template/test/blocksEmptyData.spec.ts +52 -0
- package/template/test/carouselKeyboard.spec.ts +272 -0
- package/template/test/carouselRing.browser.spec.ts +193 -0
- package/template/test/carouselSlideWidth.browser.spec.ts +102 -0
- package/template/test/cartDrawer.spec.ts +295 -0
- package/template/test/cartToast.spec.ts +86 -0
- package/template/test/countPill.browser.spec.ts +196 -0
- package/template/test/demoImages.spec.ts +140 -0
- package/template/test/deps.spec.ts +102 -0
- package/template/test/eldraIcon.spec.ts +58 -0
- package/template/test/framing.spec.ts +76 -0
- package/template/test/i18n.spec.ts +200 -0
- package/template/test/iconComponent.spec.ts +91 -0
- package/template/test/inlineEditable.spec.ts +334 -0
- package/template/test/localePrefix.spec.ts +342 -0
- package/template/test/mainCss.spec.ts +107 -0
- package/template/test/mockGatewayContract.spec.ts +122 -0
- package/template/test/mocks/blocks.ts +20 -0
- package/template/test/mocks/breakpoints.ts +7 -0
- package/template/test/mocks/manifest.ts +11 -0
- package/template/test/moneyFormatting.spec.ts +89 -0
- package/template/test/pageTitle.spec.ts +49 -0
- package/template/test/pages/article.spec.ts +231 -0
- package/template/test/pages/category.spec.ts +185 -0
- package/template/test/pages/collection.spec.ts +396 -0
- package/template/test/pages/home.spec.ts +295 -0
- package/template/test/pages/hydration.spec.ts +557 -0
- package/template/test/pages/product.spec.ts +396 -0
- package/template/test/pages/seededPages.spec.ts +190 -0
- package/template/test/pages/ssr.spec.ts +393 -0
- package/template/test/prerenderCommerce.spec.ts +134 -0
- package/template/test/prerenderFailure.spec.ts +130 -0
- package/template/test/prerenderRefresh.browser.spec.ts +1431 -0
- package/template/test/previewsFresh.spec.ts +56 -0
- package/template/test/richText.spec.ts +161 -0
- package/template/test/richTextTypography.spec.ts +182 -0
- package/template/test/setup.ts +21 -0
- package/template/test/slugPage.spec.ts +244 -0
- package/template/test/slugPageTemplate.spec.ts +194 -0
- package/template/test/starter.spec.ts +1416 -0
- package/template/test/storefront/cart.spec.ts +404 -0
- package/template/test/storefront/categories.spec.ts +235 -0
- package/template/test/storefront/collectionSelector.spec.ts +74 -0
- package/template/test/storefront/demo.spec.ts +555 -0
- package/template/test/storefront/facets.spec.ts +425 -0
- package/template/test/storefront/feedback.spec.ts +97 -0
- package/template/test/storefront/gateway.spec.ts +2673 -0
- package/template/test/storefront/gatewayCart.spec.ts +289 -0
- package/template/test/storefront/gatewayPrerender.spec.ts +1037 -0
- package/template/test/storefront/history.spec.ts +341 -0
- package/template/test/storefront/money.spec.ts +171 -0
- package/template/test/storefront/toProductCard.spec.ts +138 -0
- package/template/test/storefront/volatile.spec.ts +284 -0
- package/template/test/support/axe.ts +14 -0
- package/template/test/support/dialog.ts +51 -0
- package/template/test/support/hydrate.ts +118 -0
- package/template/test/support/i18nText.ts +11 -0
- package/template/test/support/mockGateway.ts +1017 -0
- package/template/test/support/mountBlock.ts +194 -0
- package/template/test/support/mountPage.spec.ts +72 -0
- package/template/test/support/mountPage.ts +254 -0
- package/template/test/support/mountProbe.client.ts +69 -0
- package/template/test/support/renderSsr.ts +97 -0
- package/template/test/support/staticServer.ts +96 -0
- package/template/test/themeIcons.spec.ts +140 -0
- package/template/test/toastAnnouncement.spec.ts +133 -0
- package/template/test/tokens.spec.ts +102 -0
- package/template/test/uiMessagesPlugin.spec.ts +104 -0
- package/template/test/useBlockData.spec.ts +46 -0
- package/template/test/useEditing.spec.ts +54 -0
- package/template/test/useEldraIcon.spec.ts +126 -0
- package/template/test/useUiId.spec.ts +40 -0
- package/template/tokens.json +149 -0
- package/template/tsconfig.json +3 -0
- package/template/tsconfig.storybook.json +17 -0
- package/template/vitest-axe.d.ts +13 -0
- package/template/vitest.config.ts +44 -0
|
@@ -0,0 +1,861 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `CatalogFacets → FilterFacet[]` adapter: turns the storefront's own description of a scope
|
|
3
|
+
* into the shape `@eldrajs/ui`'s `FilterPanel` draws (`Block.vue`'s own `facets` computed calls
|
|
4
|
+
* `buildFilterFacets`). The package never sees a catalogue shape or a router — this file, and
|
|
5
|
+
* `Block.vue`'s own state around it, are the only place that translation happens.
|
|
6
|
+
*
|
|
7
|
+
* `filters[].source` is the *field's* vocabulary (`options`, `option:size`) while the storefront's
|
|
8
|
+
* `CatalogFacets` is the *store's* (`options[].key` = `size`, `colour`) — `facetValuesFor` is the
|
|
9
|
+
* one place those two are reconciled, together with the rule that decides which values a facet
|
|
10
|
+
* offers at all. Everything here is pure, so the price arithmetic a slider and a URL share
|
|
11
|
+
* (`parsePriceRange`/`rangeFromSlider`) and the panel-selection bridge (`applyPanelSelection`) are
|
|
12
|
+
* provable without mounting anything.
|
|
13
|
+
*/
|
|
14
|
+
import { IN_STOCK, OUT_OF_STOCK } from '../../../app/storefront/facets';
|
|
15
|
+
import type { CatalogFacetTerm, CatalogFacets } from '../../../app/storefront/types';
|
|
16
|
+
import type {
|
|
17
|
+
FilterFacet,
|
|
18
|
+
FilterFacetLayout,
|
|
19
|
+
FilterFacetType,
|
|
20
|
+
FilterFacetValue,
|
|
21
|
+
FilterSelection as UiFilterSelection,
|
|
22
|
+
} from '@eldrajs/ui';
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* One variant option, by the key the store names it with: `option:size`, `option:colour`,
|
|
26
|
+
* `option:fabric` — **any** key, not a list this theme maintains. The keys belong to the merchant's
|
|
27
|
+
* own product options, so a store that spells its colour option `color` or sells by `fabric` is a
|
|
28
|
+
* store this block filters for without a code change.
|
|
29
|
+
*/
|
|
30
|
+
export type OptionFilterSource = `option:${string}`;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* One yes/no facet beyond availability (`facets.toggles[]`), by the platform's own key. Folded as
|
|
34
|
+
* more switch rows into the same combined `availability` facet — see `availabilityFacetValues`.
|
|
35
|
+
*/
|
|
36
|
+
export type ToggleFilterSource = `toggle:${string}`;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* **What a rendered facet filters on.** `options` is deliberately not here: it is a *field* value
|
|
40
|
+
* that stands for "one facet per option key the store has" (`FilterFieldSource`), and the block
|
|
41
|
+
* expands it into the `option:<key>` sources below before a facet exists.
|
|
42
|
+
*/
|
|
43
|
+
export type FilterSource =
|
|
44
|
+
| 'category'
|
|
45
|
+
| 'collection'
|
|
46
|
+
| 'price'
|
|
47
|
+
| 'availability'
|
|
48
|
+
| OptionFilterSource
|
|
49
|
+
| ToggleFilterSource;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* What an author may put in `filters[].source` — every rendered source, plus `options`.
|
|
53
|
+
*
|
|
54
|
+
* **`options` is a meta source**: one facet per option key the storefront's facets answer, labelled
|
|
55
|
+
* by the facet's own `name`, in the facets' order. It exists because a merchant's option keys are
|
|
56
|
+
* theirs, not the theme's — an author cannot list `option:size` and `option:colour` by hand without
|
|
57
|
+
* knowing what the store sells by, and a store that adds `fabric` next season would need the page
|
|
58
|
+
* edited to offer it. An explicit `option:<key>` row still works and **wins** for that key: that is
|
|
59
|
+
* how an author pins one option's position in the panel or renames its facet.
|
|
60
|
+
*/
|
|
61
|
+
export type FilterFieldSource = FilterSource | 'options';
|
|
62
|
+
|
|
63
|
+
/** The `filters[].source` prefix one variant option is named with. */
|
|
64
|
+
export const OPTION_SOURCE = 'option:';
|
|
65
|
+
/** The internal source prefix a `facets.toggles[]` entry is named with — never an author-facing
|
|
66
|
+
* vocabulary; these rows are always folded into the `availability` facet (see below). */
|
|
67
|
+
export const TOGGLE_SOURCE = 'toggle:';
|
|
68
|
+
|
|
69
|
+
/** The option key a source names, or `null` for a source that is not an option. */
|
|
70
|
+
export function optionKeyOf(source: string): string | null {
|
|
71
|
+
if (!source.startsWith(OPTION_SOURCE)) return null;
|
|
72
|
+
const key = source.slice(OPTION_SOURCE.length);
|
|
73
|
+
return key === '' ? null : key;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The toggle key a source names, or `null` for a source that is not one. */
|
|
77
|
+
export function toggleKeyOf(source: string): string | null {
|
|
78
|
+
if (!source.startsWith(TOGGLE_SOURCE)) return null;
|
|
79
|
+
const key = source.slice(TOGGLE_SOURCE.length);
|
|
80
|
+
return key === '' ? null : key;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** `option:<key>` for one of the store's own option keys. */
|
|
84
|
+
export function optionSourceFor(key: string): OptionFilterSource {
|
|
85
|
+
return `${OPTION_SOURCE}${key}`;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** `toggle:<key>` for one of the store's own toggle keys. */
|
|
89
|
+
export function toggleSourceFor(key: string): ToggleFilterSource {
|
|
90
|
+
return `${TOGGLE_SOURCE}${key}`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* A toggle's own `on_sale`-style key, camelCased for the one query key it reads and writes
|
|
95
|
+
* (`onSale`) — the URL's own convention for everything else in this file is the bare option/source
|
|
96
|
+
* key, but a toggle is a single yes/no flag rather than a value list, so it reads best as the
|
|
97
|
+
* camelCase query parameters the rest of the app already uses for boolean state.
|
|
98
|
+
*/
|
|
99
|
+
export function toggleQueryKey(key: string): string {
|
|
100
|
+
return key.replace(/_([a-z0-9])/g, (_match, letter: string) => letter.toUpperCase());
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The four sources that are **one facet each, always** — everything a URL and a request can carry
|
|
105
|
+
* besides the open-ended option/toggle keys. The block enumerates these when it reads the query
|
|
106
|
+
* string and when it clears it; the option and toggle keys it enumerates from the facets and from
|
|
107
|
+
* its own selection, since there is no closed list of them.
|
|
108
|
+
*/
|
|
109
|
+
export const FIXED_FILTER_SOURCES = [
|
|
110
|
+
'category',
|
|
111
|
+
'collection',
|
|
112
|
+
'price',
|
|
113
|
+
'availability',
|
|
114
|
+
] as const satisfies readonly FilterSource[];
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* **The query keys an option or toggle key may not take**, because something else already owns them
|
|
118
|
+
* in the one namespace they share — the URL.
|
|
119
|
+
*
|
|
120
|
+
* Four are the fixed filter sources' own (`?category=`, `?collection=`, `?price=`, `?availability=`);
|
|
121
|
+
* five more are the keys the storefront route reads into typed fields of its own before a block ever
|
|
122
|
+
* sees them (`q`, `page`, `token`, `sort`, `columns` — `app/plugins/eldra-storefront.ts`'s
|
|
123
|
+
* `RESERVED_QUERY_KEYS`, which is where `route.filters` gets everything *else*).
|
|
124
|
+
*
|
|
125
|
+
* An option key is a merchant's own word, and `options` exists so any of them works — so a store
|
|
126
|
+
* whose variant option happens to be keyed `category` or `sort` is a real possibility, and both
|
|
127
|
+
* collisions are silent and ugly. A key colliding with a filter source reads the *same* query key as
|
|
128
|
+
* that source, so one `?category=ceramics` goes out as both a `categoryId` and an `option=…`, AND-ed,
|
|
129
|
+
* emptying the grid; ticking it then writes the key twice in one patch and the filter un-applies on
|
|
130
|
+
* the next read. A key colliding with a route field (`sort` above all) is never readable from the URL
|
|
131
|
+
* at all — it lands in `route.sort`, not in `route.filters` — while every state write would clear the
|
|
132
|
+
* shopper's sort out of the URL.
|
|
133
|
+
*
|
|
134
|
+
* So such a key is **dropped**, with a dev warning naming it (`usableOptionKey`). Namespacing it
|
|
135
|
+
* instead was the alternative and was not taken: the query key is a shopper-visible, shareable part
|
|
136
|
+
* of the URL, and inventing `?opt_category=` for one store means a link that no other spelling of
|
|
137
|
+
* this theme reads. Dropping offers one facet fewer and keeps every other filter exactly right.
|
|
138
|
+
*/
|
|
139
|
+
const RESERVED_QUERY_KEYS: ReadonlySet<string> = new Set<string>([
|
|
140
|
+
...FIXED_FILTER_SOURCES,
|
|
141
|
+
'q',
|
|
142
|
+
'page',
|
|
143
|
+
'token',
|
|
144
|
+
'sort',
|
|
145
|
+
'columns',
|
|
146
|
+
]);
|
|
147
|
+
|
|
148
|
+
const warnedKeys = new Set<string>();
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* One option (or toggle) key, or `null` when the URL already belongs to something else
|
|
152
|
+
* (`RESERVED_QUERY_KEYS`).
|
|
153
|
+
*
|
|
154
|
+
* The warning is once per key per session and dev-only, read through the same cast
|
|
155
|
+
* `app/storefront/commerce.ts` uses — the starter does not depend on `vite` itself, and an
|
|
156
|
+
* environment defining neither `DEV` flag simply never warns, which is the safe direction.
|
|
157
|
+
*/
|
|
158
|
+
export function usableOptionKey(key: string | null): string | null {
|
|
159
|
+
if (key === null) return null;
|
|
160
|
+
if (!RESERVED_QUERY_KEYS.has(key)) return key;
|
|
161
|
+
const meta = import.meta as ImportMeta & { env?: { DEV?: boolean } };
|
|
162
|
+
if (meta.env?.DEV === true && !warnedKeys.has(key)) {
|
|
163
|
+
warnedKeys.add(key);
|
|
164
|
+
console.warn(
|
|
165
|
+
`[eldra] collection-grid: the variant option "${key}" cannot be filtered on, because ` +
|
|
166
|
+
`?${key}= already belongs to another filter or to the page's own route state. ` +
|
|
167
|
+
'Its facet is not shown. Rename the option key in the store to offer it.'
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function isFilterSource(value: unknown): value is FilterSource {
|
|
174
|
+
if (typeof value !== 'string') return false;
|
|
175
|
+
if ((FIXED_FILTER_SOURCES as readonly string[]).includes(value)) return true;
|
|
176
|
+
return optionKeyOf(value) !== null || toggleKeyOf(value) !== null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Whether a `filters[].source` is one this block understands — `isFilterSource`, plus `options`. */
|
|
180
|
+
export function isFilterFieldSource(value: unknown): value is FilterFieldSource {
|
|
181
|
+
return value === 'options' || isFilterSource(value);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* **The query key a source is spelled with**, in both directions: what the block writes and what it
|
|
186
|
+
* reads back. An option is its bare key (`?colour=oat`) — the `option:` prefix is the *field's*
|
|
187
|
+
* vocabulary, never a shopper's URL. A toggle is its own camelCase key (`?onSale=1`).
|
|
188
|
+
*/
|
|
189
|
+
export function queryKeyFor(source: FilterSource): string {
|
|
190
|
+
const toggleKey = toggleKeyOf(source);
|
|
191
|
+
if (toggleKey !== null) return toggleQueryKey(toggleKey);
|
|
192
|
+
return optionKeyOf(source) ?? source;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** The two `availability` values, as this theme's own strings name them. */
|
|
196
|
+
export interface AvailabilityLabels {
|
|
197
|
+
inStock: string;
|
|
198
|
+
outOfStock: string;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Selected values per `filters[].source`, the block's own internal state — the price range lives
|
|
202
|
+
* in its own two strings (`priceMin`/`priceMax`), and a toggle's is `[]` or `['1']`. */
|
|
203
|
+
export type FilterSelection = Partial<Record<FilterSource, string[]>>;
|
|
204
|
+
|
|
205
|
+
/** One `filters[]` row, expanded (`options` already resolved to real sources) and ready to build. */
|
|
206
|
+
export interface FacetBuildRow {
|
|
207
|
+
source: FilterSource;
|
|
208
|
+
label?: string;
|
|
209
|
+
collapsed?: boolean;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** What a request to build a facet's values needs to know, besides the source itself. */
|
|
213
|
+
interface ValueBuildContext {
|
|
214
|
+
facets: CatalogFacets | undefined;
|
|
215
|
+
availability: AvailabilityLabels;
|
|
216
|
+
categoryScopeSlug: string | null;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* The values a facet offers: the store's own, each with the count the facets report.
|
|
221
|
+
*
|
|
222
|
+
* **A value the store counts zero of is offered disabled, not dropped** (spec → Do / Don't): facet
|
|
223
|
+
* counts leave their own family's filter out, so a zero means "another filter rules this out", and a
|
|
224
|
+
* control that disappears the moment a neighbour is ticked moves every control after it. The one
|
|
225
|
+
* exception is a value the shopper has already selected — never disabled, so the filter stays
|
|
226
|
+
* removable — and a selected value the facets no longer *list at all* is appended rather than lost,
|
|
227
|
+
* labelled by its raw value since nothing describes it any more.
|
|
228
|
+
*/
|
|
229
|
+
export function facetValuesFor(
|
|
230
|
+
source: FilterSource,
|
|
231
|
+
context: ValueBuildContext,
|
|
232
|
+
selected: readonly string[]
|
|
233
|
+
): FilterFacetValue[] {
|
|
234
|
+
const out: FilterFacetValue[] = rawFacetValuesFor(source, context).map((value) => {
|
|
235
|
+
// A row its ticked parent already covers is neither operable nor countable on its own: the
|
|
236
|
+
// request carries the parent's id and the platform expands it, so this row's own count
|
|
237
|
+
// describes a filter nobody sent. It is drawn ticked and inoperable instead
|
|
238
|
+
// (`FilterFacetValue.implied`), which is also why the `disabled` rule below does not get a say
|
|
239
|
+
// — an implied row is disabled whatever it counts.
|
|
240
|
+
if (value.parent !== undefined && selected.includes(value.parent)) {
|
|
241
|
+
return { ...value, implied: true, disabled: true };
|
|
242
|
+
}
|
|
243
|
+
return {
|
|
244
|
+
...value,
|
|
245
|
+
...(value.count === 0 && !selected.includes(value.value) ? { disabled: true } : {}),
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
const listed = new Set(out.map((value) => value.value));
|
|
249
|
+
for (const value of selected) {
|
|
250
|
+
if (!listed.has(value)) {
|
|
251
|
+
out.push({ value, label: unlistedLabel(source, value, context.availability), count: 0 });
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return out;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* What a kept-but-unlisted value is called. The store's own words are gone with the value, so a
|
|
259
|
+
* category or an option value can only be labelled by its raw slug — but **`availability` is the
|
|
260
|
+
* one family the theme names itself**, so it is never shown the platform's spelling.
|
|
261
|
+
*
|
|
262
|
+
* That case is reachable: a store whose stock cannot be read answers no `availability` facet at
|
|
263
|
+
* all, and a shopper arriving on a shared `?availability=in_stock` link still has the value
|
|
264
|
+
* selected. The generic fallback drew them a checkbox and a chip reading literally `in_stock`, in
|
|
265
|
+
* both locales.
|
|
266
|
+
*/
|
|
267
|
+
function unlistedLabel(
|
|
268
|
+
source: FilterSource,
|
|
269
|
+
value: string,
|
|
270
|
+
availability: AvailabilityLabels
|
|
271
|
+
): string {
|
|
272
|
+
if (source !== 'availability') return value;
|
|
273
|
+
if (value === IN_STOCK) return availability.inStock;
|
|
274
|
+
if (value === OUT_OF_STOCK) return availability.outOfStock;
|
|
275
|
+
return value;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function rawFacetValuesFor(
|
|
279
|
+
source: FilterSource,
|
|
280
|
+
{ facets, categoryScopeSlug }: ValueBuildContext
|
|
281
|
+
): FilterFacetValue[] {
|
|
282
|
+
if (facets === undefined) return [];
|
|
283
|
+
if (source === 'category') {
|
|
284
|
+
// On a category page the facet is that category's children and nothing else — flat, because
|
|
285
|
+
// one level down is all there is to offer (see `childCategoryTerms`).
|
|
286
|
+
if (categoryScopeSlug !== null) {
|
|
287
|
+
return childCategoryTerms(facets.categories, categoryScopeSlug);
|
|
288
|
+
}
|
|
289
|
+
// **A parent row is only offered by a source that can honour one.** `categoryCounts:
|
|
290
|
+
// 'rolled-up'` is the platform saying it counted the ancestors itself — and the contract that
|
|
291
|
+
// added those counts (3.8.0) added the `parentId` that places them *and* the `categoryId` that
|
|
292
|
+
// matches a whole subtree, together. A gateway answering anything else counts assignments and
|
|
293
|
+
// matches direct membership only, so a `Tableware` row there is a filter it cannot honour: the
|
|
294
|
+
// shopper would tick it and get an empty grid under a URL saying otherwise, which is the one
|
|
295
|
+
// thing this whole server-side filter path exists to remove. So that family stays exactly as
|
|
296
|
+
// flat as it always was.
|
|
297
|
+
if (facets.categoryCounts !== 'rolled-up') return facets.categories.map(flatTermValue);
|
|
298
|
+
return nestCategoryTerms(facets.categories);
|
|
299
|
+
}
|
|
300
|
+
if (source === 'collection') {
|
|
301
|
+
// Flat, and not by omission: a collection is a curated list, not a level of anything.
|
|
302
|
+
return facets.collections.map(flatTermValue);
|
|
303
|
+
}
|
|
304
|
+
const optionKey = optionKeyOf(source);
|
|
305
|
+
if (optionKey === null) return [];
|
|
306
|
+
const option = facets.options.find((candidate) => candidate.key === optionKey);
|
|
307
|
+
return (option?.values ?? []).map((value) => ({
|
|
308
|
+
value: value.value,
|
|
309
|
+
label: value.label,
|
|
310
|
+
count: value.count,
|
|
311
|
+
...(value.swatch === undefined ? {} : { swatch: value.swatch }),
|
|
312
|
+
...(value.group === undefined ? {} : { group: value.group }),
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* **The children of one category, as rows the panel can draw** — the `category` facet on a
|
|
318
|
+
* category page.
|
|
319
|
+
*
|
|
320
|
+
* The scope already *is* that category, so the whole tree is the wrong vocabulary: a row for the
|
|
321
|
+
* category itself would narrow nothing, and a row for a sibling or a cousin would narrow to nothing
|
|
322
|
+
* at all. Its children are the only values that divide the page, and each of them filters within
|
|
323
|
+
* the subtree the page is scoped to, which is what the platform's `categoryId` already matches.
|
|
324
|
+
*
|
|
325
|
+
* The current category is found by **slug**, because that is what the route carries and what the
|
|
326
|
+
* facets spell their terms with; its children are the terms whose `parentId` is its id. Flat, and
|
|
327
|
+
* with the counts exactly as the source gave them — one level needs no indent, and no number here
|
|
328
|
+
* is derived.
|
|
329
|
+
*
|
|
330
|
+
* `[]` whenever the family cannot be read that way: a source that places no term (`parentId`
|
|
331
|
+
* absent, so no row can be known to be a child), or a scope whose own category the facets do not
|
|
332
|
+
* name — an empty category, say. The block drops a facet with no values, which is the honest
|
|
333
|
+
* answer: there is nothing here to divide.
|
|
334
|
+
*/
|
|
335
|
+
export function childCategoryTerms(
|
|
336
|
+
terms: readonly CatalogFacetTerm[],
|
|
337
|
+
categoryScopeSlug: string
|
|
338
|
+
): FilterFacetValue[] {
|
|
339
|
+
const current = terms.find((term) => term.slug === categoryScopeSlug);
|
|
340
|
+
if (current === undefined || current.id === '') return [];
|
|
341
|
+
return terms.filter((term) => term.parentId === current.id).map(flatTermValue);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function flatTermValue(term: CatalogFacetTerm): FilterFacetValue {
|
|
345
|
+
return { value: term.slug, label: term.title, count: term.count };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/** How deep the walk up a term's ancestors may go before it stops. A guard against a `parentId`
|
|
349
|
+
* cycle in the data, not a product decision — this runs inside a `computed`, where a hang is the
|
|
350
|
+
* block. */
|
|
351
|
+
const MAX_CATEGORY_DEPTH = 6;
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* **The category family as rows the panel can draw: each parent followed by its children, indented
|
|
355
|
+
* one level.**
|
|
356
|
+
*
|
|
357
|
+
* Only ever called for a family the platform has **placed and counted up itself** (public contract
|
|
358
|
+
* 3.8.0 — see `rawFacetValuesFor`), so the terms arrive as a tree with every reported ancestor in
|
|
359
|
+
* the same list and every count already the subtree's. A term that still carries no `parentId` at
|
|
360
|
+
* all comes out flat: the whole of this is skipped, not approximated.
|
|
361
|
+
*
|
|
362
|
+
* **One level of indent, ever.** A category three deep is rendered under its top-most listed
|
|
363
|
+
* ancestor, not at its own depth: a filter panel is not a tree view, every indent costs a 15rem
|
|
364
|
+
* sidebar a column of label width, and a shopper ticking a grandchild gets the same filter either
|
|
365
|
+
* way. Nothing is dropped — only flattened.
|
|
366
|
+
*
|
|
367
|
+
* **Order is the source's.** 3.8.0 answers depth-first by title, and this preserves whatever order
|
|
368
|
+
* it was handed: the top rows keep their input order and each parent's children keep theirs, which
|
|
369
|
+
* on an already depth-first list is no change at all. Nothing here sorts by count.
|
|
370
|
+
*
|
|
371
|
+
* **No count is derived.** A parent's number is the platform's own, deduplicated over its subtree,
|
|
372
|
+
* and a sum over the children on screen is not — a product in Cups and in Bowls is one product and
|
|
373
|
+
* two counts. So the numbers are used exactly as they came.
|
|
374
|
+
*/
|
|
375
|
+
export function nestCategoryTerms(terms: readonly CatalogFacetTerm[]): FilterFacetValue[] {
|
|
376
|
+
if (!terms.some((term) => term.parentId !== undefined)) return terms.map(flatTermValue);
|
|
377
|
+
const byId = new Map(terms.map((term) => [term.id, term] as const));
|
|
378
|
+
|
|
379
|
+
/** The top-most ancestor of a term that is **listed here** — the term itself for a top row. */
|
|
380
|
+
const topOf = (term: CatalogFacetTerm): CatalogFacetTerm => {
|
|
381
|
+
let top = term;
|
|
382
|
+
const seen = new Set<string>([term.id]);
|
|
383
|
+
for (let depth = 0; depth < MAX_CATEGORY_DEPTH; depth += 1) {
|
|
384
|
+
const parentId = top.parentId;
|
|
385
|
+
if (parentId === null || parentId === undefined || seen.has(parentId)) break;
|
|
386
|
+
const parent = byId.get(parentId);
|
|
387
|
+
if (parent === undefined) break;
|
|
388
|
+
seen.add(parentId);
|
|
389
|
+
top = parent;
|
|
390
|
+
}
|
|
391
|
+
return top;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
// Input order decides the order of the top rows, and a child follows the row it sits under — so a
|
|
395
|
+
// depth-first list (contract 3.8.0's own order) comes out exactly as it went in.
|
|
396
|
+
const topIdOf = new Map(terms.map((term) => [term.id, topOf(term).id] as const));
|
|
397
|
+
// Which terms are top rows: the ones that are their own top. A `parentId` cycle has none — every
|
|
398
|
+
// term in it stops one short of itself — and a term whose top row does not exist is promoted to
|
|
399
|
+
// one rather than nested under nothing, which is what keeps every row on screen and tickable.
|
|
400
|
+
const topIds = new Set(terms.filter((term) => topIdOf.get(term.id) === term.id).map((t) => t.id));
|
|
401
|
+
const tops: CatalogFacetTerm[] = [];
|
|
402
|
+
const childrenOf = new Map<string, CatalogFacetTerm[]>();
|
|
403
|
+
for (const term of terms) {
|
|
404
|
+
const top = topIdOf.get(term.id);
|
|
405
|
+
if (top === undefined || top === term.id || !topIds.has(top)) {
|
|
406
|
+
tops.push(term);
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
const siblings = childrenOf.get(top);
|
|
410
|
+
if (siblings === undefined) childrenOf.set(top, [term]);
|
|
411
|
+
else siblings.push(term);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
const out: FilterFacetValue[] = [];
|
|
415
|
+
for (const top of tops) {
|
|
416
|
+
out.push(flatTermValue(top));
|
|
417
|
+
for (const child of childrenOf.get(top.id) ?? []) {
|
|
418
|
+
out.push({ ...flatTermValue(child), parent: top.slug });
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return out;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* The combined `availability` facet's own values: "In stock only" (when the store can read stock)
|
|
426
|
+
* followed by one switch row per `facets.toggles[]` entry (`on_sale`, …) — see `queryKeyFor` for
|
|
427
|
+
* the one query key each toggle reads and writes. A selected value nothing lists any more (a stale
|
|
428
|
+
* `?availability=out_of_stock` link, a toggle the store has stopped sending) is kept, unlabelled
|
|
429
|
+
* where this theme has no name for it, the same rule every other family follows.
|
|
430
|
+
*/
|
|
431
|
+
export function availabilityFacetValues(
|
|
432
|
+
facets: CatalogFacets | undefined,
|
|
433
|
+
selection: FilterSelection,
|
|
434
|
+
availability: AvailabilityLabels
|
|
435
|
+
): FilterFacetValue[] {
|
|
436
|
+
if (facets === undefined) return [];
|
|
437
|
+
const out: FilterFacetValue[] = [];
|
|
438
|
+
if (facets.availability !== undefined) {
|
|
439
|
+
const selected = (selection.availability ?? []).includes(IN_STOCK);
|
|
440
|
+
out.push({
|
|
441
|
+
value: IN_STOCK,
|
|
442
|
+
label: availability.inStock,
|
|
443
|
+
count: facets.availability.in_stock,
|
|
444
|
+
...(facets.availability.in_stock === 0 && !selected ? { disabled: true } : {}),
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
for (const toggle of facets.toggles ?? []) {
|
|
448
|
+
const selected = (selection[toggleSourceFor(toggle.key)] ?? []).length > 0;
|
|
449
|
+
out.push({
|
|
450
|
+
value: toggle.key,
|
|
451
|
+
label: toggle.label,
|
|
452
|
+
count: toggle.count,
|
|
453
|
+
...(toggle.count === 0 && !selected ? { disabled: true } : {}),
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
const listed = new Set(out.map((value) => value.value));
|
|
457
|
+
for (const value of selection.availability ?? []) {
|
|
458
|
+
if (!listed.has(value)) {
|
|
459
|
+
out.push({ value, label: unlistedLabel('availability', value, availability), count: 0 });
|
|
460
|
+
listed.add(value);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
for (const source of Object.keys(selection)) {
|
|
464
|
+
const key = toggleKeyOf(source);
|
|
465
|
+
if (key === null || listed.has(key) || (selection[source as FilterSource]?.length ?? 0) === 0) {
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
out.push({ value: key, label: key, count: 0 });
|
|
469
|
+
listed.add(key);
|
|
470
|
+
}
|
|
471
|
+
return out;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* How a facet draws its values — **decided by the values, for an option.**
|
|
476
|
+
*
|
|
477
|
+
* A swatch is a colour the store sent as data, and the only control that can show one is
|
|
478
|
+
* `ColourFacet`'s own dot, so an option whose values carry one is a colour facet whatever it is
|
|
479
|
+
* keyed; a `group` (a size system — "Knitwear", "Socks (EU)") makes it a `size` facet; every other
|
|
480
|
+
* option is a plain `list`. That is what lets an arbitrary option key render correctly without this
|
|
481
|
+
* file knowing the key at all — the two hard-coded `size`/`colour` keys it used to carry meant a
|
|
482
|
+
* store spelling its option `color` got a facet with no values and no group.
|
|
483
|
+
*
|
|
484
|
+
* `category` and `collection` are always `list` — neither is ever a colour or a size.
|
|
485
|
+
*/
|
|
486
|
+
export function facetTypeFor(
|
|
487
|
+
source: FilterSource,
|
|
488
|
+
values: readonly FilterFacetValue[]
|
|
489
|
+
): FilterFacetType {
|
|
490
|
+
if (optionKeyOf(source) === null) return 'list';
|
|
491
|
+
if (values.some((value) => value.swatch !== undefined)) return 'colour';
|
|
492
|
+
if (values.some((value) => value.group !== undefined)) return 'size';
|
|
493
|
+
return 'list';
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/** Everything `buildFilterFacets` needs besides the rows themselves. */
|
|
497
|
+
export interface FacetBuildOptions {
|
|
498
|
+
facets: CatalogFacets | undefined;
|
|
499
|
+
/** The block's own internal selection (per `FilterSource`, price excluded). */
|
|
500
|
+
selection: FilterSelection;
|
|
501
|
+
availability: AvailabilityLabels;
|
|
502
|
+
categoryScopeSlug: string | null;
|
|
503
|
+
/** Sources this scope cannot narrow by (`StorefrontCollectionProducts.unfilterable`). */
|
|
504
|
+
unfilterable: ReadonlySet<string>;
|
|
505
|
+
/** A facet's title when the author set none. */
|
|
506
|
+
labelFor: (source: FilterSource) => string;
|
|
507
|
+
/** The block's `colourLayout` field: swatch rows (`list`, the default) or swatch tiles (`grid`). */
|
|
508
|
+
colourLayout: FilterFacetLayout;
|
|
509
|
+
/** The block's `sizeGuideHref` field, already resolved to a plain URL. */
|
|
510
|
+
sizeGuideHref?: string;
|
|
511
|
+
price: {
|
|
512
|
+
min: number;
|
|
513
|
+
max: number;
|
|
514
|
+
step: number;
|
|
515
|
+
/** The block's `priceSlider` field. */
|
|
516
|
+
slider: boolean;
|
|
517
|
+
/** Whether this range is money the store can actually name (`money.currency` is known). */
|
|
518
|
+
currency: boolean;
|
|
519
|
+
histogram?: number[];
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
/**
|
|
524
|
+
* **The adapter's one entry point.** Builds the ordered `FilterFacet[]` `FilterPanel` draws, from
|
|
525
|
+
* the author's own `filters[]` rows (already expanded — `options` resolved to real sources) and the
|
|
526
|
+
* storefront's facets. A renderable-but-empty range is still included: `FilterPanel`'s own
|
|
527
|
+
* `renderableFacets` drops a range with fewer than two distinct values, so this file does not
|
|
528
|
+
* duplicate that rule. Every other facet with no values to offer (no store data, an empty
|
|
529
|
+
* `availability`) is dropped here, same as it always was.
|
|
530
|
+
*/
|
|
531
|
+
export function buildFilterFacets(
|
|
532
|
+
rows: readonly FacetBuildRow[],
|
|
533
|
+
options: FacetBuildOptions
|
|
534
|
+
): FilterFacet[] {
|
|
535
|
+
const out: FilterFacet[] = [];
|
|
536
|
+
const seen = new Set<FilterSource>();
|
|
537
|
+
for (const row of rows) {
|
|
538
|
+
const { source } = row;
|
|
539
|
+
if (options.unfilterable.has(source) || seen.has(source)) continue;
|
|
540
|
+
seen.add(source);
|
|
541
|
+
const label = (row.label ?? '').trim() || options.labelFor(source);
|
|
542
|
+
const collapsed = row.collapsed === true;
|
|
543
|
+
|
|
544
|
+
if (source === 'price') {
|
|
545
|
+
// Omit a range where min === max: nothing for the panel to filter.
|
|
546
|
+
if (options.price.min === options.price.max) continue;
|
|
547
|
+
out.push({
|
|
548
|
+
id: 'price',
|
|
549
|
+
label,
|
|
550
|
+
type: 'range',
|
|
551
|
+
collapsed,
|
|
552
|
+
min: options.price.min,
|
|
553
|
+
max: options.price.max,
|
|
554
|
+
step: options.price.step,
|
|
555
|
+
currency: options.price.currency,
|
|
556
|
+
slider: options.price.slider,
|
|
557
|
+
...(options.price.histogram ? { histogram: options.price.histogram } : {}),
|
|
558
|
+
});
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
if (source === 'availability') {
|
|
563
|
+
const values = availabilityFacetValues(
|
|
564
|
+
options.facets,
|
|
565
|
+
options.selection,
|
|
566
|
+
options.availability
|
|
567
|
+
);
|
|
568
|
+
if (values.length === 0) continue;
|
|
569
|
+
out.push({ id: 'availability', label, type: 'toggle', collapsed, values });
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
const values = facetValuesFor(source, options, options.selection[source] ?? []);
|
|
574
|
+
if (values.length === 0) continue;
|
|
575
|
+
const type = facetTypeFor(source, values);
|
|
576
|
+
out.push({
|
|
577
|
+
id: queryKeyFor(source),
|
|
578
|
+
label,
|
|
579
|
+
type,
|
|
580
|
+
collapsed,
|
|
581
|
+
values,
|
|
582
|
+
...(type === 'colour' ? { layout: options.colourLayout } : {}),
|
|
583
|
+
...(type === 'size' && options.sizeGuideHref ? { sizeGuideHref: options.sizeGuideHref } : {}),
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
return out;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
/**
|
|
590
|
+
* Every rendered source's own `FilterSource`, keyed by the `FilterFacet.id` (the query key) the
|
|
591
|
+
* panel uses — the reverse of `queryKeyFor`, for translating the panel's own selection back into
|
|
592
|
+
* the block's internal one (`applyPanelSelection`). `availability` and `price` are deliberately not
|
|
593
|
+
* here: both are translated by hand, the first because one ui facet id can span several internal
|
|
594
|
+
* sources (availability plus every toggle), the second because it is never a value list.
|
|
595
|
+
*/
|
|
596
|
+
export function facetSourceMap(rows: readonly FacetBuildRow[]): ReadonlyMap<string, FilterSource> {
|
|
597
|
+
const out = new Map<string, FilterSource>();
|
|
598
|
+
for (const row of rows) {
|
|
599
|
+
if (row.source === 'price' || row.source === 'availability') continue;
|
|
600
|
+
out.set(queryKeyFor(row.source), row.source);
|
|
601
|
+
}
|
|
602
|
+
return out;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/**
|
|
606
|
+
* The panel's own view of the selection (`FilterSelection` keyed by facet id), derived from the
|
|
607
|
+
* block's internal one. One-way: the panel is controlled from this, and every change it reports
|
|
608
|
+
* comes back through `applyPanelSelection`.
|
|
609
|
+
*/
|
|
610
|
+
export function panelSelectionFor(
|
|
611
|
+
selection: FilterSelection,
|
|
612
|
+
priceRange: { min: string; max: string },
|
|
613
|
+
priceSpan: { min: number; max: number }
|
|
614
|
+
): UiFilterSelection {
|
|
615
|
+
const out: UiFilterSelection = {};
|
|
616
|
+
for (const [source, values] of Object.entries(selection)) {
|
|
617
|
+
if (!values || values.length === 0) continue;
|
|
618
|
+
if (toggleKeyOf(source) !== null) continue; // folded into `availability` below
|
|
619
|
+
out[queryKeyFor(source as FilterSource)] = values;
|
|
620
|
+
}
|
|
621
|
+
const toggleKeys = Object.keys(selection)
|
|
622
|
+
.filter((source) => (selection[source as FilterSource]?.length ?? 0) > 0)
|
|
623
|
+
.map((source) => toggleKeyOf(source))
|
|
624
|
+
.filter((key): key is string => key !== null);
|
|
625
|
+
if (toggleKeys.length > 0) {
|
|
626
|
+
out.availability = [...(out.availability ?? []), ...toggleKeys];
|
|
627
|
+
}
|
|
628
|
+
if (priceRange.min !== '' || priceRange.max !== '') {
|
|
629
|
+
out.price = sliderValueFor(priceRange, priceSpan);
|
|
630
|
+
}
|
|
631
|
+
return out;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* The reverse of `panelSelectionFor`: what the panel's own `change` event reports, translated back
|
|
636
|
+
* into the block's internal selection plus the price range it writes to the URL.
|
|
637
|
+
*
|
|
638
|
+
* Every non-price, non-`availability` key is resolved through `sources` (an unknown key — a stale
|
|
639
|
+
* one from a facet the author has since removed — is simply dropped, the honest answer for a
|
|
640
|
+
* selection nothing can act on). `availability`'s own array is split back into the canonical
|
|
641
|
+
* `in_stock`/`out_of_stock` tokens and whichever toggle keys ride along with it, by key:
|
|
642
|
+
* `toggleKeys` is this scope's current `facets.toggles[]` keys, so a value that is neither a known
|
|
643
|
+
* availability token nor a current toggle key is dropped the same way an unknown source is.
|
|
644
|
+
*
|
|
645
|
+
* The price pair is run through `rangeFromSlider`, which is idempotent when nothing moved — safe to
|
|
646
|
+
* call on every change, not only a price one.
|
|
647
|
+
*/
|
|
648
|
+
export function applyPanelSelection(
|
|
649
|
+
next: UiFilterSelection,
|
|
650
|
+
context: {
|
|
651
|
+
sources: ReadonlyMap<string, FilterSource>;
|
|
652
|
+
toggleKeys: ReadonlySet<string>;
|
|
653
|
+
priceSpan: PriceSpan;
|
|
654
|
+
appliedPrice: PriceRange;
|
|
655
|
+
}
|
|
656
|
+
): { selection: FilterSelection; price: PriceRange } {
|
|
657
|
+
const selection: FilterSelection = {};
|
|
658
|
+
for (const [facetId, raw] of Object.entries(next)) {
|
|
659
|
+
if (facetId === 'price') continue;
|
|
660
|
+
const values = Array.isArray(raw) ? raw.filter((v): v is string => typeof v === 'string') : [];
|
|
661
|
+
if (facetId === 'availability') {
|
|
662
|
+
const avail = values.filter((value) => value === IN_STOCK || value === OUT_OF_STOCK);
|
|
663
|
+
if (avail.length > 0) selection.availability = avail;
|
|
664
|
+
for (const value of values) {
|
|
665
|
+
if (avail.includes(value)) continue;
|
|
666
|
+
if (context.toggleKeys.has(value)) selection[toggleSourceFor(value)] = ['1'];
|
|
667
|
+
}
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
const source = context.sources.get(facetId);
|
|
671
|
+
if (source !== undefined && values.length > 0) selection[source] = values;
|
|
672
|
+
}
|
|
673
|
+
const priceVal = next.price;
|
|
674
|
+
const pair: [number, number] =
|
|
675
|
+
Array.isArray(priceVal) && typeof priceVal[0] === 'number' && typeof priceVal[1] === 'number'
|
|
676
|
+
? (priceVal as [number, number])
|
|
677
|
+
: [context.priceSpan.min, context.priceSpan.max];
|
|
678
|
+
const price = rangeFromSlider(pair, context.priceSpan, context.appliedPrice);
|
|
679
|
+
return { selection, price };
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* A price input's value, reduced to what a price can be: digits only, leading zeroes dropped, and
|
|
684
|
+
* at most nine of them (a shopper pasting a phone number into "Max" must not turn into a request
|
|
685
|
+
* for a 10-digit ceiling). Empty means "no bound", which is why `''` is a valid result rather than
|
|
686
|
+
* a `0` the request would then have to special-case.
|
|
687
|
+
*/
|
|
688
|
+
export function sanitizeAmount(raw: string): string {
|
|
689
|
+
const digits = raw
|
|
690
|
+
.replace(/\D+/g, '')
|
|
691
|
+
.slice(0, 9)
|
|
692
|
+
.replace(/^0+(?=\d)/, '');
|
|
693
|
+
return digits;
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
/**
|
|
697
|
+
* The shopper's own price bounds, as the URL and the storefront request both spell them:
|
|
698
|
+
* `"<min>-<max>"` in whole major units, either end empty for "no bound".
|
|
699
|
+
*/
|
|
700
|
+
export interface PriceRange {
|
|
701
|
+
min: string;
|
|
702
|
+
max: string;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
export const NO_PRICE_RANGE: PriceRange = { min: '', max: '' };
|
|
706
|
+
|
|
707
|
+
/** Whether either end is set — `''`/`''` is no filter at all. */
|
|
708
|
+
export function hasPriceRange(range: PriceRange): boolean {
|
|
709
|
+
return range.min !== '' || range.max !== '';
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
/** `"1200-4800"` → `{min: '1200', max: '4800'}`; either end may be empty, and so may the whole. */
|
|
713
|
+
export function parsePriceRange(raw: string | undefined): PriceRange {
|
|
714
|
+
if (raw === undefined) return NO_PRICE_RANGE;
|
|
715
|
+
const [min = '', max = ''] = raw.split('-');
|
|
716
|
+
return { min: sanitizeAmount(min), max: sanitizeAmount(max) };
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/** The query/request value, or `null` when there is no bound to send. */
|
|
720
|
+
export function formatPriceRange(range: PriceRange): string | null {
|
|
721
|
+
return hasPriceRange(range) ? `${range.min}-${range.max}` : null;
|
|
722
|
+
}
|
|
723
|
+
|
|
724
|
+
/** The span a price control works across: the catalogue's own lowest and highest price. */
|
|
725
|
+
export interface PriceSpan {
|
|
726
|
+
min: number;
|
|
727
|
+
max: number;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
/** The span of a set of products, or `null` when there are none to span. */
|
|
731
|
+
export function priceSpanOf(items: ReadonlyArray<{ price: { amount: number } }>): PriceSpan | null {
|
|
732
|
+
let min = Number.POSITIVE_INFINITY;
|
|
733
|
+
let max = Number.NEGATIVE_INFINITY;
|
|
734
|
+
for (const item of items) {
|
|
735
|
+
const amount = item.price.amount;
|
|
736
|
+
if (!Number.isFinite(amount)) continue;
|
|
737
|
+
if (amount < min) min = amount;
|
|
738
|
+
if (amount > max) max = amount;
|
|
739
|
+
}
|
|
740
|
+
if (!Number.isFinite(min) || !Number.isFinite(max)) return null;
|
|
741
|
+
return { min, max };
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/**
|
|
745
|
+
* The wider of two spans.
|
|
746
|
+
*
|
|
747
|
+
* Only the facets' own `price` is filter-independent (it is counted with every filter *except*
|
|
748
|
+
* price applied). A span read off the loaded items instead — the fallback for a storefront that
|
|
749
|
+
* answers no facets — shrinks as the shopper narrows the range, and a slider whose track shrinks
|
|
750
|
+
* under the thumb can only ever be narrowed: each drag inward moves the bound inward too. So the
|
|
751
|
+
* fallback keeps the widest span it has seen for this collection.
|
|
752
|
+
*/
|
|
753
|
+
export function widenPriceSpan(
|
|
754
|
+
current: PriceSpan | null,
|
|
755
|
+
next: PriceSpan | null
|
|
756
|
+
): PriceSpan | null {
|
|
757
|
+
if (current === null) return next;
|
|
758
|
+
if (next === null) return current;
|
|
759
|
+
return { min: Math.min(current.min, next.min), max: Math.max(current.max, next.max) };
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* The span widened to hold the shopper's own bounds.
|
|
764
|
+
*
|
|
765
|
+
* The facets' price span is counted with every filter *except* price applied, so it narrows as the
|
|
766
|
+
* other filters narrow — tick a colour and the span becomes that colour's own prices. A slider
|
|
767
|
+
* cannot show a value outside its bounds, so without this a range of 50–150 read back as 96–96 the
|
|
768
|
+
* moment another facet was touched, and dragging either thumb would then write that back as the
|
|
769
|
+
* shopper's range. Including their own bounds keeps the thumbs where they put them and keeps the
|
|
770
|
+
* filter removable by dragging back out.
|
|
771
|
+
*/
|
|
772
|
+
export function spanWithRange(span: PriceSpan, range: PriceRange): PriceSpan {
|
|
773
|
+
const bounds: number[] = [];
|
|
774
|
+
if (range.min !== '') bounds.push(Number(range.min));
|
|
775
|
+
if (range.max !== '') bounds.push(Number(range.max));
|
|
776
|
+
if (bounds.length === 0) return span;
|
|
777
|
+
return widenPriceSpan(span, { min: Math.min(...bounds), max: Math.max(...bounds) }) ?? span;
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
/**
|
|
781
|
+
* The pair the slider shows: the shopper's own bound where they set one, the span's where they did
|
|
782
|
+
* not — so an untouched control spans the whole catalogue (spec Default content: "both thumbs at
|
|
783
|
+
* an end"), which is also what makes "no filter" visible as a state.
|
|
784
|
+
*/
|
|
785
|
+
export function sliderValueFor(range: PriceRange, span: PriceSpan): [number, number] {
|
|
786
|
+
const min = range.min === '' ? span.min : Number(range.min);
|
|
787
|
+
const max = range.max === '' ? span.max : Number(range.max);
|
|
788
|
+
return [min, max];
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/**
|
|
792
|
+
* The range written back out of the slider, once a move is over.
|
|
793
|
+
*
|
|
794
|
+
* **The applied range is the source of truth, and only the end that moved is rewritten.** One
|
|
795
|
+
* gesture moves one thumb — a drag, an arrow-key run, a typed value — so the other end keeps the
|
|
796
|
+
* string the shopper already applied, character for character. Deriving *both* ends from the pair
|
|
797
|
+
* is what made a nudge of one thumb erase the other bound: the span the thumbs are drawn across is
|
|
798
|
+
* widened to hold the shopper's own bounds (`spanWithRange`, because the facets' span is counted
|
|
799
|
+
* with every filter but price and can narrow inside their range), so on `?price=50-150&colour=oat`
|
|
800
|
+
* both thumbs sit on "an end" and reading either as "no bound" threw away a filter the shopper had
|
|
801
|
+
* set, on screen, in the URL and in the request.
|
|
802
|
+
*
|
|
803
|
+
* For the end that *did* move, **a thumb parked on the span's own end is "no bound"** — not a bound
|
|
804
|
+
* that happens to equal it. That is what lets a shopper drag a filter back off, keeps the URL free
|
|
805
|
+
* of a range nobody asked for, and keeps the request identical to one made before any filter
|
|
806
|
+
* existed.
|
|
807
|
+
*
|
|
808
|
+
* Idempotent when the pair did not move at all (the panel reports the whole selection on every
|
|
809
|
+
* change, price included): both ends still equal `wasMin`/`wasMax`, so the same `applied` comes
|
|
810
|
+
* back out.
|
|
811
|
+
*/
|
|
812
|
+
export function rangeFromSlider(
|
|
813
|
+
value: readonly [number, number],
|
|
814
|
+
span: PriceSpan,
|
|
815
|
+
applied: PriceRange = NO_PRICE_RANGE
|
|
816
|
+
): PriceRange {
|
|
817
|
+
// Where the thumbs stood before this move: anything still there is an end nobody touched.
|
|
818
|
+
const [wasMin, wasMax] = sliderValueFor(applied, span);
|
|
819
|
+
return {
|
|
820
|
+
min:
|
|
821
|
+
value[0] === wasMin ? applied.min : value[0] <= span.min ? '' : String(Math.round(value[0])),
|
|
822
|
+
max:
|
|
823
|
+
value[1] === wasMax ? applied.max : value[1] >= span.max ? '' : String(Math.round(value[1])),
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
/**
|
|
828
|
+
* The price slider's step when the author set no `priceStep`: one unit of the store currency, with
|
|
829
|
+
* the one documented exception (spec Fields, `priceStep`: "one unit of the store currency, ISK
|
|
830
|
+
* 100, USD 1"). ISK has no minor unit and its amounts run two orders of magnitude larger, so a
|
|
831
|
+
* step of one króna gives a 20 000-step track nobody can drag accurately.
|
|
832
|
+
*
|
|
833
|
+
* A store whose currency wants its own step says so in the field; this is only the default.
|
|
834
|
+
*/
|
|
835
|
+
const CURRENCY_PRICE_STEP: Readonly<Record<string, number>> = { ISK: 100 };
|
|
836
|
+
|
|
837
|
+
export function defaultPriceStep(currency: string | undefined): number {
|
|
838
|
+
return CURRENCY_PRICE_STEP[currency ?? ''] ?? 1;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
/**
|
|
842
|
+
* How many stops a track needs to be worth dragging. Below this the step is the control's whole
|
|
843
|
+
* range, so the thumbs can only sit at either end.
|
|
844
|
+
*/
|
|
845
|
+
const MIN_PRICE_STOPS = 10;
|
|
846
|
+
|
|
847
|
+
/**
|
|
848
|
+
* The currency's own step, narrowed to one the catalogue can hold.
|
|
849
|
+
*
|
|
850
|
+
* A step is a granularity, not a unit of faith: a store selling in krónur whose whole collection
|
|
851
|
+
* spans 50 of them gets a two-stop track from ISK's 100, and every value between the ends — a
|
|
852
|
+
* typed figure, a bound out of a shared URL — then snaps to one end or the other. So a default
|
|
853
|
+
* step the span cannot hold ten of falls back to one unit, which every span can.
|
|
854
|
+
*
|
|
855
|
+
* Only the default is fitted. An author who sets `priceStep` themselves gets exactly that.
|
|
856
|
+
*/
|
|
857
|
+
export function fitPriceStep(step: number, span: PriceSpan): number {
|
|
858
|
+
const width = span.max - span.min;
|
|
859
|
+
if (!(width > 0)) return step;
|
|
860
|
+
return Math.floor(width / MIN_PRICE_STOPS) >= step ? step : 1;
|
|
861
|
+
}
|