PageSourceSearch

https://lemontsaintmichel.fr/wp-content/plugins/pw-gift-cards/assets/js/pw-gift-cards.js?ver=3.69

js lemontsaintmichel.fr collected 2026-09-26 02:28:08 UTC 20,372 bytes, 515 lines download raw bytes

1function pwgc_init() {
2    var checkBalanceForm = jQuery('#pwgc-balance-form');
3    if ( checkBalanceForm.length ) {
4        checkBalanceForm.on('submit', function(e) {
5            jQuery('#pwgc-balance-message').html('');
6            pwgc_check_balance();
7
8            e.preventDefault();
9            return false;
10        });
11        pwgc_check_balance();
12    }
13
14    jQuery('#pwgc-balance-reload').off('click.pwgc').on('click.pwgc', function(e) {
15        var cardNumber = jQuery(this).attr('data-card-number');
16        var productUrl = jQuery(this).attr('data-url');
17        if (cardNumber) {
18            var separator = '?';
19            if (productUrl.includes('?')) {
20                separator = '&';
21            }
22
23            location.href = productUrl + separator + pwgc.reload_key + '=' + cardNumber;
24        }
25        e.preventDefault();
26        return false;
27    });
28
29    jQuery('#pwgc-manual-debit').off('click.pwgc').on('click.pwgc', function(e) {
30        jQuery(this).prop('disabled', true);
31        var errorMessage = jQuery('#pwgc-balance-error');
32        errorMessage.text('');
33
34        var cardNumber = jQuery(this).attr('data-card-number');
35        var remainingBalance = jQuery(this).attr('data-card-remaining-balance');
36        if (cardNumber && remainingBalance) {
37            var amount = prompt( pwgc.i18n.debit_amount_prompt, remainingBalance );
38            if (amount) {
39                var note = prompt( pwgc.i18n.debit_note_prompt );
40                if (note !== null) {
41                    var balance = jQuery('#pwgc-balance-amount');
42                    var balanceMessage = jQuery('#pwgc-balance-message');
43
44                    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-debit', 'card_number': cardNumber, 'amount': amount, 'note': note, 'security': pwgc.nonces.debit_balance}, function(result) {
45                        balance.html(result.balance);
46                        balanceMessage.html(result.message);
47                        pwgc_check_balance();
48                    }).fail(function(xhr, textStatus, errorThrown) {
49                        if (errorThrown) {
50                            errorMessage.text(errorThrown);
51                        } else {
52                            errorMessage.text('Unknown ajax error');
53                        }
54                    });
55                }
56            }
57        }
58        jQuery(this).prop('disabled', false);
59        e.preventDefault();
60        return false;
61    });
62
63    jQuery(document.body).on('updated_wc_div', pwgc_bind_redeem_form);
64    jQuery(document.body).on('updated_shipping_method', pwgc_bind_redeem_form);
65    pwgc_bind_redeem_form();
66
67    jQuery(document.body).on('updated_wc_div', pwgc_bind_remove_link);
68    jQuery(document.body).on('updated_checkout', pwgc_bind_remove_link);
69    jQuery(document.body).on('updated_shipping_method', pwgc_bind_remove_link);
70    pwgc_bind_remove_link();
71
72    jQuery('#pwgc-message').on('input propertychange', function() {
73        pwgc_message_characters_remaining();
74    });
75    pwgc_message_characters_remaining();
76
77    jQuery('form.variations_form').on('show_variation', function() {
78        jQuery('#pwgc-purchase-container').show();
79        pwgc_toggle_quantity();
80        pwgc_toggle_custom_amount_visibility();
81    });
82
83    jQuery('form.variations_form').on('hide_variation', function() {
84        jQuery('#pwgc-purchase-container').hide();
85        pwgc_toggle_quantity();
86        pwgc_toggle_custom_amount_visibility();
87    });
88
89    var attributeSelector = jQuery(document.getElementById(pwgc.denomination_attribute_slug));
90    if (attributeSelector.length == 0) {
91        attributeSelector = jQuery(document.getElementsByName('attribute_' + pwgc.denomination_attribute_slug));
92    }
93
94    if (attributeSelector.val()) {
95        jQuery('#pwgc-purchase-container').show();
96    } else {
97        jQuery('#pwgc-purchase-container').hide();
98    }
99
100    pwgc_toggle_custom_amount_visibility();
101
102    jQuery('#pwgc-custom-amount').on('blur', function() {
103        jQuery('#pwgc-custom-amount-error').text('');
104
105        var amount = jQuery(this).val();
106
107        amount = amount.replace(pwgc.thousand_separator, '');
108        amount = amount.replace(pwgc.decimal_separator, '.');
109        amount = amount.replace(/[^0-9.]/g,'');
110        if (amount) {
111            var floatAmount = parseFloat(amount);
112            var container = jQuery('#pwgc-purchase-container');
113            var minAmount = parseFloat(container.attr('data-min-amount'));
114            var maxAmount = parseFloat(container.attr('data-max-amount'));
115
116            if (floatAmount < minAmount) {
117                jQuery('#pwgc-custom-amount-error').html(pwgc.i18n.min_amount_error + minAmount.toFixed(pwgc.decimal_places).replace('.', pwgc.decimal_separator));
118            } else if (floatAmount > maxAmount) {
119                jQuery('#pwgc-custom-amount-error').html(pwgc.i18n.max_amount_error + maxAmount.toFixed(pwgc.decimal_places).replace('.', pwgc.decimal_separator));
120            } else {
121                jQuery(this).val(floatAmount.toFixed(pwgc.decimal_places).replace('.', pwgc.decimal_separator).replace(/\B(?=(\d{3})+(?!\d))/g, pwgc.thousand_separator));
122            }
123        } else {
124            jQuery('#pwgc-custom-amount-error').html(pwgc.i18n.custom_amount_required_error);
125        }
126    });
127
128    jQuery('#pwgc-to').on('blur', function() {
129        var recipients = jQuery(this).val();
130        if (recipients) {
131            // For clarity, ensure we do a comma followed by a space.
132            // Babel translation of this line:
133            // jQuery(this).val(recipients.trim().split(/[ ,]+/).map(item=>item.trim()).join(', '));
134
135            // This is compatible with IE11
136
137            recipients = recipients.trim().split(/[ ,]+/).map(function (item) {
138                return item.trim();
139            });
140
141            if (pwgc.allow_multiple_recipients != 'yes' && recipients.length > 1) {
142                recipients = [recipients[0]];
143            }
144
145            jQuery(this).val(recipients.join(', '));
146        }
147
148        pwgc_toggle_quantity();
149    });
150
151    jQuery('.variations_form').on( 'found_variation.wc-variation-form', function() {
152        pwgc_toggle_quantity();
153    });
154
155    jQuery('.variations_form').on( 'reset_data', function() {
156        pwgc_toggle_quantity();
157    });
158
159    jQuery('.variations_form').on( 'submit', function(e) {
160        if (jQuery('#pwgc-to').length) {
161            var recipients = jQuery('#pwgc-to').val().split(/[ ,]+/);
162            var badRecipients = [];
163
164            for (var i = 0; i < recipients.length; i++) {
165                if (!pwgc_is_email(recipients[i])) {
166                    badRecipients.push(recipients[i]);
167                }
168            }
169
170            if (badRecipients.length) {
171                alert(pwgc.i18n.invalid_recipient_error + '\n\n' + badRecipients.join('\n'));
172                e.preventDefault();
173                return false;
174            }
175        }
176    });
177
178    jQuery('.show-pw-gift-card').off('click.pwgc').on('click.pwgc', function(e) {
179        jQuery('.checkout_pw_gift_card').slideToggle(400, function() {
180            jQuery('.checkout_pw_gift_card').find(':input:eq(0)').focus();
181        });
182
183        e.preventDefault();
184        return false;
185    });
186
187    jQuery('#pwgc-apply-gift-card-checkout').off('click.pwgc').on('click.pwgc', function(e) {
188        pwgc_checkout_redeem_gift_card(jQuery(this));
189        e.preventDefault();
190        return false;
191    });
192
193    jQuery('#pwgc-redeem-gift-card-number').off('keypress.pwgc').on('keypress.pwgc', function(e) {
194        if (e.keyCode == 13) {
195            pwgc_checkout_redeem_gift_card(jQuery('#pwgc-apply-gift-card-checkout'));
196
197            e.preventDefault();
198            return false;
199        }
200    });
201
202    var deliveryDateField = document.getElementById('pwgc-delivery-date');
203    if (deliveryDateField) {
204        var today = new Date();
205        today.setDate(today.getDate());
206
207        var nextYear = new Date();
208        nextYear.setDate(nextYear.getDate() + 365);
209
210        var picker = new Pikaday({
211            field: deliveryDateField,
212            minDate: today,
213            maxDate: nextYear,
214            format: pwgc.pikaday.format,
215            firstDay: pwgc.pikaday.firstDay,
216            i18n: {
217                previousMonth : pwgc.i18n.previousMonth,
218                nextMonth     : pwgc.i18n.nextMonth,
219                months        : [pwgc.i18n.jan, pwgc.i18n.feb, pwgc.i18n.mar, pwgc.i18n.apr, pwgc.i18n.may, pwgc.i18n.jun, pwgc.i18n.jul, pwgc.i18n.aug, pwgc.i18n.sep, pwgc.i18n.oct, pwgc.i18n.nov, pwgc.i18n.dec],
220                weekdays      : [pwgc.i18n.sunday, pwgc.i18n.monday, pwgc.i18n.tuesday, pwgc.i18n.wednesday, pwgc.i18n.thursday, pwgc.i18n.friday, pwgc.i18n.saturday],
221                weekdaysShort : [pwgc.i18n.sun, pwgc.i18n.mon, pwgc.i18n.tue, pwgc.i18n.wed, pwgc.i18n.thu, pwgc.i18n.fri, pwgc.i18n.sat]
222            },
223        });
224    }
225
226    jQuery('#pwgc-email-preview-container').off('load.pwgc').on('load.pwgc', function() {
227        jQuery('#pwgc-email-preview-container').hide();
228
229        var iFrameBody = jQuery(this).contents().find('body');
230        if (iFrameBody.length) {
231            jQuery('#pwgc-email-preview-container').show();
232
233            // Set the iFrame to the height of the content, plus a little extra.
234            jQuery(this).css('height', ( parseInt( pwgc.preview_padding ) + iFrameBody.prop('scrollHeight') ) + "px" );
235
236            // Disable the Redeem button.
237            iFrameBody.find('#pwgc-email-redeem-button a').off('click.pwgc').on('click.pwgc', function(e) {
238                e.preventDefault();
239            });
240        }
241        jQuery('#pwgc-email-preview-button').prop( 'disabled', false );
242    });
243
244    jQuery('#pwgc-email-preview-button').off('click.pwgc').on('click.pwgc', function(e) {
245        pwgc_show_email_preview();
246        e.preventDefault();
247    });
248
249    jQuery('#pwgc-email-design-id').off('change.pwgc').on('change.pwgc', function(e) {
250       pwgc_show_email_preview();
251    });
252}
253
254function pwgc_show_email_preview() {
255    var forms = document.getElementsByClassName('variations_form');
256    if (forms.length > 0 && !forms[0].reportValidity()) {
257        return;
258    }
259
260    jQuery('#pwgc-email-preview-button').prop( 'disabled', true );
261    jQuery('#pwgc-email-preview-container').contents().find('body').html('...');
262    jQuery('#pwgc-email-preview-container').show();
263
264    var previewUrl = pwgc.preview_email_url;
265
266    amount = jQuery('#pwgc-custom-amount').val();
267    amount = amount.replace(pwgc.thousand_separator, '');
268    amount = amount.replace(pwgc.decimal_separator, '.');
269    amount = amount.replace(/[^0-9.]/g,'');
270
271
272    previewUrl += '&pwgc_number=' + pwgc.example_gift_card_number;
273    previewUrl += '&variation_id=' + jQuery('.variation_id').val();
274    previewUrl += '&design_id=' + jQuery('#pwgc-email-design-id').val();
275    previewUrl += '&to=' + encodeURIComponent(jQuery('#pwgc-to').val());
276    previewUrl += '&from=' + encodeURIComponent(jQuery('#pwgc-from').val());
277    previewUrl += '&message=' + encodeURIComponent(jQuery('#pwgc-message').val());
278    previewUrl += '&other_amount=' + encodeURIComponent(amount);
279    previewUrl += '&pdf=' + pwgc.preview_email_pdf;
280
281    if (jQuery('#pwgc-recipient-name').length) {
282        previewUrl += '&recipient_name=' + encodeURIComponent(jQuery('#pwgc-recipient-name').val());
283    }
284
285    jQuery('#pwgc-email-preview-container').attr('src', previewUrl);
286}
287
288function pwgc_pad(n, width, z) {
289    z = z || '0';
290    n = n + '';
291    return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
292}
293
294function pwgc_is_email(email) {
295    var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
296    return regex.test(email);
297}
298
299function pwgc_toggle_quantity() {
300    if (jQuery('#pwgc-to').length) {
301        var recipients = jQuery('#pwgc-to').val().split(/[ ,]+/);
302        if (recipients.length > 1) {
303            jQuery('#pwgc-recipient-count').text(recipients.length);
304            jQuery('#pwgc-quantity-one-per-recipient').show();
305            jQuery('input.qty').val('1');
306            jQuery('.quantity').hide();
307        } else {
308            jQuery('#pwgc-quantity-one-per-recipient').hide();
309            jQuery('.quantity').show();
310        }
311    }
312}
313
314function pwgc_toggle_custom_amount_visibility() {
315    var showOtherAmount = false;
316    var attributeSelector = jQuery(document.getElementById(pwgc.denomination_attribute_slug));
317    if (attributeSelector.length != 0) {
318        showOtherAmount = (attributeSelector.val() == pwgc.other_amount_prompt);
319    } else {
320        var selectedRadioButton = jQuery("input[type='radio'][name='attribute_" + pwgc.denomination_attribute_slug + "']:checked");
321        if (selectedRadioButton.length > 0) {
322            showOtherAmount = (selectedRadioButton.val() == pwgc.other_amount_prompt);
323        } else {
324            attributeSelector = jQuery(document.getElementsByName('attribute_' + pwgc.denomination_attribute_slug));
325            showOtherAmount = (attributeSelector.val() == pwgc.other_amount_prompt);
326        }
327    }
328
329    if (showOtherAmount) {
330        jQuery('#pwgc-custom-amount-form').show();
331        if (pwgc.auto_focus_other_amount) {
332            jQuery('#pwgc-custom-amount').focus();
333        }
334    } else {
335        jQuery('#pwgc-custom-amount').val('');
336        jQuery('#pwgc-custom-amount-form').hide();
337    }
338}
339
340function pwgc_check_balance() {
341    var cardNumber = jQuery('#pwgc-balance-number');
342    if (!cardNumber.val()) { return; }
343
344    jQuery('#pwgc-balance-error').text('');
345    jQuery('#pwgc-balance-activity,#pwgc-balance-expiration-date-container,#pwgc-balance-reload,#pwgc-manual-debit').hide();
346    jQuery('#pwgc-balance-amount').html(pwgc.balance_check_icon);
347
348    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-balance', 'card_number': cardNumber.val(), 'security': pwgc.nonces.check_balance}, function( result ) {
349        if (result.success) {
350
351            jQuery('#pwgc-balance-amount').html(result.data.balance);
352
353            if (result.data.expiration_date) {
354                jQuery('#pwgc-balance-expiration-date').html(result.data.expiration_date);
355                jQuery('#pwgc-balance-expiration-date-container').show();
356            }
357
358            jQuery('#pwgc-balance-activity').html(result.data.activity).show();
359            jQuery('#pwgc-balance-reload,#pwgc-manual-debit').attr('data-card-number', result.data.card_number).attr('data-url', result.data.url).show();
360            jQuery('#pwgc-manual-debit').attr('data-card-remaining-balance', result.data.remaining_balance).show();
361        } else {
362            jQuery('#pwgc-balance-error').text(result.data.message);
363            jQuery('#pwgc-balance-amount').text('');
364            jQuery('#pwgc-balance-activity,#pwgc-balance-expiration-date-container,#pwgc-balance-reload,#pwgc-manual-debit').hide();
365        }
366        cardNumber.focus();
367
368    }).fail(function(xhr, textStatus, errorThrown) {
369        if (errorThrown) {
370            jQuery('#pwgc-balance-error').text(errorThrown);
371        } else {
372            jQuery('#pwgc-balance-error').text('Unknown Error');
373        }
374        jQuery('#pwgc-balance-amount').text('');
375        jQuery('#pwgc-balance-reload,#pwgc-manual-debit').hide();
376        cardNumber.focus();
377    });
378}
379
380function pwgc_bind_remove_link() {
381    jQuery('.pwgc-remove-card').off('click.pwgc').on('click.pwgc', function(e) {
382        var cardNumber = jQuery(this).attr('data-card-number');
383        var checkoutContainer = jQuery(this).parents( '.woocommerce-checkout-review-order' );
384
385        if (checkoutContainer.length) {
386            checkoutContainer.addClass( 'processing' ).block({
387                message: null,
388                overlayCSS: {
389                    background: '#fff',
390                    opacity: 0.6
391                }
392            });
393        }
394
395        jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-remove', 'card_number': cardNumber, 'security': pwgc.nonces.remove_card}, function( result ) {
396            if (checkoutContainer.length) {
397                jQuery( '.woocommerce-error, .woocommerce-message' ).remove();
398                checkoutContainer.removeClass( 'processing' ).unblock();
399                jQuery( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
400            } else {
401                jQuery( document.body ).trigger( 'wc_update_cart' );
402            }
403        }).fail(function(xhr, textStatus, errorThrown) {
404            if (errorThrown) {
405                alert(errorThrown);
406            } else {
407                alert('Unknown Error');
408            }
409        });
410
411        e.preventDefault();
412        return false;
413    });
414}
415
416function pwgc_bind_redeem_form() {
417    jQuery('#pwgc-redeem-form').off('submit.pwgc').on('submit.pwgc', function(e) {
418        var redeemButton = jQuery('#pwgc-redeem-button');
419
420        pwgc_redeem_gift_card(redeemButton);
421
422        e.preventDefault();
423        return false;
424    });
425
426    jQuery('#pwgc-apply-gift-card,#pwgc-redeem-button').off('click.pwgc').on('click.pwgc', function(e) {
427        pwgc_redeem_gift_card(jQuery(this));
428
429        e.preventDefault();
430        return false;
431    });
432
433    jQuery('#pwgc-redeem-gift-card-number').off('keypress.pwgc').on('keypress.pwgc', function(e) {
434        if (e.keyCode == 13) {
435            pwgc_redeem_gift_card(jQuery('#pwgc-apply-gift-card'));
436
437            e.preventDefault();
438            return false;
439        }
440    });
441}
442
443function pwgc_redeem_gift_card(redeemButton) {
444    var cardNumber = jQuery('#pwgc-redeem-gift-card-number');
445    var errorContainer = jQuery('#pwgc-redeem-error');
446
447    errorContainer.text('');
448    redeemButton.attr('data-apply-text', redeemButton.attr('value')).attr('value', redeemButton.attr('data-wait-text')).prop('disabled', true);
449
450    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-redeem', 'card_number': cardNumber.val(), 'security': pwgc.nonces.apply_gift_card}, function( result ) {
451        if (result.success) {
452            jQuery( document.body ).trigger( 'wc_update_cart' );
453        } else {
454            errorContainer.text(result.data.message);
455            redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
456            cardNumber.focus();
457        }
458    }).fail(function(xhr, textStatus, errorThrown) {
459        if (errorThrown) {
460            errorContainer.text(errorThrown);
461        } else {
462            errorContainer.text('Unknown Error');
463        }
464        redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
465        cardNumber.focus();
466    });
467}
468
469function pwgc_message_characters_remaining() {
470    var charsRemaining = pwgc.max_message_characters;
471
472    var messageElement = jQuery('#pwgc-message').val();
473    if (messageElement) {
474        charsRemaining -= messageElement.length;
475    }
476
477    jQuery('#pwgc-message-characters-remaining').text(charsRemaining);
478}
479
480function pwgc_checkout_redeem_gift_card(redeemButton) {
481    var errorContainer = jQuery('#pwgc-redeem-error');
482    var cardNumber = jQuery('#pwgc-redeem-gift-card-number');
483
484    errorContainer.text('');
485    redeemButton.attr('data-apply-text', redeemButton.attr('value')).attr('value', redeemButton.attr('data-wait-text')).prop('disabled', true);
486
487    jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-redeem', 'card_number': cardNumber.val(), 'security': pwgc.nonces.apply_gift_card}, function( result ) {
488        var form = jQuery('.checkout_pw_gift_card');
489        if (result.success) {
490            form.slideUp();
491            cardNumber.val('');
492            jQuery( document.body ).trigger( 'update_checkout', { update_shipping_method: false } );
493        } else {
494            errorContainer.text(result.data.message);
495            cardNumber.focus();
496        }
497        redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
498    }).fail(function(xhr, textStatus, errorThrown) {
499        if (errorThrown) {
500            errorContainer.text(errorThrown);
501        } else {
502            errorContainer.text('Unknown Error');
503        }
504        redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false);
505        cardNumber.focus();
506    });
507}
508
509if ( document.readyState !== 'loading' ) {
510    pwgc_init();
511} else {
512    document.addEventListener('DOMContentLoaded', function () {
513        pwgc_init();
514    });
515}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.