@backgold/template-kit 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +12 -2
- package/dist/chunk-RJAVFRC6.js +67 -0
- package/dist/constants-BAIo_Q5U.d-lQbj1e1-.d.ts +48 -0
- package/dist/content-api-Cox1-wz5.d.ts +1096 -0
- package/dist/contract/content-api-examples.d.ts +72 -0
- package/dist/contract/content-api-examples.js +926 -0
- package/dist/contract/content-api.d.ts +2 -0
- package/dist/contract/content-api.js +1 -0
- package/dist/contract/site-templates.d.ts +253 -0
- package/dist/contract/site-templates.js +185 -0
- package/package.json +70 -3
- package/src/components/EntryCard.astro +35 -0
- package/src/components/EventMap.astro +118 -0
- package/src/components/EventPage.astro +194 -0
- package/src/components/FieldValue.astro +41 -0
- package/src/components/LeadingHero.astro +21 -0
- package/src/components/NotFound.astro +20 -0
- package/src/components/PageSections.astro +47 -0
- package/src/components/Prose.astro +18 -0
- package/src/components/chrome/Brand.astro +61 -0
- package/src/components/chrome/ClosingCall.astro +94 -0
- package/src/components/chrome/DonateStrip.astro +61 -0
- package/src/components/chrome/FooterColumns.astro +32 -0
- package/src/components/chrome/FooterContact.astro +28 -0
- package/src/components/chrome/MenuList.astro +45 -0
- package/src/components/chrome/ModeToggle.astro +125 -0
- package/src/components/chrome/SignedInNotice.astro +53 -0
- package/src/components/chrome/SignupBand.astro +12 -0
- package/src/components/chrome/TakeoverBanner.astro +56 -0
- package/src/components/chrome/VisitorAccount.astro +82 -0
- package/src/components/entries/AuthorBox.astro +47 -0
- package/src/components/entries/EntryFields.astro +29 -0
- package/src/components/entries/EntryHeader.astro +77 -0
- package/src/components/entries/ListFrame.astro +126 -0
- package/src/components/entries/ListSearch.astro +24 -0
- package/src/components/entries/Share.astro +24 -0
- package/src/components/entries/course/classroom.astro +26 -0
- package/src/components/entries/course/lesson.astro +18 -0
- package/src/components/entries/course/section.ts +20 -0
- package/src/components/entries/course/syllabus.astro +15 -0
- package/src/components/entries/index.ts +77 -0
- package/src/components/entries/newsletter/issue.astro +41 -0
- package/src/components/entries/newsletter/list.astro +26 -0
- package/src/components/entries/podcast/episode.astro +42 -0
- package/src/components/entries/podcast/list.astro +36 -0
- package/src/components/entries/post/article.astro +31 -0
- package/src/components/entries/post/list.astro +85 -0
- package/src/components/entries/props.ts +61 -0
- package/src/components/entries/video/cards.astro +39 -0
- package/src/components/entries/video/grid.astro +15 -0
- package/src/components/entries/video/watch.astro +41 -0
- package/src/components/live/LivePage.astro +215 -0
- package/src/components/live/LocalTime.astro +29 -0
- package/src/components/sections/About/books.astro +188 -0
- package/src/components/sections/About/credentials.ts +80 -0
- package/src/components/sections/About/index.astro +60 -0
- package/src/components/sections/About/left.astro +57 -0
- package/src/components/sections/About/letter.astro +111 -0
- package/src/components/sections/About/portrait.astro +308 -0
- package/src/components/sections/Advocacy/compact.astro +14 -0
- package/src/components/sections/Advocacy/index.astro +14 -0
- package/src/components/sections/Band.astro +39 -0
- package/src/components/sections/Blog/feature.astro +176 -0
- package/src/components/sections/Blog/index.astro +37 -0
- package/src/components/sections/Blog/latest.ts +80 -0
- package/src/components/sections/Blog/list.astro +186 -0
- package/src/components/sections/Booking/index.astro +139 -0
- package/src/components/sections/Booking/list.astro +144 -0
- package/src/components/sections/Booking/types.ts +60 -0
- package/src/components/sections/Calendar/agenda.astro +14 -0
- package/src/components/sections/Calendar/index.astro +14 -0
- package/src/components/sections/CandidateRoster/ballot.astro +394 -0
- package/src/components/sections/CandidateRoster/candidates.ts +69 -0
- package/src/components/sections/CandidateRoster/compact.astro +115 -0
- package/src/components/sections/CandidateRoster/index.astro +57 -0
- package/src/components/sections/CaseStudies/beforeafter.astro +222 -0
- package/src/components/sections/CaseStudies/cases.ts +111 -0
- package/src/components/sections/CaseStudies/grid.astro +261 -0
- package/src/components/sections/CaseStudies/index.astro +333 -0
- package/src/components/sections/Classroom/CourseCard.astro +181 -0
- package/src/components/sections/Classroom/Courses.astro +88 -0
- package/src/components/sections/Classroom/classroom.ts +141 -0
- package/src/components/sections/Classroom/index.astro +20 -0
- package/src/components/sections/Classroom/list.astro +15 -0
- package/src/components/sections/Collection/compact.astro +7 -0
- package/src/components/sections/Collection/entries.ts +62 -0
- package/src/components/sections/Collection/feature.astro +180 -0
- package/src/components/sections/Collection/index.astro +46 -0
- package/src/components/sections/Collection/library.astro +271 -0
- package/src/components/sections/Collection/list.astro +7 -0
- package/src/components/sections/Collection/outline.astro +134 -0
- package/src/components/sections/Collection/shelves.astro +191 -0
- package/src/components/sections/Comparison/Unlisted.astro +6 -0
- package/src/components/sections/Comparison/cards.astro +171 -0
- package/src/components/sections/Comparison/index.astro +191 -0
- package/src/components/sections/Comparison/sides.ts +98 -0
- package/src/components/sections/Comparison/spectrum.astro +261 -0
- package/src/components/sections/ContentPost/banner.astro +152 -0
- package/src/components/sections/ContentPost/index.astro +35 -0
- package/src/components/sections/ContentPost/lesson.ts +126 -0
- package/src/components/sections/ContentPost/player.astro +254 -0
- package/src/components/sections/ContentPost/prose.astro +80 -0
- package/src/components/sections/ContentPost/theater.astro +329 -0
- package/src/components/sections/Countdown/index.astro +53 -0
- package/src/components/sections/Course/Bar.astro +46 -0
- package/src/components/sections/Course/CourseHead.astro +123 -0
- package/src/components/sections/Course/Crumbs.astro +44 -0
- package/src/components/sections/Course/Icon.astro +29 -0
- package/src/components/sections/Course/Ring.astro +62 -0
- package/src/components/sections/Course/Syllabus.astro +146 -0
- package/src/components/sections/Course/course.ts +146 -0
- package/src/components/sections/Course/index.astro +143 -0
- package/src/components/sections/Course/outline.astro +39 -0
- package/src/components/sections/Cta/banner.astro +64 -0
- package/src/components/sections/Cta/card.astro +88 -0
- package/src/components/sections/Cta/index.astro +31 -0
- package/src/components/sections/Cta/photo.astro +114 -0
- package/src/components/sections/Cta/strip.astro +91 -0
- package/src/components/sections/Donate/index.astro +136 -0
- package/src/components/sections/Donate/story.astro +7 -0
- package/src/components/sections/Event/card.astro +14 -0
- package/src/components/sections/Event/index.astro +14 -0
- package/src/components/sections/EventsList/AgendaMonths.astro +105 -0
- package/src/components/sections/EventsList/CalendarLine.astro +23 -0
- package/src/components/sections/EventsList/EventRows.astro +196 -0
- package/src/components/sections/EventsList/LiveCounts.astro +75 -0
- package/src/components/sections/EventsList/TypeTabs.astro +53 -0
- package/src/components/sections/EventsList/agenda.astro +55 -0
- package/src/components/sections/EventsList/calendar.astro +392 -0
- package/src/components/sections/EventsList/cards.astro +186 -0
- package/src/components/sections/EventsList/cohorts.astro +205 -0
- package/src/components/sections/EventsList/compact.astro +163 -0
- package/src/components/sections/EventsList/events.ts +168 -0
- package/src/components/sections/EventsList/index.astro +46 -0
- package/src/components/sections/EventsList/map.astro +436 -0
- package/src/components/sections/EventsList/shifts.astro +355 -0
- package/src/components/sections/EventsList/week.astro +361 -0
- package/src/components/sections/Faq/Contact.astro +63 -0
- package/src/components/sections/Faq/Questions.astro +98 -0
- package/src/components/sections/Faq/cards.astro +74 -0
- package/src/components/sections/Faq/faq.ts +101 -0
- package/src/components/sections/Faq/index.astro +49 -0
- package/src/components/sections/Faq/topics.astro +178 -0
- package/src/components/sections/Financials/Foot.astro +38 -0
- package/src/components/sections/Financials/Head.astro +47 -0
- package/src/components/sections/Financials/donut.astro +197 -0
- package/src/components/sections/Financials/financials.ts +52 -0
- package/src/components/sections/Financials/index.astro +148 -0
- package/src/components/sections/Financials/table.astro +157 -0
- package/src/components/sections/FormBlock/index.astro +33 -0
- package/src/components/sections/FormBlock/wide.astro +7 -0
- package/src/components/sections/Hero/cover.astro +176 -0
- package/src/components/sections/Hero/donate.astro +209 -0
- package/src/components/sections/Hero/field.astro +390 -0
- package/src/components/sections/Hero/index.astro +52 -0
- package/src/components/sections/Hero/overlap.astro +151 -0
- package/src/components/sections/Hero/rotating.astro +30 -0
- package/src/components/sections/Hero/split.astro +99 -0
- package/src/components/sections/Hero/statement.astro +55 -0
- package/src/components/sections/Hero/video.astro +399 -0
- package/src/components/sections/Issues/checklist.astro +204 -0
- package/src/components/sections/Issues/drawer.astro +237 -0
- package/src/components/sections/Issues/index.astro +115 -0
- package/src/components/sections/Issues/issues.ts +37 -0
- package/src/components/sections/Issues/numbered.astro +79 -0
- package/src/components/sections/Issues/rows.astro +137 -0
- package/src/components/sections/Issues/steps.astro +81 -0
- package/src/components/sections/Issues/tabs.astro +322 -0
- package/src/components/sections/Leaderboard/index.astro +29 -0
- package/src/components/sections/Lesson/Door.astro +121 -0
- package/src/components/sections/Lesson/LessonBody.astro +416 -0
- package/src/components/sections/Lesson/Rail.astro +182 -0
- package/src/components/sections/Lesson/Tracker.astro +137 -0
- package/src/components/sections/Lesson/index.astro +27 -0
- package/src/components/sections/Lesson/lesson.ts +233 -0
- package/src/components/sections/Lesson/reading.astro +21 -0
- package/src/components/sections/Map/CoverageLine.astro +19 -0
- package/src/components/sections/Map/RegionMap.astro +109 -0
- package/src/components/sections/Map/bar.astro +160 -0
- package/src/components/sections/Map/index.astro +192 -0
- package/src/components/sections/Map/map.astro +275 -0
- package/src/components/sections/MediaKit/BioCard.astro +108 -0
- package/src/components/sections/MediaKit/Downloads.astro +88 -0
- package/src/components/sections/MediaKit/index.astro +183 -0
- package/src/components/sections/MediaKit/kit.ts +140 -0
- package/src/components/sections/MediaKit/press.astro +189 -0
- package/src/components/sections/MediaKit/speaker.astro +205 -0
- package/src/components/sections/Membership/Benefits.astro +42 -0
- package/src/components/sections/Membership/Check.astro +12 -0
- package/src/components/sections/Membership/Price.astro +61 -0
- package/src/components/sections/Membership/compare.astro +312 -0
- package/src/components/sections/Membership/index.astro +281 -0
- package/src/components/sections/Membership/offers.ts +169 -0
- package/src/components/sections/Membership/tiers.astro +217 -0
- package/src/components/sections/MovementMap/index.astro +50 -0
- package/src/components/sections/MovementTicker/index.astro +43 -0
- package/src/components/sections/Pathways/Doors.astro +147 -0
- package/src/components/sections/Pathways/Head.astro +37 -0
- package/src/components/sections/Pathways/index.astro +27 -0
- package/src/components/sections/Pathways/pathways.ts +100 -0
- package/src/components/sections/Pathways/quiz.astro +286 -0
- package/src/components/sections/Pathways/tabs.astro +271 -0
- package/src/components/sections/PdfEmbed/card.astro +99 -0
- package/src/components/sections/PdfEmbed/index.astro +26 -0
- package/src/components/sections/PdfEmbed/medium.astro +7 -0
- package/src/components/sections/PdfEmbed/tall.astro +7 -0
- package/src/components/sections/Petition/coverage.astro +14 -0
- package/src/components/sections/Petition/image.astro +14 -0
- package/src/components/sections/Petition/index.astro +14 -0
- package/src/components/sections/Petition/map.astro +14 -0
- package/src/components/sections/PlatformPlanks/anchors.ts +66 -0
- package/src/components/sections/PlatformPlanks/columns.astro +171 -0
- package/src/components/sections/PlatformPlanks/index.astro +56 -0
- package/src/components/sections/PlatformPlanks/longread.astro +250 -0
- package/src/components/sections/PollBlock/index.astro +30 -0
- package/src/components/sections/Portfolio/featured.astro +343 -0
- package/src/components/sections/Portfolio/index.astro +571 -0
- package/src/components/sections/Portfolio/strip.astro +173 -0
- package/src/components/sections/Portfolio/work.ts +100 -0
- package/src/components/sections/Progress/MatchClock.astro +166 -0
- package/src/components/sections/Progress/compact.astro +150 -0
- package/src/components/sections/Progress/index.astro +32 -0
- package/src/components/sections/Progress/match.astro +126 -0
- package/src/components/sections/Progress/ring.astro +179 -0
- package/src/components/sections/QuickDonate/DonateCard.astro +149 -0
- package/src/components/sections/QuickDonate/Picker.astro +95 -0
- package/src/components/sections/QuickDonate/band.astro +164 -0
- package/src/components/sections/QuickDonate/bar.astro +208 -0
- package/src/components/sections/QuickDonate/donate.ts +126 -0
- package/src/components/sections/QuickDonate/hero.astro +143 -0
- package/src/components/sections/QuickDonate/index.astro +73 -0
- package/src/components/sections/QuickDonate/jar.astro +225 -0
- package/src/components/sections/QuickDonate/ladder.astro +248 -0
- package/src/components/sections/QuoteStrip/carousel.astro +340 -0
- package/src/components/sections/QuoteStrip/index.astro +58 -0
- package/src/components/sections/QuoteStrip/logos.astro +226 -0
- package/src/components/sections/QuoteStrip/quotes.ts +61 -0
- package/src/components/sections/QuoteStrip/single.astro +92 -0
- package/src/components/sections/QuoteStrip/ticker.astro +319 -0
- package/src/components/sections/QuoteStrip/wall.astro +113 -0
- package/src/components/sections/Resources/band.astro +93 -0
- package/src/components/sections/Resources/featured.astro +239 -0
- package/src/components/sections/Resources/index.astro +225 -0
- package/src/components/sections/Resources/resources.ts +92 -0
- package/src/components/sections/SampleBallot/Position.astro +39 -0
- package/src/components/sections/SampleBallot/ballot.ts +92 -0
- package/src/components/sections/SampleBallot/cards.astro +299 -0
- package/src/components/sections/SampleBallot/index.astro +326 -0
- package/src/components/sections/SampleBallot/measures.astro +172 -0
- package/src/components/sections/Scorecard/grades.astro +141 -0
- package/src/components/sections/Scorecard/index.astro +294 -0
- package/src/components/sections/Scorecard/promise.astro +167 -0
- package/src/components/sections/Scorecard/votes.ts +83 -0
- package/src/components/sections/SectionHead.astro +31 -0
- package/src/components/sections/Services/Button.astro +24 -0
- package/src/components/sections/Services/Check.astro +12 -0
- package/src/components/sections/Services/Includes.astro +42 -0
- package/src/components/sections/Services/compare.astro +328 -0
- package/src/components/sections/Services/index.astro +187 -0
- package/src/components/sections/Services/list.astro +181 -0
- package/src/components/sections/Services/services.ts +80 -0
- package/src/components/sections/ShareKit/Actions.astro +71 -0
- package/src/components/sections/ShareKit/Buttons.astro +62 -0
- package/src/components/sections/ShareKit/compact.astro +123 -0
- package/src/components/sections/ShareKit/index.astro +160 -0
- package/src/components/sections/ShareKit/share.ts +60 -0
- package/src/components/sections/ShareKit/strip.astro +173 -0
- package/src/components/sections/SignupCapture/band.astro +7 -0
- package/src/components/sections/SignupCapture/index.astro +103 -0
- package/src/components/sections/SignupCapture/inline.astro +7 -0
- package/src/components/sections/SocialFeed/index.astro +34 -0
- package/src/components/sections/StatCards/feature.astro +143 -0
- package/src/components/sections/StatCards/index.astro +51 -0
- package/src/components/sections/StatCards/ribbon.astro +113 -0
- package/src/components/sections/StatCards/wall.astro +177 -0
- package/src/components/sections/Store/featured.astro +14 -0
- package/src/components/sections/Store/index.astro +14 -0
- package/src/components/sections/Stories/Status.astro +42 -0
- package/src/components/sections/Stories/StoryCards.astro +171 -0
- package/src/components/sections/Stories/cards.astro +42 -0
- package/src/components/sections/Stories/index.astro +238 -0
- package/src/components/sections/Stories/stories.ts +97 -0
- package/src/components/sections/Stories/timeline.astro +135 -0
- package/src/components/sections/TeamGrid/board.astro +280 -0
- package/src/components/sections/TeamGrid/index.astro +71 -0
- package/src/components/sections/TeamGrid/list.astro +306 -0
- package/src/components/sections/TeamGrid/people.ts +178 -0
- package/src/components/sections/VideoEmbed/index.astro +52 -0
- package/src/components/sections/VideoEmbed/landscape.astro +7 -0
- package/src/components/sections/VideoEmbed/portrait.astro +7 -0
- package/src/components/sections/VideoEmbed/square.astro +7 -0
- package/src/components/sections/VoterTools/DaysLeft.astro +33 -0
- package/src/components/sections/VoterTools/Lookup.astro +43 -0
- package/src/components/sections/VoterTools/dates.astro +153 -0
- package/src/components/sections/VoterTools/index.astro +231 -0
- package/src/components/sections/VoterTools/polling.astro +144 -0
- package/src/components/sections/VoterTools/voter-tools.ts +71 -0
- package/src/components/sections/index.ts +373 -0
- package/src/components/sections/modules/Headed.astro +39 -0
- package/src/components/sections/modules/ModuleFrame.astro +181 -0
- package/src/components/store/CollectionPage.astro +40 -0
- package/src/components/store/ProductCard.astro +32 -0
- package/src/components/store/ProductGrid.astro +16 -0
- package/src/components/store/ProductPage.astro +146 -0
- package/src/components/store/StorePage.astro +137 -0
- package/src/components/store/StoreScript.astro +572 -0
- package/src/components/volunteer/ShiftLive.astro +90 -0
- package/src/components/volunteer/detail.astro +247 -0
- package/src/components/volunteer/list.astro +298 -0
- package/src/components/volunteer/props.ts +21 -0
- package/src/env.d.ts +22 -0
- package/src/index.d.ts +10 -0
- package/src/index.js +63 -0
- package/src/layouts/Site.astro +387 -0
- package/src/lib/academy.ts +146 -0
- package/src/lib/brand.ts +534 -0
- package/src/lib/chrome.ts +121 -0
- package/src/lib/donate-frame.ts +104 -0
- package/src/lib/entries.ts +190 -0
- package/src/lib/env.ts +34 -0
- package/src/lib/event-map-draw.ts +91 -0
- package/src/lib/events.ts +476 -0
- package/src/lib/fonts.ts +116 -0
- package/src/lib/footer.ts +24 -0
- package/src/lib/format.ts +160 -0
- package/src/lib/geo.ts +131 -0
- package/src/lib/live.ts +55 -0
- package/src/lib/menu.ts +48 -0
- package/src/lib/petition-text.ts +29 -0
- package/src/lib/reading.ts +17 -0
- package/src/lib/regions.ts +215 -0
- package/src/lib/section.ts +105 -0
- package/src/lib/shifts.ts +187 -0
- package/src/lib/store.ts +521 -0
- package/src/lib/stream-time.ts +20 -0
- package/src/lib/stream.ts +141 -0
- package/src/lib/visitor.ts +54 -0
- package/src/routes/catch-all.astro +435 -0
- package/src/routes/form.astro +30 -0
- package/src/routes/live.astro +49 -0
- package/src/routes/not-found.astro +18 -0
- package/src/routes/preview.astro +60 -0
- package/src/routes/setup.astro +62 -0
- package/src/routes/volunteer-shift.astro +61 -0
- package/src/styles/entries.css +784 -0
- package/src/styles/site.css +1910 -0
- package/src/styles/store.css +634 -0
- package/src/template.ts +182 -0
|
@@ -0,0 +1,572 @@
|
|
|
1
|
+
---
|
|
2
|
+
// The storefront's live parts, one script per store page, no framework:
|
|
3
|
+
//
|
|
4
|
+
// - The header's Cart link (`[data-bg-cart]`, Site.astro's `cart`): the lines
|
|
5
|
+
// this site keeps under `gg-cart:<org>` in localStorage, counted; hidden
|
|
6
|
+
// while there are none. Its address, and the island's Check out's, carry
|
|
7
|
+
// them to the hosted checkout in the fragment (`#cart=<base64url JSON>`,
|
|
8
|
+
// never the query, so the answers reach no server log), kept current as
|
|
9
|
+
// the cart changes so a new tab gets them too; with no lines it is the
|
|
10
|
+
// checkout's own address (the checkout keeps its own cart). Followed (a
|
|
11
|
+
// click, a modified click or a middle click), the link forgets them here
|
|
12
|
+
// in the same click: the checkout's cart holds them from then on.
|
|
13
|
+
// - The product page's island (`[data-bg-island]`, ProductPage.astro): on
|
|
14
|
+
// view it reads GET /products/live?ids=<the listing> (lib/live.ts: the
|
|
15
|
+
// same-origin path first-party, else the api's address with the
|
|
16
|
+
// publishable key; with neither, the island stays as built) and draws one
|
|
17
|
+
// of five states: in stock, few left, sold out (Add to cart says so and
|
|
18
|
+
// does nothing), the price moved since the build (the built price struck,
|
|
19
|
+
// the live one charged, and a sentence that says so), or the read failed
|
|
20
|
+
// (the built price "as of the last build", Add to cart says it cannot, and
|
|
21
|
+
// Try again reads again). It draws the questions the line asks: the
|
|
22
|
+
// once-per-order ones once, each per-unit one once per unit, so a quantity
|
|
23
|
+
// of 2 asks it twice. Add to cart puts the line in the cart in the hosted
|
|
24
|
+
// checkout's line shape (variant_id, product_slug, product_name,
|
|
25
|
+
// variant_name, unit_price_cents at the live price, quantity, photo_url,
|
|
26
|
+
// needs_shipping, and `answers` as { order, units } keyed by label, the
|
|
27
|
+
// way the checkout's own add does it: a second add of the same item adds
|
|
28
|
+
// its quantity and its units' answers to the line).
|
|
29
|
+
// - The gallery's thumbnails swap the large photo.
|
|
30
|
+
import { liveSource } from '../../lib/live'
|
|
31
|
+
import { AVAILABILITY_CHIPS, AVAILABILITY_WORDS } from '../../lib/store'
|
|
32
|
+
|
|
33
|
+
const bg = Astro.locals.backgold
|
|
34
|
+
const source = await liveSource(bg.setup === null && bg.firstParty, 'products')
|
|
35
|
+
const words = JSON.stringify({ words: AVAILABILITY_WORDS, chips: AVAILABILITY_CHIPS })
|
|
36
|
+
---
|
|
37
|
+
<script is:inline data-bg-store-api={source?.url ?? ''} data-bg-store-key={source?.key ?? undefined} data-bg-store-words={words}>
|
|
38
|
+
;(function (script) {
|
|
39
|
+
var api = script.getAttribute('data-bg-store-api')
|
|
40
|
+
var key = script.getAttribute('data-bg-store-key')
|
|
41
|
+
var vocab = JSON.parse(script.getAttribute('data-bg-store-words') || '{}')
|
|
42
|
+
var PREFIX = 'gg-cart:'
|
|
43
|
+
/** The checkout's bounds for a hand-over: lines, a line's quantity, the JSON's size, a name's length. */
|
|
44
|
+
var MOST_LINES = 50
|
|
45
|
+
var MOST_QUANTITY = 99
|
|
46
|
+
var MOST_BYTES = 64 * 1024
|
|
47
|
+
var MOST_TEXT = 255
|
|
48
|
+
|
|
49
|
+
// ── The cart ──
|
|
50
|
+
function readCart(org) {
|
|
51
|
+
try {
|
|
52
|
+
var raw = window.localStorage.getItem(PREFIX + org)
|
|
53
|
+
var lines = raw ? JSON.parse(raw) : []
|
|
54
|
+
return Array.isArray(lines) ? lines : []
|
|
55
|
+
} catch (e) {
|
|
56
|
+
return []
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function writeCart(org, lines) {
|
|
60
|
+
try {
|
|
61
|
+
if (lines.length === 0) window.localStorage.removeItem(PREFIX + org)
|
|
62
|
+
else window.localStorage.setItem(PREFIX + org, JSON.stringify(lines))
|
|
63
|
+
} catch (e) {}
|
|
64
|
+
document.dispatchEvent(new CustomEvent('bg-cart'))
|
|
65
|
+
}
|
|
66
|
+
function mergeAnswers(had, added) {
|
|
67
|
+
if (!had && !added) return undefined
|
|
68
|
+
var order = (added && added.order) || (had && had.order)
|
|
69
|
+
var units = ((had && had.units) || []).concat((added && added.units) || [])
|
|
70
|
+
var out = {}
|
|
71
|
+
if (order) out.order = order
|
|
72
|
+
if (units.length > 0) out.units = units
|
|
73
|
+
return out
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Adds the line, merged into the item's own line when it has one (the
|
|
77
|
+
* checkout keeps one line per item). False, and nothing written, when
|
|
78
|
+
* the cart would break the checkout's bounds for a hand-over: at most
|
|
79
|
+
* MOST_LINES lines, a quantity of at most MOST_QUANTITY per line, and
|
|
80
|
+
* at most MOST_BYTES of JSON.
|
|
81
|
+
*/
|
|
82
|
+
function addLine(org, line) {
|
|
83
|
+
var lines = readCart(org).slice()
|
|
84
|
+
var at = -1
|
|
85
|
+
for (var i = 0; i < lines.length; i++) if (lines[i] && lines[i].variant_id === line.variant_id) at = i
|
|
86
|
+
if (at >= 0) {
|
|
87
|
+
var merged = Object.assign({}, lines[at], { quantity: lines[at].quantity + line.quantity })
|
|
88
|
+
if (lines[at].answers || line.answers) merged.answers = mergeAnswers(lines[at].answers, line.answers)
|
|
89
|
+
if (merged.quantity > MOST_QUANTITY) return false
|
|
90
|
+
lines[at] = merged
|
|
91
|
+
} else {
|
|
92
|
+
if (lines.length >= MOST_LINES) return false
|
|
93
|
+
lines.push(line)
|
|
94
|
+
}
|
|
95
|
+
if (new TextEncoder().encode(JSON.stringify(lines)).length > MOST_BYTES) return false
|
|
96
|
+
writeCart(org, lines)
|
|
97
|
+
return true
|
|
98
|
+
}
|
|
99
|
+
function base64url(text) {
|
|
100
|
+
var bytes = new TextEncoder().encode(text)
|
|
101
|
+
var binary = ''
|
|
102
|
+
for (var i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i])
|
|
103
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
|
|
104
|
+
}
|
|
105
|
+
function count(org) {
|
|
106
|
+
return readCart(org).reduce(function (n, line) {
|
|
107
|
+
return n + (line && typeof line.quantity === 'number' ? line.quantity : 0)
|
|
108
|
+
}, 0)
|
|
109
|
+
}
|
|
110
|
+
/** The checkout's address with this site's lines in its fragment. */
|
|
111
|
+
function checkoutHref(link) {
|
|
112
|
+
var org = link.getAttribute('data-bg-cart')
|
|
113
|
+
var base = link.getAttribute('data-bg-checkout')
|
|
114
|
+
var lines = readCart(org)
|
|
115
|
+
return lines.length > 0 ? base + '#cart=' + base64url(JSON.stringify(lines)) : base
|
|
116
|
+
}
|
|
117
|
+
function drawCart() {
|
|
118
|
+
var links = document.querySelectorAll('[data-bg-cart]')
|
|
119
|
+
for (var i = 0; i < links.length; i++) {
|
|
120
|
+
var n = count(links[i].getAttribute('data-bg-cart'))
|
|
121
|
+
var shown = links[i].querySelector('[data-bg-cart-count]')
|
|
122
|
+
if (shown) shown.textContent = String(n)
|
|
123
|
+
links[i].setAttribute('aria-label', 'Cart, ' + n + (n === 1 ? ' item' : ' items'))
|
|
124
|
+
links[i].setAttribute('href', checkoutHref(links[i]))
|
|
125
|
+
links[i].hidden = n === 0
|
|
126
|
+
}
|
|
127
|
+
var cart = links[0]
|
|
128
|
+
var now = document.querySelectorAll('[data-bg-checkout-now]')
|
|
129
|
+
for (var j = 0; cart && j < now.length; j++) now[j].setAttribute('href', checkoutHref(cart))
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The hand-over: the followed link's address is set with the lines
|
|
133
|
+
* first, then the cart is forgotten here. The count is redrawn after the
|
|
134
|
+
* click, once the browser has taken the address.
|
|
135
|
+
*/
|
|
136
|
+
function handOver(event) {
|
|
137
|
+
var link = event.target instanceof Element ? event.target.closest('[data-bg-cart], [data-bg-checkout-now]') : null
|
|
138
|
+
var cart = document.querySelector('[data-bg-cart]')
|
|
139
|
+
if (!link || !cart) return
|
|
140
|
+
link.setAttribute('href', checkoutHref(cart))
|
|
141
|
+
try {
|
|
142
|
+
window.localStorage.removeItem(PREFIX + cart.getAttribute('data-bg-cart'))
|
|
143
|
+
} catch (e) {}
|
|
144
|
+
setTimeout(function () {
|
|
145
|
+
document.dispatchEvent(new CustomEvent('bg-cart'))
|
|
146
|
+
}, 0)
|
|
147
|
+
}
|
|
148
|
+
document.addEventListener('click', function (event) {
|
|
149
|
+
if (event.button === 0) handOver(event)
|
|
150
|
+
})
|
|
151
|
+
document.addEventListener('auxclick', function (event) {
|
|
152
|
+
if (event.button === 1) handOver(event)
|
|
153
|
+
})
|
|
154
|
+
document.addEventListener('bg-cart', drawCart)
|
|
155
|
+
window.addEventListener('storage', drawCart)
|
|
156
|
+
window.addEventListener('pageshow', drawCart)
|
|
157
|
+
drawCart()
|
|
158
|
+
|
|
159
|
+
// ── The gallery ──
|
|
160
|
+
var main = document.querySelector('[data-bg-gallery-main]')
|
|
161
|
+
var thumbs = document.querySelectorAll('[data-bg-gallery-thumb]')
|
|
162
|
+
for (var t = 0; t < thumbs.length; t++) {
|
|
163
|
+
thumbs[t].addEventListener('click', function (event) {
|
|
164
|
+
if (!main) return
|
|
165
|
+
event.preventDefault()
|
|
166
|
+
main.setAttribute('src', this.getAttribute('href'))
|
|
167
|
+
for (var u = 0; u < thumbs.length; u++) thumbs[u].removeAttribute('aria-current')
|
|
168
|
+
this.setAttribute('aria-current', 'true')
|
|
169
|
+
})
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// ── The island ──
|
|
173
|
+
var island = document.querySelector('[data-bg-island]')
|
|
174
|
+
var data = document.querySelector('[data-bg-island-product]')
|
|
175
|
+
var cartLink = document.querySelector('[data-bg-cart]')
|
|
176
|
+
if (!island || !data || !api || !cartLink) return
|
|
177
|
+
var product = JSON.parse(data.textContent || '{}')
|
|
178
|
+
var org = cartLink.getAttribute('data-bg-cart')
|
|
179
|
+
var form = island.querySelector('[data-bg-form]')
|
|
180
|
+
var priceEl = island.querySelector('[data-bg-price]')
|
|
181
|
+
var wasEl = island.querySelector('[data-bg-was]')
|
|
182
|
+
var lineEl = island.querySelector('[data-bg-line]')
|
|
183
|
+
var wordEl = island.querySelector('[data-bg-word]')
|
|
184
|
+
var qtyEl = island.querySelector('[data-bg-qty]')
|
|
185
|
+
var addEl = island.querySelector('[data-bg-add]')
|
|
186
|
+
var retryEl = island.querySelector('[data-bg-retry]')
|
|
187
|
+
var statusEl = island.querySelector('[data-bg-status]')
|
|
188
|
+
var questionsEl = island.querySelector('[data-bg-questions]')
|
|
189
|
+
var unitNote = island.querySelector('[data-bg-unit-note]')
|
|
190
|
+
var fallback = island.querySelector('[data-bg-fallback]')
|
|
191
|
+
var radios = island.querySelectorAll('[data-bg-item]')
|
|
192
|
+
var built = product.built_at ? new Date(product.built_at) : null
|
|
193
|
+
var builtDay = built && !isNaN(built.getTime()) ? built.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) : null
|
|
194
|
+
var live = null
|
|
195
|
+
var failed = false
|
|
196
|
+
|
|
197
|
+
if (fallback) fallback.hidden = true
|
|
198
|
+
form.hidden = false
|
|
199
|
+
var contribution = island.querySelector('[data-bg-contribution]')
|
|
200
|
+
if (contribution) addEl.parentNode.insertBefore(contribution, addEl)
|
|
201
|
+
for (var r = 0; r < radios.length; r++) radios[r].disabled = false
|
|
202
|
+
|
|
203
|
+
function money(cents) {
|
|
204
|
+
var whole = cents % 100 === 0
|
|
205
|
+
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: whole ? 0 : 2, maximumFractionDigits: whole ? 0 : 2 }).format(cents / 100)
|
|
206
|
+
}
|
|
207
|
+
function quantity() {
|
|
208
|
+
var n = Math.floor(Number(qtyEl.value))
|
|
209
|
+
return isFinite(n) && n >= 1 ? Math.min(n, 99) : 1
|
|
210
|
+
}
|
|
211
|
+
function selected() {
|
|
212
|
+
var id = null
|
|
213
|
+
for (var i = 0; i < radios.length; i++) if (radios[i].checked) id = radios[i].value
|
|
214
|
+
var items = product.items || []
|
|
215
|
+
for (var j = 0; j < items.length; j++) if (items[j].id === id) return items[j]
|
|
216
|
+
return items[0] || null
|
|
217
|
+
}
|
|
218
|
+
function liveItem(item) {
|
|
219
|
+
if (!live || !item) return null
|
|
220
|
+
for (var i = 0; i < live.items.length; i++) if (live.items[i].id === item.id) return live.items[i]
|
|
221
|
+
return null
|
|
222
|
+
}
|
|
223
|
+
function setWord(availability) {
|
|
224
|
+
if (!wordEl) return
|
|
225
|
+
wordEl.hidden = !availability
|
|
226
|
+
if (!availability) return
|
|
227
|
+
wordEl.textContent = vocab.words[availability] || ''
|
|
228
|
+
wordEl.className = 'chip' + (vocab.chips[availability] ? ' ' + vocab.chips[availability] : '')
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// The answers' keys, as the checkout keys them: each question's label, a
|
|
232
|
+
// repeated label numbered " (2)", " (3)" within its scope.
|
|
233
|
+
function keysOf(questions) {
|
|
234
|
+
var used = []
|
|
235
|
+
return questions.map(function (q, i) {
|
|
236
|
+
var raw = q.label || 'Question ' + (i + 1)
|
|
237
|
+
var k = raw
|
|
238
|
+
for (var n = 2; used.indexOf(k) >= 0; n++) k = raw + ' (' + n + ')'
|
|
239
|
+
used.push(k)
|
|
240
|
+
return k
|
|
241
|
+
})
|
|
242
|
+
}
|
|
243
|
+
function scoped(scope) {
|
|
244
|
+
return ((live && live.questions) || []).filter(function (q) {
|
|
245
|
+
return q.scope === scope
|
|
246
|
+
})
|
|
247
|
+
}
|
|
248
|
+
/** True when a list holds a question that takes an answer (a note takes none). */
|
|
249
|
+
function asks(questions) {
|
|
250
|
+
return questions.some(function (q) {
|
|
251
|
+
return q.type !== 'note'
|
|
252
|
+
})
|
|
253
|
+
}
|
|
254
|
+
/** The value a choice's "Other" carries until the typed text replaces it. */
|
|
255
|
+
var OTHER = '__other__'
|
|
256
|
+
function otherBox(name, keep) {
|
|
257
|
+
var other = document.createElement('input')
|
|
258
|
+
other.type = 'text'
|
|
259
|
+
other.name = name + '#other'
|
|
260
|
+
other.className = 'island-other'
|
|
261
|
+
other.setAttribute('aria-label', 'Other')
|
|
262
|
+
other.placeholder = 'Other'
|
|
263
|
+
if (typeof keep[name + '#other'] === 'string') other.value = keep[name + '#other']
|
|
264
|
+
return other
|
|
265
|
+
}
|
|
266
|
+
/** Shows a choice's "Other" text field while Other is chosen, and asks for it then. */
|
|
267
|
+
function syncOther(wrap) {
|
|
268
|
+
var other = wrap.querySelector('.island-other')
|
|
269
|
+
if (!other) return
|
|
270
|
+
var select = wrap.querySelector('select')
|
|
271
|
+
var tick = wrap.querySelector('input[type="checkbox"][value="' + OTHER + '"]')
|
|
272
|
+
var on = select ? select.value === OTHER : !!(tick && tick.checked)
|
|
273
|
+
other.hidden = !on
|
|
274
|
+
other.required = on
|
|
275
|
+
}
|
|
276
|
+
var AGREE = ['checkbox', 'sms_consent', 'email_consent']
|
|
277
|
+
var INPUTS = { text: 'text', email: 'email', phone: 'tel', date: 'date' }
|
|
278
|
+
function field(q, name, label, keep) {
|
|
279
|
+
var wrap = document.createElement('div')
|
|
280
|
+
wrap.className = 'island-field'
|
|
281
|
+
if (q.type === 'note') {
|
|
282
|
+
String(q.body || '')
|
|
283
|
+
.split(/\n{2,}/)
|
|
284
|
+
.map(function (para) {
|
|
285
|
+
return para.trim()
|
|
286
|
+
})
|
|
287
|
+
.filter(Boolean)
|
|
288
|
+
.forEach(function (para) {
|
|
289
|
+
var p = document.createElement('p')
|
|
290
|
+
p.className = 'island-note-text'
|
|
291
|
+
p.textContent = para
|
|
292
|
+
wrap.appendChild(p)
|
|
293
|
+
})
|
|
294
|
+
return wrap
|
|
295
|
+
}
|
|
296
|
+
var id = 'bg-q-' + name.replace(/[^a-z0-9]+/gi, '-')
|
|
297
|
+
if (AGREE.indexOf(q.type) >= 0) {
|
|
298
|
+
var box = document.createElement('label')
|
|
299
|
+
box.className = 'island-agree'
|
|
300
|
+
var input = document.createElement('input')
|
|
301
|
+
input.type = 'checkbox'
|
|
302
|
+
input.name = name
|
|
303
|
+
input.checked = keep[name] === 'Yes'
|
|
304
|
+
box.appendChild(input)
|
|
305
|
+
box.appendChild(document.createTextNode(' ' + label))
|
|
306
|
+
wrap.appendChild(box)
|
|
307
|
+
} else if (q.type === 'checkboxes') {
|
|
308
|
+
var set = document.createElement('fieldset')
|
|
309
|
+
var legend = document.createElement('legend')
|
|
310
|
+
legend.textContent = label + (q.required ? ' *' : '')
|
|
311
|
+
set.appendChild(legend)
|
|
312
|
+
;(q.options || []).forEach(function (option) {
|
|
313
|
+
var row = document.createElement('label')
|
|
314
|
+
row.className = 'island-agree'
|
|
315
|
+
var tick = document.createElement('input')
|
|
316
|
+
tick.type = 'checkbox'
|
|
317
|
+
tick.setAttribute('data-multi', '')
|
|
318
|
+
tick.name = name
|
|
319
|
+
tick.value = option
|
|
320
|
+
tick.checked = Array.isArray(keep[name]) && keep[name].indexOf(option) >= 0
|
|
321
|
+
row.appendChild(tick)
|
|
322
|
+
row.appendChild(document.createTextNode(' ' + option))
|
|
323
|
+
set.appendChild(row)
|
|
324
|
+
})
|
|
325
|
+
if (q.allow_other) {
|
|
326
|
+
var otherRow = document.createElement('label')
|
|
327
|
+
otherRow.className = 'island-agree'
|
|
328
|
+
var otherTick = document.createElement('input')
|
|
329
|
+
otherTick.type = 'checkbox'
|
|
330
|
+
otherTick.setAttribute('data-multi', '')
|
|
331
|
+
otherTick.name = name
|
|
332
|
+
otherTick.value = OTHER
|
|
333
|
+
otherTick.checked = Array.isArray(keep[name]) && keep[name].indexOf(OTHER) >= 0
|
|
334
|
+
otherRow.appendChild(otherTick)
|
|
335
|
+
otherRow.appendChild(document.createTextNode(' Other'))
|
|
336
|
+
set.appendChild(otherRow)
|
|
337
|
+
set.appendChild(otherBox(name, keep))
|
|
338
|
+
}
|
|
339
|
+
wrap.appendChild(set)
|
|
340
|
+
} else {
|
|
341
|
+
var lab = document.createElement('label')
|
|
342
|
+
lab.setAttribute('for', id)
|
|
343
|
+
lab.textContent = label + (q.required ? ' *' : '')
|
|
344
|
+
wrap.appendChild(lab)
|
|
345
|
+
var control
|
|
346
|
+
if (q.type === 'textarea') {
|
|
347
|
+
control = document.createElement('textarea')
|
|
348
|
+
control.rows = 3
|
|
349
|
+
} else if (q.type === 'select' || q.type === 'radio') {
|
|
350
|
+
control = document.createElement('select')
|
|
351
|
+
var blank = document.createElement('option')
|
|
352
|
+
blank.value = ''
|
|
353
|
+
blank.textContent = 'Choose one'
|
|
354
|
+
control.appendChild(blank)
|
|
355
|
+
;(q.options || []).forEach(function (option) {
|
|
356
|
+
var o = document.createElement('option')
|
|
357
|
+
o.value = option
|
|
358
|
+
o.textContent = option
|
|
359
|
+
control.appendChild(o)
|
|
360
|
+
})
|
|
361
|
+
if (q.allow_other) {
|
|
362
|
+
var o2 = document.createElement('option')
|
|
363
|
+
o2.value = OTHER
|
|
364
|
+
o2.textContent = 'Other'
|
|
365
|
+
control.appendChild(o2)
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
control = document.createElement('input')
|
|
369
|
+
control.type = INPUTS[q.type] || 'text'
|
|
370
|
+
}
|
|
371
|
+
control.id = id
|
|
372
|
+
control.name = name
|
|
373
|
+
if (q.required) control.required = true
|
|
374
|
+
if (q.placeholder && control.tagName !== 'SELECT') control.placeholder = q.placeholder
|
|
375
|
+
if (typeof keep[name] === 'string') control.value = keep[name]
|
|
376
|
+
wrap.appendChild(control)
|
|
377
|
+
if (q.allow_other && control.tagName === 'SELECT') wrap.appendChild(otherBox(name, keep))
|
|
378
|
+
}
|
|
379
|
+
if (q.help) {
|
|
380
|
+
var help = document.createElement('p')
|
|
381
|
+
help.className = 'island-help'
|
|
382
|
+
help.textContent = q.help
|
|
383
|
+
wrap.appendChild(help)
|
|
384
|
+
}
|
|
385
|
+
syncOther(wrap)
|
|
386
|
+
return wrap
|
|
387
|
+
}
|
|
388
|
+
/** What every drawn field holds now, by name, so a redraw keeps it. */
|
|
389
|
+
function held() {
|
|
390
|
+
var out = {}
|
|
391
|
+
var controls = questionsEl.querySelectorAll('input, select, textarea')
|
|
392
|
+
for (var i = 0; i < controls.length; i++) {
|
|
393
|
+
var c = controls[i]
|
|
394
|
+
if (c.type === 'checkbox' && c.hasAttribute('data-multi')) {
|
|
395
|
+
out[c.name] = out[c.name] || []
|
|
396
|
+
if (c.checked) out[c.name].push(c.value)
|
|
397
|
+
} else if (c.type === 'checkbox') {
|
|
398
|
+
out[c.name] = c.checked ? 'Yes' : 'No'
|
|
399
|
+
} else {
|
|
400
|
+
out[c.name] = c.value
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return out
|
|
404
|
+
}
|
|
405
|
+
function drawQuestions() {
|
|
406
|
+
var keep = held()
|
|
407
|
+
questionsEl.textContent = ''
|
|
408
|
+
var order = scoped('order')
|
|
409
|
+
var unit = scoped('unit')
|
|
410
|
+
var n = quantity()
|
|
411
|
+
order.forEach(function (q, i) {
|
|
412
|
+
questionsEl.appendChild(field(q, 'o:' + i, q.label, keep))
|
|
413
|
+
})
|
|
414
|
+
for (var u = 0; u < (asks(unit) ? n : 0); u++) {
|
|
415
|
+
unit.forEach(function (q, i) {
|
|
416
|
+
questionsEl.appendChild(field(q, 'u:' + u + ':' + i, n > 1 ? q.label + ' · unit ' + (u + 1) : q.label, keep))
|
|
417
|
+
})
|
|
418
|
+
}
|
|
419
|
+
unitNote.hidden = !(asks(unit) && n > 1)
|
|
420
|
+
}
|
|
421
|
+
questionsEl.addEventListener('change', function (event) {
|
|
422
|
+
var wrap = event.target instanceof Element ? event.target.closest('.island-field') : null
|
|
423
|
+
if (wrap) syncOther(wrap)
|
|
424
|
+
})
|
|
425
|
+
function answersOf(prefix, questions) {
|
|
426
|
+
var keys = keysOf(questions)
|
|
427
|
+
var out = {}
|
|
428
|
+
var now = held()
|
|
429
|
+
questions.forEach(function (q, i) {
|
|
430
|
+
if (q.type === 'note') return
|
|
431
|
+
var value = now[prefix + i]
|
|
432
|
+
if (value === undefined) return
|
|
433
|
+
// "Other" is answered by the text typed beside it.
|
|
434
|
+
var typed = String(now[prefix + i + '#other'] || '').trim()
|
|
435
|
+
if (value === OTHER) value = typed
|
|
436
|
+
if (Array.isArray(value)) {
|
|
437
|
+
value = value
|
|
438
|
+
.map(function (v) {
|
|
439
|
+
return v === OTHER ? typed : v
|
|
440
|
+
})
|
|
441
|
+
.filter(Boolean)
|
|
442
|
+
if (value.length > 0) out[keys[i]] = value
|
|
443
|
+
} else if (value !== '') out[keys[i]] = value
|
|
444
|
+
})
|
|
445
|
+
return out
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function draw() {
|
|
449
|
+
var item = selected()
|
|
450
|
+
if (!item) return
|
|
451
|
+
var n = quantity()
|
|
452
|
+
var now = liveItem(item)
|
|
453
|
+
statusEl.textContent = ''
|
|
454
|
+
retryEl.hidden = !failed
|
|
455
|
+
if (failed || !now) {
|
|
456
|
+
priceEl.textContent = money(item.price_cents * n)
|
|
457
|
+
wasEl.hidden = true
|
|
458
|
+
lineEl.textContent = (builtDay ? 'As of the last build, ' + builtDay + '. ' : 'As of the last build. ') + (failed ? 'We could not reach the shop for the live price.' : 'This item is not on sale right now.')
|
|
459
|
+
setWord(null)
|
|
460
|
+
addEl.disabled = true
|
|
461
|
+
addEl.textContent = failed ? 'Can’t add to cart right now' : 'Not available'
|
|
462
|
+
return
|
|
463
|
+
}
|
|
464
|
+
priceEl.textContent = money(now.price_cents * n)
|
|
465
|
+
var moved = now.price_cents !== item.price_cents
|
|
466
|
+
// The live compare-at price when the read carries one, else the build's.
|
|
467
|
+
var compareAt = typeof now.compare_at_cents === 'number' || now.compare_at_cents === null ? now.compare_at_cents : item.compare_at_cents
|
|
468
|
+
var compare = !moved && compareAt !== null && compareAt > now.price_cents ? compareAt : null
|
|
469
|
+
wasEl.hidden = !(moved || compare !== null)
|
|
470
|
+
wasEl.textContent = moved ? money(item.price_cents * n) : compare !== null ? money(compare * n) : ''
|
|
471
|
+
var each = n > 1 ? n + ' × ' + money(now.price_cents) + ' · ' : ''
|
|
472
|
+
lineEl.textContent = moved
|
|
473
|
+
? each + 'Price changed since this page was built. The live price is the one you pay.'
|
|
474
|
+
: n > 1
|
|
475
|
+
? each + 'updated just now.'
|
|
476
|
+
: 'Price updated just now.'
|
|
477
|
+
setWord(now.availability)
|
|
478
|
+
var out = now.availability === 'out'
|
|
479
|
+
addEl.disabled = out
|
|
480
|
+
addEl.textContent = out ? 'Sold out' : n > 1 ? 'Add ' + n + ' to cart' : 'Add to cart'
|
|
481
|
+
if (out) lineEl.textContent += ' Nothing to add right now.'
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function read() {
|
|
485
|
+
failed = false
|
|
486
|
+
addEl.disabled = true
|
|
487
|
+
addEl.textContent = 'Checking the price…'
|
|
488
|
+
retryEl.hidden = true
|
|
489
|
+
var headers = { Accept: 'application/json' }
|
|
490
|
+
if (key) headers.Authorization = 'Bearer ' + key
|
|
491
|
+
var controller = typeof AbortController === 'function' ? new AbortController() : null
|
|
492
|
+
var timer = controller ? setTimeout(function () { controller.abort() }, 8000) : null
|
|
493
|
+
fetch(api + '?ids=' + encodeURIComponent(product.id), { headers: headers, signal: controller ? controller.signal : undefined })
|
|
494
|
+
.then(function (res) {
|
|
495
|
+
return res.ok ? res.json() : null
|
|
496
|
+
})
|
|
497
|
+
.then(function (body) {
|
|
498
|
+
var found = body && Array.isArray(body.products) ? body.products.filter(function (p) { return p && p.id === product.id })[0] : null
|
|
499
|
+
if (found && Array.isArray(found.items)) live = { items: found.items, questions: Array.isArray(found.questions) ? found.questions : [] }
|
|
500
|
+
else if (body && Array.isArray(body.products)) live = { items: [], questions: [] }
|
|
501
|
+
else failed = true
|
|
502
|
+
})
|
|
503
|
+
.catch(function () {
|
|
504
|
+
failed = true
|
|
505
|
+
})
|
|
506
|
+
.then(function () {
|
|
507
|
+
if (timer) clearTimeout(timer)
|
|
508
|
+
drawQuestions()
|
|
509
|
+
draw()
|
|
510
|
+
})
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
qtyEl.addEventListener('input', function () {
|
|
514
|
+
drawQuestions()
|
|
515
|
+
draw()
|
|
516
|
+
})
|
|
517
|
+
island.addEventListener('click', function (event) {
|
|
518
|
+
var step = event.target instanceof Element ? event.target.closest('[data-bg-step]') : null
|
|
519
|
+
if (!step) return
|
|
520
|
+
qtyEl.value = String(Math.max(1, Math.min(99, quantity() + Number(step.getAttribute('data-bg-step')))))
|
|
521
|
+
drawQuestions()
|
|
522
|
+
draw()
|
|
523
|
+
})
|
|
524
|
+
for (var k = 0; k < radios.length; k++) radios[k].addEventListener('change', draw)
|
|
525
|
+
retryEl.addEventListener('click', read)
|
|
526
|
+
form.addEventListener('submit', function (event) {
|
|
527
|
+
event.preventDefault()
|
|
528
|
+
var item = selected()
|
|
529
|
+
var now = liveItem(item)
|
|
530
|
+
if (!item || !now || now.availability === 'out') return
|
|
531
|
+
if (!form.reportValidity()) return
|
|
532
|
+
var n = quantity()
|
|
533
|
+
var order = scoped('order')
|
|
534
|
+
var unit = scoped('unit')
|
|
535
|
+
var line = {
|
|
536
|
+
variant_id: item.id,
|
|
537
|
+
product_slug: product.slug,
|
|
538
|
+
product_name: String(product.name).slice(0, MOST_TEXT),
|
|
539
|
+
variant_name: String(item.name || product.name).slice(0, MOST_TEXT),
|
|
540
|
+
unit_price_cents: now.price_cents,
|
|
541
|
+
quantity: n,
|
|
542
|
+
photo_url: product.photo_url,
|
|
543
|
+
needs_shipping: product.ships && product.ship_to !== 'store',
|
|
544
|
+
}
|
|
545
|
+
if (asks(order) || asks(unit)) {
|
|
546
|
+
var answers = {}
|
|
547
|
+
if (asks(order)) answers.order = answersOf('o:', order)
|
|
548
|
+
if (asks(unit)) {
|
|
549
|
+
answers.units = []
|
|
550
|
+
for (var u = 0; u < n; u++) answers.units.push(answersOf('u:' + u + ':', unit))
|
|
551
|
+
}
|
|
552
|
+
line.answers = answers
|
|
553
|
+
}
|
|
554
|
+
if (!addLine(org, line)) {
|
|
555
|
+
statusEl.textContent = 'That is more than one cart can hold. Check out what is in it, then come back for more.'
|
|
556
|
+
return
|
|
557
|
+
}
|
|
558
|
+
questionsEl.textContent = ''
|
|
559
|
+
qtyEl.value = '1'
|
|
560
|
+
drawQuestions()
|
|
561
|
+
draw()
|
|
562
|
+
statusEl.textContent = n > 1 ? n + ' added to your cart. ' : 'Added to your cart. '
|
|
563
|
+
var go = document.createElement('a')
|
|
564
|
+
go.className = 'text-link'
|
|
565
|
+
go.setAttribute('data-bg-checkout-now', '')
|
|
566
|
+
go.href = checkoutHref(cartLink)
|
|
567
|
+
go.textContent = 'Check out'
|
|
568
|
+
statusEl.appendChild(go)
|
|
569
|
+
})
|
|
570
|
+
read()
|
|
571
|
+
})(document.currentScript)
|
|
572
|
+
</script>
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
---
|
|
2
|
+
// The volunteer pages' live counts: a shift's "6 of 10 filled · 4 spots
|
|
3
|
+
// left" and its state are never only the cached page's. Each volunteer
|
|
4
|
+
// layout places this once; it writes the script the first time on a page,
|
|
5
|
+
// when the site has a live route to ask (src/lib/live.ts, the shifts' feed:
|
|
6
|
+
// first-party the same-origin path with no key, with a site key the api's
|
|
7
|
+
// address and the publishable key). After the document is parsed the script
|
|
8
|
+
// reads `GET /shifts/live?ids=` for every `data-bg-shift` slot on the page
|
|
9
|
+
// (50 at a time) and, for each shift the answer names: writes its counts
|
|
10
|
+
// into its slots as words (lib/shifts.ts countWords' words), redraws its
|
|
11
|
+
// dots (`data-bg-shift-dots`, one per spot up to 24, `data-taken` on each
|
|
12
|
+
// one taken), and moves its buttons (`data-bg-shift-action`) to its state's
|
|
13
|
+
// words, quiet when there is nothing to sign up for. A shift the answer
|
|
14
|
+
// leaves out, or a failed read, keeps what the page was built with, as it
|
|
15
|
+
// does with scripts off.
|
|
16
|
+
import { liveSource } from '../../lib/live'
|
|
17
|
+
import { MOST_DOTS, SHIFT_ACTION_LABELS, firstShiftScript } from '../../lib/shifts'
|
|
18
|
+
|
|
19
|
+
const bg = Astro.locals.backgold
|
|
20
|
+
const source = firstShiftScript(Astro.request) ? await liveSource(bg.setup === null && bg.firstParty, 'shifts') : null
|
|
21
|
+
---
|
|
22
|
+
{source && (
|
|
23
|
+
<script is:inline data-bg-shifts-api={source.url} data-bg-shifts-key={source.key ?? undefined} data-bg-shifts-labels={SHIFT_ACTION_LABELS} data-bg-shifts-most={String(MOST_DOTS)}>
|
|
24
|
+
(function (script) {
|
|
25
|
+
var api = script.getAttribute('data-bg-shifts-api')
|
|
26
|
+
var key = script.getAttribute('data-bg-shifts-key')
|
|
27
|
+
var labels = JSON.parse(script.getAttribute('data-bg-shifts-labels') || '{}')
|
|
28
|
+
var most = Number(script.getAttribute('data-bg-shifts-most'))
|
|
29
|
+
var words = function (going, capacity) {
|
|
30
|
+
if (capacity > 0) {
|
|
31
|
+
var left = capacity - going
|
|
32
|
+
return going + ' of ' + capacity + ' filled · ' + (left > 0 ? left + (left === 1 ? ' spot left' : ' spots left') : 'Full')
|
|
33
|
+
}
|
|
34
|
+
return going > 0 ? going + ' signed up' : ''
|
|
35
|
+
}
|
|
36
|
+
var each = function (selector, id, fn) {
|
|
37
|
+
var found = document.querySelectorAll(selector)
|
|
38
|
+
for (var i = 0; i < found.length; i++) if (found[i].getAttribute(selector.slice(1, -1)) === id) fn(found[i])
|
|
39
|
+
}
|
|
40
|
+
var fill = function () {
|
|
41
|
+
var slots = document.querySelectorAll('[data-bg-shift]')
|
|
42
|
+
var ids = []
|
|
43
|
+
for (var i = 0; i < slots.length; i++) {
|
|
44
|
+
var id = slots[i].getAttribute('data-bg-shift')
|
|
45
|
+
if (id && ids.indexOf(id) < 0) ids.push(id)
|
|
46
|
+
}
|
|
47
|
+
for (var start = 0; start < ids.length; start += 50) {
|
|
48
|
+
var headers = { Accept: 'application/json' }
|
|
49
|
+
if (key) headers.Authorization = 'Bearer ' + key
|
|
50
|
+
fetch(api + '?ids=' + ids.slice(start, start + 50).map(encodeURIComponent).join(','), { headers: headers })
|
|
51
|
+
.then(function (res) {
|
|
52
|
+
return res.ok ? res.json() : null
|
|
53
|
+
})
|
|
54
|
+
.then(function (body) {
|
|
55
|
+
var live = body && Array.isArray(body.shifts) ? body.shifts : []
|
|
56
|
+
for (var l = 0; l < live.length; l++) {
|
|
57
|
+
var one = live[l]
|
|
58
|
+
if (!one || typeof one.id !== 'string' || typeof one.signed_up !== 'number' || !isFinite(one.signed_up)) continue
|
|
59
|
+
var going = Math.max(0, one.signed_up)
|
|
60
|
+
each('[data-bg-shift]', one.id, function (slot) {
|
|
61
|
+
var capacity = Number(slot.getAttribute('data-capacity'))
|
|
62
|
+
slot.setAttribute('data-going', String(going))
|
|
63
|
+
slot.textContent = words(going, capacity)
|
|
64
|
+
each('[data-bg-shift-dots]', one.id, function (dots) {
|
|
65
|
+
dots.textContent = ''
|
|
66
|
+
if (!(capacity > 0) || capacity > most) return
|
|
67
|
+
for (var d = 0; d < capacity; d++) {
|
|
68
|
+
var dot = document.createElement('i')
|
|
69
|
+
if (d < going) dot.setAttribute('data-taken', '')
|
|
70
|
+
dots.appendChild(dot)
|
|
71
|
+
}
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
if (!labels[one.state]) continue
|
|
75
|
+
each('[data-bg-shift-action]', one.id, function (button) {
|
|
76
|
+
var label = button.querySelector('[data-bg-shift-label]')
|
|
77
|
+
if (label) label.textContent = labels[one.state]
|
|
78
|
+
button.classList.toggle('button-quiet', one.state === 'full' || one.state === 'closed')
|
|
79
|
+
button.setAttribute('data-state', one.state)
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.catch(function () {})
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', fill)
|
|
87
|
+
else fill()
|
|
88
|
+
})(document.currentScript)
|
|
89
|
+
</script>
|
|
90
|
+
)}
|