1function pwgc_init() { 2 jQuery(document.body).on('updated_wc_div', pwgc_bind_redeem_form); 3 jQuery(document.body).on('updated_shipping_method', pwgc_bind_redeem_form); 4 pwgc_bind_redeem_form(); 5 6 jQuery(document.body).on('updated_wc_div', pwgc_bind_remove_link); 7 jQuery(document.body).on('updated_checkout', pwgc_bind_remove_link); 8 jQuery(document.body).on('updated_shipping_method', pwgc_bind_remove_link); 9 pwgc_bind_remove_link(); 10 11 jQuery('#pwgc-message').on('input propertychange', function() { 12 pwgc_message_characters_remaining(); 13 }); 14 pwgc_message_characters_remaining(); 15 16 jQuery('form.variations_form').on('show_variation', function() { 17 jQuery('#pwgc-purchase-container').show(); 18 pwgc_toggle_quantity(); 19 }); 20 21 jQuery('form.variations_form').on('hide_variation', function() { 22 jQuery('#pwgc-purchase-container').hide(); 23 pwgc_toggle_quantity(); 24 }); 25 26 jQuery('#pwgc-to').on('blur', function() { 27 var recipients = jQuery(this).val(); 28 if (recipients) { 29 // For clarity, ensure we do a comma followed by a space. 30 // Babel translation of this line: 31 // jQuery(this).val(recipients.trim().split(/[ ,]+/).map(item=>item.trim()).join(', ')); 32 33 // This is compatible with IE11 34 jQuery(this).val(recipients.trim().split(/[ ,]+/).map(function (item) { 35 return item.trim(); 36 }).join(', ')); 37 } 38 39 pwgc_toggle_quantity(); 40 }); 41 42 jQuery('.variations_form').on( 'found_variation.wc-variation-form', function() { 43 pwgc_toggle_quantity(); 44 }); 45 46 jQuery('.variations_form').on( 'reset_data', function() { 47 pwgc_toggle_quantity(); 48 }); 49 50 jQuery('.variations_form').on( 'submit', function(e) { 51 if (jQuery('#pwgc-to').length) { 52 var recipients = jQuery('#pwgc-to').val().split(/[ ,]+/); 53 var badRecipients = []; 54 55 for (var i = 0; i < recipients.length; i++) { 56 if (!pwgc_is_email(recipients[i])) { 57 badRecipients.push(recipients[i]); 58 } 59 } 60 61 if (badRecipients.length) { 62 alert(pwgc.i18n.invalid_recipient_error + '\n\n' + badRecipients.join('\n')); 63 e.preventDefault(); 64 return false; 65 } 66 } 67 }); 68 69 jQuery('.show-pw-gift-card').off('click.pwgc').on('click.pwgc', function(e) { 70 jQuery('.checkout_pw_gift_card').slideToggle(400, function() { 71 jQuery('.checkout_pw_gift_card').find(':input:eq(0)').trigger('focus'); 72 }); 73 74 e.preventDefault(); 75 return false; 76 }); 77 78 jQuery('#pwgc-apply-gift-card-checkout').off('click.pwgc').on('click.pwgc', function(e) { 79 pwgc_checkout_redeem_gift_card(jQuery(this)); 80 e.preventDefault(); 81 return false; 82 }); 83 84 jQuery('#pwgc-redeem-gift-card-number').off('keypress.pwgc').on('keypress.pwgc', function(e) { 85 if (e.keyCode == 13) { 86 pwgc_checkout_redeem_gift_card(jQuery('#pwgc-apply-gift-card-checkout')); 87 88 e.preventDefault(); 89 return false; 90 } 91 }); 92} 93 94function pwgc_is_email(email) { 95 var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; 96 return regex.test(email); 97} 98 99function pwgc_toggle_quantity() { 100 if (jQuery('#pwgc-to').length) { 101 var recipients = jQuery('#pwgc-to').val().split(/[ ,]+/); 102 if (recipients.length > 1) { 103 jQuery('#pwgc-recipient-count').text(recipients.length); 104 jQuery('#pwgc-quantity-one-per-recipient').show(); 105 jQuery('input.qty').val('1'); 106 jQuery('.quantity').hide(); 107 } else { 108 jQuery('#pwgc-quantity-one-per-recipient').hide(); 109 jQuery('.quantity').show(); 110 } 111 } 112} 113 114function pwgc_bind_remove_link() { 115 jQuery('.pwgc-remove-card').off('click.pwgc').on('click.pwgc', function(e) { 116 var cardNumber = jQuery(this).attr('data-card-number'); 117 var checkoutContainer = jQuery(this).parents( '.woocommerce-checkout-review-order' ); 118 119 if (checkoutContainer.length) { 120 checkoutContainer.addClass( 'processing' ).block({ 121 message: null, 122 overlayCSS: { 123 background: '#fff', 124 opacity: 0.6 125 } 126 }); 127 } 128 129 jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-remove', 'card_number': cardNumber, 'security': pwgc.nonces.remove_card}, function( result ) { 130 if (checkoutContainer.length) { 131 jQuery( '.woocommerce-error, .woocommerce-message' ).remove(); 132 checkoutContainer.removeClass( 'processing' ).unblock(); 133 jQuery( document.body ).trigger( 'update_checkout', { update_shipping_method: false } ); 134 } else { 135 jQuery( document.body ).trigger( 'wc_update_cart' ); 136 } 137 }).fail(function(xhr, textStatus, errorThrown) { 138 if (errorThrown) { 139 console.log(errorThrown); 140 } else { 141 console.log('Unknown Error'); 142 } 143 }); 144 145 e.preventDefault(); 146 return false;
147 }); 148} 149 150function pwgc_bind_redeem_form() { 151 jQuery('#pwgc-redeem-form').off('submit.pwgc').on('submit.pwgc', function(e) { 152 var redeemButton = jQuery('#pwgc-redeem-button'); 153 154 pwgc_redeem_gift_card(redeemButton); 155 156 e.preventDefault(); 157 return false; 158 }); 159 160 jQuery('#pwgc-apply-gift-card,#pwgc-redeem-button').off('click.pwgc').on('click.pwgc', function(e) { 161 pwgc_redeem_gift_card(jQuery(this)); 162 163 e.preventDefault(); 164 return false; 165 }); 166 167 jQuery('#pwgc-redeem-gift-card-number').off('keypress.pwgc').on('keypress.pwgc', function(e) { 168 if (e.keyCode == 13) { 169 pwgc_redeem_gift_card(jQuery('#pwgc-apply-gift-card')); 170 171 e.preventDefault(); 172 return false; 173 } 174 }); 175} 176 177function pwgc_redeem_gift_card(redeemButton) { 178 var cardNumber = jQuery('#pwgc-redeem-gift-card-number'); 179 var errorContainer = jQuery('#pwgc-redeem-error'); 180 181 errorContainer.text(''); 182 redeemButton.attr('data-apply-text', redeemButton.attr('value')).attr('value', redeemButton.attr('data-wait-text')).prop('disabled', true); 183 184 jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-redeem', 'card_number': cardNumber.val(), 'security': pwgc.nonces.apply_gift_card}, function( result ) { 185 if (result.success) { 186 jQuery( document.body ).trigger( 'wc_update_cart' ); 187 } else { 188 errorContainer.text(result.data.message); 189 redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false); 190 cardNumber.trigger('focus'); 191 } 192 }).fail(function(xhr, textStatus, errorThrown) { 193 if (errorThrown) { 194 errorContainer.text(errorThrown); 195 } else { 196 errorContainer.text('Unknown Error'); 197 } 198 redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false); 199 cardNumber.trigger('focus'); 200 }); 201} 202 203function pwgc_message_characters_remaining() { 204 var charsRemaining = pwgc.max_message_characters; 205 206 var messageElement = jQuery('#pwgc-message').val(); 207 if (messageElement) { 208 charsRemaining -= messageElement.length; 209 } 210 211 jQuery('#pwgc-message-characters-remaining').text(charsRemaining); 212} 213 214function pwgc_checkout_redeem_gift_card(redeemButton) { 215 var errorContainer = jQuery('#pwgc-redeem-error'); 216 var cardNumber = jQuery('#pwgc-redeem-gift-card-number'); 217 218 errorContainer.text(''); 219 redeemButton.attr('data-apply-text', redeemButton.attr('value')).attr('value', redeemButton.attr('data-wait-text')).prop('disabled', true); 220 221 jQuery.post(pwgc.ajaxurl, {'action': 'pw-gift-cards-redeem', 'card_number': cardNumber.val(), 'security': pwgc.nonces.apply_gift_card}, function( result ) { 222 var form = jQuery('.checkout_pw_gift_card'); 223 if (result.success) { 224 form.slideUp(); 225 cardNumber.val(''); 226 jQuery( document.body ).trigger( 'update_checkout', { update_shipping_method: false } ); 227 } else { 228 errorContainer.text(result.data.message); 229 cardNumber.trigger('focus'); 230 } 231 redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false); 232 }).fail(function(xhr, textStatus, errorThrown) { 233 if (errorThrown) { 234 errorContainer.text(errorThrown); 235 } else { 236 errorContainer.text('Unknown Error'); 237 } 238 redeemButton.attr('value', redeemButton.attr('data-apply-text')).prop('disabled', false); 239 cardNumber.trigger('focus'); 240 }); 241} 242 243if ( document.readyState !== 'loading' ) { 244 pwgc_init(); 245} else { 246 document.addEventListener('DOMContentLoaded', function () { 247 pwgc_init(); 248 }); 249}
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.