@semics-tech/emdash-events 0.0.0-stage → 1.0.0-rc.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 +83 -2
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +416 -0
- package/dist/manifest.json +425 -0
- package/dist/plugin.d.mts +11 -0
- package/dist/plugin.mjs +7 -0
- package/dist/ts/astro/lib/call.d.ts +54 -0
- package/dist/ts/astro/lib/call.js +121 -0
- package/dist/ts/integration/index.d.ts +48 -0
- package/dist/ts/integration/index.js +76 -0
- package/dist/ts/ui/index.d.ts +6 -0
- package/dist/ts/ui/index.js +46 -0
- package/emdash-plugin.jsonc +222 -0
- package/package.json +78 -3
- package/src/astro/AddToCalendar.astro +47 -0
- package/src/astro/AttendeeList.astro +157 -0
- package/src/astro/BookingBlock.astro +30 -0
- package/src/astro/BookingPage.astro +230 -0
- package/src/astro/BookingStatus.astro +112 -0
- package/src/astro/BookingWidget.astro +776 -0
- package/src/astro/CheckIn.astro +186 -0
- package/src/astro/DoorSignPage.astro +62 -0
- package/src/astro/EventCard.astro +81 -0
- package/src/astro/EventList.astro +83 -0
- package/src/astro/OptOutPage.astro +58 -0
- package/src/astro/PaymentReturn.astro +96 -0
- package/src/astro/TicketPass.astro +95 -0
- package/src/astro/checkout-endpoint.ts +11 -0
- package/src/astro/index.js +18 -0
- package/src/astro/lib/attendees.ts +170 -0
- package/src/astro/lib/call.ts +185 -0
- package/src/astro/lib/checkout-form.ts +271 -0
- package/src/astro/lib/list.ts +99 -0
- package/src/astro/lib/opt-out.ts +49 -0
- package/src/astro/lib/pass.ts +35 -0
- package/src/astro/lib/payment.ts +73 -0
- package/src/astro/lib/picker.ts +245 -0
- package/src/astro/lib/promo.ts +162 -0
- package/src/astro/lib/questions.ts +107 -0
- package/src/astro/lib/receipt.ts +132 -0
- package/src/astro/lib/waitlist.ts +72 -0
- package/src/astro/lib/widget.ts +299 -0
- package/src/astro/promo-endpoint.ts +11 -0
- package/src/astro/virtual.d.ts +16 -0
- package/src/astro/waitlist-endpoint.ts +12 -0
- package/src/client/booking.ts +190 -0
- package/src/client/checkin.ts +260 -0
- package/src/client/payment.ts +76 -0
- package/src/client/picker.ts +143 -0
- package/src/client/receipt.ts +75 -0
- package/src/i18n/ar.ts +251 -0
- package/src/i18n/en.ts +255 -0
- package/src/i18n/index.ts +121 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Progressive enhancement for `<BookingWidget>` (EU-FR-04, EU-NFR-01..03). The form works without
|
|
3
|
+
* this script (a plain POST to `/_events/checkout`); with it the buyer gets:
|
|
4
|
+
*
|
|
5
|
+
* - a fresh idempotency key when the page loads (so a cached page never shares one), kept for every
|
|
6
|
+
* resubmit from that page so a retry finds the same booking;
|
|
7
|
+
* - errors next to their fields before anything is sent, with focus on the first one;
|
|
8
|
+
* - a live order total;
|
|
9
|
+
* - a guard against double submits, and the submit button enabled when Turnstile needs scripts.
|
|
10
|
+
*
|
|
11
|
+
* Self-contained (no imports) and budgeted at 15 KB gzipped (`tests/ui/client-bundle.build.test.ts`).
|
|
12
|
+
* Text only ever goes in through `textContent`. `validateBooking` mirrors the server's
|
|
13
|
+
* `validateCheckoutFields` (`src/astro/lib/widget.ts`); a test keeps them identical.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export type BookingField = "occurrence" | "promo" | "tickets" | "name" | "email" | `q:${string}`;
|
|
17
|
+
|
|
18
|
+
export interface BookingValues {
|
|
19
|
+
name: string;
|
|
20
|
+
email: string;
|
|
21
|
+
lines: number;
|
|
22
|
+
needsOccurrence: boolean;
|
|
23
|
+
occurrenceId: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// `promo` is only ever set by the server (a refused code); listing it clears that error on a resubmit.
|
|
27
|
+
const FIELDS: readonly BookingField[] = ["occurrence", "promo", "tickets", "name", "email"];
|
|
28
|
+
|
|
29
|
+
/** A custom question's control (Events Pro, T080): its field and whether it has an answer. */
|
|
30
|
+
export interface QuestionState {
|
|
31
|
+
field: BookingField;
|
|
32
|
+
required: boolean;
|
|
33
|
+
filled: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The required questions without an answer, in form order. Pro's reserve checks them again. */
|
|
37
|
+
export function missingAnswers(questions: readonly QuestionState[]): BookingField[] {
|
|
38
|
+
return questions.filter((q) => q.required && !q.filled).map((q) => q.field);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function questionStates(form: HTMLFormElement): QuestionState[] {
|
|
42
|
+
return Array.from(form.querySelectorAll<HTMLInputElement | HTMLSelectElement>("[data-question]"), (el) => ({
|
|
43
|
+
field: (el.dataset.field ?? "") as BookingField,
|
|
44
|
+
required: el.required,
|
|
45
|
+
filled: el instanceof HTMLInputElement && el.type === "checkbox" ? el.checked : el.value.trim() !== "",
|
|
46
|
+
}));
|
|
47
|
+
}
|
|
48
|
+
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
49
|
+
|
|
50
|
+
/** 32 random bytes as 43 unpadded base64url characters. */
|
|
51
|
+
export function freshKey(): string {
|
|
52
|
+
const bytes = crypto.getRandomValues(new Uint8Array(32));
|
|
53
|
+
let binary = "";
|
|
54
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
55
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function validateBooking(v: BookingValues): BookingField[] {
|
|
59
|
+
const name = v.name.trim();
|
|
60
|
+
const email = v.email.trim();
|
|
61
|
+
const bad: BookingField[] = [];
|
|
62
|
+
if (v.needsOccurrence && !v.occurrenceId) bad.push("occurrence");
|
|
63
|
+
if (v.lines < 1) bad.push("tickets");
|
|
64
|
+
if (name.length < 1 || name.length > 200) bad.push("name");
|
|
65
|
+
if (email.length > 254 || !EMAIL.test(email)) bad.push("email");
|
|
66
|
+
return bad;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function orderTotalMinor(lines: ReadonlyArray<{ qty: number; priceMinor: number }>): number {
|
|
70
|
+
let total = 0;
|
|
71
|
+
for (const line of lines) if (line.qty > 0) total += line.qty * line.priceMinor;
|
|
72
|
+
return total;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** `free` is the page's word for a zero total (the widget renders it as `data-free`, T102). */
|
|
76
|
+
export function formatMinor(minor: number, currency: string, locale: string, free: string): string {
|
|
77
|
+
if (minor === 0) return free;
|
|
78
|
+
try {
|
|
79
|
+
return new Intl.NumberFormat(locale, { style: "currency", currency }).format(minor / 100);
|
|
80
|
+
} catch {
|
|
81
|
+
return `${(minor / 100).toFixed(2)} ${currency}`;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const qtyOf = (input: HTMLInputElement) => {
|
|
86
|
+
const n = Number.parseInt(input.value, 10);
|
|
87
|
+
return Number.isFinite(n) ? n : 0;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
function fieldValue(form: HTMLFormElement, name: string): string {
|
|
91
|
+
const el = form.elements.namedItem(name);
|
|
92
|
+
return el instanceof HTMLInputElement || el instanceof HTMLSelectElement ? el.value : "";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function showErrors(form: HTMLFormElement, bad: readonly BookingField[]): void {
|
|
96
|
+
for (const field of [...FIELDS, ...questionStates(form).map((q) => q.field)]) {
|
|
97
|
+
const control = form.querySelector<HTMLElement>(`[data-field="${field}"]`);
|
|
98
|
+
const message = form.querySelector<HTMLElement>(`[data-error-for="${field}"]`);
|
|
99
|
+
if (!control || !message) continue;
|
|
100
|
+
const invalid = bad.includes(field);
|
|
101
|
+
message.textContent = invalid ? (message.dataset.message ?? "") : "";
|
|
102
|
+
if (invalid) control.setAttribute("aria-invalid", "true");
|
|
103
|
+
else control.removeAttribute("aria-invalid");
|
|
104
|
+
}
|
|
105
|
+
const first = bad[0] && form.querySelector<HTMLElement>(`[data-field="${bad[0]}"]`);
|
|
106
|
+
if (!first) return;
|
|
107
|
+
// A fieldset: its first live input, or the day picker's focusable day (T130).
|
|
108
|
+
const live = 'input:not([disabled]), button[tabindex="0"]'; // i18n-ignore
|
|
109
|
+
const target = first.matches("fieldset") ? first.querySelector<HTMLElement>(live) : first;
|
|
110
|
+
target?.focus();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function formAlert(form: HTMLFormElement, text: string): void {
|
|
114
|
+
const alert = form.querySelector<HTMLElement>("[data-form-alert]");
|
|
115
|
+
if (!alert) return;
|
|
116
|
+
alert.textContent = text;
|
|
117
|
+
alert.hidden = text === "";
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Wire one booking form. Safe to call twice. */
|
|
121
|
+
export function enhance(form: HTMLFormElement): void {
|
|
122
|
+
if (form.dataset.enhanced) return;
|
|
123
|
+
form.dataset.enhanced = "1";
|
|
124
|
+
form.noValidate = true;
|
|
125
|
+
|
|
126
|
+
const key = form.elements.namedItem("idempotencyKey");
|
|
127
|
+
if (key instanceof HTMLInputElement) key.value = freshKey();
|
|
128
|
+
|
|
129
|
+
// The promo code's Apply button sits inside the widget but belongs to its own form.
|
|
130
|
+
const button = form.querySelector<HTMLButtonElement>('button[type="submit"]:not([data-promo-apply])');
|
|
131
|
+
if (button) button.disabled = false;
|
|
132
|
+
|
|
133
|
+
const qtyInputs = Array.from(form.querySelectorAll<HTMLInputElement>('input[name^="qty:"]'));
|
|
134
|
+
const total = form.querySelector<HTMLElement>("[data-total]");
|
|
135
|
+
const totalValue = total?.querySelector<HTMLElement>("[data-total-value]");
|
|
136
|
+
const updateTotal = () => {
|
|
137
|
+
if (!total || !totalValue) return;
|
|
138
|
+
const minor = orderTotalMinor(
|
|
139
|
+
qtyInputs.map((i) => ({ qty: qtyOf(i), priceMinor: Number(i.dataset.priceMinor) || 0 })),
|
|
140
|
+
);
|
|
141
|
+
totalValue.textContent = formatMinor(
|
|
142
|
+
minor,
|
|
143
|
+
form.dataset.currency ?? "GBP",
|
|
144
|
+
form.dataset.locale ?? "en-GB",
|
|
145
|
+
form.dataset.free ?? "0",
|
|
146
|
+
);
|
|
147
|
+
total.hidden = false;
|
|
148
|
+
};
|
|
149
|
+
for (const input of qtyInputs) input.addEventListener("input", updateTotal);
|
|
150
|
+
updateTotal();
|
|
151
|
+
|
|
152
|
+
form.addEventListener("submit", (event) => {
|
|
153
|
+
if (form.dataset.submitting) {
|
|
154
|
+
event.preventDefault();
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
const bad = [
|
|
158
|
+
...validateBooking({
|
|
159
|
+
name: fieldValue(form, "name"),
|
|
160
|
+
email: fieldValue(form, "email"),
|
|
161
|
+
lines: qtyInputs.filter((i) => qtyOf(i) > 0).length,
|
|
162
|
+
needsOccurrence: fieldValue(form, "needsOccurrence") === "1",
|
|
163
|
+
occurrenceId: fieldValue(form, "occurrenceId"),
|
|
164
|
+
}),
|
|
165
|
+
...missingAnswers(questionStates(form)),
|
|
166
|
+
];
|
|
167
|
+
const needsCheck = form.dataset.turnstile === "on" && fieldValue(form, "cf-turnstile-response") === "";
|
|
168
|
+
if (bad.length > 0 || needsCheck) {
|
|
169
|
+
event.preventDefault();
|
|
170
|
+
formAlert(form, needsCheck && bad.length === 0 ? (form.dataset.turnstileMessage ?? "") : "");
|
|
171
|
+
showErrors(form, bad);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
formAlert(form, "");
|
|
175
|
+
showErrors(form, []);
|
|
176
|
+
form.dataset.submitting = "1";
|
|
177
|
+
button?.setAttribute("aria-busy", "true");
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Back/forward cache: the same page (and key) may submit again; let it.
|
|
181
|
+
window.addEventListener("pageshow", (event) => {
|
|
182
|
+
if (!event.persisted) return;
|
|
183
|
+
delete form.dataset.submitting;
|
|
184
|
+
button?.removeAttribute("aria-busy");
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (typeof document !== "undefined") {
|
|
189
|
+
for (const form of document.querySelectorAll<HTMLFormElement>("form[data-semics-booking]")) enhance(form);
|
|
190
|
+
}
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The staff check-in screen's script (`/_events/check-in`, EV-FR-11, EV-FR-12, T082). It posts the
|
|
3
|
+
* typed or scanned code to the core's private `checkin/code` route (Events Pro) with the staff
|
|
4
|
+
* member's admin session (same-origin cookie and EmDash's `X-EmDash-Request` header), and shows the
|
|
5
|
+
* answer as one large banner: checked in, already checked in, or refused with the reason; a pending
|
|
6
|
+
* waiver gets the door signing link, and only a link to this site's `/_events/sign` is ever shown.
|
|
7
|
+
* The camera scans QR passes with the browser's `BarcodeDetector` where there is one; elsewhere the
|
|
8
|
+
* code field is the way in. Free has no such route: the screen says it needs Events Pro.
|
|
9
|
+
*
|
|
10
|
+
* Self-contained (no imports), no HTML sinks.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export type Tone = "ok" | "warn" | "error";
|
|
14
|
+
|
|
15
|
+
export interface CheckInView {
|
|
16
|
+
tone: Tone;
|
|
17
|
+
title: string;
|
|
18
|
+
message: string;
|
|
19
|
+
/** WAIVER_PENDING: this site's door signing page for the attendee. */
|
|
20
|
+
signLink?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const MAX_CODE = 200;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The screen's messages in the page's language, from the form's `data-messages` (the page renders them,
|
|
27
|
+
* T102): keys without the `checkin.` prefix, plus `status.<booking status>`. A missing key shows the key.
|
|
28
|
+
*/
|
|
29
|
+
export type CheckInMessages = Record<string, string>;
|
|
30
|
+
|
|
31
|
+
const say = (m: CheckInMessages, key: string, vars: Record<string, string> = {}) =>
|
|
32
|
+
(m[key] ?? key).replace(/\{(\w+)\}/g, (whole, name: string) => vars[name] ?? whole);
|
|
33
|
+
|
|
34
|
+
/** A typed or scanned code, as the route takes it. */
|
|
35
|
+
export function codeFromScan(raw: string): string {
|
|
36
|
+
return raw.trim().slice(0, MAX_CODE);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The link, when it is this site's door signing page; anything else is dropped. */
|
|
40
|
+
export function safeSignLink(link: unknown, origin: string): string | undefined {
|
|
41
|
+
if (typeof link !== "string") return undefined;
|
|
42
|
+
try {
|
|
43
|
+
const url = new URL(link);
|
|
44
|
+
return url.origin === origin && url.pathname === "/_events/sign" ? url.href : undefined;
|
|
45
|
+
} catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function timeOf(iso: unknown, lang: string | undefined): string {
|
|
51
|
+
const at = typeof iso === "string" ? new Date(iso) : null;
|
|
52
|
+
if (!at || Number.isNaN(at.getTime())) return "";
|
|
53
|
+
try {
|
|
54
|
+
return at.toLocaleTimeString(lang || [], { hour: "2-digit", minute: "2-digit" });
|
|
55
|
+
} catch {
|
|
56
|
+
return at.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type Answer = {
|
|
61
|
+
ok?: unknown;
|
|
62
|
+
checkedIn?: unknown;
|
|
63
|
+
reason?: unknown;
|
|
64
|
+
name?: unknown;
|
|
65
|
+
checkedInAt?: unknown;
|
|
66
|
+
bookingStatus?: unknown;
|
|
67
|
+
signLink?: unknown;
|
|
68
|
+
error?: { code?: unknown };
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** What the screen shows for the route's HTTP status and (unwrapped or enveloped) answer. */
|
|
72
|
+
export function checkInView(
|
|
73
|
+
status: number,
|
|
74
|
+
raw: unknown,
|
|
75
|
+
origin: string,
|
|
76
|
+
m: CheckInMessages,
|
|
77
|
+
lang?: string,
|
|
78
|
+
): CheckInView {
|
|
79
|
+
const view = (tone: Tone, key: string, message = `${key}.message`, vars?: Record<string, string>): CheckInView => ({
|
|
80
|
+
tone,
|
|
81
|
+
title: say(m, `${key}.title`, vars),
|
|
82
|
+
message: say(m, message, vars),
|
|
83
|
+
});
|
|
84
|
+
if (status === 401 || status === 403) return view("error", "notSignedIn");
|
|
85
|
+
if (status === 404) return view("error", "unavailable");
|
|
86
|
+
let body = raw as Answer | null;
|
|
87
|
+
if (body && typeof body === "object" && "success" in body && "data" in body) {
|
|
88
|
+
body = (body as { data: unknown }).data as Answer | null;
|
|
89
|
+
}
|
|
90
|
+
if (status < 200 || status > 299 || !body || typeof body !== "object") return view("error", "failed");
|
|
91
|
+
const name = typeof body.name === "string" && body.name ? body.name : say(m, "attendee");
|
|
92
|
+
if (body.ok !== true) {
|
|
93
|
+
const code = String(body.error?.code ?? "");
|
|
94
|
+
if (code.startsWith("LICENCE_")) return view("error", "licence");
|
|
95
|
+
if (code === "BAD_REQUEST") return view("error", "enterCode");
|
|
96
|
+
return view("error", "failed");
|
|
97
|
+
}
|
|
98
|
+
if (body.checkedIn === true) {
|
|
99
|
+
const time = timeOf(body.checkedInAt, lang);
|
|
100
|
+
return view("ok", "checkedIn", time ? "checkedIn.at" : "checkedIn.done", { name, time });
|
|
101
|
+
}
|
|
102
|
+
switch (body.reason) {
|
|
103
|
+
case "ALREADY_CHECKED_IN": {
|
|
104
|
+
const time = timeOf(body.checkedInAt, lang);
|
|
105
|
+
return view("warn", "already", time ? "already.messageAt" : "already.message", { name, time });
|
|
106
|
+
}
|
|
107
|
+
case "NOT_FOUND":
|
|
108
|
+
return view("error", "notFound");
|
|
109
|
+
case "BOOKING_NOT_CONFIRMED": {
|
|
110
|
+
const code = String(body.bookingStatus ?? "unknown");
|
|
111
|
+
return view("error", "notConfirmed", undefined, { status: m[`status.${code}`] ?? code });
|
|
112
|
+
}
|
|
113
|
+
case "WAIVER_PENDING": {
|
|
114
|
+
const signLink = safeSignLink(body.signLink, origin);
|
|
115
|
+
return {
|
|
116
|
+
...view("error", "waiver", signLink ? "waiver.messageLink" : "waiver.message", { name }),
|
|
117
|
+
...(signLink ? { signLink } : {}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
default:
|
|
121
|
+
return view("error", "invalid");
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
type Detector = { detect(source: HTMLVideoElement): Promise<Array<{ rawValue: string }>> };
|
|
126
|
+
type DetectorClass = {
|
|
127
|
+
new (options: { formats: string[] }): Detector;
|
|
128
|
+
getSupportedFormats?(): Promise<string[]>;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/** Wires the page's form, result region and camera button. */
|
|
132
|
+
export function startCheckIn(doc: Document = document): boolean {
|
|
133
|
+
const form = doc.querySelector<HTMLFormElement>("form[data-semics-checkin]");
|
|
134
|
+
const input = form?.querySelector<HTMLInputElement>('input[name="code"]');
|
|
135
|
+
const out = doc.querySelector<HTMLElement>("[data-semics-checkin-result]");
|
|
136
|
+
const api = form?.dataset.api;
|
|
137
|
+
if (!form || !input || !out || !api) return false;
|
|
138
|
+
const win = doc.defaultView as (Window & { BarcodeDetector?: DetectorClass }) | null;
|
|
139
|
+
let m: CheckInMessages = {};
|
|
140
|
+
try {
|
|
141
|
+
m = JSON.parse(form.dataset.messages ?? "{}") as CheckInMessages;
|
|
142
|
+
} catch {
|
|
143
|
+
// No messages: the screen shows their keys.
|
|
144
|
+
}
|
|
145
|
+
const lang = doc.documentElement.lang || undefined;
|
|
146
|
+
let busy = false;
|
|
147
|
+
|
|
148
|
+
const show = (view: CheckInView) => {
|
|
149
|
+
out.dataset.tone = view.tone;
|
|
150
|
+
const title = doc.createElement("p");
|
|
151
|
+
title.className = "semics-checkin-title";
|
|
152
|
+
title.textContent = view.title;
|
|
153
|
+
const message = doc.createElement("p");
|
|
154
|
+
message.textContent = view.message;
|
|
155
|
+
out.replaceChildren(title, message);
|
|
156
|
+
if (view.signLink) {
|
|
157
|
+
const link = doc.createElement("a");
|
|
158
|
+
link.href = view.signLink;
|
|
159
|
+
link.target = "_blank";
|
|
160
|
+
link.rel = "noopener noreferrer";
|
|
161
|
+
link.className = "semics-checkin-link";
|
|
162
|
+
link.textContent = say(m, "signLink");
|
|
163
|
+
out.append(link);
|
|
164
|
+
}
|
|
165
|
+
out.hidden = false;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const submit = async (raw: string) => {
|
|
169
|
+
const code = codeFromScan(raw);
|
|
170
|
+
if (busy) return;
|
|
171
|
+
if (!code) {
|
|
172
|
+
show({ tone: "error", title: say(m, "enterCode.title"), message: say(m, "enterCode.message") });
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
busy = true;
|
|
176
|
+
const eventId = form.querySelector<HTMLSelectElement>('select[name="eventId"]')?.value ?? "";
|
|
177
|
+
let status = 0;
|
|
178
|
+
let body: unknown = null;
|
|
179
|
+
try {
|
|
180
|
+
const res = await fetch(api, {
|
|
181
|
+
method: "POST",
|
|
182
|
+
credentials: "same-origin",
|
|
183
|
+
headers: { "content-type": "application/json", "X-EmDash-Request": "1" },
|
|
184
|
+
body: JSON.stringify(eventId ? { code, eventId } : { code }),
|
|
185
|
+
});
|
|
186
|
+
status = res.status;
|
|
187
|
+
body = await res.json().catch(() => null);
|
|
188
|
+
} catch {
|
|
189
|
+
// Offline or blocked: shown as "Something went wrong".
|
|
190
|
+
}
|
|
191
|
+
show(checkInView(status, body, doc.location.origin, m, lang));
|
|
192
|
+
busy = false;
|
|
193
|
+
input.value = "";
|
|
194
|
+
input.focus();
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
form.addEventListener("submit", (event) => {
|
|
198
|
+
event.preventDefault();
|
|
199
|
+
void submit(input.value);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
// The camera, where the browser can read QR codes itself.
|
|
203
|
+
const button = doc.querySelector<HTMLButtonElement>("[data-semics-checkin-camera]");
|
|
204
|
+
const video = doc.querySelector<HTMLVideoElement>("[data-semics-checkin-video]");
|
|
205
|
+
const note = doc.querySelector<HTMLElement>("[data-semics-checkin-nocamera]");
|
|
206
|
+
const Barcode = win?.BarcodeDetector;
|
|
207
|
+
if (!button || !video || !Barcode || !win?.navigator.mediaDevices?.getUserMedia) {
|
|
208
|
+
if (note) note.hidden = false;
|
|
209
|
+
return true;
|
|
210
|
+
}
|
|
211
|
+
let stream: MediaStream | null = null;
|
|
212
|
+
let last = "";
|
|
213
|
+
let lastAt = 0;
|
|
214
|
+
const stop = () => {
|
|
215
|
+
for (const track of stream?.getTracks() ?? []) track.stop();
|
|
216
|
+
stream = null;
|
|
217
|
+
video.hidden = true;
|
|
218
|
+
button.textContent = say(m, "camera");
|
|
219
|
+
};
|
|
220
|
+
const tick = async (detector: Detector) => {
|
|
221
|
+
if (!stream) return;
|
|
222
|
+
try {
|
|
223
|
+
const [hit] = await detector.detect(video);
|
|
224
|
+
const value = hit ? codeFromScan(hit.rawValue) : "";
|
|
225
|
+
// The same pass held in front of the camera is read once every few seconds, not every frame.
|
|
226
|
+
if (value && !busy && (value !== last || Date.now() - lastAt > 4000)) {
|
|
227
|
+
last = value;
|
|
228
|
+
lastAt = Date.now();
|
|
229
|
+
await submit(value);
|
|
230
|
+
}
|
|
231
|
+
} catch {
|
|
232
|
+
// A frame that is not ready yet.
|
|
233
|
+
}
|
|
234
|
+
win.setTimeout(() => void tick(detector), 250);
|
|
235
|
+
};
|
|
236
|
+
void Promise.resolve(Barcode.getSupportedFormats?.() ?? ["qr_code"]).then((formats) => {
|
|
237
|
+
if (!formats.includes("qr_code")) {
|
|
238
|
+
if (note) note.hidden = false;
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
button.hidden = false;
|
|
242
|
+
button.addEventListener("click", async () => {
|
|
243
|
+
if (stream) return stop();
|
|
244
|
+
try {
|
|
245
|
+
stream = await win.navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" }, audio: false });
|
|
246
|
+
} catch {
|
|
247
|
+
show({ tone: "error", title: say(m, "cameraFailed.title"), message: say(m, "cameraFailed.message") });
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
video.srcObject = stream;
|
|
251
|
+
video.hidden = false;
|
|
252
|
+
await video.play().catch(() => undefined);
|
|
253
|
+
button.textContent = say(m, "stopCamera");
|
|
254
|
+
void tick(new Barcode({ formats: ["qr_code"] }));
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
return true;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
if (typeof document !== "undefined") startCheckIn();
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Polls the booking page while a payment waits for the provider's webhook (EU-FR-09). The access
|
|
3
|
+
* token is an HttpOnly cookie, so the script cannot call the core itself: it re-reads the same
|
|
4
|
+
* page (the cookie goes with it) and looks at the payment region's `data-state`. Polite: growing
|
|
5
|
+
* delays, nothing fetched while the tab is hidden, and it gives up after about 80 seconds; the page
|
|
6
|
+
* then says the buyer will get an email. Without JavaScript the page has a "Check again" link.
|
|
7
|
+
*
|
|
8
|
+
* Self-contained (no imports), no HTML sinks.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Waits before each re-read, in order: 3, 5, 8, 13, 21 and 30 seconds (80 s in all). */
|
|
12
|
+
export const POLL_DELAYS_MS = [3000, 5000, 8000, 13000, 21000, 30000];
|
|
13
|
+
|
|
14
|
+
export interface PollDeps {
|
|
15
|
+
/** The region's state now (`data-state`), or `null` when the page could not be read. */
|
|
16
|
+
fetchState(): Promise<string | null>;
|
|
17
|
+
wait(ms: number): Promise<void>;
|
|
18
|
+
isHidden(): boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type PollOutcome = { result: "changed"; state: string } | { result: "gave_up" };
|
|
22
|
+
|
|
23
|
+
/** Re-reads until the state is no longer `pending`, the schedule runs out, or a read fails. */
|
|
24
|
+
export async function pollPayment(deps: PollDeps): Promise<PollOutcome> {
|
|
25
|
+
for (const delay of POLL_DELAYS_MS) {
|
|
26
|
+
await deps.wait(delay);
|
|
27
|
+
// A hidden tab asks nothing; the round is spent.
|
|
28
|
+
if (deps.isHidden()) continue;
|
|
29
|
+
const state = await deps.fetchState();
|
|
30
|
+
if (state === null) return { result: "gave_up" };
|
|
31
|
+
if (state !== "pending") return { result: "changed", state };
|
|
32
|
+
}
|
|
33
|
+
return { result: "gave_up" };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function readState(doc: Document): Promise<string | null> {
|
|
37
|
+
try {
|
|
38
|
+
const res = await fetch(doc.location.href, {
|
|
39
|
+
headers: { accept: "text/html" },
|
|
40
|
+
credentials: "same-origin",
|
|
41
|
+
cache: "no-store",
|
|
42
|
+
});
|
|
43
|
+
if (!res.ok) return null;
|
|
44
|
+
const next = new DOMParser().parseFromString(await res.text(), "text/html");
|
|
45
|
+
return next.querySelector("[data-semics-payment]")?.getAttribute("data-state") ?? null;
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Wires the pending region on the page. Returns true when it started polling. */
|
|
52
|
+
export function startPaymentPolling(doc: Document = document): boolean {
|
|
53
|
+
const region = doc.querySelector<HTMLElement>("[data-semics-payment][data-state='pending']");
|
|
54
|
+
if (!region) return false;
|
|
55
|
+
const live = region.querySelector<HTMLElement>("[data-semics-payment-live]");
|
|
56
|
+
const slow = region.querySelector<HTMLElement>("[data-semics-payment-slow]");
|
|
57
|
+
const checking = region.querySelector<HTMLElement>("[data-semics-payment-checking]");
|
|
58
|
+
if (checking) checking.hidden = false;
|
|
59
|
+
void pollPayment({
|
|
60
|
+
fetchState: () => readState(doc),
|
|
61
|
+
wait: (ms) => new Promise((resolve) => setTimeout(resolve, ms)),
|
|
62
|
+
isHidden: () => doc.hidden,
|
|
63
|
+
}).then((outcome) => {
|
|
64
|
+
if (checking) checking.hidden = true;
|
|
65
|
+
if (outcome.result === "gave_up") {
|
|
66
|
+
if (slow) slow.hidden = false;
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
// The state moved on: say so in the live region, then show the page afresh (tickets, passes).
|
|
70
|
+
if (live) live.textContent = live.dataset.message ?? "";
|
|
71
|
+
setTimeout(() => doc.location.reload(), 1500);
|
|
72
|
+
});
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (typeof document !== "undefined") startPaymentPolling();
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The booking form's day-then-time picker (T130, EV-FR-17). `<BookingWidget>` renders the month
|
|
3
|
+
* calendar (`[data-picker]`, hidden) beside a plain `<select name="occurrenceId">` grouped by day
|
|
4
|
+
* (`[data-picker-fallback]`), so the form works without this script. With it, the calendar replaces the
|
|
5
|
+
* select: a day button (`data-day`) shows that day's times (`[data-times]`, a radio group of buttons,
|
|
6
|
+
* full ones `aria-disabled`), and choosing a time sets the select, which is what the form posts.
|
|
7
|
+
*
|
|
8
|
+
* Keyboard: roving tabindex over the day buttons (arrows by a day, up and down by a week, Home/End; to
|
|
9
|
+
* the next day that has slots), and over a day's times (arrows, wrapping, skipping full ones). Text
|
|
10
|
+
* only comes from the page's markup; this script writes none. Self-contained (no imports).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const DELTAS: Record<string, number> = { ArrowRight: 1, ArrowLeft: -1, ArrowDown: 7, ArrowUp: -7 };
|
|
14
|
+
|
|
15
|
+
/** The day (of `days`, ascending) a key moves to from `current`, or `null` to stay. */
|
|
16
|
+
export function stepDay(days: readonly number[], current: number, key: string, rtl: boolean): number | null {
|
|
17
|
+
// i18n-ignore
|
|
18
|
+
if (key === "Home") return days[0] ?? null;
|
|
19
|
+
// i18n-ignore
|
|
20
|
+
if (key === "End") return days[days.length - 1] ?? null;
|
|
21
|
+
let delta = DELTAS[key];
|
|
22
|
+
if (delta === undefined) return null;
|
|
23
|
+
if (rtl && Math.abs(delta) === 1) delta = -delta;
|
|
24
|
+
const target = current + delta;
|
|
25
|
+
const found = delta > 0 ? days.find((d) => d >= target) : [...days].reverse().find((d) => d <= target);
|
|
26
|
+
return found ?? null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The time (index into `open`) a key moves to from `current`, wrapping and skipping full ones. */
|
|
30
|
+
export function stepRadio(open: readonly boolean[], current: number, key: string, rtl: boolean): number | null {
|
|
31
|
+
let delta = DELTAS[key];
|
|
32
|
+
if (delta === undefined) return null;
|
|
33
|
+
delta = Math.sign(delta);
|
|
34
|
+
if (rtl && (key === "ArrowLeft" || key === "ArrowRight")) delta = -delta;
|
|
35
|
+
const n = open.length;
|
|
36
|
+
for (let i = 1; i <= n; i++) {
|
|
37
|
+
const at = (((current + delta * i) % n) + n) % n;
|
|
38
|
+
if (open[at] && at !== current) return at;
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const isRtl = (el: Element) => getComputedStyle(el).direction === "rtl";
|
|
44
|
+
|
|
45
|
+
/** Wire one picker. Safe to call twice. */
|
|
46
|
+
export function enhancePicker(picker: HTMLElement): void {
|
|
47
|
+
if (picker.dataset.enhanced) return;
|
|
48
|
+
const form = picker.closest("form");
|
|
49
|
+
const select = form?.querySelector<HTMLSelectElement>('select[name="occurrenceId"]');
|
|
50
|
+
if (!form || !select) return;
|
|
51
|
+
picker.dataset.enhanced = "1";
|
|
52
|
+
const fallback = form.querySelector<HTMLElement>("[data-picker-fallback]");
|
|
53
|
+
if (fallback) fallback.hidden = true;
|
|
54
|
+
select.required = false;
|
|
55
|
+
picker.hidden = false;
|
|
56
|
+
|
|
57
|
+
const dayButtons = Array.from(picker.querySelectorAll<HTMLButtonElement>("button[data-day]"));
|
|
58
|
+
const groups = Array.from(picker.querySelectorAll<HTMLElement>("[data-times]"));
|
|
59
|
+
const hint = picker.querySelector<HTMLElement>("[data-picker-hint]");
|
|
60
|
+
const radios = (group: HTMLElement) => Array.from(group.querySelectorAll<HTMLButtonElement>('[role="radio"]'));
|
|
61
|
+
const isOpen = (b: HTMLElement) => b.getAttribute("aria-disabled") !== "true";
|
|
62
|
+
|
|
63
|
+
const setSelect = (value: string) => {
|
|
64
|
+
if (select.value === value) return;
|
|
65
|
+
select.value = value;
|
|
66
|
+
select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const chooseTime = (button: HTMLButtonElement, focus: boolean) => {
|
|
70
|
+
if (!isOpen(button)) return;
|
|
71
|
+
for (const group of groups) {
|
|
72
|
+
for (const r of radios(group)) {
|
|
73
|
+
const on = r === button;
|
|
74
|
+
r.setAttribute("aria-checked", on ? "true" : "false");
|
|
75
|
+
if (group.contains(button)) r.tabIndex = on ? 0 : -1;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
if (focus) button.focus();
|
|
79
|
+
setSelect(button.dataset.occurrence ?? "");
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const showDay = (button: HTMLButtonElement, focus: boolean) => {
|
|
83
|
+
const key = button.dataset.day ?? "";
|
|
84
|
+
for (const b of dayButtons) {
|
|
85
|
+
b.setAttribute("aria-pressed", b === button ? "true" : "false");
|
|
86
|
+
b.tabIndex = b === button ? 0 : -1;
|
|
87
|
+
}
|
|
88
|
+
if (focus) button.focus();
|
|
89
|
+
let keep = false;
|
|
90
|
+
for (const group of groups) {
|
|
91
|
+
const mine = group.dataset.times === key;
|
|
92
|
+
group.hidden = !mine;
|
|
93
|
+
if (mine) keep = radios(group).some((r) => r.dataset.occurrence === select.value);
|
|
94
|
+
}
|
|
95
|
+
if (hint) hint.hidden = true;
|
|
96
|
+
if (!keep) {
|
|
97
|
+
for (const group of groups) for (const r of radios(group)) r.setAttribute("aria-checked", "false");
|
|
98
|
+
setSelect("");
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
for (const button of dayButtons) {
|
|
103
|
+
button.addEventListener("click", () => showDay(button, false));
|
|
104
|
+
button.addEventListener("keydown", (event) => {
|
|
105
|
+
const days = dayButtons.map((b) => Number(b.dataset.n));
|
|
106
|
+
const to = stepDay(days, Number(button.dataset.n), event.key, isRtl(picker));
|
|
107
|
+
if (to === null) return;
|
|
108
|
+
event.preventDefault();
|
|
109
|
+
const target = dayButtons.find((b) => Number(b.dataset.n) === to);
|
|
110
|
+
if (target) {
|
|
111
|
+
for (const b of dayButtons) b.tabIndex = b === target ? 0 : -1;
|
|
112
|
+
target.focus();
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
for (const group of groups) {
|
|
117
|
+
const list = radios(group);
|
|
118
|
+
for (const [i, radio] of list.entries()) {
|
|
119
|
+
radio.addEventListener("click", () => chooseTime(radio, false));
|
|
120
|
+
radio.addEventListener("keydown", (event) => {
|
|
121
|
+
const to = stepRadio(list.map(isOpen), i, event.key, isRtl(picker));
|
|
122
|
+
if (to === null) return;
|
|
123
|
+
event.preventDefault();
|
|
124
|
+
const target = list[to];
|
|
125
|
+
if (target) chooseTime(target, true);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// A value the browser kept (back/forward, a failed submit): show its day and time.
|
|
131
|
+
const kept =
|
|
132
|
+
select.value && picker.querySelector<HTMLButtonElement>(`[data-occurrence="${CSS.escape(select.value)}"]`);
|
|
133
|
+
const keptDay =
|
|
134
|
+
kept && dayButtons.find((b) => b.dataset.day === kept.closest<HTMLElement>("[data-times]")?.dataset.times);
|
|
135
|
+
if (kept && keptDay) {
|
|
136
|
+
showDay(keptDay, false);
|
|
137
|
+
chooseTime(kept, false);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (typeof document !== "undefined") {
|
|
142
|
+
for (const picker of document.querySelectorAll<HTMLElement>("[data-picker]")) enhancePicker(picker);
|
|
143
|
+
}
|