1/** 2 * WPFactory - Add to cart click feedback. 3 * 4 * Our buy CTAs are plain links that navigate to add-to-cart. The server 5 * does not always answer instantly, so the click can look ignored, and 6 * people click again. Each extra click is another add-to-cart request, 7 * which stacks duplicate quantities into the cart. 8 * 9 * On the first click the button locks and shows a spinner. Any further 10 * click is swallowed. The navigation itself is left alone. 11 * 12 * Scoped to a.btn on purpose. WooCommerce and Storefront render their 13 * own add-to-cart links with .button and .ajax_add_to_cart, and those 14 * do not navigate, they fire AJAX and stay on the page. Locking one of 15 * those would break it, so they are excluded twice over. 16 */ 17(function () { 18 'use strict'; 19 20 var SELECTOR = 'a.btn[href*="add-to-cart="]:not(.ajax_add_to_cart):not(.add_to_cart_button)'; 21 var RESET_AFTER = 12000; 22 23 function lockOnClick(link) { 24 link.addEventListener('click', function (e) { 25 if (link.classList.contains('is-buying')) { 26 e.preventDefault(); 27 return; 28 } 29 30 var original = link.textContent; 31 link.classList.add('is-buying'); 32 link.setAttribute('aria-busy', 'true'); 33 link.textContent = 'Adding to cart'; 34 35 // If the navigation never lands (offline, blocked, or the visitor 36 // comes back via the bfcache), hand the button back rather than 37 // leaving it stuck spinning forever. 38 var reset = function () { 39 link.classList.remove('is-buying'); 40 link.removeAttribute('aria-busy'); 41 link.textContent = original; 42 }; 43 44 setTimeout(reset, RESET_AFTER); 45 window.addEventListener('pageshow', function (ev) { 46 if (ev.persisted) reset(); 47 }); 48 }); 49 } 50 51 function init() { 52 document.querySelectorAll(SELECTOR).forEach(lockOnClick); 53 } 54 55 if (document.readyState === 'loading') { 56 document.addEventListener('DOMContentLoaded', init); 57 } else { 58 init(); 59 } 60})();
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.