wickes-css2 2.106.0-IC-1222-back-functionality-poc.4 → 2.106.0-IC-1222-back-functionality-poc.5
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/build/js/page/plp-load-more.js +71 -122
- package/build/js/plp.bundle.min.js +1 -1
- package/package.json +1 -1
- package/src/js/page/plp-load-more.js +71 -122
|
@@ -18,11 +18,13 @@ Wick.PLPLoadMore = {
|
|
|
18
18
|
$sortOptions: $('#sort_form1'),
|
|
19
19
|
productsList: '.products-list',
|
|
20
20
|
productCard: '.card.product-card',
|
|
21
|
+
productCardLink: '.card.product-card a',
|
|
21
22
|
loadPreviousWrap: '.load-previous-wrap',
|
|
22
23
|
loadPreviousBtn: '.btn-load-previous',
|
|
23
24
|
},
|
|
24
25
|
|
|
25
26
|
numberOfPagesPLP: Number($('#numberOfPagesPLP').val()) || 0,
|
|
27
|
+
currentPage: 0,
|
|
26
28
|
|
|
27
29
|
loaderPayload: {
|
|
28
30
|
wrapper: 'body',
|
|
@@ -30,8 +32,6 @@ Wick.PLPLoadMore = {
|
|
|
30
32
|
modifier: 'basket-loader',
|
|
31
33
|
},
|
|
32
34
|
|
|
33
|
-
currentPage: 0,
|
|
34
|
-
|
|
35
35
|
getPerPage() {
|
|
36
36
|
const params = new URLSearchParams(window.location.search);
|
|
37
37
|
|
|
@@ -64,6 +64,10 @@ Wick.PLPLoadMore = {
|
|
|
64
64
|
return $(Wick.PLPLoadMore.el.productCard).length;
|
|
65
65
|
},
|
|
66
66
|
|
|
67
|
+
getLastAvailablePage() {
|
|
68
|
+
return Math.max(0, Wick.PLPLoadMore.numberOfPagesPLP - 1);
|
|
69
|
+
},
|
|
70
|
+
|
|
67
71
|
getCurrentUrlBase() {
|
|
68
72
|
const url = new URL(window.location.href);
|
|
69
73
|
|
|
@@ -73,28 +77,10 @@ Wick.PLPLoadMore = {
|
|
|
73
77
|
return `${url.pathname}${url.search}`;
|
|
74
78
|
},
|
|
75
79
|
|
|
76
|
-
getLastAvailablePage() {
|
|
77
|
-
return Math.max(0, Wick.PLPLoadMore.numberOfPagesPLP - 1);
|
|
78
|
-
},
|
|
79
|
-
|
|
80
80
|
isRestoreStateExpired(state) {
|
|
81
81
|
return !state.timestamp || Date.now() - state.timestamp > Wick.PLPLoadMore.RESTORE_TTL;
|
|
82
82
|
},
|
|
83
83
|
|
|
84
|
-
isBackForwardNavigation() {
|
|
85
|
-
try {
|
|
86
|
-
const entries = performance.getEntriesByType('navigation');
|
|
87
|
-
|
|
88
|
-
if (entries && entries.length) {
|
|
89
|
-
return entries[0].type === 'back_forward';
|
|
90
|
-
}
|
|
91
|
-
} catch (error) {
|
|
92
|
-
// do nothing
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return false;
|
|
96
|
-
},
|
|
97
|
-
|
|
98
84
|
getRestoreState() {
|
|
99
85
|
try {
|
|
100
86
|
const raw = sessionStorage.getItem(Wick.PLPLoadMore.RESTORE_KEY);
|
|
@@ -133,16 +119,16 @@ Wick.PLPLoadMore = {
|
|
|
133
119
|
sessionStorage.removeItem(Wick.PLPLoadMore.RESTORE_KEY);
|
|
134
120
|
},
|
|
135
121
|
|
|
136
|
-
buildRestoreUrl(
|
|
122
|
+
buildRestoreUrl(clickedIndex, loadedCount) {
|
|
137
123
|
const url = new URL(window.location.href);
|
|
138
124
|
|
|
139
125
|
url.searchParams.delete('perPage');
|
|
140
126
|
url.searchParams.delete('page');
|
|
141
127
|
|
|
142
|
-
if (
|
|
128
|
+
if (loadedCount <= Wick.PLPLoadMore.MAX_VISIBLE) {
|
|
143
129
|
url.searchParams.set('perPage', Wick.PLPLoadMore.MAX_VISIBLE);
|
|
144
130
|
} else {
|
|
145
|
-
const clickedPage = Math.floor((
|
|
131
|
+
const clickedPage = Math.floor((clickedIndex - 1) / Wick.PLPLoadMore.STEP);
|
|
146
132
|
|
|
147
133
|
url.searchParams.set('page', clickedPage);
|
|
148
134
|
}
|
|
@@ -151,7 +137,8 @@ Wick.PLPLoadMore = {
|
|
|
151
137
|
},
|
|
152
138
|
|
|
153
139
|
saveRestoreState(e) {
|
|
154
|
-
const $
|
|
140
|
+
const $link = $(e.target).closest(Wick.PLPLoadMore.el.productCardLink);
|
|
141
|
+
const $card = $link.closest(Wick.PLPLoadMore.el.productCard);
|
|
155
142
|
|
|
156
143
|
if (!$card.length) {
|
|
157
144
|
return;
|
|
@@ -163,92 +150,46 @@ Wick.PLPLoadMore = {
|
|
|
163
150
|
return;
|
|
164
151
|
}
|
|
165
152
|
|
|
153
|
+
if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (typeof e.button !== 'undefined' && e.button !== 0) {
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
166
161
|
const $cards = $(Wick.PLPLoadMore.el.productCard);
|
|
167
162
|
const clickedIndex = $cards.index($card) + 1;
|
|
168
163
|
const loadedCount = Wick.PLPLoadMore.getPerPage();
|
|
164
|
+
const restoreMode = loadedCount <= Wick.PLPLoadMore.MAX_VISIBLE ? 'perPage' : 'page';
|
|
165
|
+
const restoreUrl = Wick.PLPLoadMore.buildRestoreUrl(clickedIndex, loadedCount);
|
|
169
166
|
|
|
170
167
|
const state = {
|
|
171
168
|
baseUrl: Wick.PLPLoadMore.getCurrentUrlBase(),
|
|
172
169
|
productCode,
|
|
173
170
|
clickedIndex,
|
|
174
171
|
loadedCount,
|
|
175
|
-
|
|
172
|
+
restoreMode,
|
|
173
|
+
restoreUrl,
|
|
176
174
|
timestamp: Date.now(),
|
|
177
|
-
|
|
178
|
-
restoreApplied: false,
|
|
179
|
-
restoreMode: loadedCount <= Wick.PLPLoadMore.MAX_VISIBLE ? 'perPage' : 'page',
|
|
175
|
+
scrollApplied: false,
|
|
180
176
|
};
|
|
181
177
|
|
|
182
|
-
state.restoreUrl = Wick.PLPLoadMore.buildRestoreUrl(state);
|
|
183
|
-
|
|
184
|
-
Wick.PLPLoadMore.setRestoreState(state);
|
|
185
|
-
},
|
|
186
|
-
|
|
187
|
-
shouldRedirectToRestoreUrl() {
|
|
188
|
-
const state = Wick.PLPLoadMore.getRestoreState();
|
|
189
|
-
|
|
190
|
-
if (!state || !state.restoreUrl) {
|
|
191
|
-
return false;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
if (!Wick.PLPLoadMore.isBackForwardNavigation()) {
|
|
195
|
-
return false;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
if (state.redirected) {
|
|
199
|
-
return false;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const currentUrl = window.location.href;
|
|
203
|
-
const restoreUrl = state.restoreUrl;
|
|
204
|
-
|
|
205
|
-
return currentUrl !== restoreUrl;
|
|
206
|
-
},
|
|
207
|
-
|
|
208
|
-
redirectToRestoreUrl() {
|
|
209
|
-
const state = Wick.PLPLoadMore.getRestoreState();
|
|
210
|
-
|
|
211
|
-
if (!state || !state.restoreUrl) {
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
state.redirected = true;
|
|
216
178
|
Wick.PLPLoadMore.setRestoreState(state);
|
|
217
179
|
|
|
218
|
-
|
|
180
|
+
history.replaceState(history.state, '', restoreUrl);
|
|
219
181
|
},
|
|
220
182
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if (currentUrl !== state.restoreUrl) {
|
|
231
|
-
return;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
requestAnimationFrame(function () {
|
|
235
|
-
requestAnimationFrame(function () {
|
|
236
|
-
const $target = $(
|
|
237
|
-
`${Wick.PLPLoadMore.el.productCard}[data-product-code="${state.productCode}"]`
|
|
238
|
-
);
|
|
239
|
-
|
|
240
|
-
if ($target.length) {
|
|
241
|
-
const offsetTop = $target.offset().top - 120;
|
|
242
|
-
|
|
243
|
-
window.scrollTo({
|
|
244
|
-
top: offsetTop,
|
|
245
|
-
behavior: 'auto',
|
|
246
|
-
});
|
|
247
|
-
}
|
|
183
|
+
loadPage(pageNumber) {
|
|
184
|
+
return new Promise((resolve, reject) => {
|
|
185
|
+
const eventDetail = { resolve, reject };
|
|
186
|
+
const payload = {
|
|
187
|
+
currentPage: pageNumber,
|
|
188
|
+
sort: Wick.PLPLoadMore.getSortValue(),
|
|
189
|
+
};
|
|
190
|
+
const event = createEvent(Wick.PLPLoadMore.PLP_LOAD_MORE_EVENT, payload, eventDetail);
|
|
248
191
|
|
|
249
|
-
|
|
250
|
-
Wick.PLPLoadMore.setRestoreState(state);
|
|
251
|
-
});
|
|
192
|
+
window.dispatchEvent(event);
|
|
252
193
|
});
|
|
253
194
|
},
|
|
254
195
|
|
|
@@ -272,19 +213,6 @@ Wick.PLPLoadMore = {
|
|
|
272
213
|
Wick.PLP?.bindEvents?.();
|
|
273
214
|
},
|
|
274
215
|
|
|
275
|
-
loadPage(pageNumber) {
|
|
276
|
-
return new Promise((resolve, reject) => {
|
|
277
|
-
const eventDetail = { resolve, reject };
|
|
278
|
-
const payload = {
|
|
279
|
-
currentPage: pageNumber,
|
|
280
|
-
sort: Wick.PLPLoadMore.getSortValue(),
|
|
281
|
-
};
|
|
282
|
-
const event = createEvent(Wick.PLPLoadMore.PLP_LOAD_MORE_EVENT, payload, eventDetail);
|
|
283
|
-
|
|
284
|
-
window.dispatchEvent(event);
|
|
285
|
-
});
|
|
286
|
-
},
|
|
287
|
-
|
|
288
216
|
isRestorePageMode() {
|
|
289
217
|
const state = Wick.PLPLoadMore.getRestoreState();
|
|
290
218
|
|
|
@@ -292,10 +220,6 @@ Wick.PLPLoadMore = {
|
|
|
292
220
|
return false;
|
|
293
221
|
}
|
|
294
222
|
|
|
295
|
-
if (!state.redirected) {
|
|
296
|
-
return false;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
223
|
if (state.restoreMode !== 'page') {
|
|
300
224
|
return false;
|
|
301
225
|
}
|
|
@@ -357,6 +281,38 @@ Wick.PLPLoadMore = {
|
|
|
357
281
|
);
|
|
358
282
|
},
|
|
359
283
|
|
|
284
|
+
handleRestoreScroll() {
|
|
285
|
+
const state = Wick.PLPLoadMore.getRestoreState();
|
|
286
|
+
|
|
287
|
+
if (!state || state.scrollApplied) {
|
|
288
|
+
return;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (window.location.href !== state.restoreUrl) {
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const $target = $(
|
|
296
|
+
`${Wick.PLPLoadMore.el.productCard}[data-product-code="${state.productCode}"]`
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
if (!$target.length) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
requestAnimationFrame(function () {
|
|
304
|
+
requestAnimationFrame(function () {
|
|
305
|
+
window.scrollTo({
|
|
306
|
+
top: $target.offset().top - 120,
|
|
307
|
+
behavior: 'auto',
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
state.scrollApplied = true;
|
|
311
|
+
Wick.PLPLoadMore.setRestoreState(state);
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
},
|
|
315
|
+
|
|
360
316
|
loadMore() {
|
|
361
317
|
const nextPage = Wick.PLPLoadMore.currentPage + 1;
|
|
362
318
|
|
|
@@ -378,6 +334,7 @@ Wick.PLPLoadMore = {
|
|
|
378
334
|
const cardsCount = Wick.PLPLoadMore.getCardsCountInDOM();
|
|
379
335
|
|
|
380
336
|
Wick.PLPLoadMore.setPerPage(cardsCount);
|
|
337
|
+
|
|
381
338
|
Wick.PLPLoadMore.updateLoadPreviousVisibility();
|
|
382
339
|
Wick.PLPLoadMore.toggleLoadMoreBtn();
|
|
383
340
|
Wick.PLPLoadMore.afterProductsRendered();
|
|
@@ -423,6 +380,7 @@ Wick.PLPLoadMore = {
|
|
|
423
380
|
const cardsCount = Wick.PLPLoadMore.getCardsCountInDOM();
|
|
424
381
|
|
|
425
382
|
Wick.PLPLoadMore.setPerPage(cardsCount);
|
|
383
|
+
|
|
426
384
|
Wick.PLPLoadMore.updateLoadPreviousVisibility();
|
|
427
385
|
Wick.PLPLoadMore.toggleLoadMoreBtn();
|
|
428
386
|
Wick.PLPLoadMore.afterProductsRendered();
|
|
@@ -445,12 +403,6 @@ Wick.PLPLoadMore = {
|
|
|
445
403
|
});
|
|
446
404
|
},
|
|
447
405
|
|
|
448
|
-
onPageShow() {
|
|
449
|
-
if (Wick.PLPLoadMore.shouldRedirectToRestoreUrl()) {
|
|
450
|
-
Wick.PLPLoadMore.redirectToRestoreUrl();
|
|
451
|
-
}
|
|
452
|
-
},
|
|
453
|
-
|
|
454
406
|
initCurrentPage() {
|
|
455
407
|
const params = new URLSearchParams(window.location.search);
|
|
456
408
|
|
|
@@ -480,13 +432,10 @@ Wick.PLPLoadMore = {
|
|
|
480
432
|
});
|
|
481
433
|
|
|
482
434
|
$(document)
|
|
483
|
-
.off('click.plpRestoreState', Wick.PLPLoadMore.el.
|
|
484
|
-
.on('click.plpRestoreState', Wick.PLPLoadMore.el.
|
|
435
|
+
.off('click.plpRestoreState', Wick.PLPLoadMore.el.productCardLink)
|
|
436
|
+
.on('click.plpRestoreState', Wick.PLPLoadMore.el.productCardLink, function (e) {
|
|
485
437
|
Wick.PLPLoadMore.saveRestoreState(e);
|
|
486
438
|
});
|
|
487
|
-
|
|
488
|
-
window.removeEventListener('pageshow', Wick.PLPLoadMore.onPageShow);
|
|
489
|
-
window.addEventListener('pageshow', Wick.PLPLoadMore.onPageShow);
|
|
490
439
|
},
|
|
491
440
|
|
|
492
441
|
init() {
|
|
@@ -495,7 +444,7 @@ Wick.PLPLoadMore = {
|
|
|
495
444
|
Wick.PLPLoadMore.bindEvents();
|
|
496
445
|
Wick.PLPLoadMore.renderLoadPrevious();
|
|
497
446
|
Wick.PLPLoadMore.toggleLoadMoreBtn();
|
|
498
|
-
Wick.PLPLoadMore.
|
|
447
|
+
Wick.PLPLoadMore.handleRestoreScroll();
|
|
499
448
|
},
|
|
500
449
|
};
|
|
501
450
|
|