@vectoriox/iox-ui 5.99.0 → 5.101.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/actions-core/index.d.ts +7 -4
- package/commerce-core/index.d.ts +2 -0
- package/composite-core/index.d.ts +238 -2
- package/design-core/index.d.ts +334 -1
- package/fesm2022/vectoriox-iox-ui-actions-core.mjs +16 -7
- package/fesm2022/vectoriox-iox-ui-actions-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs +3 -0
- package/fesm2022/vectoriox-iox-ui-commerce-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-composite-core.mjs +1005 -256
- package/fesm2022/vectoriox-iox-ui-composite-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-design-core.mjs +416 -1
- package/fesm2022/vectoriox-iox-ui-design-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-forms-core.mjs +3 -24
- package/fesm2022/vectoriox-iox-ui-forms-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs +23 -13
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-identity-core.mjs +300 -22
- package/fesm2022/vectoriox-iox-ui-identity-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui.mjs +7 -61
- package/fesm2022/vectoriox-iox-ui.mjs.map +1 -1
- package/forms-core/index.d.ts +2 -23
- package/generation-core/index.d.ts +3 -1
- package/identity-core/index.d.ts +190 -14
- package/index.d.ts +0 -12
- package/package.json +1 -1
- package/src/actions-core/action.model.ts +1 -1
- package/src/actions-core/element-actions.ts +16 -6
- package/src/commerce-core/site-checkout.ts +3 -0
- package/src/composite-core/account-runtime.ts +461 -0
- package/src/composite-core/account.ts +309 -0
- package/src/composite-core/public-api.ts +2 -0
- package/src/composite-core/recipe-parts.ts +48 -0
- package/src/composite-core/shop.ts +6 -26
- package/src/design-core/field-classes.ts +65 -0
- package/src/design-core/flavor-spec.generated.ts +346 -0
- package/src/design-core/public-api.ts +1 -0
- package/src/forms-core/field-markup.ts +2 -22
- package/src/generation-core/compose-site.ts +19 -9
- package/src/generation-core/nav-shape.ts +4 -4
- package/src/identity-core/provider-marks.ts +60 -0
- package/src/identity-core/public-api.ts +3 -0
- package/src/identity-core/sign-in.ts +43 -6
- package/src/identity-core/site-auth-transport.ts +142 -0
- package/src/identity-core/site-auth.ts +36 -0
- package/src/identity-core/site-methods.ts +78 -0
- package/src/identity-core/words.ts +60 -17
- package/styles/iox-design.css +38 -0
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import { writeFieldError } from '@vectoriox/iox-ui/design-core';
|
|
2
|
+
import {
|
|
3
|
+
CUSTOMER_SLOT, customerDisplayName, customerSlotOf, EMAIL_CODE_TTL_MINUTES, isEmailCodeShape, normalizeEmailCode, normalizeSignInEmail,
|
|
4
|
+
SIGN_IN_PROVIDERS, SignInProvider, signInRefusal, SignInSlot, SignInWord, signInWord, siteAuthErrorCode, SiteAuthTransport, SiteCustomer,
|
|
5
|
+
SiteSignInMethod, SiteSignInRefusalCode, SiteSignInReturnRefusal,
|
|
6
|
+
} from '@vectoriox/iox-ui/identity-core';
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
ACCOUNT_HIDDEN_ATTRIBUTE, ACCOUNT_LIVE_CLASS, accountSelector, AccountRole, PROVIDER_ROLES, SIGN_IN_STEP_ATTRIBUTE, SignInStep, SIGNED_IN_ATTRIBUTE,
|
|
10
|
+
} from './account';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* THE ACCOUNT RUNTIME (commerce E3a) — ONE copy, DOM only, for the builder canvas and the live site, the `ShopRuntime`
|
|
14
|
+
* shape. It finds the account parts by their role classes inside a scope (the page, globals included), asks the host's
|
|
15
|
+
* `SiteAuthTransport` who is signed in, publishes `$customer` for the ordinary bindings (`$customer.initial`), and runs
|
|
16
|
+
* the sign-in dialog: Google (the browser goes to the broker), or an email then the six-digit code sent to it. Every
|
|
17
|
+
* refusal is said on its field (`signInRefusal` → `SignInSlot`); a pressed button is busy until its answer.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface AccountHost {
|
|
21
|
+
/** The engine's (over iox-cms's site routes) on a live site; `simulatedSiteAuthTransport` on the builder canvas. */
|
|
22
|
+
transport: SiteAuthTransport;
|
|
23
|
+
/** The page's language (the words the runtime writes). */
|
|
24
|
+
locale?: () => string | null | undefined;
|
|
25
|
+
/** Write `$customer` into the host's context channel (`ContextStore.set`). */
|
|
26
|
+
publish?: (slot: string, value: unknown) => void;
|
|
27
|
+
/** Open / close the sign-in dialog through the host's own dialog path (the Lenis lock, the opener's focus). */
|
|
28
|
+
openDialog?: (dialog: HTMLElement) => void;
|
|
29
|
+
closeDialog?: (dialog: HTMLElement) => void;
|
|
30
|
+
/** Open / close the account menu beside its button (the host's popover path). */
|
|
31
|
+
openMenu?: (menu: HTMLElement, opener: HTMLElement) => void;
|
|
32
|
+
closeMenu?: (menu: HTMLElement) => void;
|
|
33
|
+
/** Send the browser to an address (the broker's start). The canvas simulates. */
|
|
34
|
+
navigate?: (url: string) => void;
|
|
35
|
+
/** The page to come back to after a provider's sign-in (a same-origin path; the engine: the current path). */
|
|
36
|
+
returnPath?: () => string;
|
|
37
|
+
/** The account page (`accountHref(orgLabel)`) — the menu's "Your orders" goes there. */
|
|
38
|
+
accountHref?: () => string;
|
|
39
|
+
/** A provider sign-in that came back refused (`?iox_signin_error=` + `iox_signin_via`, read by the host with
|
|
40
|
+
* identity-core `siteSignInReturnRefusal`) — said in the dialog on load, naming the provider. */
|
|
41
|
+
pendingError?: () => SiteSignInReturnRefusal | string | null;
|
|
42
|
+
/** The signed-in state changed (the host may re-render personal parts). */
|
|
43
|
+
onChange?: (customer: SiteCustomer | null) => void;
|
|
44
|
+
/** Re-sync when the markup under the scope changes (the builder canvas re-renders parts). */
|
|
45
|
+
observe?: boolean;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const sel = accountSelector;
|
|
49
|
+
const SCOPES = new Map<Element, AccountRuntime>();
|
|
50
|
+
|
|
51
|
+
function setAttr(el: Element | null | undefined, name: string, value: string | null): void {
|
|
52
|
+
if (!el) return;
|
|
53
|
+
if (value === null) { if (el.hasAttribute(name)) el.removeAttribute(name); }
|
|
54
|
+
else if (el.getAttribute(name) !== value) el.setAttribute(name, value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** The deepest element of a Text part that holds its words (a host may wrap them). */
|
|
58
|
+
function wordsOf(el: Element): Element {
|
|
59
|
+
let cur = el;
|
|
60
|
+
while (cur.children.length === 1) cur = cur.children[0];
|
|
61
|
+
return cur;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function setWords(el: Element | null | undefined, words: string): void {
|
|
65
|
+
if (!el) return;
|
|
66
|
+
const target = wordsOf(el);
|
|
67
|
+
if (target.textContent !== words) target.textContent = words;
|
|
68
|
+
setAttr(el, ACCOUNT_HIDDEN_ATTRIBUTE, words ? null : '');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function controlOf(field: Element | null | undefined): HTMLInputElement | null {
|
|
72
|
+
return field?.querySelector<HTMLInputElement>('.iox-field__control, input') ?? null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** The Field element (`.iox-field`) of a field part — the part itself on the string path, inside it on the canvas. */
|
|
76
|
+
function fieldElementOf(part: Element | null | undefined): HTMLElement | null {
|
|
77
|
+
if (!part) return null;
|
|
78
|
+
return (part.matches('.iox-field') ? part : part.querySelector('.iox-field') ?? part) as HTMLElement;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class AccountRuntime {
|
|
82
|
+
private customer: SiteCustomer | null = null;
|
|
83
|
+
private step: SignInStep = SignInStep.Email;
|
|
84
|
+
private email = '';
|
|
85
|
+
private busy = false;
|
|
86
|
+
private destroyed = false;
|
|
87
|
+
private observer: MutationObserver | null = null;
|
|
88
|
+
private syncing = false;
|
|
89
|
+
private syncQueued = false;
|
|
90
|
+
private lastPublished = '';
|
|
91
|
+
/** The ways the site offers right now (`transport.methods`); null until known — nothing is hidden then. */
|
|
92
|
+
private offered: ReadonlySet<SiteSignInMethod> | null = null;
|
|
93
|
+
/** The provider the last start was for — its refusal names it. */
|
|
94
|
+
private provider: SignInProvider | null = null;
|
|
95
|
+
|
|
96
|
+
constructor(readonly scope: HTMLElement, private readonly host: AccountHost) {
|
|
97
|
+
SCOPES.set(scope, this);
|
|
98
|
+
scope.addEventListener('click', this.onClick, true);
|
|
99
|
+
scope.addEventListener('keydown', this.onKeydown, true);
|
|
100
|
+
if (host.observe && typeof MutationObserver !== 'undefined') {
|
|
101
|
+
this.observer = new MutationObserver(() => { if (!this.syncing) this.queueSync(); });
|
|
102
|
+
this.observer.observe(scope, { childList: true, subtree: true });
|
|
103
|
+
}
|
|
104
|
+
this.sync();
|
|
105
|
+
void this.host.transport.methods?.().then(methods => {
|
|
106
|
+
if (this.destroyed) return;
|
|
107
|
+
this.offered = new Set(methods);
|
|
108
|
+
this.sync();
|
|
109
|
+
}, () => undefined);
|
|
110
|
+
void this.host.transport.me().then(customer => {
|
|
111
|
+
this.setCustomer(customer);
|
|
112
|
+
const pending = this.host.pendingError?.();
|
|
113
|
+
if (!pending) return;
|
|
114
|
+
if (typeof pending === 'string') this.openSignIn(pending);
|
|
115
|
+
else { this.provider = pending.provider; this.openSignIn(pending.code); }
|
|
116
|
+
}, () => this.setCustomer(null));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
private get lang(): string {
|
|
120
|
+
return this.host.locale?.() || 'en';
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** The signed-in buyer, or null. */
|
|
124
|
+
get signedIn(): SiteCustomer | null {
|
|
125
|
+
return this.customer;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get currentStep(): SignInStep {
|
|
129
|
+
return this.step;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
destroy(): void {
|
|
133
|
+
this.destroyed = true;
|
|
134
|
+
if (SCOPES.get(this.scope) === this) SCOPES.delete(this.scope);
|
|
135
|
+
this.scope.removeEventListener('click', this.onClick, true);
|
|
136
|
+
this.scope.removeEventListener('keydown', this.onKeydown, true);
|
|
137
|
+
this.observer?.disconnect();
|
|
138
|
+
this.observer = null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ── the actions ───────────────────────────────────────────────────────────────────────────
|
|
142
|
+
|
|
143
|
+
/** Sign in: open the page's sign-in dialog — or, already signed in, the menu beside `from`. False when neither exists. */
|
|
144
|
+
signIn(from?: Element | null): boolean {
|
|
145
|
+
if (this.customer) return this.openMenu(from ?? null);
|
|
146
|
+
return this.openSignIn(null);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async signOut(): Promise<boolean> {
|
|
150
|
+
try {
|
|
151
|
+
await this.host.transport.signOut();
|
|
152
|
+
} catch {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
for (const menu of this.all(AccountRole.Menu)) this.host.closeMenu?.(menu);
|
|
156
|
+
this.setCustomer(null);
|
|
157
|
+
this.announce(signInWord(SignInWord.SignedOut, this.lang));
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Open the sign-in dialog at the email step, saying `error` (a refusal code) when given. */
|
|
162
|
+
openSignIn(error: string | null): boolean {
|
|
163
|
+
const dialog = this.dialog();
|
|
164
|
+
if (!dialog) return false;
|
|
165
|
+
this.toStep(SignInStep.Email);
|
|
166
|
+
this.clearErrors();
|
|
167
|
+
if (error) this.say(error);
|
|
168
|
+
if (this.host.openDialog) this.host.openDialog(dialog);
|
|
169
|
+
else if (dialog instanceof HTMLDialogElement && !dialog.open && typeof dialog.showModal === 'function') dialog.showModal();
|
|
170
|
+
return true;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** The transport answered who is signed in: publish `$customer`, mark the parts. */
|
|
174
|
+
setCustomer(customer: SiteCustomer | null): void {
|
|
175
|
+
if (this.destroyed) return;
|
|
176
|
+
const changed = (this.customer?.id ?? null) !== (customer?.id ?? null) || this.customer?.name !== customer?.name;
|
|
177
|
+
this.customer = customer;
|
|
178
|
+
this.sync();
|
|
179
|
+
if (changed) this.host.onChange?.(customer);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ── the dialog ────────────────────────────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
private dialog(): HTMLElement | null {
|
|
185
|
+
return this.scope.querySelector<HTMLElement>(sel(AccountRole.SignIn));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private part(role: AccountRole): HTMLElement | null {
|
|
189
|
+
return this.dialog()?.querySelector<HTMLElement>(sel(role)) ?? null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private all(role: AccountRole): HTMLElement[] {
|
|
193
|
+
return Array.from(this.scope.querySelectorAll<HTMLElement>(sel(role)));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
private toStep(step: SignInStep): void {
|
|
197
|
+
this.step = step;
|
|
198
|
+
setAttr(this.dialog(), SIGN_IN_STEP_ATTRIBUTE, step);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
private clearErrors(): void {
|
|
202
|
+
for (const role of [AccountRole.Email, AccountRole.Code]) {
|
|
203
|
+
const field = fieldElementOf(this.part(role));
|
|
204
|
+
if (field) writeFieldError(field, '');
|
|
205
|
+
}
|
|
206
|
+
setWords(this.part(AccountRole.ProviderError), '');
|
|
207
|
+
setWords(this.part(AccountRole.DialogError), '');
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Say a refusal on its slot, and move focus to the field it is about. */
|
|
211
|
+
private say(code: unknown): void {
|
|
212
|
+
const refusal = signInRefusal(code, this.lang, this.provider);
|
|
213
|
+
if (refusal.slot === SignInSlot.Email || refusal.slot === SignInSlot.Code) {
|
|
214
|
+
const role = refusal.slot === SignInSlot.Email ? AccountRole.Email : AccountRole.Code;
|
|
215
|
+
const field = fieldElementOf(this.part(role));
|
|
216
|
+
if (field) {
|
|
217
|
+
writeFieldError(field, refusal.words);
|
|
218
|
+
controlOf(field)?.focus();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const line = refusal.slot === SignInSlot.Provider ? this.part(AccountRole.ProviderError) : this.part(AccountRole.DialogError);
|
|
223
|
+
setWords(line, refusal.words);
|
|
224
|
+
if (line) setAttr(line, 'role', 'alert');
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
private async run(button: HTMLElement | null, work: () => Promise<void>): Promise<void> {
|
|
228
|
+
if (this.busy) return;
|
|
229
|
+
this.busy = true;
|
|
230
|
+
const dialog = this.dialog();
|
|
231
|
+
setAttr(dialog, 'aria-busy', 'true');
|
|
232
|
+
setAttr(button, 'aria-busy', 'true');
|
|
233
|
+
setAttr(button, 'aria-disabled', 'true');
|
|
234
|
+
try {
|
|
235
|
+
await work();
|
|
236
|
+
} catch (error) {
|
|
237
|
+
this.say(siteAuthErrorCode(error));
|
|
238
|
+
} finally {
|
|
239
|
+
this.busy = false;
|
|
240
|
+
setAttr(dialog, 'aria-busy', null);
|
|
241
|
+
setAttr(button, 'aria-busy', null);
|
|
242
|
+
setAttr(button, 'aria-disabled', null);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Continue with a provider: the studio asks the broker for a ticket; the browser goes to its start address. */
|
|
247
|
+
async startProvider(provider: SignInProvider = SignInProvider.Google, button: HTMLElement | null = null): Promise<void> {
|
|
248
|
+
this.clearErrors();
|
|
249
|
+
this.provider = provider;
|
|
250
|
+
await this.run(button, async () => {
|
|
251
|
+
const { startUrl } = await this.host.transport.startProvider(provider, this.host.returnPath?.() ?? '/');
|
|
252
|
+
this.host.navigate?.(startUrl);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/** Send a code to the typed email, then ask for it. */
|
|
257
|
+
async sendCode(button: HTMLElement | null = null): Promise<void> {
|
|
258
|
+
const email = normalizeSignInEmail(controlOf(this.part(AccountRole.Email))?.value ?? this.email);
|
|
259
|
+
this.clearErrors();
|
|
260
|
+
if (!email) { this.say(SiteSignInRefusalCode.InvalidEmail); return; }
|
|
261
|
+
await this.run(button, async () => {
|
|
262
|
+
await this.host.transport.sendCode(email);
|
|
263
|
+
this.email = email;
|
|
264
|
+
this.toStep(SignInStep.Code);
|
|
265
|
+
setWords(this.part(AccountRole.CodeSent), signInWord(SignInWord.CodeSent, this.lang, { email }));
|
|
266
|
+
setWords(this.part(AccountRole.CodeValid), signInWord(SignInWord.CodeValid, this.lang, { minutes: EMAIL_CODE_TTL_MINUTES }));
|
|
267
|
+
const code = controlOf(this.part(AccountRole.Code));
|
|
268
|
+
if (code) { code.value = ''; code.focus(); }
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Check the code; signed in, the dialog closes and the page says so. */
|
|
273
|
+
async verify(button: HTMLElement | null = null): Promise<void> {
|
|
274
|
+
const code = normalizeEmailCode(controlOf(this.part(AccountRole.Code))?.value ?? '');
|
|
275
|
+
this.clearErrors();
|
|
276
|
+
if (!isEmailCodeShape(code)) { this.say(SiteSignInRefusalCode.CodeWrong); return; }
|
|
277
|
+
await this.run(button, async () => {
|
|
278
|
+
const customer = await this.host.transport.verifyCode(this.email, code);
|
|
279
|
+
this.setCustomer(customer);
|
|
280
|
+
const dialog = this.dialog();
|
|
281
|
+
if (dialog) {
|
|
282
|
+
if (this.host.closeDialog) this.host.closeDialog(dialog);
|
|
283
|
+
else if (dialog instanceof HTMLDialogElement && dialog.open) dialog.close();
|
|
284
|
+
}
|
|
285
|
+
this.toStep(SignInStep.Email);
|
|
286
|
+
this.announce(signInWord(SignInWord.SignedIn, this.lang));
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Back to the email step, the typed email kept for editing. */
|
|
291
|
+
otherEmail(): void {
|
|
292
|
+
this.clearErrors();
|
|
293
|
+
this.toStep(SignInStep.Email);
|
|
294
|
+
const input = controlOf(this.part(AccountRole.Email));
|
|
295
|
+
if (input) { input.value = this.email; input.focus(); }
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private openMenu(from: Element | null): boolean {
|
|
299
|
+
const root = from?.closest(sel(AccountRole.Account)) ?? this.scope.querySelector(sel(AccountRole.Account));
|
|
300
|
+
const menu = root?.querySelector<HTMLElement>(sel(AccountRole.Menu));
|
|
301
|
+
const opener = (from?.closest(sel(AccountRole.Entry)) ?? root?.querySelector(sel(AccountRole.Entry))) as HTMLElement | null;
|
|
302
|
+
if (!menu || !opener) return false;
|
|
303
|
+
this.host.openMenu?.(menu, opener);
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// ── the parts ─────────────────────────────────────────────────────────────────────────────
|
|
308
|
+
|
|
309
|
+
private queueSync(): void {
|
|
310
|
+
if (this.syncQueued) return;
|
|
311
|
+
this.syncQueued = true;
|
|
312
|
+
const run = () => { this.syncQueued = false; this.sync(); };
|
|
313
|
+
if (typeof queueMicrotask === 'function') queueMicrotask(run); else Promise.resolve().then(run);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** Write the signed-in state into every part. Idempotent; the observer is deaf while it runs. */
|
|
317
|
+
sync(): void {
|
|
318
|
+
if (this.destroyed) return;
|
|
319
|
+
this.syncing = true;
|
|
320
|
+
try {
|
|
321
|
+
const slot = customerSlotOf(this.customer);
|
|
322
|
+
const json = JSON.stringify(slot);
|
|
323
|
+
if (json !== this.lastPublished) {
|
|
324
|
+
this.lastPublished = json;
|
|
325
|
+
this.host.publish?.(CUSTOMER_SLOT, slot);
|
|
326
|
+
}
|
|
327
|
+
const signedIn = this.customer ? '' : null;
|
|
328
|
+
const name = customerDisplayName(this.customer);
|
|
329
|
+
for (const root of this.all(AccountRole.Account)) setAttr(root, SIGNED_IN_ATTRIBUTE, signedIn);
|
|
330
|
+
for (const entry of this.all(AccountRole.Entry)) {
|
|
331
|
+
setAttr(entry, SIGNED_IN_ATTRIBUTE, signedIn);
|
|
332
|
+
setAttr(entry, 'aria-haspopup', 'dialog');
|
|
333
|
+
setAttr(entry, 'aria-label', this.customer
|
|
334
|
+
? `${signInWord(SignInWord.Account, this.lang)} — ${name}`
|
|
335
|
+
: signInWord(SignInWord.SignIn, this.lang));
|
|
336
|
+
}
|
|
337
|
+
for (const greeting of this.all(AccountRole.MenuGreeting)) if (this.customer) setWords(greeting, signInWord(SignInWord.Hello, this.lang, { name }));
|
|
338
|
+
const href = this.host.accountHref?.();
|
|
339
|
+
if (href) for (const link of this.all(AccountRole.Orders)) { const a = link.matches('a') ? link : link.querySelector('a'); setAttr(a, 'href', href); }
|
|
340
|
+
const dialog = this.dialog();
|
|
341
|
+
if (dialog) {
|
|
342
|
+
setAttr(dialog, SIGN_IN_STEP_ATTRIBUTE, this.step);
|
|
343
|
+
for (const role of [AccountRole.ProviderError, AccountRole.DialogError]) {
|
|
344
|
+
const line = this.part(role);
|
|
345
|
+
if (line && !wordsOf(line).textContent) setAttr(line, ACCOUNT_HIDDEN_ATTRIBUTE, '');
|
|
346
|
+
}
|
|
347
|
+
this.showOffered(dialog);
|
|
348
|
+
this.prepareInputs();
|
|
349
|
+
}
|
|
350
|
+
} finally {
|
|
351
|
+
this.observer?.takeRecords();
|
|
352
|
+
this.syncing = false;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Only the providers the site offers right now; with none, the "or" between them and the email goes too. */
|
|
357
|
+
private showOffered(dialog: HTMLElement): void {
|
|
358
|
+
if (!this.offered) return;
|
|
359
|
+
let any = false;
|
|
360
|
+
for (const provider of SIGN_IN_PROVIDERS) {
|
|
361
|
+
const on = this.offered.has(provider);
|
|
362
|
+
for (const button of Array.from(dialog.querySelectorAll<HTMLElement>(sel(PROVIDER_ROLES[provider])))) {
|
|
363
|
+
setAttr(button, ACCOUNT_HIDDEN_ATTRIBUTE, on ? null : '');
|
|
364
|
+
any ||= on;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
setAttr(dialog.querySelector(sel(AccountRole.Providers)), ACCOUNT_HIDDEN_ATTRIBUTE, any ? null : '');
|
|
368
|
+
setAttr(dialog.querySelector(sel(AccountRole.Or)), ACCOUNT_HIDDEN_ATTRIBUTE, any ? null : '');
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/** What a browser and a phone keyboard need to know about the two inputs (autofill, the one-time code). */
|
|
372
|
+
private prepareInputs(): void {
|
|
373
|
+
const email = controlOf(this.part(AccountRole.Email));
|
|
374
|
+
if (email) {
|
|
375
|
+
setAttr(email, 'type', 'email');
|
|
376
|
+
setAttr(email, 'autocomplete', 'email');
|
|
377
|
+
setAttr(email, 'inputmode', 'email');
|
|
378
|
+
setAttr(email, 'spellcheck', 'false');
|
|
379
|
+
}
|
|
380
|
+
const code = controlOf(this.part(AccountRole.Code));
|
|
381
|
+
if (code) {
|
|
382
|
+
setAttr(code, 'autocomplete', 'one-time-code');
|
|
383
|
+
setAttr(code, 'inputmode', 'numeric');
|
|
384
|
+
setAttr(code, 'maxlength', '7');
|
|
385
|
+
setAttr(code, 'dir', 'ltr');
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
private announce(words: string): void {
|
|
390
|
+
const doc = this.scope.ownerDocument;
|
|
391
|
+
if (!doc) return;
|
|
392
|
+
let live = this.scope.querySelector<HTMLElement>(`.${ACCOUNT_LIVE_CLASS}`);
|
|
393
|
+
if (!live) {
|
|
394
|
+
live = doc.createElement('div');
|
|
395
|
+
live.className = ACCOUNT_LIVE_CLASS;
|
|
396
|
+
live.setAttribute('role', 'status');
|
|
397
|
+
live.setAttribute('aria-live', 'polite');
|
|
398
|
+
this.scope.appendChild(live);
|
|
399
|
+
}
|
|
400
|
+
live.textContent = '';
|
|
401
|
+
live.textContent = words;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// ── events ────────────────────────────────────────────────────────────────────────────────
|
|
405
|
+
|
|
406
|
+
private readonly onClick = (event: Event): void => {
|
|
407
|
+
const target = event.target as Element | null;
|
|
408
|
+
if (!target?.closest) return;
|
|
409
|
+
const button = (role: AccountRole) => target.closest<HTMLElement>(sel(role));
|
|
410
|
+
const entry = button(AccountRole.Entry);
|
|
411
|
+
if (entry) { this.signIn(entry); return; }
|
|
412
|
+
if (!target.closest(sel(AccountRole.SignIn))) return;
|
|
413
|
+
for (const provider of SIGN_IN_PROVIDERS) {
|
|
414
|
+
const pressed = button(PROVIDER_ROLES[provider]);
|
|
415
|
+
if (pressed) { void this.startProvider(provider, pressed); return; }
|
|
416
|
+
}
|
|
417
|
+
if (button(AccountRole.SendCode)) { void this.sendCode(button(AccountRole.SendCode)); return; }
|
|
418
|
+
if (button(AccountRole.SendAgain)) { void this.sendCode(button(AccountRole.SendAgain)); return; }
|
|
419
|
+
if (button(AccountRole.Verify)) { void this.verify(button(AccountRole.Verify)); return; }
|
|
420
|
+
if (button(AccountRole.OtherEmail)) this.otherEmail();
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
/** Enter in a field presses its step's button — the dialog holds no `<form>` that could post. */
|
|
424
|
+
private readonly onKeydown = (event: Event): void => {
|
|
425
|
+
const key = event as KeyboardEvent;
|
|
426
|
+
if (key.key !== 'Enter' || key.isComposing) return;
|
|
427
|
+
const target = key.target as Element | null;
|
|
428
|
+
if (!target?.closest) return;
|
|
429
|
+
if (target.closest(sel(AccountRole.Email))) { key.preventDefault(); void this.sendCode(this.part(AccountRole.SendCode)); }
|
|
430
|
+
else if (target.closest(sel(AccountRole.Code))) { key.preventDefault(); void this.verify(this.part(AccountRole.Verify)); }
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
/** Attach the account runtime to a page scope, unless one runs there already. */
|
|
435
|
+
export function attachAccount(scope: HTMLElement, host: AccountHost): AccountRuntime {
|
|
436
|
+
return SCOPES.get(scope) ?? new AccountRuntime(scope, host);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** The account runtime an element sits under, or null. */
|
|
440
|
+
export function accountRuntimeOf(el: Element | null | undefined): AccountRuntime | null {
|
|
441
|
+
for (let cur: Element | null | undefined = el; cur; cur = cur.parentElement) {
|
|
442
|
+
const rt = SCOPES.get(cur);
|
|
443
|
+
if (rt) return rt;
|
|
444
|
+
}
|
|
445
|
+
return null;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/** The account actions (actions-core `ACCOUNT_ACTION_TYPES`). */
|
|
449
|
+
export enum AccountActionType {
|
|
450
|
+
SignIn = 'signIn',
|
|
451
|
+
SignOut = 'signOut',
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Run an account action from the element that fired it (both interaction engines call this). False when none runs. */
|
|
455
|
+
export async function runAccountAction(el: Element, type: string): Promise<boolean> {
|
|
456
|
+
const rt = accountRuntimeOf(el);
|
|
457
|
+
if (!rt) return false;
|
|
458
|
+
if (type === AccountActionType.SignIn) return rt.signIn(el);
|
|
459
|
+
if (type === AccountActionType.SignOut) return rt.signOut();
|
|
460
|
+
return false;
|
|
461
|
+
}
|