@oxvo/ai-live-assist 7.5.20 → 7.5.21
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/cjs/AiLiveAssist.d.ts +8 -1
- package/cjs/AiLiveAssist.js +113 -8
- package/cjs/client.d.ts +5 -3
- package/cjs/client.js +44 -5
- package/cjs/commerce/cartContracts.d.ts +158 -0
- package/cjs/commerce/cartContracts.js +228 -0
- package/cjs/commerce/cartSession.d.ts +86 -0
- package/cjs/commerce/cartSession.js +475 -0
- package/cjs/commerce/contracts.d.ts +152 -0
- package/cjs/commerce/contracts.js +322 -0
- package/cjs/commerce/pageContext.d.ts +50 -0
- package/cjs/commerce/pageContext.js +173 -0
- package/cjs/commerce/productPanel.d.ts +65 -0
- package/cjs/commerce/productPanel.js +662 -0
- package/cjs/commerce/realtimeCommerce.d.ts +73 -0
- package/cjs/commerce/realtimeCommerce.js +342 -0
- package/cjs/commerce/shopifyCart.d.ts +130 -0
- package/cjs/commerce/shopifyCart.js +1135 -0
- package/cjs/commerce/shopifyCartPresentation.d.ts +12 -0
- package/cjs/commerce/shopifyCartPresentation.js +379 -0
- package/cjs/commerce/shoppingNotes.d.ts +61 -0
- package/cjs/commerce/shoppingNotes.js +313 -0
- package/cjs/commerce/shoppingNotesView.d.ts +46 -0
- package/cjs/commerce/shoppingNotesView.js +269 -0
- package/cjs/control.d.ts +9 -1
- package/cjs/control.js +51 -1
- package/cjs/guideAdmission.d.ts +5 -2
- package/cjs/guideAdmission.js +21 -2
- package/cjs/guideInstaller.js +1 -1
- package/cjs/guideMeasurement.d.ts +5 -0
- package/cjs/guideMeasurement.js +29 -0
- package/cjs/guideSubjectStore.js +2 -0
- package/cjs/guideTypes.d.ts +18 -0
- package/cjs/liveControlChannel.d.ts +43 -1
- package/cjs/liveControlChannel.js +166 -14
- package/cjs/liveGuideAdmission.d.ts +3 -2
- package/cjs/liveGuideAdmission.js +10 -2
- package/cjs/liveGuideWidget.d.ts +18 -0
- package/cjs/liveGuideWidget.js +228 -23
- package/cjs/locales/resources.js +2240 -0
- package/cjs/messages.js +56 -0
- package/cjs/tabLock.d.ts +2 -0
- package/cjs/tabLock.js +11 -1
- package/cjs/types.d.ts +6 -2
- package/cjs/types.js +56 -0
- package/cjs/ui.d.ts +37 -0
- package/cjs/ui.js +259 -9
- package/cjs/version.d.ts +1 -1
- package/cjs/version.js +1 -1
- package/lib/AiLiveAssist.d.ts +8 -1
- package/lib/AiLiveAssist.js +113 -8
- package/lib/client.d.ts +5 -3
- package/lib/client.js +44 -5
- package/lib/commerce/cartContracts.d.ts +158 -0
- package/lib/commerce/cartContracts.js +221 -0
- package/lib/commerce/cartSession.d.ts +86 -0
- package/lib/commerce/cartSession.js +471 -0
- package/lib/commerce/contracts.d.ts +152 -0
- package/lib/commerce/contracts.js +300 -0
- package/lib/commerce/pageContext.d.ts +50 -0
- package/lib/commerce/pageContext.js +168 -0
- package/lib/commerce/productPanel.d.ts +65 -0
- package/lib/commerce/productPanel.js +657 -0
- package/lib/commerce/realtimeCommerce.d.ts +73 -0
- package/lib/commerce/realtimeCommerce.js +337 -0
- package/lib/commerce/shopifyCart.d.ts +130 -0
- package/lib/commerce/shopifyCart.js +1130 -0
- package/lib/commerce/shopifyCartPresentation.d.ts +12 -0
- package/lib/commerce/shopifyCartPresentation.js +376 -0
- package/lib/commerce/shoppingNotes.d.ts +61 -0
- package/lib/commerce/shoppingNotes.js +307 -0
- package/lib/commerce/shoppingNotesView.d.ts +46 -0
- package/lib/commerce/shoppingNotesView.js +265 -0
- package/lib/control.d.ts +9 -1
- package/lib/control.js +51 -1
- package/lib/guideAdmission.d.ts +5 -2
- package/lib/guideAdmission.js +21 -2
- package/lib/guideInstaller.js +1 -1
- package/lib/guideMeasurement.d.ts +5 -0
- package/lib/guideMeasurement.js +24 -0
- package/lib/guideSubjectStore.js +2 -0
- package/lib/guideTypes.d.ts +18 -0
- package/lib/liveControlChannel.d.ts +43 -1
- package/lib/liveControlChannel.js +166 -14
- package/lib/liveGuideAdmission.d.ts +3 -2
- package/lib/liveGuideAdmission.js +10 -2
- package/lib/liveGuideWidget.d.ts +18 -0
- package/lib/liveGuideWidget.js +228 -23
- package/lib/locales/resources.js +2240 -0
- package/lib/messages.js +56 -0
- package/lib/tabLock.d.ts +2 -0
- package/lib/tabLock.js +11 -1
- package/lib/types.d.ts +6 -2
- package/lib/types.js +56 -0
- package/lib/ui.d.ts +37 -0
- package/lib/ui.js +259 -9
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,657 @@
|
|
|
1
|
+
export const COMMERCE_PRODUCT_PANEL_STYLES = `
|
|
2
|
+
.commerce-panel { box-sizing: border-box; min-width: 0; max-width: 100%; padding: 12px; color: var(--assist-text, var(--voice-text)); background: var(--assist-surface, var(--voice-surface)); border: 1px solid var(--assist-border, var(--voice-border)); border-radius: 16px; font: 400 14px/1.45 system-ui, sans-serif; pointer-events: auto; }
|
|
3
|
+
.commerce-panel *, .commerce-panel *::before, .commerce-panel *::after { box-sizing: border-box; }
|
|
4
|
+
.commerce-panel[hidden], .commerce-panel [hidden] { display: none !important; }
|
|
5
|
+
.commerce-panel-header, .commerce-panel-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
|
|
6
|
+
.commerce-panel-header { justify-content: space-between; }
|
|
7
|
+
.commerce-panel h2, .commerce-panel h3, .commerce-panel p { margin: 0; }
|
|
8
|
+
.commerce-panel h2 { flex: 1; font: 600 15px/1.4 system-ui, sans-serif; }
|
|
9
|
+
.commerce-panel h3 { font: 600 14px/1.4 system-ui, sans-serif; overflow-wrap: anywhere; }
|
|
10
|
+
.commerce-panel-status, .commerce-panel-ranking { margin-block: 8px !important; color: var(--assist-muted, var(--voice-muted)); font-size: 12px; }
|
|
11
|
+
.commerce-panel-content { max-height: min(540px, 55dvh); overflow: auto; overscroll-behavior: contain; scrollbar-gutter: stable; padding: 2px; }
|
|
12
|
+
.commerce-panel-products { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); align-items: start; gap: 12px; }
|
|
13
|
+
.commerce-panel[data-comparing="true"] .commerce-panel-products { grid-template-columns: repeat(auto-fit, minmax(min(100%, 180px), 1fr)); }
|
|
14
|
+
.commerce-product { display: grid; min-width: 0; gap: 10px; padding: 12px; border: 1px solid var(--assist-border, var(--voice-border)); border-radius: 12px; background: var(--assist-surface-muted, var(--voice-surface)); }
|
|
15
|
+
.commerce-product-summary { display: grid; gap: 4px; }
|
|
16
|
+
.commerce-product-image { display: block; width: 100%; height: 112px; object-fit: contain; border-radius: 8px; }
|
|
17
|
+
.commerce-product-price { font-weight: 600; }
|
|
18
|
+
.commerce-product-availability, .commerce-product-description, .commerce-product-note { font-size: 12px; overflow-wrap: anywhere; }
|
|
19
|
+
.commerce-product-description { white-space: pre-line; }
|
|
20
|
+
.commerce-product-note { color: var(--assist-muted, var(--voice-muted)); }
|
|
21
|
+
.commerce-product-options { display: grid; min-width: 0; gap: 8px; border: 0; padding: 0; margin: 0; }
|
|
22
|
+
.commerce-product-label { display: grid; min-width: 0; gap: 4px; font-size: 12px; }
|
|
23
|
+
.commerce-product-compare { display: flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; font-size: 12px; }
|
|
24
|
+
.commerce-panel button, .commerce-panel select, .commerce-panel input[type="number"], .commerce-product-link { min-height: 44px; max-width: 100%; padding: 8px 10px; border: 1px solid var(--assist-border, var(--voice-border)); border-radius: 8px; color: inherit; background: var(--assist-surface, var(--voice-surface)); font: inherit; }
|
|
25
|
+
.commerce-panel select, .commerce-panel input[type="number"] { width: 100%; min-width: 0; }
|
|
26
|
+
.commerce-panel button { cursor: pointer; white-space: normal; overflow-wrap: anywhere; }
|
|
27
|
+
.commerce-panel button:disabled, .commerce-panel select:disabled, .commerce-panel input:disabled { opacity: .55; cursor: default; }
|
|
28
|
+
.commerce-panel :is(a, button, input, select):focus-visible { outline: 2px solid currentColor; outline-offset: 2px; }
|
|
29
|
+
.commerce-product-link { display: flex; align-items: center; justify-content: center; text-align: center; text-decoration: underline; text-underline-offset: 3px; }
|
|
30
|
+
.commerce-panel-actions { margin-block-start: 10px; }
|
|
31
|
+
.commerce-panel-actions > button { flex: 1; }
|
|
32
|
+
.commerce-panel-close { flex: none; font-size: 12px !important; }
|
|
33
|
+
@media (forced-colors: active) { .commerce-panel, .commerce-product { color: CanvasText; background: Canvas; border-color: CanvasText; } .commerce-product-link { color: LinkText; } .commerce-panel :is(a, button, input, select):focus-visible { outline-color: Highlight; } }
|
|
34
|
+
`;
|
|
35
|
+
const validQuantityRule = (rule) => Number.isSafeInteger(rule.minimum) &&
|
|
36
|
+
rule.minimum > 0 &&
|
|
37
|
+
Number.isSafeInteger(rule.increment) &&
|
|
38
|
+
rule.increment > 0 &&
|
|
39
|
+
rule.minimum % rule.increment === 0 &&
|
|
40
|
+
(rule.maximum === null ||
|
|
41
|
+
(Number.isSafeInteger(rule.maximum) &&
|
|
42
|
+
rule.maximum >= rule.minimum &&
|
|
43
|
+
rule.maximum % rule.increment === 0));
|
|
44
|
+
const rankingMessages = {
|
|
45
|
+
shopify_best_selling: "commerceBestSelling",
|
|
46
|
+
relevance: "commerceRelevant",
|
|
47
|
+
price_ascending: "commercePriceOrder",
|
|
48
|
+
catalog: "commerceCatalogOrder",
|
|
49
|
+
};
|
|
50
|
+
const parentElement = (element) => {
|
|
51
|
+
if (element.parentElement)
|
|
52
|
+
return element.parentElement;
|
|
53
|
+
const root = element.getRootNode();
|
|
54
|
+
return root instanceof ShadowRoot ? root.host : null;
|
|
55
|
+
};
|
|
56
|
+
/** Includes clipping and hidden shadow hosts: attachment alone is not display. */
|
|
57
|
+
const visiblyRendered = (element) => {
|
|
58
|
+
if (!element.isConnected || document.visibilityState === "hidden")
|
|
59
|
+
return false;
|
|
60
|
+
const rect = element.getBoundingClientRect();
|
|
61
|
+
if (rect.width <= 0 || rect.height <= 0 || !element.getClientRects().length)
|
|
62
|
+
return false;
|
|
63
|
+
let left = Math.max(0, rect.left);
|
|
64
|
+
let top = Math.max(0, rect.top);
|
|
65
|
+
let right = Math.min(window.innerWidth, rect.right);
|
|
66
|
+
let bottom = Math.min(window.innerHeight, rect.bottom);
|
|
67
|
+
for (let current = element; current; current = parentElement(current)) {
|
|
68
|
+
const style = window.getComputedStyle(current);
|
|
69
|
+
if (current.hasAttribute("hidden") ||
|
|
70
|
+
current.getAttribute("aria-hidden") === "true" ||
|
|
71
|
+
style.display === "none" ||
|
|
72
|
+
style.visibility === "hidden" ||
|
|
73
|
+
style.visibility === "collapse" ||
|
|
74
|
+
style.opacity === "0")
|
|
75
|
+
return false;
|
|
76
|
+
if (current !== element) {
|
|
77
|
+
const bounds = current.getBoundingClientRect();
|
|
78
|
+
if (/(hidden|clip|auto|scroll)/u.test(style.overflowX || style.overflow)) {
|
|
79
|
+
left = Math.max(left, bounds.left);
|
|
80
|
+
right = Math.min(right, bounds.right);
|
|
81
|
+
}
|
|
82
|
+
if (/(hidden|clip|auto|scroll)/u.test(style.overflowY || style.overflow)) {
|
|
83
|
+
top = Math.max(top, bounds.top);
|
|
84
|
+
bottom = Math.min(bottom, bounds.bottom);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// A summary must be fully in the visible area before claiming it was shown.
|
|
89
|
+
// Tall product descriptions and controls are deliberately outside this summary.
|
|
90
|
+
return right - left >= rect.width - 1 && bottom - top >= rect.height - 1;
|
|
91
|
+
};
|
|
92
|
+
const safeUrl = (value, origin) => {
|
|
93
|
+
try {
|
|
94
|
+
const url = new URL(value);
|
|
95
|
+
return url.protocol === "https:" &&
|
|
96
|
+
!url.username &&
|
|
97
|
+
!url.password &&
|
|
98
|
+
(!origin || url.origin === origin)
|
|
99
|
+
? url.href
|
|
100
|
+
: null;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
// Keep the catalog's decimal exact, including large values and sub-cent prices.
|
|
107
|
+
// Formatting a Number first can round a valid decimal into a different price.
|
|
108
|
+
export const formatCommercePrice = (amount, currency, locale) => {
|
|
109
|
+
const [integer = "0", decimal = ""] = amount.split(".");
|
|
110
|
+
const currencyDigits = new Intl.NumberFormat(locale, {
|
|
111
|
+
style: "currency",
|
|
112
|
+
currency,
|
|
113
|
+
}).resolvedOptions().minimumFractionDigits ?? 0;
|
|
114
|
+
const fraction = decimal.replace(/0+$/u, "").padEnd(currencyDigits, "0");
|
|
115
|
+
const formatter = new Intl.NumberFormat(locale, {
|
|
116
|
+
style: "currency",
|
|
117
|
+
currency,
|
|
118
|
+
minimumFractionDigits: fraction.length,
|
|
119
|
+
maximumFractionDigits: fraction.length,
|
|
120
|
+
});
|
|
121
|
+
const digits = new Intl.NumberFormat(locale, { useGrouping: false });
|
|
122
|
+
return formatter
|
|
123
|
+
.formatToParts(BigInt(integer))
|
|
124
|
+
.map((part) => part.type === "fraction"
|
|
125
|
+
? [...fraction].map((digit) => digits.format(Number(digit))).join("")
|
|
126
|
+
: part.value)
|
|
127
|
+
.join("");
|
|
128
|
+
};
|
|
129
|
+
export class CommerceProductPanel {
|
|
130
|
+
constructor(message, callbacks) {
|
|
131
|
+
this.message = message;
|
|
132
|
+
this.callbacks = callbacks;
|
|
133
|
+
this.element = document.createElement("section");
|
|
134
|
+
this.content = document.createElement("div");
|
|
135
|
+
this.products = document.createElement("div");
|
|
136
|
+
this.status = document.createElement("p");
|
|
137
|
+
this.ranking = document.createElement("p");
|
|
138
|
+
this.catalog = null;
|
|
139
|
+
this.states = [];
|
|
140
|
+
this.comparison = null;
|
|
141
|
+
this.seen = new Set();
|
|
142
|
+
this.displayed = false;
|
|
143
|
+
this.reported = new Set();
|
|
144
|
+
this.busy = false;
|
|
145
|
+
this.failed = false;
|
|
146
|
+
this.errorMessage = "commerceError";
|
|
147
|
+
this.destroyed = false;
|
|
148
|
+
this.expiryTimer = null;
|
|
149
|
+
this.visibilityFrame = null;
|
|
150
|
+
this.observer = null;
|
|
151
|
+
this.addHandler = null;
|
|
152
|
+
this.canAdd = null;
|
|
153
|
+
/** Call after opening/moving the widget; observers also cover clipping/scroll. */
|
|
154
|
+
this.refreshVisibility = () => {
|
|
155
|
+
if (this.destroyed ||
|
|
156
|
+
this.displayed ||
|
|
157
|
+
!this.catalog ||
|
|
158
|
+
this.visibilityFrame !== null)
|
|
159
|
+
return;
|
|
160
|
+
this.visibilityFrame = window.requestAnimationFrame(() => {
|
|
161
|
+
this.visibilityFrame = null;
|
|
162
|
+
const catalog = this.catalog;
|
|
163
|
+
if (!catalog || !this.fresh() || this.busy || this.displayed)
|
|
164
|
+
return;
|
|
165
|
+
for (const state of this.states) {
|
|
166
|
+
if (!state.dirty && visiblyRendered(state.summary))
|
|
167
|
+
this.seen.add(state.product.productRef);
|
|
168
|
+
}
|
|
169
|
+
if (this.callbacks.displayMode !== "visible" &&
|
|
170
|
+
this.states.some((state) => !this.seen.has(state.product.productRef)))
|
|
171
|
+
return;
|
|
172
|
+
if (this.states.length && !this.seen.size)
|
|
173
|
+
return;
|
|
174
|
+
if (!this.states.length && !visiblyRendered(this.status))
|
|
175
|
+
return;
|
|
176
|
+
const newlySeen = this.states.filter((state) => this.seen.has(state.product.productRef) &&
|
|
177
|
+
!this.reported.has(state.product.productRef));
|
|
178
|
+
if (this.states.length && !newlySeen.length)
|
|
179
|
+
return;
|
|
180
|
+
for (const state of newlySeen)
|
|
181
|
+
this.reported.add(state.product.productRef);
|
|
182
|
+
this.displayed = this.reported.size === this.states.length;
|
|
183
|
+
this.callbacks.onDisplayed?.(catalog.resultSetId, this.states
|
|
184
|
+
.filter((state) => this.reported.has(state.product.productRef))
|
|
185
|
+
.map((state) => ({
|
|
186
|
+
productRef: state.product.productRef,
|
|
187
|
+
variantRef: state.product.variant?.variantRef ?? null,
|
|
188
|
+
})));
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
this.element.className = "commerce-panel";
|
|
192
|
+
this.element.hidden = true;
|
|
193
|
+
this.element.setAttribute("aria-label", message("commerceTitle"));
|
|
194
|
+
const header = document.createElement("div");
|
|
195
|
+
header.className = "commerce-panel-header";
|
|
196
|
+
const title = document.createElement("h2");
|
|
197
|
+
title.textContent = message("commerceTitle");
|
|
198
|
+
const close = this.button("commerceClose", () => {
|
|
199
|
+
if (this.destroyed)
|
|
200
|
+
return;
|
|
201
|
+
this.clear();
|
|
202
|
+
callbacks.onClose?.();
|
|
203
|
+
});
|
|
204
|
+
close.className = "commerce-panel-close";
|
|
205
|
+
header.append(title);
|
|
206
|
+
if (callbacks.onShoppingNotes)
|
|
207
|
+
header.append(this.button("shoppingNotesTitle", () => {
|
|
208
|
+
if (!this.destroyed)
|
|
209
|
+
callbacks.onShoppingNotes?.();
|
|
210
|
+
}));
|
|
211
|
+
header.append(close);
|
|
212
|
+
this.status.className = "commerce-panel-status";
|
|
213
|
+
this.status.setAttribute("role", "status");
|
|
214
|
+
this.status.setAttribute("aria-live", "polite");
|
|
215
|
+
this.status.setAttribute("aria-atomic", "true");
|
|
216
|
+
this.ranking.className = "commerce-panel-ranking";
|
|
217
|
+
this.content.className = "commerce-panel-content";
|
|
218
|
+
this.products.className = "commerce-panel-products";
|
|
219
|
+
this.content.append(this.products);
|
|
220
|
+
this.compareButton = this.button("commerceCompare", () => this.compare());
|
|
221
|
+
this.backButton = this.button("commerceBack", () => {
|
|
222
|
+
this.comparison = null;
|
|
223
|
+
this.update();
|
|
224
|
+
this.refreshVisibility();
|
|
225
|
+
this.compareButton.focus({ preventScroll: true });
|
|
226
|
+
});
|
|
227
|
+
this.refreshButton = this.button("commerceRefresh", () => {
|
|
228
|
+
if (this.busy || this.destroyed)
|
|
229
|
+
return;
|
|
230
|
+
this.setBusy(true);
|
|
231
|
+
this.runAction(() => callbacks.onRefresh());
|
|
232
|
+
});
|
|
233
|
+
const actions = document.createElement("div");
|
|
234
|
+
actions.className = "commerce-panel-actions";
|
|
235
|
+
actions.append(this.compareButton, this.backButton, this.refreshButton);
|
|
236
|
+
this.element.append(header, this.ranking, this.status, this.content, actions);
|
|
237
|
+
if (typeof IntersectionObserver !== "undefined") {
|
|
238
|
+
this.observer = new IntersectionObserver(() => this.refreshVisibility(), {
|
|
239
|
+
threshold: [0, 1],
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
document.addEventListener("scroll", this.refreshVisibility, true);
|
|
243
|
+
document.addEventListener("visibilitychange", this.refreshVisibility);
|
|
244
|
+
window.addEventListener("resize", this.refreshVisibility);
|
|
245
|
+
}
|
|
246
|
+
/** Input must already pass parseCommerceCatalog for the current binding/page. */
|
|
247
|
+
setCatalog(catalog) {
|
|
248
|
+
if (this.destroyed)
|
|
249
|
+
return;
|
|
250
|
+
this.clear();
|
|
251
|
+
if (catalog.products.length > 3) {
|
|
252
|
+
this.showError();
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
// Isolate click handlers and receipts from subsequent caller object changes.
|
|
256
|
+
this.catalog = JSON.parse(JSON.stringify(catalog));
|
|
257
|
+
this.element.hidden = false;
|
|
258
|
+
this.ranking.textContent = this.message(rankingMessages[catalog.ranking.basis]);
|
|
259
|
+
this.states = this.catalog.products.map((product) => this.renderProduct(product));
|
|
260
|
+
this.products.replaceChildren(...this.states.map((state) => state.element));
|
|
261
|
+
for (const state of this.states)
|
|
262
|
+
this.observer?.observe(state.summary);
|
|
263
|
+
if (!this.states.length)
|
|
264
|
+
this.observer?.observe(this.status);
|
|
265
|
+
this.update();
|
|
266
|
+
const remaining = Date.parse(catalog.expiresAt) - Date.now();
|
|
267
|
+
if (remaining > 0 && Number.isFinite(remaining)) {
|
|
268
|
+
this.expiryTimer = setTimeout(() => this.update(), Math.min(remaining, 300000));
|
|
269
|
+
}
|
|
270
|
+
this.refreshVisibility();
|
|
271
|
+
}
|
|
272
|
+
/** The parent supplies real operation authority; a catalog never enables cart writes. */
|
|
273
|
+
setCartActionHandler(handler, canAdd) {
|
|
274
|
+
this.addHandler = handler;
|
|
275
|
+
this.canAdd = handler && canAdd ? canAdd : null;
|
|
276
|
+
this.update();
|
|
277
|
+
}
|
|
278
|
+
setBusy(busy) {
|
|
279
|
+
if (this.destroyed)
|
|
280
|
+
return;
|
|
281
|
+
this.busy = busy;
|
|
282
|
+
if (busy)
|
|
283
|
+
this.element.hidden = false;
|
|
284
|
+
this.update();
|
|
285
|
+
if (!busy)
|
|
286
|
+
this.refreshVisibility();
|
|
287
|
+
}
|
|
288
|
+
showError(message = "commerceError") {
|
|
289
|
+
if (this.destroyed)
|
|
290
|
+
return;
|
|
291
|
+
this.busy = false;
|
|
292
|
+
this.failed = true;
|
|
293
|
+
this.errorMessage = message;
|
|
294
|
+
this.element.hidden = false;
|
|
295
|
+
this.update();
|
|
296
|
+
}
|
|
297
|
+
clear() {
|
|
298
|
+
if (this.expiryTimer !== null)
|
|
299
|
+
clearTimeout(this.expiryTimer);
|
|
300
|
+
if (this.visibilityFrame !== null)
|
|
301
|
+
window.cancelAnimationFrame(this.visibilityFrame);
|
|
302
|
+
this.expiryTimer = null;
|
|
303
|
+
this.visibilityFrame = null;
|
|
304
|
+
this.observer?.disconnect();
|
|
305
|
+
this.catalog = null;
|
|
306
|
+
this.states = [];
|
|
307
|
+
this.comparison = null;
|
|
308
|
+
this.seen.clear();
|
|
309
|
+
this.displayed = false;
|
|
310
|
+
this.reported.clear();
|
|
311
|
+
this.busy = false;
|
|
312
|
+
this.failed = false;
|
|
313
|
+
this.errorMessage = "commerceError";
|
|
314
|
+
this.products.replaceChildren();
|
|
315
|
+
this.status.textContent = "";
|
|
316
|
+
this.ranking.textContent = "";
|
|
317
|
+
this.element.hidden = true;
|
|
318
|
+
this.element.removeAttribute("aria-busy");
|
|
319
|
+
this.element.removeAttribute("data-comparing");
|
|
320
|
+
}
|
|
321
|
+
destroy() {
|
|
322
|
+
this.clear();
|
|
323
|
+
this.destroyed = true;
|
|
324
|
+
this.addHandler = null;
|
|
325
|
+
this.canAdd = null;
|
|
326
|
+
document.removeEventListener("scroll", this.refreshVisibility, true);
|
|
327
|
+
document.removeEventListener("visibilitychange", this.refreshVisibility);
|
|
328
|
+
window.removeEventListener("resize", this.refreshVisibility);
|
|
329
|
+
this.element.remove();
|
|
330
|
+
}
|
|
331
|
+
button(key, action) {
|
|
332
|
+
const button = document.createElement("button");
|
|
333
|
+
button.type = "button";
|
|
334
|
+
button.textContent = this.message(key);
|
|
335
|
+
button.addEventListener("click", action);
|
|
336
|
+
return button;
|
|
337
|
+
}
|
|
338
|
+
renderProduct(product) {
|
|
339
|
+
const card = document.createElement("article");
|
|
340
|
+
card.className = "commerce-product";
|
|
341
|
+
card.dataset.productRef = product.productRef;
|
|
342
|
+
const imageUrl = product.image && safeUrl(product.image.url);
|
|
343
|
+
let image = null;
|
|
344
|
+
if (imageUrl) {
|
|
345
|
+
image = document.createElement("img");
|
|
346
|
+
image.className = "commerce-product-image";
|
|
347
|
+
image.src = imageUrl;
|
|
348
|
+
image.alt = product.image.alt;
|
|
349
|
+
image.loading = "lazy";
|
|
350
|
+
image.decoding = "async";
|
|
351
|
+
image.referrerPolicy = "no-referrer";
|
|
352
|
+
image.addEventListener("error", () => {
|
|
353
|
+
image.hidden = true;
|
|
354
|
+
image.dataset.failed = "true";
|
|
355
|
+
});
|
|
356
|
+
card.append(image);
|
|
357
|
+
}
|
|
358
|
+
const summary = document.createElement("div");
|
|
359
|
+
summary.className = "commerce-product-summary";
|
|
360
|
+
const title = document.createElement("h3");
|
|
361
|
+
title.textContent = product.title;
|
|
362
|
+
const price = document.createElement("p");
|
|
363
|
+
price.className = "commerce-product-price";
|
|
364
|
+
const availability = document.createElement("p");
|
|
365
|
+
availability.className = "commerce-product-availability";
|
|
366
|
+
summary.append(title, price, availability);
|
|
367
|
+
card.append(summary);
|
|
368
|
+
if (product.description) {
|
|
369
|
+
const description = document.createElement("p");
|
|
370
|
+
description.className = "commerce-product-description";
|
|
371
|
+
description.textContent = product.description;
|
|
372
|
+
card.append(description);
|
|
373
|
+
}
|
|
374
|
+
const compareLabel = document.createElement("label");
|
|
375
|
+
compareLabel.className = "commerce-product-compare";
|
|
376
|
+
const compare = document.createElement("input");
|
|
377
|
+
compare.type = "checkbox";
|
|
378
|
+
const compareText = document.createElement("span");
|
|
379
|
+
compareText.textContent = this.message("commerceCompareSelect")
|
|
380
|
+
.split("{product}")
|
|
381
|
+
.join(product.title);
|
|
382
|
+
compareLabel.append(compare, compareText);
|
|
383
|
+
compare.addEventListener("change", () => this.update());
|
|
384
|
+
card.append(compareLabel);
|
|
385
|
+
const state = {
|
|
386
|
+
product,
|
|
387
|
+
element: card,
|
|
388
|
+
image,
|
|
389
|
+
planNote: null,
|
|
390
|
+
summary,
|
|
391
|
+
price,
|
|
392
|
+
availability,
|
|
393
|
+
inputs: [],
|
|
394
|
+
select: null,
|
|
395
|
+
compare,
|
|
396
|
+
quantity: null,
|
|
397
|
+
add: null,
|
|
398
|
+
save: null,
|
|
399
|
+
dirty: false,
|
|
400
|
+
};
|
|
401
|
+
if (product.options.length) {
|
|
402
|
+
const options = document.createElement("fieldset");
|
|
403
|
+
options.className = "commerce-product-options";
|
|
404
|
+
const legend = document.createElement("legend");
|
|
405
|
+
legend.textContent = this.message("commerceOptions");
|
|
406
|
+
options.append(legend);
|
|
407
|
+
for (const option of product.options) {
|
|
408
|
+
const label = document.createElement("label");
|
|
409
|
+
label.className = "commerce-product-label";
|
|
410
|
+
const text = document.createElement("span");
|
|
411
|
+
text.textContent = option.name;
|
|
412
|
+
const input = document.createElement("select");
|
|
413
|
+
input.name = option.name;
|
|
414
|
+
const placeholder = document.createElement("option");
|
|
415
|
+
placeholder.value = "";
|
|
416
|
+
placeholder.textContent = this.message("commerceChoose")
|
|
417
|
+
.split("{option}")
|
|
418
|
+
.join(option.name);
|
|
419
|
+
input.append(placeholder);
|
|
420
|
+
for (const value of option.values) {
|
|
421
|
+
const entry = document.createElement("option");
|
|
422
|
+
entry.value = value;
|
|
423
|
+
entry.textContent = value;
|
|
424
|
+
input.append(entry);
|
|
425
|
+
}
|
|
426
|
+
input.value =
|
|
427
|
+
product.variant?.selectedOptions.find((choice) => choice.name === option.name)?.value ?? "";
|
|
428
|
+
input.addEventListener("change", () => {
|
|
429
|
+
state.dirty =
|
|
430
|
+
!product.variant ||
|
|
431
|
+
state.inputs.some((select) => product.variant.selectedOptions.find((choice) => choice.name === select.name)?.value !== select.value);
|
|
432
|
+
// The previously rendered variant cannot attest to a new option selection.
|
|
433
|
+
if (state.dirty)
|
|
434
|
+
this.seen.delete(product.productRef);
|
|
435
|
+
this.update();
|
|
436
|
+
this.refreshVisibility();
|
|
437
|
+
});
|
|
438
|
+
state.inputs.push(input);
|
|
439
|
+
label.append(text, input);
|
|
440
|
+
options.append(label);
|
|
441
|
+
}
|
|
442
|
+
state.select = this.button("commerceOptions", () => {
|
|
443
|
+
if (!this.fresh() ||
|
|
444
|
+
this.busy ||
|
|
445
|
+
state.inputs.some((input) => !input.value))
|
|
446
|
+
return;
|
|
447
|
+
const selected = state.inputs.map((input) => ({
|
|
448
|
+
name: input.name,
|
|
449
|
+
value: input.value,
|
|
450
|
+
}));
|
|
451
|
+
this.setBusy(true);
|
|
452
|
+
this.runAction(() => this.callbacks.onSelection(product.productRef, selected));
|
|
453
|
+
});
|
|
454
|
+
options.append(state.select);
|
|
455
|
+
card.append(options);
|
|
456
|
+
}
|
|
457
|
+
if (product.variant) {
|
|
458
|
+
const quantityLabel = document.createElement("label");
|
|
459
|
+
quantityLabel.className =
|
|
460
|
+
"commerce-product-label commerce-product-quantity";
|
|
461
|
+
const text = document.createElement("span");
|
|
462
|
+
text.textContent = this.message("commerceQuantity");
|
|
463
|
+
const quantity = document.createElement("input");
|
|
464
|
+
quantity.type = "number";
|
|
465
|
+
quantity.inputMode = "numeric";
|
|
466
|
+
quantity.required = true;
|
|
467
|
+
const rule = product.variant.quantityRule;
|
|
468
|
+
quantity.min = String(rule.minimum);
|
|
469
|
+
if (rule.maximum !== null)
|
|
470
|
+
quantity.max = String(rule.maximum);
|
|
471
|
+
quantity.step = String(rule.increment);
|
|
472
|
+
quantity.value = validQuantityRule(rule) ? String(rule.minimum) : "";
|
|
473
|
+
quantity.addEventListener("input", () => this.update());
|
|
474
|
+
quantityLabel.append(text, quantity);
|
|
475
|
+
state.quantity = quantity;
|
|
476
|
+
state.add = this.button("commerceAdd", () => {
|
|
477
|
+
if (!this.addAllowed(state))
|
|
478
|
+
return;
|
|
479
|
+
const amount = Number(quantity.value);
|
|
480
|
+
this.setBusy(true);
|
|
481
|
+
this.runAction(() => this.addHandler(product.productRef, product.variant.variantRef, amount), "commerceCartUncertain");
|
|
482
|
+
});
|
|
483
|
+
card.append(quantityLabel, state.add);
|
|
484
|
+
}
|
|
485
|
+
if (product.variant?.requiresSellingPlan) {
|
|
486
|
+
const note = document.createElement("p");
|
|
487
|
+
note.className = "commerce-product-note";
|
|
488
|
+
note.textContent = this.message("commerceSubscription");
|
|
489
|
+
state.planNote = note;
|
|
490
|
+
card.append(note);
|
|
491
|
+
}
|
|
492
|
+
if (this.callbacks.onSaveItem) {
|
|
493
|
+
state.save = this.button("shoppingNotesSaveItem", () => {
|
|
494
|
+
if (!this.saveAllowed(state))
|
|
495
|
+
return;
|
|
496
|
+
const label = product.title.trim();
|
|
497
|
+
this.callbacks.onSaveItem?.({
|
|
498
|
+
productRef: product.productRef,
|
|
499
|
+
variantRef: product.variant?.variantRef ?? null,
|
|
500
|
+
selectedOptions: product.variant?.selectedOptions.map((option) => ({ ...option })) ??
|
|
501
|
+
[],
|
|
502
|
+
...(label && label.length <= 120 ? { label } : {}),
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
card.append(state.save);
|
|
506
|
+
}
|
|
507
|
+
const url = safeUrl(product.url, this.catalog.store.origin);
|
|
508
|
+
if (url) {
|
|
509
|
+
const link = document.createElement("a");
|
|
510
|
+
link.className = "commerce-product-link";
|
|
511
|
+
link.href = url;
|
|
512
|
+
link.target = "_self";
|
|
513
|
+
link.textContent = this.message(product.options.some((option) => !option.complete)
|
|
514
|
+
? "commerceMoreOptions"
|
|
515
|
+
: "commerceView");
|
|
516
|
+
card.append(link);
|
|
517
|
+
}
|
|
518
|
+
return state;
|
|
519
|
+
}
|
|
520
|
+
fresh() {
|
|
521
|
+
return (!this.destroyed &&
|
|
522
|
+
!this.failed &&
|
|
523
|
+
!!this.catalog &&
|
|
524
|
+
Date.parse(this.catalog.expiresAt) > Date.now());
|
|
525
|
+
}
|
|
526
|
+
saveAllowed(state) {
|
|
527
|
+
return (this.fresh() &&
|
|
528
|
+
!this.busy &&
|
|
529
|
+
!state.dirty &&
|
|
530
|
+
!state.element.hidden &&
|
|
531
|
+
state.inputs.every((input) => input.value ===
|
|
532
|
+
(state.product.variant?.selectedOptions.find((option) => option.name === input.name)?.value ?? "")));
|
|
533
|
+
}
|
|
534
|
+
addAllowed(state) {
|
|
535
|
+
const variant = state.product.variant;
|
|
536
|
+
if (!this.fresh() ||
|
|
537
|
+
this.busy ||
|
|
538
|
+
state.dirty ||
|
|
539
|
+
!variant ||
|
|
540
|
+
!variant.available ||
|
|
541
|
+
variant.requiresSellingPlan ||
|
|
542
|
+
!state.quantity ||
|
|
543
|
+
!this.addHandler ||
|
|
544
|
+
!this.canAdd)
|
|
545
|
+
return false;
|
|
546
|
+
const quantity = Number(state.quantity.value);
|
|
547
|
+
const rule = variant.quantityRule;
|
|
548
|
+
if (!validQuantityRule(rule) ||
|
|
549
|
+
!state.quantity.value ||
|
|
550
|
+
!Number.isSafeInteger(quantity) ||
|
|
551
|
+
quantity < rule.minimum ||
|
|
552
|
+
(rule.maximum !== null && quantity > rule.maximum) ||
|
|
553
|
+
quantity % rule.increment !== 0)
|
|
554
|
+
return false;
|
|
555
|
+
try {
|
|
556
|
+
return (this.canAdd(state.product.productRef, variant.variantRef, quantity) ===
|
|
557
|
+
true);
|
|
558
|
+
}
|
|
559
|
+
catch {
|
|
560
|
+
return false;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
update() {
|
|
564
|
+
this.element.setAttribute("aria-busy", String(this.busy));
|
|
565
|
+
const fresh = this.fresh();
|
|
566
|
+
this.status.textContent = this.busy
|
|
567
|
+
? this.message("commerceLoading")
|
|
568
|
+
: this.failed
|
|
569
|
+
? this.message(this.errorMessage)
|
|
570
|
+
: this.catalog && !fresh
|
|
571
|
+
? this.message("commerceExpired")
|
|
572
|
+
: this.catalog && !this.states.length
|
|
573
|
+
? this.message("commerceEmpty")
|
|
574
|
+
: "";
|
|
575
|
+
this.status.hidden = !this.status.textContent;
|
|
576
|
+
this.ranking.hidden = !this.catalog || !this.states.length;
|
|
577
|
+
this.element.dataset.comparing = String(!!this.comparison);
|
|
578
|
+
let checked = 0;
|
|
579
|
+
for (const state of this.states) {
|
|
580
|
+
const product = state.product;
|
|
581
|
+
if (state.image)
|
|
582
|
+
state.image.hidden =
|
|
583
|
+
state.dirty || state.image.dataset.failed === "true";
|
|
584
|
+
if (state.planNote)
|
|
585
|
+
state.planNote.hidden = state.dirty;
|
|
586
|
+
state.element.hidden =
|
|
587
|
+
!!this.comparison && !this.comparison.has(product.productRef);
|
|
588
|
+
if (state.save)
|
|
589
|
+
state.save.disabled = !this.saveAllowed(state);
|
|
590
|
+
state.compare.disabled = !fresh || this.busy;
|
|
591
|
+
state.compare.parentElement.hidden = !!this.comparison;
|
|
592
|
+
if (state.compare.checked)
|
|
593
|
+
checked += 1;
|
|
594
|
+
for (const input of state.inputs)
|
|
595
|
+
input.disabled = !fresh || this.busy;
|
|
596
|
+
if (state.select)
|
|
597
|
+
state.select.disabled =
|
|
598
|
+
!fresh ||
|
|
599
|
+
this.busy ||
|
|
600
|
+
!state.dirty ||
|
|
601
|
+
state.inputs.some((input) => !input.value);
|
|
602
|
+
const variant = !state.dirty ? product.variant : null;
|
|
603
|
+
state.price.textContent = variant
|
|
604
|
+
? formatCommercePrice(variant.price.amount, variant.price.currency, this.catalog.market.locale)
|
|
605
|
+
: "";
|
|
606
|
+
state.price.hidden = !variant;
|
|
607
|
+
state.availability.textContent = this.message(!variant
|
|
608
|
+
? "commerceUnknown"
|
|
609
|
+
: variant.available
|
|
610
|
+
? "commerceAvailable"
|
|
611
|
+
: "commerceUnavailable");
|
|
612
|
+
if (state.quantity && state.add) {
|
|
613
|
+
const hasCartHandler = !!this.addHandler &&
|
|
614
|
+
!!this.canAdd &&
|
|
615
|
+
!!variant &&
|
|
616
|
+
variant.available &&
|
|
617
|
+
validQuantityRule(variant.quantityRule) &&
|
|
618
|
+
!variant.requiresSellingPlan;
|
|
619
|
+
state.quantity.parentElement.hidden = !hasCartHandler;
|
|
620
|
+
state.quantity.disabled = !fresh || this.busy || !hasCartHandler;
|
|
621
|
+
state.add.hidden = !hasCartHandler;
|
|
622
|
+
state.add.disabled = !this.addAllowed(state);
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
this.compareButton.hidden = !!this.comparison || this.states.length < 2;
|
|
626
|
+
this.compareButton.disabled = !fresh || this.busy || checked < 2;
|
|
627
|
+
this.backButton.hidden = !this.comparison;
|
|
628
|
+
this.backButton.disabled = this.busy;
|
|
629
|
+
this.refreshButton.disabled = this.busy;
|
|
630
|
+
}
|
|
631
|
+
compare() {
|
|
632
|
+
if (!this.fresh() || this.busy)
|
|
633
|
+
return;
|
|
634
|
+
const selected = this.states.filter((state) => state.compare.checked);
|
|
635
|
+
if (selected.length < 2)
|
|
636
|
+
return;
|
|
637
|
+
this.comparison = new Set(selected.map((state) => state.product.productRef));
|
|
638
|
+
this.update();
|
|
639
|
+
this.refreshVisibility();
|
|
640
|
+
this.backButton.focus({ preventScroll: true });
|
|
641
|
+
const productRefs = [...this.comparison];
|
|
642
|
+
this.runAction(() => this.callbacks.onCompare(productRefs));
|
|
643
|
+
}
|
|
644
|
+
runAction(action, error = "commerceError") {
|
|
645
|
+
const catalog = this.catalog;
|
|
646
|
+
const reject = () => {
|
|
647
|
+
if (!this.destroyed && this.catalog === catalog)
|
|
648
|
+
this.showError(error);
|
|
649
|
+
};
|
|
650
|
+
try {
|
|
651
|
+
void Promise.resolve(action()).catch(reject);
|
|
652
|
+
}
|
|
653
|
+
catch {
|
|
654
|
+
reject();
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
}
|