/
product.js
521 lines (457 loc) · 14.3 KB
/
product.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/
import $ from 'jquery';
import prestashop from 'prestashop';
import {psGetRequestParameter} from './common';
// Used to be able to abort request if user modify something
let currentRequest = null;
// Used to clearTimeout if user flood the product quantity input
let currentRequestDelayedId = null;
// Check for popState event
let isOnPopStateEvent = false;
// Register form of first update
const firstFormData = [];
// Detect if the form has changed one time
let formChanged = false;
/**
* Get product update URL from different
* sources if needed (for compatibility)
*
* @return {Promise}
*/
function getProductUpdateUrl() {
const dfd = $.Deferred();
const $productActions = $(prestashop.selectors.product.actions);
const $quantityWantedInput = $(prestashop.selectors.quantityWanted);
if (
prestashop !== null
&& prestashop.urls !== null
&& prestashop.urls.pages !== null
&& prestashop.urls.pages.product !== ''
&& prestashop.urls.pages.product !== null
) {
dfd.resolve(prestashop.urls.pages.product);
return dfd.promise();
}
const formData = {};
$($productActions.find('form:first').serializeArray()).each((k, v) => {
formData[v.name] = v.value;
});
$.ajax({
url: $productActions.find('form:first').attr('action'),
method: 'POST',
data: {
ajax: 1,
action: 'productrefresh',
quantity_wanted: $quantityWantedInput.val(),
...formData,
},
dataType: 'json',
success(data) {
const productUpdateUrl = data.productUrl;
prestashop.page.canonical = productUpdateUrl;
dfd.resolve(productUpdateUrl);
},
error(jqXHR, textStatus, errorThrown) {
dfd.reject({
jqXHR,
textStatus,
errorThrown,
});
},
});
return dfd.promise();
}
/**
* @param {string} errorMessage
*/
function showErrorNextToAddtoCartButton(errorMessage) {
showError(
/* eslint-disable */
$(
".quickview #product-availability, .page-product:not(.modal-open) .row #product-availability, .page-product:not(.modal-open) .product-container #product-availability"
),
/* eslint-enable */
errorMessage,
);
}
/**
* Update the product html
*
* @param {string} event
* @param {string} eventType
* @param {string} updateUrl
*/
function updateProduct(event, eventType, updateUrl) {
const $productActions = $(prestashop.selectors.product.actions);
const $quantityWantedInput = $productActions.find(
prestashop.selectors.quantityWanted,
);
const $form = $productActions.find('form:first');
const formSerialized = $form.serialize();
let preview = psGetRequestParameter('preview');
let updateRatingEvent;
if (typeof Event === 'function') {
updateRatingEvent = new Event('updateRating');
} else {
updateRatingEvent = document.createEvent('Event');
updateRatingEvent.initEvent('updateRating', true, true);
}
if (preview !== null) {
preview = `&preview=${preview}`;
} else {
preview = '';
}
// Can not get product ajax url
if (updateUrl === null) {
showErrorNextToAddtoCartButton();
return;
}
// New request only if new value
if (
event
&& event.type === 'keyup'
&& $quantityWantedInput.val() === $quantityWantedInput.data('old-value')
) {
return;
}
$quantityWantedInput.data('old-value', $quantityWantedInput.val());
if (currentRequestDelayedId) {
clearTimeout(currentRequestDelayedId);
}
// Most update need to occur (almost) instantly, but in some cases (like keyboard actions)
// we need to delay the update a bit more
let updateDelay = 30;
if (eventType === 'updatedProductQuantity') {
updateDelay = 750;
}
currentRequestDelayedId = setTimeout(() => {
if (formSerialized === '') {
return;
}
currentRequest = $.ajax({
url:
updateUrl
+ (updateUrl.indexOf('?') === -1 ? '?' : '&')
+ formSerialized
+ preview,
method: 'POST',
data: {
quickview: $('.modal.quickview.in').length,
ajax: 1,
action: 'refresh',
quantity_wanted:
eventType === 'updatedProductCombination'
? $quantityWantedInput.attr('min')
: $quantityWantedInput.val(),
},
dataType: 'json',
beforeSend() {
if (currentRequest !== null) {
currentRequest.abort();
}
},
error(jqXHR, textStatus) {
if (
textStatus !== 'abort'
&& $('section#main > .ajax-error').length === 0
) {
showErrorNextToAddtoCartButton();
}
},
success(data) {
// Avoid image to blink each time we modify the product quantity
// Can not compare directly cause of HTML comments in data.
const $newImagesContainer = $('<div>').append(
data.product_cover_thumbnails,
);
// Used to avoid image blinking if same image = epileptic friendly
if (
$(prestashop.selectors.product.imageContainer).html()
!== $newImagesContainer
.find(prestashop.selectors.product.imageContainer)
.html()
) {
$(prestashop.selectors.product.imageContainer).replaceWith(
data.product_cover_thumbnails,
);
}
$(prestashop.selectors.product.prices)
.first()
.replaceWith(data.product_prices);
$(prestashop.selectors.product.customization)
.first()
.replaceWith(data.product_customization);
// refill customizationId input value when updating quantity or combination
if (
(eventType === 'updatedProductQuantity' || eventType === 'updatedProductCombination')
&& data.id_customization
) {
$(prestashop.selectors.cart.productCustomizationId).val(data.id_customization);
} else {
$(prestashop.selectors.product.inputCustomization).val(0);
}
$(prestashop.selectors.product.variantsUpdate)
.first()
.replaceWith(data.product_variants);
$(prestashop.selectors.product.discounts)
.first()
.replaceWith(data.product_discounts);
$(prestashop.selectors.product.additionalInfos)
.first()
.replaceWith(data.product_additional_info);
$(prestashop.selectors.product.details).replaceWith(
data.product_details,
);
$(prestashop.selectors.product.flags)
.first()
.replaceWith(data.product_flags);
replaceAddToCartSections(data);
const minimalProductQuantity = parseInt(
data.product_minimal_quantity,
10,
);
document.dispatchEvent(updateRatingEvent);
// Prevent quantity input from blinking with classic theme.
if (
!isNaN(minimalProductQuantity)
&& eventType !== 'updatedProductQuantity'
) {
$quantityWantedInput.attr('min', minimalProductQuantity);
$quantityWantedInput.val(minimalProductQuantity);
}
prestashop.emit('updatedProduct', data, $form.serializeArray());
},
complete() {
currentRequest = null;
currentRequestDelayedId = null;
},
});
}, updateDelay);
}
/**
* Replace all "add to cart" sections but the quantity input
* in order to keep quantity field intact i.e.
*
* @param {object} data of updated product and cat
*/
function replaceAddToCartSections(data) {
let $productAddToCart = null;
$(data.product_add_to_cart).each((index, value) => {
if ($(value).hasClass('product-add-to-cart')) {
$productAddToCart = $(value);
return false;
}
return true;
});
if ($productAddToCart === null) {
showErrorNextToAddtoCartButton();
}
const $addProductToCart = $(prestashop.selectors.product.addToCart);
const productAvailabilitySelector = '.add';
const productAvailabilityMessageSelector = '#product-availability';
const productMinimalQuantitySelector = '.product-minimal-quantity';
replaceAddToCartSection({
$addToCartSnippet: $productAddToCart,
$targetParent: $addProductToCart,
targetSelector: productAvailabilitySelector,
});
replaceAddToCartSection({
$addToCartSnippet: $productAddToCart,
$targetParent: $addProductToCart,
targetSelector: productAvailabilityMessageSelector,
});
replaceAddToCartSection({
$addToCartSnippet: $productAddToCart,
$targetParent: $addProductToCart,
targetSelector: productMinimalQuantitySelector,
});
}
/**
* Find DOM elements and replace their content
*
* @param {object} replacement Data to be replaced on the current page
*/
function replaceAddToCartSection(replacement) {
const destinationObject = $(
replacement.$targetParent.find(replacement.targetSelector),
);
if (destinationObject.length <= 0) {
return;
}
const replace = replacement.$addToCartSnippet.find(
replacement.targetSelector,
);
if (replace.length > 0) {
destinationObject.replaceWith(replace[0].outerHTML);
} else {
destinationObject.html('');
}
}
/**
* @param {jQuery} $container
* @param {string} textError
*/
function showError($container, textError) {
const $error = $(
`<div class="alert alert-danger ajax-error" role="alert">${textError}</div>`,
);
$container.replaceWith($error);
}
$(() => {
const $productActions = $(prestashop.selectors.product.actions);
// Listen on all form elements + those who have a data-product-attribute
$('body').on(
'change touchspin.on.startspin',
`${prestashop.selectors.product.variants} *[name]`,
(e) => {
formChanged = true;
prestashop.emit('updateProduct', {
eventType: 'updatedProductCombination',
event: e,
// Following variables are not used anymore, but kept for backward compatibility
resp: {},
reason: {
productUrl: prestashop.urls.pages.product || '',
},
});
},
);
// Stocking first form information
$($productActions.find('form:first').serializeArray()).each(
(k, {value, name}) => {
firstFormData.push({value, name});
},
);
window.addEventListener('popstate', (event) => {
isOnPopStateEvent = true;
if (
(!event.state
|| (event.state && event.state.form && event.state.form.length === 0))
&& !formChanged
) {
return;
}
const $form = $(prestashop.selectors.product.actions).find('form:first');
if (event.state && event.state.form) {
event.state.form.forEach((pair) => {
$form.find(`[name="${pair.name}"]`).val(pair.value);
});
} else {
firstFormData.forEach((pair) => {
$form.find(`[name="${pair.name}"]`).val(pair.value);
});
}
prestashop.emit('updateProduct', {
eventType: 'updatedProductCombination',
event,
// Following variables are not used anymore, but kept for backward compatibility
resp: {},
reason: {
productUrl: prestashop.urls.pages.product || '',
},
});
});
/**
* Button has been removed on classic theme, but event triggering has been kept for compatibility
*/
$('body').on(
'click',
prestashop.selectors.product.refresh,
(e, extraParameters) => {
e.preventDefault();
let eventType = 'updatedProductCombination';
if (typeof extraParameters !== 'undefined' && extraParameters.eventType) {
// eslint-disable-next-line
eventType = extraParameters.eventType;
}
prestashop.emit('updateProduct', {
eventType,
event: e,
// Following variables are not used anymore, but kept for backward compatibility
resp: {},
reason: {
productUrl: prestashop.urls.pages.product || '',
},
});
},
);
// Refresh all the product content
prestashop.on('updateProduct', (args) => {
const {eventType} = args;
const {event} = args;
getProductUpdateUrl()
.done((productUpdateUrl) => updateProduct(event, eventType, productUpdateUrl),
)
.fail(() => {
if ($('section#main > .ajax-error').length === 0) {
showErrorNextToAddtoCartButton();
}
});
});
prestashop.on('updatedProduct', (args, formData) => {
if (!args.product_url || !args.id_product_attribute) {
return;
}
/*
* If quickview modal is present we are not on product page, so
* we don't change the url nor title
*/
const quickView = $('.modal.quickview');
if (quickView.length) {
return;
}
let pageTitle = document.title;
if (args.product_title) {
pageTitle = args.product_title;
$(document).attr('title', pageTitle);
}
if (!isOnPopStateEvent) {
window.history.pushState(
{
id_product_attribute: args.id_product_attribute,
form: formData,
},
pageTitle,
args.product_url,
);
}
isOnPopStateEvent = false;
});
prestashop.on('updateCart', (event) => {
if (!event || !event.reason || event.reason.linkAction !== 'add-to-cart') {
return;
}
const $quantityWantedInput = $('#quantity_wanted');
// Force value to 1, it will automatically trigger updateProduct and reset the appropriate min value if needed
$quantityWantedInput.val(1);
});
prestashop.on('showErrorNextToAddtoCartButton', (event) => {
if (!event || !event.errorMessage) {
return;
}
showErrorNextToAddtoCartButton(event.errorMessage);
});
});