@blotoutio/providers-blotout-wallet-sdk 0.42.2 → 0.44.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/index.cjs.js +353 -183
- package/index.js +353 -183
- package/index.mjs +353 -183
- package/package.json +1 -1
- package/stores/shopify/index.cjs.js +3 -5
- package/stores/shopify/index.js +3 -5
- package/stores/shopify/index.mjs +3 -5
package/index.mjs
CHANGED
@@ -1,16 +1,147 @@
|
|
1
|
+
/**
|
2
|
+
* Returns whether A contains the entirety of B (A superset of B)
|
3
|
+
*
|
4
|
+
* A ⊇ B
|
5
|
+
*/
|
6
|
+
const isSuperset = (a, b) => {
|
7
|
+
for (const item of b) {
|
8
|
+
if (!a.has(item)) {
|
9
|
+
return false;
|
10
|
+
}
|
11
|
+
}
|
12
|
+
return true;
|
13
|
+
};
|
14
|
+
/**
|
15
|
+
* Returns whether A is entirely contained within B (A subset of B)
|
16
|
+
*
|
17
|
+
* A ⊆ B
|
18
|
+
*/
|
19
|
+
const isSubset = (a, b) => {
|
20
|
+
for (const item of a) {
|
21
|
+
if (!b.has(item)) {
|
22
|
+
return false;
|
23
|
+
}
|
24
|
+
}
|
25
|
+
return true;
|
26
|
+
};
|
27
|
+
/**
|
28
|
+
* Returns true when the two ets contain the same set of elements
|
29
|
+
*
|
30
|
+
* A = B
|
31
|
+
*/
|
32
|
+
const areEquivalent = (a, b) => a.size == b.size && isSuperset(a, b) && isSubset(a, b);
|
33
|
+
|
34
|
+
const expand = (str) => str.split(',').flatMap((entry) => {
|
35
|
+
if (!entry.includes('-')) {
|
36
|
+
return entry;
|
37
|
+
}
|
38
|
+
const result = [];
|
39
|
+
const [start, end] = entry.split('-').map(Number);
|
40
|
+
for (let i = start; i <= end; i++) {
|
41
|
+
result.push(i.toString());
|
42
|
+
}
|
43
|
+
return result;
|
44
|
+
});
|
45
|
+
/**
|
46
|
+
* Exported from https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes
|
47
|
+
*
|
48
|
+
* In Dev Tools, select the `tbody` element containing the area codes and run the following code,
|
49
|
+
* replacing the emdash character with a simple endash:
|
50
|
+
*
|
51
|
+
* ```ts
|
52
|
+
* [...$0.querySelectorAll('td:first-child')]
|
53
|
+
* .filter(cell => cell.firstChild.nodeName != 'A')
|
54
|
+
* .map(cell => cell.textContent.trim()).join(',')
|
55
|
+
* ```
|
56
|
+
*/
|
57
|
+
new Set([
|
58
|
+
...expand('200,211,221,222,230,232,233,235,237-238,241,243,244,245,247,255,257,258-259,261,265,266,271,273,274,275,277,278,280,282,283,285-287,288,290-299'),
|
59
|
+
...expand('300,311,322,324,327,328,333,335,338,342,344,348-349,353,355,356,357-359,362,366,369,370-379,381,382,383-384,387,388,389,390-399'),
|
60
|
+
...expand('400,411,420,421-422,426-427,428,429,433,439,444,446,449,451-454,455,456,457,459,460,461-462,465,466,467,471,476,477,481-483,485-486,487,488,489,490-499'),
|
61
|
+
...expand('511,532,535,536,537,538,542-543,545-547,549-550,552-554,555,556,558,560,565,568,569,576,578,583,589,590-599'),
|
62
|
+
...expand('611,621,624,625,627,632,633,634-635,637-638,642-643,644,648,652-654,655,663,665,666,668,673-676,677,679,685,686,687,688,690-699'),
|
63
|
+
...expand('711,722,723,729,733,735-736,739,741,744,745-746,748,749-751,752,755,756,759,761,764,766,768,776,777,783,788,789,790-799'),
|
64
|
+
...expand('811,821,822,823-824,827,834,836,841-842,846,851,852-853,871,874-875,879,880-887,889,890-899'),
|
65
|
+
...expand('911,921,922,923,924,926,927,932,933,935,942,944,946,950,953,955,957-958,960-969,974,975,976,977,981-982,987,988,990-999'),
|
66
|
+
]);
|
67
|
+
|
1
68
|
const packageName = 'blotoutWallet';
|
69
|
+
const customAttributes = {
|
70
|
+
'--bw-primary': { type: 'color', defaultValue: '#f25c2b' },
|
71
|
+
'--bw-secondary': { type: 'color', defaultValue: '#172a41' },
|
72
|
+
'--bw-button': { type: 'color', defaultValue: '#da2e3a' },
|
73
|
+
'--bw-foreground': { type: 'color', defaultValue: 'white' },
|
74
|
+
'--bw-input-border': { type: 'color', defaultValue: '#dbe2eb' },
|
75
|
+
'--bw-input-foreground': { type: 'color', defaultValue: '#172a41' },
|
76
|
+
'--bw-input-background': { type: 'color', defaultValue: 'white' },
|
77
|
+
'--bw-backdrop': { type: 'color', defaultValue: '#00000077' },
|
78
|
+
'--bw-z-index': { type: 'number', defaultValue: '9999' },
|
79
|
+
};
|
2
80
|
|
81
|
+
const canLog = () => {
|
82
|
+
try {
|
83
|
+
return localStorage.getItem('edgeTagDebug') === '1';
|
84
|
+
}
|
85
|
+
catch {
|
86
|
+
return false;
|
87
|
+
}
|
88
|
+
};
|
89
|
+
const logger = {
|
90
|
+
log: (...args) => {
|
91
|
+
if (canLog()) {
|
92
|
+
console.log(...args);
|
93
|
+
}
|
94
|
+
},
|
95
|
+
error: (...args) => {
|
96
|
+
if (canLog()) {
|
97
|
+
console.error(...args);
|
98
|
+
}
|
99
|
+
},
|
100
|
+
info: (...args) => {
|
101
|
+
if (canLog()) {
|
102
|
+
console.info(...args);
|
103
|
+
}
|
104
|
+
},
|
105
|
+
trace: (...args) => {
|
106
|
+
if (canLog()) {
|
107
|
+
console.trace(...args);
|
108
|
+
}
|
109
|
+
},
|
110
|
+
table: (...args) => {
|
111
|
+
if (canLog()) {
|
112
|
+
console.table(...args);
|
113
|
+
}
|
114
|
+
},
|
115
|
+
dir: (...args) => {
|
116
|
+
if (canLog()) {
|
117
|
+
console.dir(...args);
|
118
|
+
}
|
119
|
+
},
|
120
|
+
};
|
121
|
+
|
122
|
+
// eslint-disable-next-line @nx/enforce-module-boundaries
|
3
123
|
class APIError extends Error {
|
4
124
|
constructor(...args) {
|
5
125
|
super(...args);
|
6
126
|
}
|
7
127
|
}
|
128
|
+
const getItemKey = (item) => `${item.productId}-${item.variantId}`;
|
129
|
+
const postMessageKey = 'blotoutWallet';
|
8
130
|
class WalletAPI {
|
9
|
-
constructor({ baseUrl, userId }) {
|
131
|
+
constructor({ baseUrl, userId, enabled }) {
|
10
132
|
this.listeners = new Set();
|
11
133
|
this._cart = { cartId: null, items: [], email: false };
|
134
|
+
this.onWindowMessage = (event) => {
|
135
|
+
var _a;
|
136
|
+
if (((_a = event.data) === null || _a === void 0 ? void 0 : _a.name) == postMessageKey) {
|
137
|
+
this.cart = event.data.cart;
|
138
|
+
}
|
139
|
+
};
|
12
140
|
this.baseUrl = baseUrl;
|
13
141
|
this.userId = userId;
|
142
|
+
this.enabled = enabled;
|
143
|
+
logger.info(`[Blotout Wallet] User is ${enabled ? 'enabled' : 'disabled'}`);
|
144
|
+
window.addEventListener('message', this.onWindowMessage);
|
14
145
|
}
|
15
146
|
getHeaders(json = false) {
|
16
147
|
const headers = new Headers({
|
@@ -25,6 +156,7 @@ class WalletAPI {
|
|
25
156
|
return `${this.baseUrl}/providers/blotoutWallet${path}`;
|
26
157
|
}
|
27
158
|
notify() {
|
159
|
+
var _a;
|
28
160
|
for (const listener of this.listeners) {
|
29
161
|
try {
|
30
162
|
listener(this.cart);
|
@@ -33,13 +165,44 @@ class WalletAPI {
|
|
33
165
|
console.error(err);
|
34
166
|
}
|
35
167
|
}
|
168
|
+
(_a = window.top) === null || _a === void 0 ? void 0 : _a.postMessage({ name: postMessageKey, cart: this.cart }, '*');
|
36
169
|
}
|
37
170
|
get cart() {
|
38
171
|
return this._cart;
|
39
172
|
}
|
40
173
|
set cart(value) {
|
41
|
-
this.
|
42
|
-
|
174
|
+
if (this.isCartUpdated(value)) {
|
175
|
+
this._cart = value;
|
176
|
+
this.notify();
|
177
|
+
}
|
178
|
+
}
|
179
|
+
isCartUpdated(value) {
|
180
|
+
if (value.cartId != this.cart.cartId ||
|
181
|
+
value.email != this.cart.email ||
|
182
|
+
value.items.length != this.cart.items.length) {
|
183
|
+
return true;
|
184
|
+
}
|
185
|
+
const newItemsMap = new Map(value.items.map((item) => [item.itemId, item]));
|
186
|
+
const newItemsKeys = new Set(newItemsMap.keys());
|
187
|
+
const currenItemsMap = new Map(this.cart.items.map((item) => [item.itemId, item]));
|
188
|
+
const currentItemsKeys = new Set(currenItemsMap.keys());
|
189
|
+
if (!areEquivalent(newItemsKeys, currentItemsKeys)) {
|
190
|
+
return true;
|
191
|
+
}
|
192
|
+
for (const [key, newItem] of newItemsMap) {
|
193
|
+
const currentItem = currenItemsMap.get(key);
|
194
|
+
if (newItem.amount != currentItem.amount ||
|
195
|
+
newItem.value != currentItem.value ||
|
196
|
+
newItem.name != currentItem.name ||
|
197
|
+
newItem.productId != currentItem.productId ||
|
198
|
+
newItem.variantId != currentItem.variantId) {
|
199
|
+
return true;
|
200
|
+
}
|
201
|
+
}
|
202
|
+
return false;
|
203
|
+
}
|
204
|
+
get segmentEnabled() {
|
205
|
+
return this.enabled;
|
43
206
|
}
|
44
207
|
subscribe(listener) {
|
45
208
|
this.listeners.add(listener);
|
@@ -72,6 +235,26 @@ class WalletAPI {
|
|
72
235
|
return (this.cart = await response.json());
|
73
236
|
});
|
74
237
|
}
|
238
|
+
removeItems(items) {
|
239
|
+
const cartLookup = new Map(this._cart.items.map((item) => [getItemKey(item), item]));
|
240
|
+
const itemLookup = new Map(items.map((item) => [getItemKey(item), item]));
|
241
|
+
const updatedCartItems = [];
|
242
|
+
for (const [itemKey, walletItem] of cartLookup.entries()) {
|
243
|
+
const itemToRemove = itemLookup.get(itemKey);
|
244
|
+
if (itemToRemove) {
|
245
|
+
// if the removed amount is equal or greater, we simply don't push the
|
246
|
+
// wallet item into the updated cart contents
|
247
|
+
if (walletItem.amount > itemToRemove.amount) {
|
248
|
+
walletItem.amount -= itemToRemove.amount;
|
249
|
+
updatedCartItems.push(walletItem);
|
250
|
+
}
|
251
|
+
}
|
252
|
+
else {
|
253
|
+
updatedCartItems.push(walletItem);
|
254
|
+
}
|
255
|
+
}
|
256
|
+
return this.setItems(updatedCartItems);
|
257
|
+
}
|
75
258
|
setItems(items) {
|
76
259
|
return fetch(this.getUrl('/items'), {
|
77
260
|
method: 'PUT',
|
@@ -85,7 +268,7 @@ class WalletAPI {
|
|
85
268
|
});
|
86
269
|
}
|
87
270
|
updateItem(item) {
|
88
|
-
fetch(this.getUrl(`/items/${item.itemId}`), {
|
271
|
+
return fetch(this.getUrl(`/items/${item.itemId}`), {
|
89
272
|
method: 'PUT',
|
90
273
|
headers: this.getHeaders(true),
|
91
274
|
body: JSON.stringify({
|
@@ -102,7 +285,7 @@ class WalletAPI {
|
|
102
285
|
});
|
103
286
|
}
|
104
287
|
removeItem(itemId) {
|
105
|
-
fetch(this.getUrl(`/items/${itemId}`), {
|
288
|
+
return fetch(this.getUrl(`/items/${itemId}`), {
|
106
289
|
method: 'DELETE',
|
107
290
|
headers: this.getHeaders(),
|
108
291
|
}).then(async (response) => {
|
@@ -112,19 +295,6 @@ class WalletAPI {
|
|
112
295
|
return (this.cart = await response.json());
|
113
296
|
});
|
114
297
|
}
|
115
|
-
purchase(orderId) {
|
116
|
-
return fetch(this.getUrl('/cart/purchase'), {
|
117
|
-
method: 'POST',
|
118
|
-
body: JSON.stringify({ orderId }),
|
119
|
-
headers: this.getHeaders(true),
|
120
|
-
}).then((response) => {
|
121
|
-
if (!response.ok) {
|
122
|
-
throw new APIError(`Could not mark purchase`, { cause: response });
|
123
|
-
}
|
124
|
-
this.cart = { cartId: null, email: this.cart.email, items: [] };
|
125
|
-
this.notify();
|
126
|
-
});
|
127
|
-
}
|
128
298
|
saveEmail(email) {
|
129
299
|
return fetch(this.getUrl('/cart/email'), {
|
130
300
|
method: 'POST',
|
@@ -271,15 +441,42 @@ const spring = 'linear(0, 0.009, 0.035 2.1%, 0.141, 0.281 6.7%, 0.723 12.9%, 0.9
|
|
271
441
|
|
272
442
|
const cssVars = i$4 `
|
273
443
|
:host {
|
274
|
-
--primary: var(
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
--
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
--
|
444
|
+
--primary: var(
|
445
|
+
--bw-primary,
|
446
|
+
${r$5(customAttributes['--bw-primary'].defaultValue)}
|
447
|
+
);
|
448
|
+
--secondary: var(
|
449
|
+
--bw-secondary,
|
450
|
+
${r$5(customAttributes['--bw-secondary'].defaultValue)}
|
451
|
+
);
|
452
|
+
--button: var(
|
453
|
+
--bw-button,
|
454
|
+
${r$5(customAttributes['--bw-button'].defaultValue)}
|
455
|
+
);
|
456
|
+
--foreground: var(
|
457
|
+
--bw-foreground,
|
458
|
+
${r$5(customAttributes['--bw-foreground'].defaultValue)}
|
459
|
+
);
|
460
|
+
--input-border: var(
|
461
|
+
--bw-input-border,
|
462
|
+
${r$5(customAttributes['--bw-input-border'].defaultValue)}
|
463
|
+
);
|
464
|
+
--input-foreground: var(
|
465
|
+
--bw-input-foreground,
|
466
|
+
${r$5(customAttributes['--bw-input-foreground'].defaultValue)}
|
467
|
+
);
|
468
|
+
--input-background: var(
|
469
|
+
--bw-input-background,
|
470
|
+
${r$5(customAttributes['--bw-input-background'].defaultValue)}
|
471
|
+
);
|
472
|
+
--backdrop: var(
|
473
|
+
--bw-backdrop,
|
474
|
+
${r$5(customAttributes['--bw-backdrop'].defaultValue)}
|
475
|
+
);
|
476
|
+
--z-index: var(
|
477
|
+
--bw-z-index,
|
478
|
+
${r$5(customAttributes['--bw-z-index'].defaultValue)}
|
479
|
+
);
|
283
480
|
|
284
481
|
--spring-easing: ${r$5(spring)};
|
285
482
|
|
@@ -418,22 +615,22 @@ const validPositions = new Set([
|
|
418
615
|
'top-left',
|
419
616
|
'top-right',
|
420
617
|
]);
|
421
|
-
const getItemKey = (item) => `${item.productId}-${item.variantId}`;
|
422
618
|
const wait = (timeout) => new Promise((res) => setTimeout(res, timeout));
|
423
619
|
let BlotoutWallet = class BlotoutWallet extends s {
|
424
620
|
constructor() {
|
425
621
|
super(...arguments);
|
426
|
-
this.
|
427
|
-
this.
|
428
|
-
this.
|
429
|
-
this.
|
430
|
-
this.onWalletUpdated = () => {
|
431
|
-
|
432
|
-
this.
|
433
|
-
|
622
|
+
this.buttonVisible = false;
|
623
|
+
this.bannerVisible = false;
|
624
|
+
this.hasItemsInWallet = false;
|
625
|
+
this.hasEmail = false;
|
626
|
+
this.onWalletUpdated = (walletContent) => {
|
627
|
+
logger.log('[Blotout Wallet] Cart updated', walletContent);
|
628
|
+
this.hasItemsInWallet = walletContent.items.length > 0;
|
629
|
+
this.hasEmail = walletContent.email;
|
630
|
+
if (this.isSegmentEnabled && (this.hasItemsInWallet || !this.hasEmail)) {
|
434
631
|
this.showButton();
|
435
632
|
}
|
436
|
-
else if (!this.
|
633
|
+
else if (!this.hasItemsInWallet && this.hasEmail) {
|
437
634
|
this.hideButton();
|
438
635
|
}
|
439
636
|
};
|
@@ -442,164 +639,152 @@ let BlotoutWallet = class BlotoutWallet extends s {
|
|
442
639
|
ev.preventDefault();
|
443
640
|
ev.stopPropagation();
|
444
641
|
const modalAnimation = this.hideModal();
|
445
|
-
const email = (_a = this.
|
642
|
+
const email = (_a = this.email) === null || _a === void 0 ? void 0 : _a.value.trim();
|
446
643
|
if (email) {
|
447
|
-
this.
|
644
|
+
this.walletApi.saveEmail(email)
|
448
645
|
.then(() => {
|
449
|
-
this.
|
450
|
-
this.
|
646
|
+
this.email.value = '';
|
647
|
+
this.hasEmail = true;
|
451
648
|
this.dispatchEvent(new CustomEvent('blotout-wallet-email-saved', { bubbles: true }));
|
452
649
|
})
|
453
|
-
.catch((err) =>
|
650
|
+
.catch((err) => logger.error(err));
|
454
651
|
}
|
455
|
-
if (this.
|
456
|
-
this.
|
457
|
-
.then(() => this.
|
458
|
-
.then(() => this._walletApi.restore())
|
652
|
+
if (this.hasItemsInWallet) {
|
653
|
+
this.storeApi.addItems(this.walletApi.cart.items)
|
654
|
+
.then(() => this.walletApi.restore())
|
459
655
|
.then(() => {
|
460
656
|
this.dispatchEvent(new CustomEvent('blotout-wallet-restored', { bubbles: true }));
|
461
657
|
return modalAnimation.then(() => this.showRestorationBanner());
|
462
658
|
})
|
463
|
-
.
|
659
|
+
.then(() => wait(1000))
|
660
|
+
.then(() => (window.location.href = `${window.Shopify.routes.root}cart`))
|
661
|
+
.catch((err) => logger.error(err));
|
464
662
|
}
|
465
663
|
};
|
466
664
|
this.showRestorationBanner = async () => {
|
467
|
-
this.
|
665
|
+
if (this.bannerVisible) {
|
666
|
+
return;
|
667
|
+
}
|
668
|
+
this.bannerVisible = true;
|
468
669
|
await new Promise(requestAnimationFrame);
|
469
|
-
await springInFromBottom(this.
|
470
|
-
|
471
|
-
|
472
|
-
this.
|
670
|
+
await springInFromBottom(this.banner, '120%');
|
671
|
+
};
|
672
|
+
this.hideRestorationBanner = async () => {
|
673
|
+
if (!this.bannerVisible) {
|
674
|
+
return;
|
675
|
+
}
|
676
|
+
await springOutToBottom(this.banner, '120%');
|
677
|
+
this.bannerVisible = false;
|
473
678
|
};
|
474
679
|
}
|
475
|
-
get
|
680
|
+
get storeApi() {
|
476
681
|
var _a;
|
477
682
|
return (_a = window[registryKey]) === null || _a === void 0 ? void 0 : _a.storeAPI;
|
478
683
|
}
|
479
|
-
get
|
684
|
+
get walletApi() {
|
480
685
|
var _a;
|
481
686
|
return (_a = window[registryKey]) === null || _a === void 0 ? void 0 : _a.walletAPI;
|
482
687
|
}
|
688
|
+
get isSegmentEnabled() {
|
689
|
+
var _a;
|
690
|
+
return (_a = this.walletApi) === null || _a === void 0 ? void 0 : _a.segmentEnabled;
|
691
|
+
}
|
483
692
|
get _position() {
|
484
693
|
return validPositions.has(this.position) ? this.position : 'bottom-left';
|
485
694
|
}
|
486
695
|
connectedCallback() {
|
487
696
|
var _a;
|
488
697
|
super.connectedCallback();
|
489
|
-
this.
|
490
|
-
this.syncItems().then((
|
698
|
+
this.unsubscribe = (_a = this.walletApi) === null || _a === void 0 ? void 0 : _a.subscribe(this.onWalletUpdated);
|
699
|
+
this.syncItems().then(() => {
|
491
700
|
this.dispatchEvent(new CustomEvent('blotout-wallet-loaded', { bubbles: true }));
|
492
|
-
this._hasItemsInWallet = hasItemsSaved;
|
493
|
-
if (this._hasItemsInWallet) {
|
494
|
-
this.showButton();
|
495
|
-
}
|
496
701
|
});
|
497
702
|
}
|
498
703
|
disconnectedCallback() {
|
499
704
|
var _a;
|
500
705
|
super.disconnectedCallback();
|
501
|
-
(_a = this.
|
502
|
-
this.
|
706
|
+
(_a = this.unsubscribe) === null || _a === void 0 ? void 0 : _a.call(this);
|
707
|
+
this.unsubscribe = undefined;
|
503
708
|
}
|
504
709
|
async syncItems() {
|
505
|
-
if (!this.
|
506
|
-
|
710
|
+
if (!this.walletApi) {
|
711
|
+
logger.error('The required APIs are not available');
|
507
712
|
return false;
|
508
713
|
}
|
509
|
-
const
|
510
|
-
|
511
|
-
this._storeApi.getCart(),
|
512
|
-
]);
|
513
|
-
this._hasEmail = savedCart.email;
|
514
|
-
const savedItemsLookup = new Map(savedCart.items.map((item) => [getItemKey(item), item]));
|
515
|
-
const missingItems = [];
|
516
|
-
for (const item of shopItems) {
|
517
|
-
const walletItem = savedItemsLookup.get(getItemKey(item));
|
518
|
-
if (!walletItem) {
|
519
|
-
missingItems.push(item);
|
520
|
-
}
|
521
|
-
else if (walletItem.amount < item.amount) {
|
522
|
-
missingItems.push({
|
523
|
-
...item,
|
524
|
-
amount: item.amount - walletItem.amount,
|
525
|
-
});
|
526
|
-
}
|
527
|
-
}
|
528
|
-
if (missingItems.length > 0) {
|
529
|
-
await this._walletApi.addItems(missingItems);
|
530
|
-
}
|
714
|
+
const savedCart = await this.walletApi.getCart();
|
715
|
+
this.hasEmail = savedCart.email;
|
531
716
|
return savedCart.items.length > 0;
|
532
717
|
}
|
533
718
|
async showButton() {
|
534
|
-
if (this.
|
719
|
+
if (this.buttonVisible) {
|
535
720
|
return;
|
536
721
|
}
|
537
|
-
this.
|
722
|
+
this.buttonVisible = true;
|
538
723
|
await new Promise(requestAnimationFrame).then(() => {
|
539
|
-
switch (this.
|
724
|
+
switch (this.position) {
|
540
725
|
case 'top-left':
|
541
726
|
case 'left':
|
542
727
|
case 'bottom-left': {
|
543
|
-
return springInFromLeft(this.
|
728
|
+
return springInFromLeft(this.button);
|
544
729
|
}
|
545
730
|
case 'top-right':
|
546
731
|
case 'right':
|
547
732
|
case 'bottom-right': {
|
548
|
-
return springInFromRight(this.
|
733
|
+
return springInFromRight(this.button);
|
549
734
|
}
|
550
735
|
case 'bottom': {
|
551
|
-
return springInFromBottom(this.
|
736
|
+
return springInFromBottom(this.button);
|
552
737
|
}
|
553
738
|
case 'top': {
|
554
|
-
return springInFromTop(this.
|
739
|
+
return springInFromTop(this.button);
|
555
740
|
}
|
556
741
|
default: {
|
557
|
-
return springInFromLeft(this.
|
742
|
+
return springInFromLeft(this.button);
|
558
743
|
}
|
559
744
|
}
|
560
745
|
});
|
561
746
|
this.dispatchEvent(new CustomEvent('blotout-wallet-shown', { bubbles: true }));
|
562
747
|
}
|
563
748
|
async hideButton() {
|
564
|
-
if (!this.
|
749
|
+
if (!this.buttonVisible) {
|
565
750
|
return;
|
566
751
|
}
|
567
|
-
switch (this.
|
752
|
+
switch (this.position) {
|
568
753
|
case 'top-left':
|
569
754
|
case 'left':
|
570
755
|
case 'bottom-left': {
|
571
|
-
await springOutToLeft(this.
|
756
|
+
await springOutToLeft(this.button);
|
572
757
|
break;
|
573
758
|
}
|
574
759
|
case 'top-right':
|
575
760
|
case 'right':
|
576
761
|
case 'bottom-right': {
|
577
|
-
await springOutToRight(this.
|
762
|
+
await springOutToRight(this.button);
|
578
763
|
break;
|
579
764
|
}
|
580
765
|
case 'bottom': {
|
581
|
-
await springOutToBottom(this.
|
766
|
+
await springOutToBottom(this.button);
|
582
767
|
break;
|
583
768
|
}
|
584
769
|
case 'top': {
|
585
|
-
await springOutToTop(this.
|
770
|
+
await springOutToTop(this.button);
|
586
771
|
break;
|
587
772
|
}
|
588
773
|
default: {
|
589
|
-
await springOutToLeft(this.
|
774
|
+
await springOutToLeft(this.button);
|
590
775
|
}
|
591
776
|
}
|
592
|
-
this.
|
777
|
+
this.buttonVisible = false;
|
593
778
|
this.dispatchEvent(new CustomEvent('blotout-wallet-hidden', { bubbles: true }));
|
594
779
|
}
|
595
780
|
async showModal() {
|
596
|
-
this.
|
781
|
+
this.dialog.showModal();
|
597
782
|
await new Promise(requestAnimationFrame);
|
598
|
-
await fadeInDialog(this.
|
783
|
+
await fadeInDialog(this.dialog);
|
599
784
|
}
|
600
785
|
async hideModal() {
|
601
|
-
await fadeOutToBottom(this.
|
602
|
-
this.
|
786
|
+
await fadeOutToBottom(this.dialog);
|
787
|
+
this.dialog.close();
|
603
788
|
}
|
604
789
|
render() {
|
605
790
|
return x `
|
@@ -607,7 +792,7 @@ let BlotoutWallet = class BlotoutWallet extends s {
|
|
607
792
|
class="${e({
|
608
793
|
cta: true,
|
609
794
|
[this._position]: true,
|
610
|
-
hidden: !this.
|
795
|
+
hidden: !this.buttonVisible,
|
611
796
|
})}"
|
612
797
|
@click=${this.showModal}
|
613
798
|
>
|
@@ -618,7 +803,7 @@ let BlotoutWallet = class BlotoutWallet extends s {
|
|
618
803
|
<button class="dismiss" @click=${this.hideModal}>${cross()}</button>
|
619
804
|
${blotout({ class: 'logo' })}
|
620
805
|
<form method="dialog" @submit=${this.onSubmit}>
|
621
|
-
${this.
|
806
|
+
${this.hasItemsInWallet
|
622
807
|
? x `<p>
|
623
808
|
Hey! Your <strong>cart has expired!</strong><br />But we've got
|
624
809
|
you covered. ${wink({ style: 'vertical-align:middle' })}
|
@@ -633,15 +818,15 @@ let BlotoutWallet = class BlotoutWallet extends s {
|
|
633
818
|
type="email"
|
634
819
|
name="email"
|
635
820
|
placeholder="Enter your email ID"
|
636
|
-
?required=${!this.
|
821
|
+
?required=${!this.hasEmail}
|
637
822
|
style=${o({
|
638
|
-
display: this.
|
823
|
+
display: this.hasEmail ? 'none' : 'block',
|
639
824
|
})}
|
640
825
|
/>
|
641
826
|
</p>
|
642
827
|
<p>
|
643
828
|
<button class="restore" type="submit">
|
644
|
-
${this.
|
829
|
+
${this.hasItemsInWallet ? 'Restore Cart' : 'Save Cart'}
|
645
830
|
</button>
|
646
831
|
</p>
|
647
832
|
</form>
|
@@ -649,7 +834,7 @@ let BlotoutWallet = class BlotoutWallet extends s {
|
|
649
834
|
|
650
835
|
<div
|
651
836
|
class="banner"
|
652
|
-
style=${o({ display: this.
|
837
|
+
style=${o({ display: this.bannerVisible ? '' : 'none' })}
|
653
838
|
>
|
654
839
|
${blotout({
|
655
840
|
class: 'logo',
|
@@ -752,7 +937,6 @@ BlotoutWallet.styles = [
|
|
752
937
|
}
|
753
938
|
|
754
939
|
dialog {
|
755
|
-
position: relative;
|
756
940
|
width: 430px;
|
757
941
|
max-width: 100vw;
|
758
942
|
background: var(--secondary);
|
@@ -836,97 +1020,92 @@ __decorate([
|
|
836
1020
|
__decorate([
|
837
1021
|
e$2('dialog'),
|
838
1022
|
__metadata("design:type", HTMLDialogElement)
|
839
|
-
], BlotoutWallet.prototype, "
|
1023
|
+
], BlotoutWallet.prototype, "dialog", void 0);
|
840
1024
|
__decorate([
|
841
1025
|
e$2('button.cta'),
|
842
1026
|
__metadata("design:type", HTMLButtonElement)
|
843
|
-
], BlotoutWallet.prototype, "
|
1027
|
+
], BlotoutWallet.prototype, "button", void 0);
|
844
1028
|
__decorate([
|
845
1029
|
e$2('input[name=email]'),
|
846
1030
|
__metadata("design:type", HTMLInputElement)
|
847
|
-
], BlotoutWallet.prototype, "
|
1031
|
+
], BlotoutWallet.prototype, "email", void 0);
|
848
1032
|
__decorate([
|
849
1033
|
e$2('.banner'),
|
850
1034
|
__metadata("design:type", HTMLDivElement)
|
851
|
-
], BlotoutWallet.prototype, "
|
1035
|
+
], BlotoutWallet.prototype, "banner", void 0);
|
852
1036
|
__decorate([
|
853
1037
|
r(),
|
854
1038
|
__metadata("design:type", Boolean)
|
855
|
-
], BlotoutWallet.prototype, "
|
1039
|
+
], BlotoutWallet.prototype, "buttonVisible", void 0);
|
856
1040
|
__decorate([
|
857
1041
|
r(),
|
858
1042
|
__metadata("design:type", Boolean)
|
859
|
-
], BlotoutWallet.prototype, "
|
1043
|
+
], BlotoutWallet.prototype, "bannerVisible", void 0);
|
860
1044
|
__decorate([
|
861
1045
|
r(),
|
862
1046
|
__metadata("design:type", Boolean)
|
863
|
-
], BlotoutWallet.prototype, "
|
1047
|
+
], BlotoutWallet.prototype, "hasItemsInWallet", void 0);
|
864
1048
|
__decorate([
|
865
1049
|
r(),
|
866
1050
|
__metadata("design:type", Boolean)
|
867
|
-
], BlotoutWallet.prototype, "
|
1051
|
+
], BlotoutWallet.prototype, "hasEmail", void 0);
|
868
1052
|
BlotoutWallet = __decorate([
|
869
1053
|
t$1('blotout-wallet')
|
870
1054
|
], BlotoutWallet);
|
871
1055
|
|
872
1056
|
const init = (params) => {
|
873
|
-
var _a, _b;
|
874
|
-
if (
|
875
|
-
|
1057
|
+
var _a, _b, _c;
|
1058
|
+
if (
|
1059
|
+
// if loaded in non-browser SDKs
|
1060
|
+
!window ||
|
1061
|
+
!document) {
|
1062
|
+
return;
|
1063
|
+
}
|
1064
|
+
let walletAPI = window[registryKey].walletAPI;
|
1065
|
+
if (!window[registryKey].walletAPI) {
|
1066
|
+
walletAPI = window[registryKey].walletAPI = new WalletAPI({
|
876
1067
|
baseUrl: params.baseUrl,
|
877
1068
|
userId: params.userId,
|
1069
|
+
enabled: !!((_a = params.manifest.variables) === null || _a === void 0 ? void 0 : _a['enabled']),
|
878
1070
|
});
|
879
|
-
|
880
|
-
|
1071
|
+
}
|
1072
|
+
let store = window[registryKey].storeAPI;
|
1073
|
+
if (!store) {
|
1074
|
+
store = window[registryKey].storeAPI =
|
1075
|
+
(_c = (_b = window[registryKey]).storeAPIFactory) === null || _c === void 0 ? void 0 : _c.call(_b);
|
881
1076
|
if (!store) {
|
882
1077
|
throw new Error('Implementation for store API missing!');
|
883
1078
|
}
|
1079
|
+
}
|
1080
|
+
const initComponent = () => {
|
1081
|
+
var _a, _b;
|
884
1082
|
const element = (window[registryKey].wallet =
|
885
1083
|
document.createElement('blotout-wallet'));
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
}
|
890
|
-
|
891
|
-
const canLog = () => {
|
892
|
-
try {
|
893
|
-
return localStorage.getItem('edgeTagDebug') === '1';
|
894
|
-
}
|
895
|
-
catch {
|
896
|
-
return false;
|
897
|
-
}
|
898
|
-
};
|
899
|
-
const logger = {
|
900
|
-
log: (...args) => {
|
901
|
-
if (canLog()) {
|
902
|
-
console.log(...args);
|
903
|
-
}
|
904
|
-
},
|
905
|
-
error: (...args) => {
|
906
|
-
if (canLog()) {
|
907
|
-
console.error(...args);
|
908
|
-
}
|
909
|
-
},
|
910
|
-
info: (...args) => {
|
911
|
-
if (canLog()) {
|
912
|
-
console.info(...args);
|
1084
|
+
const theme = (_a = params.manifest.variables) === null || _a === void 0 ? void 0 : _a['theme'];
|
1085
|
+
if (theme) {
|
1086
|
+
element.style.cssText = Object.entries(theme)
|
1087
|
+
.map(([name, value]) => `${name}:${value}`)
|
1088
|
+
.join(';');
|
913
1089
|
}
|
914
|
-
|
915
|
-
|
916
|
-
|
917
|
-
console.trace(...args);
|
1090
|
+
const position = (_b = params.manifest.variables) === null || _b === void 0 ? void 0 : _b['position'];
|
1091
|
+
if (position) {
|
1092
|
+
element.setAttribute('position', position);
|
918
1093
|
}
|
919
|
-
|
920
|
-
|
921
|
-
|
922
|
-
|
1094
|
+
document.body.append(element);
|
1095
|
+
};
|
1096
|
+
if (window.top === window && !window[registryKey].wallet) {
|
1097
|
+
if (params.isNewUser) {
|
1098
|
+
// sync items from cart to wallet if a new user is detected
|
1099
|
+
store
|
1100
|
+
.getCart()
|
1101
|
+
.then((cartContent) => walletAPI.addItems(cartContent).catch(logger.error))
|
1102
|
+
.then(() => initComponent())
|
1103
|
+
.catch((err) => logger.error(err));
|
923
1104
|
}
|
924
|
-
|
925
|
-
|
926
|
-
if (canLog()) {
|
927
|
-
console.dir(...args);
|
1105
|
+
else {
|
1106
|
+
initComponent();
|
928
1107
|
}
|
929
|
-
}
|
1108
|
+
}
|
930
1109
|
};
|
931
1110
|
|
932
1111
|
const transformItems = (data) => {
|
@@ -940,44 +1119,35 @@ const transformItems = (data) => {
|
|
940
1119
|
}));
|
941
1120
|
};
|
942
1121
|
const tag = (params) => {
|
943
|
-
|
944
|
-
if (!
|
1122
|
+
var _a;
|
1123
|
+
if (!((_a = window === null || window === void 0 ? void 0 : window[registryKey]) === null || _a === void 0 ? void 0 : _a.walletAPI)) {
|
1124
|
+
logger.error(`[Blotout Wallet] No wallet API available`);
|
945
1125
|
return;
|
946
1126
|
}
|
1127
|
+
const wallet = window[registryKey].walletAPI;
|
947
1128
|
switch (params.eventName) {
|
948
1129
|
case 'AddToCart': {
|
949
1130
|
const items = transformItems(params.data);
|
950
|
-
if (items) {
|
1131
|
+
if (items === null || items === void 0 ? void 0 : items.length) {
|
951
1132
|
wallet.addItems(items).catch(logger.error);
|
952
1133
|
}
|
953
1134
|
return;
|
954
1135
|
}
|
955
|
-
case '
|
1136
|
+
case 'RemoveFromCart': {
|
956
1137
|
const items = transformItems(params.data);
|
957
|
-
if (items) {
|
958
|
-
wallet.
|
1138
|
+
if (items === null || items === void 0 ? void 0 : items.length) {
|
1139
|
+
wallet.removeItems(items).catch(logger.error);
|
959
1140
|
}
|
960
1141
|
return;
|
961
1142
|
}
|
962
|
-
case 'Purchase': {
|
963
|
-
const { orderId } = params.data;
|
964
|
-
wallet.purchase(orderId).catch(logger.error);
|
965
|
-
return;
|
966
|
-
}
|
967
1143
|
}
|
968
1144
|
};
|
969
1145
|
|
970
|
-
// eslint-disable-next-line @nx/enforce-module-boundaries
|
971
|
-
const user = (params) => {
|
972
|
-
console.log(`[${packageName}]: user`, params);
|
973
|
-
};
|
974
|
-
|
975
1146
|
// eslint-disable-next-line @nx/enforce-module-boundaries
|
976
1147
|
const data = {
|
977
1148
|
name: packageName,
|
978
1149
|
init,
|
979
1150
|
tag,
|
980
|
-
user,
|
981
1151
|
};
|
982
1152
|
try {
|
983
1153
|
if (window) {
|