@xenia-martech/sdk-js 1.0.0-dev.27 → 1.0.0-dev.29
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/CSS_VARIABLES.md +7 -0
- package/README.md +50 -1
- package/dist/{actions-CkiSdvSa.d.cts → actions-CcBYV--3.d.cts} +177 -1
- package/dist/{actions-CkiSdvSa.d.ts → actions-CcBYV--3.d.ts} +177 -1
- package/dist/{chunk-UOW3GK4I.cjs → chunk-WYM4CQVG.cjs} +96 -4
- package/dist/chunk-WYM4CQVG.cjs.map +1 -0
- package/dist/{chunk-7D2HJMW6.js → chunk-XXNDHYAS.js} +96 -4
- package/dist/chunk-XXNDHYAS.js.map +1 -0
- package/dist/client/index.cjs +543 -27
- package/dist/client/index.cjs.map +1 -1
- package/dist/client/index.d.cts +112 -3
- package/dist/client/index.d.ts +112 -3
- package/dist/client/index.js +540 -28
- package/dist/client/index.js.map +1 -1
- package/dist/client/react/index.cjs +539 -27
- package/dist/client/react/index.cjs.map +1 -1
- package/dist/client/react/index.d.cts +2 -2
- package/dist/client/react/index.d.ts +2 -2
- package/dist/client/react/index.js +539 -27
- package/dist/client/react/index.js.map +1 -1
- package/dist/{cookie-names-C3LKDhsg.d.cts → cookie-names-B3b0XNc2.d.cts} +153 -1
- package/dist/{cookie-names-C3LKDhsg.d.ts → cookie-names-B3b0XNc2.d.ts} +153 -1
- package/dist/index.cjs +20 -20
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/server/index.cjs +15 -15
- package/dist/server/index.d.cts +2 -2
- package/dist/server/index.d.ts +2 -2
- package/dist/server/index.js +2 -2
- package/dist/xenia-sdk.iife.js +37 -3
- package/dist/xenia-sdk.iife.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-7D2HJMW6.js.map +0 -1
- package/dist/chunk-UOW3GK4I.cjs.map +0 -1
|
@@ -20,6 +20,7 @@ var MESSAGE_POSITIONS = /* @__PURE__ */ new Set([
|
|
|
20
20
|
"top",
|
|
21
21
|
"bottom"
|
|
22
22
|
]);
|
|
23
|
+
var WIDGET_TYPES = /* @__PURE__ */ new Set(["standard", "spin-wheel"]);
|
|
23
24
|
var ALL_TYPES = /* @__PURE__ */ new Set([
|
|
24
25
|
"swap",
|
|
25
26
|
"hide",
|
|
@@ -66,15 +67,67 @@ function toAction(raw) {
|
|
|
66
67
|
message: asString(raw.message),
|
|
67
68
|
imageUrl: asString(raw.imageUrl),
|
|
68
69
|
html: asString(raw.html),
|
|
69
|
-
|
|
70
|
+
// A link-only CTA (empty label, e.g. a spin-wheel's data-xenia-wheel-cta —
|
|
71
|
+
// label is meaningless there, the winHtml/lossHtml design supplies its own button text)
|
|
72
|
+
// must still parse through — gating on `cta.label` alone silently dropped it.
|
|
73
|
+
cta: cta && (cta.label || cta.url) ? cta : void 0,
|
|
70
74
|
dismissAfterMs: asNumber(raw.dismissAfterMs),
|
|
71
|
-
position: asMessagePosition(raw.position)
|
|
75
|
+
position: asMessagePosition(raw.position),
|
|
76
|
+
widgetType: asWidgetType(raw.widgetType),
|
|
77
|
+
segments: asSegments(raw.segments),
|
|
78
|
+
formSubmit: asFormSubmit(raw.formSubmit),
|
|
79
|
+
style: asWidgetStyle(raw.style),
|
|
80
|
+
spinnerHtml: asString(raw.spinnerHtml),
|
|
81
|
+
winHtml: asString(raw.winHtml),
|
|
82
|
+
lossHtml: asString(raw.lossHtml)
|
|
72
83
|
};
|
|
73
84
|
}
|
|
74
85
|
function asMessagePosition(v) {
|
|
75
86
|
const s = asString(v);
|
|
76
87
|
return s && MESSAGE_POSITIONS.has(s) ? s : void 0;
|
|
77
88
|
}
|
|
89
|
+
function asWidgetType(v) {
|
|
90
|
+
const s = asString(v);
|
|
91
|
+
return s && WIDGET_TYPES.has(s) ? s : void 0;
|
|
92
|
+
}
|
|
93
|
+
function asSegments(v) {
|
|
94
|
+
if (!Array.isArray(v)) return void 0;
|
|
95
|
+
const segments = [];
|
|
96
|
+
for (const entry of v) {
|
|
97
|
+
if (!isObject(entry)) continue;
|
|
98
|
+
const id = asString(entry.id);
|
|
99
|
+
const label = asString(entry.label);
|
|
100
|
+
if (!id || !label) continue;
|
|
101
|
+
segments.push({
|
|
102
|
+
id,
|
|
103
|
+
label,
|
|
104
|
+
colorHex: asString(entry.colorHex),
|
|
105
|
+
isLoss: entry.isLoss === true
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return segments.length ? segments : void 0;
|
|
109
|
+
}
|
|
110
|
+
function asFormSubmit(v) {
|
|
111
|
+
if (!isObject(v)) return void 0;
|
|
112
|
+
const mode = asString(v.mode);
|
|
113
|
+
if (!mode) return void 0;
|
|
114
|
+
return {
|
|
115
|
+
requiresResolve: v.requiresResolve === true,
|
|
116
|
+
mode,
|
|
117
|
+
flowRef: asString(v.flowRef),
|
|
118
|
+
bindingRef: asString(v.bindingRef),
|
|
119
|
+
successDisplayMs: asNumber(v.successDisplayMs)
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function asWidgetStyle(v) {
|
|
123
|
+
if (!isObject(v)) return void 0;
|
|
124
|
+
const style = {
|
|
125
|
+
hideCloseButton: v.hideCloseButton === true,
|
|
126
|
+
hideOverlay: v.hideOverlay === true
|
|
127
|
+
};
|
|
128
|
+
const hasAny = style.hideCloseButton || style.hideOverlay;
|
|
129
|
+
return hasAny ? style : void 0;
|
|
130
|
+
}
|
|
78
131
|
function parseActions(body) {
|
|
79
132
|
let list;
|
|
80
133
|
if (Array.isArray(body)) {
|
|
@@ -257,6 +310,12 @@ var TokenProvider = class {
|
|
|
257
310
|
};
|
|
258
311
|
|
|
259
312
|
// src/core/api.ts
|
|
313
|
+
function isJsonObject(v) {
|
|
314
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
315
|
+
}
|
|
316
|
+
function asWidgetPrize(v) {
|
|
317
|
+
return v !== void 0 && isJsonObject(v) ? v : void 0;
|
|
318
|
+
}
|
|
260
319
|
function compact(obj) {
|
|
261
320
|
const out = {};
|
|
262
321
|
for (const [k, v] of Object.entries(obj)) {
|
|
@@ -326,6 +385,39 @@ var XeniaApi = class {
|
|
|
326
385
|
receivedAt: Date.now()
|
|
327
386
|
};
|
|
328
387
|
}
|
|
388
|
+
/**
|
|
389
|
+
* Widget form submission — `POST /widget-submissions?ref={bindingRef}`. Used whenever the
|
|
390
|
+
* action's `formSubmit.requiresResolve` is true (spin-wheel, which must have a
|
|
391
|
+
* server-resolved outcome before anything renders) or `mode === "endpoint"` — the common
|
|
392
|
+
* plain-popup case (`mode === "journey" && !requiresResolve`) bypasses this entirely and calls
|
|
393
|
+
* {@link webhook} against `flowRef` instead (see `XeniaClient.submitWidgetForm`'s endpoint
|
|
394
|
+
* selection).
|
|
395
|
+
*
|
|
396
|
+
* Not retried (`noRetry: true`), styled like {@link webhook} — a retried POST must never
|
|
397
|
+
* double-fire a journey or double-resolve a prize server-side.
|
|
398
|
+
*/
|
|
399
|
+
async submitWidget(ref, body) {
|
|
400
|
+
const { response, data: rawBody } = await this.http.request({
|
|
401
|
+
method: "POST",
|
|
402
|
+
path: "/widget-submissions",
|
|
403
|
+
query: { ref },
|
|
404
|
+
headers: await this.authHeaders(),
|
|
405
|
+
body: body ?? {},
|
|
406
|
+
noRetry: true
|
|
407
|
+
});
|
|
408
|
+
const obj = isJsonObject(rawBody) ? rawBody : {};
|
|
409
|
+
return {
|
|
410
|
+
status: response.status,
|
|
411
|
+
body: rawBody,
|
|
412
|
+
actions: parseActions(rawBody),
|
|
413
|
+
prize: asWidgetPrize(obj.prize),
|
|
414
|
+
alreadyPlayed: obj.alreadyPlayed === true,
|
|
415
|
+
dispatched: obj.dispatched === true,
|
|
416
|
+
dispatchError: typeof obj.dispatchError === "string" ? obj.dispatchError : void 0,
|
|
417
|
+
widgetType: typeof obj.widgetType === "string" ? obj.widgetType : void 0,
|
|
418
|
+
receivedAt: Date.now()
|
|
419
|
+
};
|
|
420
|
+
}
|
|
329
421
|
/**
|
|
330
422
|
* Unified event batch (max 100/request per the backend) — `POST /api/events/v1`.
|
|
331
423
|
* Records analytics for every event and matches/advances a journey for any
|
|
@@ -1093,6 +1185,319 @@ function resolveCookieConfig(opt) {
|
|
|
1093
1185
|
return { sameSite, secure, maxAgeDays: o.maxAgeDays ?? 365, domain: o.domain };
|
|
1094
1186
|
}
|
|
1095
1187
|
|
|
1188
|
+
// src/client/widgets/form.ts
|
|
1189
|
+
function collectFields(root) {
|
|
1190
|
+
const fields = {};
|
|
1191
|
+
root.querySelectorAll("[data-xenia-field]").forEach((el) => {
|
|
1192
|
+
const name = el.getAttribute("data-xenia-field");
|
|
1193
|
+
if (!name) return;
|
|
1194
|
+
fields[name] = fieldValue(el);
|
|
1195
|
+
});
|
|
1196
|
+
return fields;
|
|
1197
|
+
}
|
|
1198
|
+
function fieldValue(el) {
|
|
1199
|
+
if (el instanceof HTMLInputElement) {
|
|
1200
|
+
if (el.type === "checkbox") return el.checked ? el.value || "on" : "";
|
|
1201
|
+
if (el.type === "radio") return el.checked ? el.value : "";
|
|
1202
|
+
return el.value;
|
|
1203
|
+
}
|
|
1204
|
+
if (el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement) return el.value;
|
|
1205
|
+
return el.textContent?.trim() ?? "";
|
|
1206
|
+
}
|
|
1207
|
+
function setDisabled(el, disabled) {
|
|
1208
|
+
if (!el) return;
|
|
1209
|
+
if (el instanceof HTMLButtonElement || el instanceof HTMLInputElement || el instanceof HTMLSelectElement || el instanceof HTMLTextAreaElement || el instanceof HTMLFieldSetElement) {
|
|
1210
|
+
el.disabled = disabled;
|
|
1211
|
+
} else if (disabled) {
|
|
1212
|
+
el.setAttribute("aria-disabled", "true");
|
|
1213
|
+
} else {
|
|
1214
|
+
el.removeAttribute("aria-disabled");
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
function showError(root, message) {
|
|
1218
|
+
const el = root.querySelector("[data-xenia-error]");
|
|
1219
|
+
if (el) el.textContent = message;
|
|
1220
|
+
}
|
|
1221
|
+
var SUBMIT_STYLE_ID = "xenia-form-submit-style";
|
|
1222
|
+
function ensureSubmitStyles(root) {
|
|
1223
|
+
if (root.querySelector(`#${SUBMIT_STYLE_ID}`)) return;
|
|
1224
|
+
const style = document.createElement("style");
|
|
1225
|
+
style.id = SUBMIT_STYLE_ID;
|
|
1226
|
+
style.textContent = "@keyframes xenia-submit-spin{to{transform:rotate(360deg)}}[data-xenia-submitting]::after{content:'';display:inline-block;width:0.85em;height:0.85em;margin-left:0.5em;vertical-align:-0.15em;border:2px solid currentColor;border-right-color:transparent;border-radius:50%;animation:xenia-submit-spin 0.6s linear infinite}";
|
|
1227
|
+
root.prepend(style);
|
|
1228
|
+
}
|
|
1229
|
+
function setSubmitting(el, submitting) {
|
|
1230
|
+
if (!el) return;
|
|
1231
|
+
if (submitting) el.setAttribute("data-xenia-submitting", "");
|
|
1232
|
+
else el.removeAttribute("data-xenia-submitting");
|
|
1233
|
+
}
|
|
1234
|
+
function fadeOutAndDismiss(root, dismiss) {
|
|
1235
|
+
if (!dismiss) return;
|
|
1236
|
+
const host = root instanceof ShadowRoot ? root.host : root;
|
|
1237
|
+
if (host instanceof HTMLElement) {
|
|
1238
|
+
host.style.transition = "opacity 220ms ease, transform 220ms ease";
|
|
1239
|
+
host.style.opacity = "0";
|
|
1240
|
+
host.style.transform = "scale(0.96)";
|
|
1241
|
+
window.setTimeout(dismiss, 220);
|
|
1242
|
+
} else {
|
|
1243
|
+
dismiss();
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
var DEFAULT_SUCCESS_DISPLAY_MS = 900;
|
|
1247
|
+
function revealSuccess(root, form) {
|
|
1248
|
+
const successEl = root.querySelector("[data-xenia-success]");
|
|
1249
|
+
if (!successEl) return false;
|
|
1250
|
+
if (successEl instanceof HTMLElement) successEl.hidden = false;
|
|
1251
|
+
else successEl.removeAttribute("hidden");
|
|
1252
|
+
if (form) {
|
|
1253
|
+
form.hidden = true;
|
|
1254
|
+
} else {
|
|
1255
|
+
root.querySelectorAll("[data-xenia-field], [data-xenia-submit]").forEach((el) => {
|
|
1256
|
+
el.hidden = true;
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
return true;
|
|
1260
|
+
}
|
|
1261
|
+
function wireFormSubmit(root, action, ctx) {
|
|
1262
|
+
const form = root.querySelector("form");
|
|
1263
|
+
let submitting = false;
|
|
1264
|
+
ensureSubmitStyles(root);
|
|
1265
|
+
const run = (control, fields) => {
|
|
1266
|
+
if (submitting) return;
|
|
1267
|
+
if (!action.formSubmit) {
|
|
1268
|
+
showError(root, "This form isn't connected yet.");
|
|
1269
|
+
return;
|
|
1270
|
+
}
|
|
1271
|
+
submitting = true;
|
|
1272
|
+
showError(root, "");
|
|
1273
|
+
setDisabled(control, true);
|
|
1274
|
+
setSubmitting(control, true);
|
|
1275
|
+
const startedAt = Date.now();
|
|
1276
|
+
const minDisplayMs = action.formSubmit.successDisplayMs ?? DEFAULT_SUCCESS_DISPLAY_MS;
|
|
1277
|
+
ctx.submitWidgetForm(fields).then(() => {
|
|
1278
|
+
setSubmitting(control, false);
|
|
1279
|
+
revealSuccess(root, form);
|
|
1280
|
+
const wait = Math.max(0, minDisplayMs - (Date.now() - startedAt));
|
|
1281
|
+
window.setTimeout(() => fadeOutAndDismiss(root, ctx.dismiss), wait);
|
|
1282
|
+
}).catch(() => {
|
|
1283
|
+
setSubmitting(control, false);
|
|
1284
|
+
setDisabled(control, false);
|
|
1285
|
+
showError(root, "Something went wrong. Please try again.");
|
|
1286
|
+
}).finally(() => {
|
|
1287
|
+
submitting = false;
|
|
1288
|
+
});
|
|
1289
|
+
};
|
|
1290
|
+
if (form) {
|
|
1291
|
+
form.addEventListener("submit", (ev) => {
|
|
1292
|
+
ev.preventDefault();
|
|
1293
|
+
const formFields = {};
|
|
1294
|
+
new FormData(form).forEach((value, key) => {
|
|
1295
|
+
if (typeof value === "string") formFields[key] = value;
|
|
1296
|
+
});
|
|
1297
|
+
const submitter = ev.submitter ?? form.querySelector('[type="submit"], button:not([type])');
|
|
1298
|
+
run(submitter, { ...collectFields(root), ...formFields });
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
root.querySelectorAll("[data-xenia-submit]").forEach((el) => {
|
|
1302
|
+
if (form?.contains(el)) return;
|
|
1303
|
+
el.addEventListener("click", () => run(el, collectFields(root)));
|
|
1304
|
+
});
|
|
1305
|
+
}
|
|
1306
|
+
|
|
1307
|
+
// src/client/widgets/types.ts
|
|
1308
|
+
function escapeHtml(s) {
|
|
1309
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1310
|
+
}
|
|
1311
|
+
function escapeAttr(s) {
|
|
1312
|
+
return escapeHtml(s).replace(/"/g, """);
|
|
1313
|
+
}
|
|
1314
|
+
function wireCloseAffordances(root, dismiss) {
|
|
1315
|
+
if (dismiss) {
|
|
1316
|
+
const closeEls = root.querySelectorAll(
|
|
1317
|
+
".xenia-close, [data-xenia-close], [class*='close' i], [id*='close' i], [class*='backdrop' i], [id*='backdrop' i]"
|
|
1318
|
+
);
|
|
1319
|
+
closeEls.forEach((el) => {
|
|
1320
|
+
el.removeAttribute("onclick");
|
|
1321
|
+
el.addEventListener("click", dismiss);
|
|
1322
|
+
});
|
|
1323
|
+
}
|
|
1324
|
+
root.querySelectorAll("[class*='backdrop' i], [id*='backdrop' i]").forEach((el) => {
|
|
1325
|
+
if (el instanceof HTMLElement && el.children.length === 0 && !el.textContent?.trim()) {
|
|
1326
|
+
el.style.opacity = "0.5";
|
|
1327
|
+
}
|
|
1328
|
+
});
|
|
1329
|
+
}
|
|
1330
|
+
|
|
1331
|
+
// src/client/widgets/spin-wheel.ts
|
|
1332
|
+
var WHEEL_SIZE = 260;
|
|
1333
|
+
var RADIUS = WHEEL_SIZE / 2;
|
|
1334
|
+
var CENTER = WHEEL_SIZE / 2;
|
|
1335
|
+
var DEFAULT_COLORS = ["#7c3aed", "#2563eb", "#059669", "#d97706", "#dc2626", "#0891b2"];
|
|
1336
|
+
var SPIN_TRANSITION = "transform 3.2s cubic-bezier(0.15, 0.85, 0.25, 1)";
|
|
1337
|
+
var WHEEL_POINTER_HTML = '<div class="xenia-wheel-pointer" aria-hidden="true"></div>';
|
|
1338
|
+
var REVEAL_TRANSITION_MS = 320;
|
|
1339
|
+
var REVEAL_TRANSITION = `opacity ${REVEAL_TRANSITION_MS}ms ease, transform ${REVEAL_TRANSITION_MS}ms ease`;
|
|
1340
|
+
var REVEAL_PERSPECTIVE_PX = 900;
|
|
1341
|
+
var revealTransform = (scale, depthPx) => `perspective(${REVEAL_PERSPECTIVE_PX}px) translateZ(${depthPx}px) scale(${scale})`;
|
|
1342
|
+
function polarPoint(angleDeg, radius) {
|
|
1343
|
+
const rad = angleDeg * Math.PI / 180;
|
|
1344
|
+
return { x: CENTER + radius * Math.sin(rad), y: CENTER - radius * Math.cos(rad) };
|
|
1345
|
+
}
|
|
1346
|
+
function segmentPath(startAngle, endAngle) {
|
|
1347
|
+
const start = polarPoint(startAngle, RADIUS);
|
|
1348
|
+
const end = polarPoint(endAngle, RADIUS);
|
|
1349
|
+
const largeArc = endAngle - startAngle > 180 ? 1 : 0;
|
|
1350
|
+
return `M ${CENTER},${CENTER} L ${start.x},${start.y} A ${RADIUS},${RADIUS} 0 ${largeArc} 1 ${end.x},${end.y} Z`;
|
|
1351
|
+
}
|
|
1352
|
+
function computeSpinRotation(segmentIndex, segmentCount, extraSpins = 0) {
|
|
1353
|
+
if (segmentCount <= 0) return 0;
|
|
1354
|
+
const segmentAngle = 360 / segmentCount;
|
|
1355
|
+
const centerAngle = segmentIndex * segmentAngle + segmentAngle / 2;
|
|
1356
|
+
const landingAngle = (360 - centerAngle) % 360;
|
|
1357
|
+
return extraSpins * 360 + landingAngle;
|
|
1358
|
+
}
|
|
1359
|
+
function crossFadeReveal(outgoing, incoming) {
|
|
1360
|
+
const outContent = outgoing ? Array.from(outgoing.children) : [];
|
|
1361
|
+
const inContent = incoming ? Array.from(incoming.children) : [];
|
|
1362
|
+
if (incoming) incoming.hidden = false;
|
|
1363
|
+
inContent.forEach((el) => {
|
|
1364
|
+
if (!(el instanceof HTMLElement)) return;
|
|
1365
|
+
el.style.transition = "none";
|
|
1366
|
+
el.style.opacity = "0";
|
|
1367
|
+
el.style.transform = revealTransform(0.94, -60);
|
|
1368
|
+
});
|
|
1369
|
+
inContent.forEach((el) => {
|
|
1370
|
+
if (el instanceof HTMLElement) void el.offsetHeight;
|
|
1371
|
+
});
|
|
1372
|
+
nextFrame(() => {
|
|
1373
|
+
outContent.forEach((el) => {
|
|
1374
|
+
if (!(el instanceof HTMLElement)) return;
|
|
1375
|
+
el.style.transition = REVEAL_TRANSITION;
|
|
1376
|
+
el.style.opacity = "0";
|
|
1377
|
+
el.style.transform = revealTransform(0.94, -60);
|
|
1378
|
+
});
|
|
1379
|
+
inContent.forEach((el) => {
|
|
1380
|
+
if (!(el instanceof HTMLElement)) return;
|
|
1381
|
+
el.style.transition = REVEAL_TRANSITION;
|
|
1382
|
+
el.style.opacity = "1";
|
|
1383
|
+
el.style.transform = revealTransform(1, 0);
|
|
1384
|
+
});
|
|
1385
|
+
});
|
|
1386
|
+
if (outgoing) {
|
|
1387
|
+
setTimeout(() => {
|
|
1388
|
+
outgoing.hidden = true;
|
|
1389
|
+
}, REVEAL_TRANSITION_MS);
|
|
1390
|
+
}
|
|
1391
|
+
}
|
|
1392
|
+
function nextFrame(fn) {
|
|
1393
|
+
if (typeof requestAnimationFrame === "function") requestAnimationFrame(fn);
|
|
1394
|
+
else setTimeout(fn, 0);
|
|
1395
|
+
}
|
|
1396
|
+
function wheelSvg(segments) {
|
|
1397
|
+
const n = segments.length || 1;
|
|
1398
|
+
const segmentAngle = 360 / n;
|
|
1399
|
+
const slices = segments.map((seg, i) => {
|
|
1400
|
+
const start = i * segmentAngle;
|
|
1401
|
+
const end = start + segmentAngle;
|
|
1402
|
+
const color = seg.colorHex || DEFAULT_COLORS[i % DEFAULT_COLORS.length];
|
|
1403
|
+
const labelAngle = start + segmentAngle / 2;
|
|
1404
|
+
const labelPoint = polarPoint(labelAngle, RADIUS * 0.62);
|
|
1405
|
+
return `<path d="${segmentPath(start, end)}" fill="${escapeAttr(color)}" stroke="#fff" stroke-width="1"></path><text x="${labelPoint.x}" y="${labelPoint.y}" fill="#fff" font-size="12" text-anchor="middle" transform="rotate(${labelAngle} ${labelPoint.x} ${labelPoint.y})">${escapeHtml(seg.label)}</text>`;
|
|
1406
|
+
}).join("");
|
|
1407
|
+
return `<svg class="xenia-wheel-svg" viewBox="0 0 ${WHEEL_SIZE} ${WHEEL_SIZE}" width="${WHEEL_SIZE}" height="${WHEEL_SIZE}"><g class="xenia-wheel-dial">${slices}</g><circle class="xenia-wheel-hub" cx="${CENTER}" cy="${CENTER}" r="6"></circle></svg>`;
|
|
1408
|
+
}
|
|
1409
|
+
var spinWheelRenderer = {
|
|
1410
|
+
html(action) {
|
|
1411
|
+
return `<div class="xenia-spin-wheel"><div data-xenia-wheel-face>${action.spinnerHtml ?? ""}</div><div class="xenia-widget-result" data-xenia-wheel-result hidden>${action.winHtml ?? ""}</div><div class="xenia-widget-result" data-xenia-wheel-loss hidden>${action.lossHtml ?? ""}</div></div>`;
|
|
1412
|
+
},
|
|
1413
|
+
wire(root, action, ctx) {
|
|
1414
|
+
wireCloseAffordances(root, ctx.dismiss);
|
|
1415
|
+
const segments = action.segments ?? [];
|
|
1416
|
+
const wheelFace = root.querySelector("[data-xenia-wheel-face]");
|
|
1417
|
+
const spinBtn = root.querySelector("[data-xenia-spin]");
|
|
1418
|
+
const winEl = root.querySelector("[data-xenia-wheel-result]");
|
|
1419
|
+
const lossEl = root.querySelector("[data-xenia-wheel-loss]");
|
|
1420
|
+
const errorEl = root.querySelector("[data-xenia-error]");
|
|
1421
|
+
const slotEl = root.querySelector("[data-xenia-wheel-slot]");
|
|
1422
|
+
if (slotEl) slotEl.insertAdjacentHTML("afterbegin", wheelSvg(segments) + WHEEL_POINTER_HTML);
|
|
1423
|
+
const dial = root.querySelector(".xenia-wheel-dial");
|
|
1424
|
+
if (!spinBtn || !action.formSubmit) return;
|
|
1425
|
+
root.querySelectorAll("[data-xenia-wheel-cta]").forEach((btn) => {
|
|
1426
|
+
btn.addEventListener("click", () => ctx.fireCta());
|
|
1427
|
+
});
|
|
1428
|
+
const revealResult = (prizeLabel, isLoss, canRetry) => {
|
|
1429
|
+
root.querySelectorAll("[data-xenia-wheel-prize]").forEach((el) => {
|
|
1430
|
+
el.textContent = prizeLabel;
|
|
1431
|
+
});
|
|
1432
|
+
const shown = isLoss ? lossEl : winEl;
|
|
1433
|
+
const other = isLoss ? winEl : lossEl;
|
|
1434
|
+
if (other) other.hidden = true;
|
|
1435
|
+
crossFadeReveal(wheelFace, shown);
|
|
1436
|
+
if (!canRetry) {
|
|
1437
|
+
playsExhausted = true;
|
|
1438
|
+
root.querySelectorAll("[data-xenia-spin-again]").forEach((btn) => {
|
|
1439
|
+
btn.hidden = true;
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
let spinning = false;
|
|
1444
|
+
let playsExhausted = false;
|
|
1445
|
+
root.querySelectorAll("[data-xenia-spin-again]").forEach((btn) => {
|
|
1446
|
+
btn.addEventListener("click", () => {
|
|
1447
|
+
if (playsExhausted) return;
|
|
1448
|
+
const shown = winEl && !winEl.hidden ? winEl : lossEl && !lossEl.hidden ? lossEl : null;
|
|
1449
|
+
if (!shown) return;
|
|
1450
|
+
spinning = false;
|
|
1451
|
+
spinBtn.disabled = false;
|
|
1452
|
+
if (errorEl) errorEl.textContent = "";
|
|
1453
|
+
crossFadeReveal(shown, wheelFace);
|
|
1454
|
+
});
|
|
1455
|
+
});
|
|
1456
|
+
spinBtn.addEventListener("click", () => {
|
|
1457
|
+
if (spinning) return;
|
|
1458
|
+
spinning = true;
|
|
1459
|
+
spinBtn.disabled = true;
|
|
1460
|
+
if (errorEl) errorEl.textContent = "";
|
|
1461
|
+
const fields = collectFields(root);
|
|
1462
|
+
ctx.submitWidgetForm(fields).then((response) => {
|
|
1463
|
+
const index = segments.findIndex((s) => s.id === response.prize?.segmentId);
|
|
1464
|
+
const prizeLabel = response.prize?.label ?? segments[index]?.label ?? "";
|
|
1465
|
+
const isLoss = index >= 0 && !!segments[index]?.isLoss;
|
|
1466
|
+
const canRetry = response.alreadyPlayed !== true;
|
|
1467
|
+
if (index < 0 || !dial) {
|
|
1468
|
+
revealResult(prizeLabel, isLoss, canRetry);
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
const extraSpins = 3 + Math.floor(Math.random() * 3);
|
|
1472
|
+
const rotation = computeSpinRotation(index, segments.length || 1, extraSpins);
|
|
1473
|
+
const priorMatch = /rotate\(([-\d.]+)deg\)/.exec(dial.style.transform);
|
|
1474
|
+
const priorRotation = priorMatch ? parseFloat(priorMatch[1]) : 0;
|
|
1475
|
+
dial.style.transition = "none";
|
|
1476
|
+
dial.style.transform = `rotate(${priorRotation % 360}deg)`;
|
|
1477
|
+
void dial.getBoundingClientRect();
|
|
1478
|
+
dial.style.transition = SPIN_TRANSITION;
|
|
1479
|
+
const onTransitionEnd = () => {
|
|
1480
|
+
dial.removeEventListener("transitionend", onTransitionEnd);
|
|
1481
|
+
revealResult(prizeLabel, isLoss, canRetry);
|
|
1482
|
+
};
|
|
1483
|
+
dial.addEventListener("transitionend", onTransitionEnd);
|
|
1484
|
+
nextFrame(() => {
|
|
1485
|
+
dial.style.transform = `rotate(${rotation}deg)`;
|
|
1486
|
+
});
|
|
1487
|
+
}).catch(() => {
|
|
1488
|
+
spinning = false;
|
|
1489
|
+
spinBtn.disabled = false;
|
|
1490
|
+
if (errorEl) errorEl.textContent = "Something went wrong. Please try again.";
|
|
1491
|
+
});
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
|
|
1496
|
+
// src/client/widgets/index.ts
|
|
1497
|
+
var WIDGET_RENDERERS = {
|
|
1498
|
+
"spin-wheel": spinWheelRenderer
|
|
1499
|
+
};
|
|
1500
|
+
|
|
1096
1501
|
// src/client/renderer.ts
|
|
1097
1502
|
function resolveTarget(target, root = document) {
|
|
1098
1503
|
if (!target) return null;
|
|
@@ -1150,6 +1555,7 @@ var ActionRenderer = class {
|
|
|
1150
1555
|
messageTemplate;
|
|
1151
1556
|
messageStyles;
|
|
1152
1557
|
messageComponent;
|
|
1558
|
+
submitWidgetFormFn;
|
|
1153
1559
|
/** Unmount callbacks for currently-mounted framework components, keyed by their container. */
|
|
1154
1560
|
mounted = /* @__PURE__ */ new Map();
|
|
1155
1561
|
constructor(onCta, opts) {
|
|
@@ -1157,6 +1563,7 @@ var ActionRenderer = class {
|
|
|
1157
1563
|
this.messageTemplate = opts?.messageTemplate;
|
|
1158
1564
|
this.messageStyles = opts?.messageStyles;
|
|
1159
1565
|
this.messageComponent = opts?.messageComponent;
|
|
1566
|
+
this.submitWidgetFormFn = opts?.submitWidgetForm;
|
|
1160
1567
|
}
|
|
1161
1568
|
/** Apply a list of actions. Returns the count successfully applied. */
|
|
1162
1569
|
apply(actions) {
|
|
@@ -1239,7 +1646,8 @@ ${this.messageStyles}` : STYLES;
|
|
|
1239
1646
|
const wrap = document.createElement("div");
|
|
1240
1647
|
const posClass = this.positionClass(action.type, action.position);
|
|
1241
1648
|
const customBgClass = action.html !== void 0 ? " xenia-custom-bg" : "";
|
|
1242
|
-
|
|
1649
|
+
const noOverlayClass = action.style?.hideOverlay ? " xenia-no-overlay" : "";
|
|
1650
|
+
wrap.className = `xenia-msg xenia-${action.type}${posClass ? ` ${posClass}` : ""}${customBgClass}${noOverlayClass}`;
|
|
1243
1651
|
shadow.appendChild(wrap);
|
|
1244
1652
|
const dismiss = () => {
|
|
1245
1653
|
this.unmount(wrap);
|
|
@@ -1252,7 +1660,10 @@ ${this.messageStyles}` : STYLES;
|
|
|
1252
1660
|
this.wireCard(wrap, action, dismiss);
|
|
1253
1661
|
}
|
|
1254
1662
|
if (action.dismissAfterMs && action.dismissAfterMs > 0) {
|
|
1255
|
-
setTimeout(dismiss, action.dismissAfterMs);
|
|
1663
|
+
const timer = setTimeout(dismiss, action.dismissAfterMs);
|
|
1664
|
+
const cancelAutoDismiss = () => clearTimeout(timer);
|
|
1665
|
+
wrap.addEventListener("click", cancelAutoDismiss, { once: true });
|
|
1666
|
+
wrap.addEventListener("focusin", cancelAutoDismiss, { once: true });
|
|
1256
1667
|
}
|
|
1257
1668
|
return true;
|
|
1258
1669
|
}
|
|
@@ -1296,38 +1707,32 @@ ${this.messageStyles}` : STYLES;
|
|
|
1296
1707
|
return "";
|
|
1297
1708
|
}
|
|
1298
1709
|
cardHtml(action, variant) {
|
|
1710
|
+
const widget = action.widgetType ? WIDGET_RENDERERS[action.widgetType] : void 0;
|
|
1711
|
+
if (widget) return widget.html(action, variant);
|
|
1299
1712
|
if (action.html !== void 0) return wrapCustomHtml(action.html, variant);
|
|
1300
1713
|
if (this.messageTemplate) {
|
|
1301
1714
|
const ctx = {
|
|
1302
1715
|
action,
|
|
1303
1716
|
variant,
|
|
1304
|
-
escapeHtml,
|
|
1305
|
-
escapeAttr
|
|
1717
|
+
escapeHtml: escapeHtml2,
|
|
1718
|
+
escapeAttr: escapeAttr2
|
|
1306
1719
|
};
|
|
1307
1720
|
return this.messageTemplate(ctx);
|
|
1308
1721
|
}
|
|
1309
|
-
const img = action.imageUrl ? `<img class="xenia-img" src="${
|
|
1310
|
-
const title = action.title ? `<div class="xenia-title">${
|
|
1311
|
-
const msg = action.message ? `<div class="xenia-body">${
|
|
1312
|
-
const cta = action.cta ? `<button class="xenia-cta" type="button">${
|
|
1722
|
+
const img = action.imageUrl ? `<img class="xenia-img" src="${escapeAttr2(action.imageUrl)}" alt="" />` : "";
|
|
1723
|
+
const title = action.title ? `<div class="xenia-title">${escapeHtml2(action.title)}</div>` : "";
|
|
1724
|
+
const msg = action.message ? `<div class="xenia-body">${escapeHtml2(action.message)}</div>` : "";
|
|
1725
|
+
const cta = action.cta ? `<button class="xenia-cta" type="button">${escapeHtml2(action.cta.label)}</button>` : "";
|
|
1313
1726
|
const close = variant === "inline" ? "" : `<button class="xenia-close" type="button" aria-label="Close">\xD7</button>`;
|
|
1314
1727
|
return `<div class="xenia-card">${close}${img}<div class="xenia-content">${title}${msg}${cta}</div></div>`;
|
|
1315
1728
|
}
|
|
1316
1729
|
wireCard(root, action, dismiss) {
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
closeEls.forEach((el) => {
|
|
1322
|
-
el.removeAttribute("onclick");
|
|
1323
|
-
el.addEventListener("click", dismiss);
|
|
1324
|
-
});
|
|
1730
|
+
const widget = action.widgetType ? WIDGET_RENDERERS[action.widgetType] : void 0;
|
|
1731
|
+
if (widget) {
|
|
1732
|
+
widget.wire(root, action, this.buildWidgetCtx(action, dismiss));
|
|
1733
|
+
return;
|
|
1325
1734
|
}
|
|
1326
|
-
root
|
|
1327
|
-
if (el instanceof HTMLElement && el.children.length === 0 && !el.textContent?.trim()) {
|
|
1328
|
-
el.style.opacity = "0.5";
|
|
1329
|
-
}
|
|
1330
|
-
});
|
|
1735
|
+
wireCloseAffordances(root, dismiss);
|
|
1331
1736
|
const ctaBtn = root.querySelector(".xenia-cta, [data-xenia-cta]");
|
|
1332
1737
|
if (ctaBtn && action.cta) {
|
|
1333
1738
|
ctaBtn.addEventListener("click", () => {
|
|
@@ -1336,6 +1741,29 @@ ${this.messageStyles}` : STYLES;
|
|
|
1336
1741
|
dismiss?.();
|
|
1337
1742
|
});
|
|
1338
1743
|
}
|
|
1744
|
+
wireFormSubmit(root, action, this.buildWidgetCtx(action, dismiss));
|
|
1745
|
+
}
|
|
1746
|
+
/** Build the imperative hooks (`dismiss`/`fireCta`/`submitWidgetForm`) handed to a widget's `wire()`. */
|
|
1747
|
+
buildWidgetCtx(action, dismiss) {
|
|
1748
|
+
const idempotencyKey = generateUlid();
|
|
1749
|
+
return {
|
|
1750
|
+
dismiss,
|
|
1751
|
+
fireCta: () => {
|
|
1752
|
+
if (action.cta?.event) this.onCta?.(action.cta.event, action.cta);
|
|
1753
|
+
if (action.cta?.url) window.location.assign(action.cta.url);
|
|
1754
|
+
dismiss?.();
|
|
1755
|
+
},
|
|
1756
|
+
submitWidgetForm: (fields) => {
|
|
1757
|
+
if (!action.formSubmit || !this.submitWidgetFormFn) {
|
|
1758
|
+
return Promise.reject(
|
|
1759
|
+
new Error(
|
|
1760
|
+
"xenia: this action has no formSubmit config, or no submitWidgetForm handler is wired (construct the ActionRenderer via XeniaClient to get one)"
|
|
1761
|
+
)
|
|
1762
|
+
);
|
|
1763
|
+
}
|
|
1764
|
+
return this.submitWidgetFormFn(action.formSubmit, fields, { idempotencyKey });
|
|
1765
|
+
}
|
|
1766
|
+
};
|
|
1339
1767
|
}
|
|
1340
1768
|
};
|
|
1341
1769
|
function wrapCustomHtml(html, variant) {
|
|
@@ -1344,11 +1772,11 @@ function wrapCustomHtml(html, variant) {
|
|
|
1344
1772
|
}
|
|
1345
1773
|
return `<div class="xenia-custom-html"><style>.xenia-custom-html{position:relative}.xenia-custom-html>*{position:static!important;inset:auto!important;top:auto!important;right:auto!important;bottom:auto!important;left:auto!important}</style>${html}</div>`;
|
|
1346
1774
|
}
|
|
1347
|
-
function
|
|
1775
|
+
function escapeHtml2(s) {
|
|
1348
1776
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1349
1777
|
}
|
|
1350
|
-
function
|
|
1351
|
-
return
|
|
1778
|
+
function escapeAttr2(s) {
|
|
1779
|
+
return escapeHtml2(s).replace(/"/g, """);
|
|
1352
1780
|
}
|
|
1353
1781
|
var STYLES = `
|
|
1354
1782
|
:host { all: initial; }
|
|
@@ -1365,12 +1793,14 @@ var STYLES = `
|
|
|
1365
1793
|
.xenia-modal { inset: 0; display: flex; align-items: center; justify-content: center;
|
|
1366
1794
|
background: var(--xenia-modal-backdrop, rgba(0,0,0,.12)); }
|
|
1367
1795
|
.xenia-modal.xenia-custom-bg { background: transparent; }
|
|
1796
|
+
.xenia-modal.xenia-no-overlay { background: transparent; }
|
|
1368
1797
|
.xenia-card { position: relative; background: var(--xenia-card-bg, #fff); color: var(--xenia-card-color, #111);
|
|
1369
1798
|
border-radius: var(--xenia-card-radius, 12px); box-shadow: var(--xenia-card-shadow, 0 10px 40px rgba(0,0,0,.18));
|
|
1370
1799
|
padding: var(--xenia-card-padding, 16px 18px); display: flex; gap: var(--xenia-card-gap, 12px);
|
|
1371
1800
|
align-items: center; }
|
|
1372
1801
|
.xenia-banner .xenia-card { border-radius: var(--xenia-banner-card-radius, 0); justify-content: center; }
|
|
1373
1802
|
.xenia-modal .xenia-card { max-width: var(--xenia-modal-max-width, 440px); width: calc(100% - 32px); }
|
|
1803
|
+
.xenia-card.xenia-widget { flex-direction: column; align-items: center; text-align: center; }
|
|
1374
1804
|
.xenia-img { width: var(--xenia-img-size, 56px); height: var(--xenia-img-size, 56px); object-fit: cover;
|
|
1375
1805
|
border-radius: var(--xenia-img-radius, 8px); }
|
|
1376
1806
|
.xenia-title { font-weight: var(--xenia-title-weight, 600); font-size: var(--xenia-title-size, 15px);
|
|
@@ -1382,6 +1812,38 @@ var STYLES = `
|
|
|
1382
1812
|
font-size: var(--xenia-cta-size, 14px); cursor: pointer; }
|
|
1383
1813
|
.xenia-close { position: absolute; top: 6px; right: 8px; background: none; border: 0;
|
|
1384
1814
|
font-size: var(--xenia-close-size, 20px); line-height: 1; cursor: pointer; color: var(--xenia-close-color, #999); }
|
|
1815
|
+
.xenia-wheel-dial { transform-box: fill-box; transform-origin: center; }
|
|
1816
|
+
.xenia-wheel-hub { fill: var(--xenia-wheel-pointer-color, #111); }
|
|
1817
|
+
.xenia-wheel-wrap { position: relative; display: inline-block; }
|
|
1818
|
+
.xenia-wheel-pointer { position: absolute; top: -6px; left: 50%; transform: translateX(-50%);
|
|
1819
|
+
width: 0; height: 0; border-left: 9px solid transparent; border-right: 9px solid transparent;
|
|
1820
|
+
border-top: 14px solid var(--xenia-wheel-pointer-color, #111); pointer-events: none; }
|
|
1821
|
+
.xenia-wheel-spin { margin-top: 10px; background: var(--xenia-cta-bg, #111); color: var(--xenia-cta-color, #fff);
|
|
1822
|
+
border: 0; border-radius: var(--xenia-cta-radius, 8px); padding: var(--xenia-cta-padding, 8px 14px);
|
|
1823
|
+
font-size: var(--xenia-cta-size, 14px); font-family: inherit; cursor: pointer; }
|
|
1824
|
+
.xenia-wheel-spin:disabled { opacity: 0.6; cursor: default; }
|
|
1825
|
+
/* No animation/transform here \u2014 spin-wheel's win/loss screens are ALWAYS full
|
|
1826
|
+
custom HTML now (winHtml/lossHtml), never this class's own generated icon/title/prize/cta
|
|
1827
|
+
content (those sub-classes below are unreachable dead CSS, kept only for backwards-compat with
|
|
1828
|
+
any already-saved artifact that might still reference them). A transform on THIS wrapper
|
|
1829
|
+
would make it the CSS containing block for any fixed/absolute-positioned descendant the
|
|
1830
|
+
author's own HTML uses (a real, common case \u2014 a "full-screen overlay" win/loss design) \u2014 the
|
|
1831
|
+
fixed child would then size/position itself relative to this wrapper's own small, in-flow,
|
|
1832
|
+
often off-center box instead of the true browser viewport, visibly popping in from/toward
|
|
1833
|
+
whatever corner the wrapper happens to sit at instead of scaling from the real screen center.
|
|
1834
|
+
spin-wheel.ts's crossFadeReveal owns the actual reveal transition (applied to the author's own
|
|
1835
|
+
top-level element, never this wrapper) \u2014 this class stays a bare, transform-free layout box. */
|
|
1836
|
+
.xenia-widget-result { display: flex; flex-direction: column; align-items: center; gap: 4px;
|
|
1837
|
+
padding: 8px 4px 2px; text-align: center; }
|
|
1838
|
+
.xenia-widget-result[hidden] { display: none; }
|
|
1839
|
+
.xenia-widget-result-icon { font-size: 40px; line-height: 1; }
|
|
1840
|
+
.xenia-widget-result-title { font-weight: var(--xenia-title-weight, 700); font-size: var(--xenia-title-size, 17px); }
|
|
1841
|
+
.xenia-widget-result-prize { font-weight: 600; font-size: 15px; color: var(--xenia-cta-bg, #111);
|
|
1842
|
+
background: var(--xenia-result-prize-bg, rgba(0,0,0,.06)); border-radius: var(--xenia-cta-radius, 8px);
|
|
1843
|
+
padding: 6px 14px; }
|
|
1844
|
+
.xenia-widget-result-cta { margin-top: 8px; background: var(--xenia-cta-bg, #111); color: var(--xenia-cta-color, #fff);
|
|
1845
|
+
border: 0; border-radius: var(--xenia-cta-radius, 8px); padding: var(--xenia-cta-padding, 8px 14px);
|
|
1846
|
+
font-size: var(--xenia-cta-size, 14px); font-family: inherit; cursor: pointer; }
|
|
1385
1847
|
`;
|
|
1386
1848
|
|
|
1387
1849
|
// src/client/xenia-client.ts
|
|
@@ -1406,7 +1868,8 @@ var XeniaClient = class {
|
|
|
1406
1868
|
this.renderer = new ActionRenderer((event, cta) => this.onCtaEvent(event, cta), {
|
|
1407
1869
|
messageTemplate: options.messageTemplate,
|
|
1408
1870
|
messageStyles: options.messageStyles,
|
|
1409
|
-
messageComponent: options.messageComponent
|
|
1871
|
+
messageComponent: options.messageComponent,
|
|
1872
|
+
submitWidgetForm: (submit, fields, subOpts) => this.submitWidgetForm(submit, fields, subOpts)
|
|
1410
1873
|
});
|
|
1411
1874
|
this.consentState = { ...options.consent };
|
|
1412
1875
|
this.analyticsQueue = new AnalyticsQueue({
|
|
@@ -1678,6 +2141,55 @@ var XeniaClient = class {
|
|
|
1678
2141
|
getResponse(key) {
|
|
1679
2142
|
return this.responses.get(key);
|
|
1680
2143
|
}
|
|
2144
|
+
/**
|
|
2145
|
+
* Submit a widget's captured form values (standard/custom popup submit, or a spin-wheel/
|
|
2146
|
+
* spin-wheel's resolve request) and apply any returned `xeniaActions`, exactly like
|
|
2147
|
+
* {@link request}.
|
|
2148
|
+
*
|
|
2149
|
+
* **Endpoint selection is config-driven, not SDK-decided** (per the cross-repo contract): a
|
|
2150
|
+
* `flowRef` (present only when `mode === "journey" && !requiresResolve` — the common
|
|
2151
|
+
* plain-popup case) bypasses the resolve endpoint entirely and POSTs straight to the existing
|
|
2152
|
+
* public `/flow` fast path; otherwise this POSTs to `/widget-submissions` via `bindingRef`. The
|
|
2153
|
+
* SDK is agnostic to *why* — it just follows whichever ref field the backend populated.
|
|
2154
|
+
*
|
|
2155
|
+
* The full ambient-context parity list {@link request}/`withVisitor()` already build
|
|
2156
|
+
* (visitorId, deviceId, url, referrer, path, query, `utm*`) rides on every call, plus `fields`
|
|
2157
|
+
* and `submit.mode`/the resolved ref, relayed verbatim/uninterpreted — the SDK never branches
|
|
2158
|
+
* on `mode`'s value itself, that's server-side.
|
|
2159
|
+
*
|
|
2160
|
+
* `opts.idempotencyKey` defaults to a freshly generated ULID; pass the SAME key back in on a
|
|
2161
|
+
* retried submit of the same interaction so a network retry can't double-fire a journey or
|
|
2162
|
+
* double-resolve a prize server-side.
|
|
2163
|
+
*/
|
|
2164
|
+
async submitWidgetForm(submit, fields, opts) {
|
|
2165
|
+
const ctx = this.identity.collectContext();
|
|
2166
|
+
const idempotencyKey = opts?.idempotencyKey ?? generateUlid();
|
|
2167
|
+
const ref = submit.flowRef ?? submit.bindingRef;
|
|
2168
|
+
const ambient = {
|
|
2169
|
+
visitorId: this.resolveVisitorId(),
|
|
2170
|
+
deviceId: this.identity.getDeviceId() ?? void 0,
|
|
2171
|
+
idempotencyKey,
|
|
2172
|
+
mode: submit.mode,
|
|
2173
|
+
ref,
|
|
2174
|
+
url: ctx.url,
|
|
2175
|
+
referrer: ctx.referrer,
|
|
2176
|
+
path: ctx.path,
|
|
2177
|
+
query: ctx.query,
|
|
2178
|
+
utmSource: ctx.utmSource,
|
|
2179
|
+
utmMedium: ctx.utmMedium,
|
|
2180
|
+
utmCampaign: ctx.utmCampaign,
|
|
2181
|
+
utmContent: ctx.utmContent,
|
|
2182
|
+
utmId: ctx.utmId
|
|
2183
|
+
};
|
|
2184
|
+
const response = submit.flowRef ? await this.api.webhook(submit.flowRef, compact({ ...ambient, ...fields }), "POST") : await this.api.submitWidget(
|
|
2185
|
+
submit.bindingRef ?? "",
|
|
2186
|
+
compact({ ...ambient, fields })
|
|
2187
|
+
);
|
|
2188
|
+
this.emit("response", { triggerId: ref, response });
|
|
2189
|
+
const apply = opts?.apply ?? this.options.autoApplyActions !== false;
|
|
2190
|
+
if (apply && response.actions.length) this.applyActions(response.actions);
|
|
2191
|
+
return response;
|
|
2192
|
+
}
|
|
1681
2193
|
// ── Imperative render helpers ─────────────────────────────────────────────────
|
|
1682
2194
|
swapContent(target, content) {
|
|
1683
2195
|
return swapContent(target, content);
|