1(function(){if(window.__charitySelectorInitialized)return;window.__charitySelectorInitialized=!0;class CharitySelector{constructor(){this.pendingUpdate=null,this.handleChange=this.handleChange.bind(this),this.handleCheckout=this.handleCheckout.bind(this),document.addEventListener("change",this.handleChange),document.addEventListener("click",this.handleCheckout,!0)}getShopifyRoot(){return window.Shopify&&window.Shopify.routes&&window.Shopify.routes.root?window.Shopify.routes.root:"/"}getCharityFromCard(card){return card?{charity_id:card.dataset.charityId||"",charity_slug:card.dataset.charitySlug||"",charity_name:card.dataset.charityName||""}:null}getSelectedCharity(selector){if(!selector)return null;const selectedRadio=selector.querySelector('input[name="selected_charity"]:checked');if(!selectedRadio)return null;const card=selectedRadio.closest(".charity-card");return this.getCharityFromCard(card)}async handleChange(event){if(!event.target.matches('input[name="selected_charity"]'))return;const card=event.target.closest(".charity-card"),charity=this.getCharityFromCard(card);if(!(!charity||!charity.charity_id)){this.saveToLocalStorage(charity),this.hideValidationErrors(),this.pendingUpdate=this.saveCharityToCart(charity);try{await this.pendingUpdate}catch(error){console.error("Unable to save charity selection.",error),this.showValidationError("We could not save your charity selection. Please select it again.",this.findVisibleSelector())}finally{this.pendingUpdate=null}}}async handleCheckout(event){const checkoutButton=event.target.closest('[data-buy-now-button], a[href^="/checkout"], button[name="checkout"], input[name="checkout"]');if(!checkoutButton)return;const selector=this.findSelectorForButton(checkoutButton),charity=this.getSelectedCharity(selector);if(!charity||!charity.charity_id){event.preventDefault(),event.stopPropagation(),event.stopImmediatePropagation(),this.showValidationError("Please choose a charity before continuing to checkout.",selector);return}event.preventDefault(),event.stopPropagation(),event.stopImmediatePropagation(),this.hideValidationErrors();const originalValue=checkoutButton.value,originalText=checkoutButton.textContent;checkoutButton.disabled=!0,checkoutButton.setAttribute("aria-disabled","true"),checkoutButton.tagName==="INPUT"?checkoutButton.value="Saving charity...":checkoutButton.textContent="Saving charity...";try{this.pendingUpdate?await this.pendingUpdate:await this.saveCharityToCart(charity),window.location.assign("/checkout")}catch(error){console.error("Unable to continue to checkout.",error),checkoutButton.disabled=!1,checkoutButton.removeAttribute("aria-disabled"),checkoutButton.tagName==="INPUT"?checkoutButton.value=originalValue:checkoutButton.textContent=originalText,this.showValidationError("We could not save your charity selection. Please select it again.",selector)}}async saveCharityToCart(charity){const root=this.getShopifyRoot(),cartResponse=await fetch(root+"cart.js",{method:"GET",credentials:"same-origin",headers:{Accept:"application/json"}});if(!cartResponse.ok)throw new Error("Unable to read cart: "+cartResponse.status);const cart=await cartResponse.json(),attributeResponse=await fetch(root+"cart/update.js",{method:"POST",credentials:"same-origin",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({attributes:{selected_charity_id:charity.charity_id,selected_charity_slug:charity.charity_slug,selected_charity_name:charity.charity_name}})});if(!attributeResponse.ok)throw new Error("Unable to update cart attributes: "+attributeResponse.status);for(let index=0;index<cart.items.length;index+=1){const item=cart.items[index],properties=Object.assign({},item.properties||{});delete properties.selected_charity_id,delete properties.selected_charity_slug,delete properties.selected_charity_name,properties._selected_charity_id=charity.charity_id,properties._selected_charity_slug=charity.charity_slug,properties._selected_charity_name=charity.charity_name;const lineResponse=await fetch(root+"cart/change.js",{method:"POST",credentials:"same-origin",headers:{"Content-Type":"application/json",Accept:"application/json"},body:JSON.stringify({id:item.key,quantity:item.quantity,properties})});if(!lineResponse.ok)throw new Error("Unable to update cart item: "+lineResponse.status)}return!0}
1saveToLocalStorage(charity){try{localStorage.setItem("selected_charity",JSON.stringify(charity))}catch(error){console.warn("Unable to save charity locally.",error)}}findSelectorForButton(checkoutButton){const cartForm=checkoutButton.closest("form");if(cartForm){const formSelector=cartForm.querySelector(".charity-selector");if(formSelector)return formSelector}const quickCart=checkoutButton.closest(".quick-cart");if(quickCart){const quickCartSelector=quickCart.querySelector(".charity-selector");if(quickCartSelector)return quickCartSelector}return this.findVisibleSelector()}findVisibleSelector(){const selectors=document.querySelectorAll(".charity-selector");for(let index=0;index<selectors.length;index+=1){const selector=selectors[index];if(selector.offsetWidth||selector.offsetHeight||selector.getClientRects().length)return selector}return selectors.length?selectors[0]:null}hideValidationErrors(){document.querySelectorAll("[data-charity-error]").forEach(function(errorElement){errorElement.classList.remove("is-visible")}),document.querySelectorAll(".charity-card").forEach(function(card){card.classList.remove("charity-card--invalid")})}showValidationError(message,selector){const activeSelector=selector||this.findVisibleSelector();if(!activeSelector)return;const errorElement=activeSelector.querySelector("[data-charity-error]");errorElement&&(errorElement.textContent=message,errorElement.classList.add("is-visible")),activeSelector.querySelectorAll(".charity-card").forEach(function(card){card.classList.add("charity-card--invalid")}),activeSelector.scrollIntoView({behavior:"smooth",block:"center"})}}new CharitySelector})(),(function(){if(window.__charityPropertyCleanupInitialized)return;window.__charityPropertyCleanupInitialized=!0;const propertySelector=".quick-cart__item-properties",hiddenPropertyPrefix="selected_charity_";function removeCharityPropertyRows(root=document){const propertyRows=[];root.nodeType===1&&root.matches&&root.matches(propertySelector)&&propertyRows.push(root),root.querySelectorAll&&propertyRows.push(...root.querySelectorAll(propertySelector)),propertyRows.forEach(propertyRow=>{(propertyRow.textContent||"").trim().toLowerCase().replace(/^_+/,"").startsWith(hiddenPropertyPrefix)&&propertyRow.remove()})}function cleanQuickCart(){removeCharityPropertyRows(document)}document.readyState==="loading"?document.addEventListener("DOMContentLoaded",cleanQuickCart,{once:!0}):cleanQuickCart(),document.addEventListener("cart:updated",function(){requestAnimationFrame(cleanQuickCart)}),window.addEventListener("pageshow",function(){requestAnimationFrame(cleanQuickCart)});const observer=new MutationObserver(function(mutations){mutations.forEach(function(mutation){mutation.addedNodes.forEach(function(addedNode){addedNode.nodeType===1&&removeCharityPropertyRows(addedNode)})})});function startObserver(){document.body&&observer.observe(document.body,{childList:!0,subtree:!0})}document.body?startObserver():document.addEventListener("DOMContentLoaded",startObserver,{once:!0})})(); 2//# sourceMappingURL=/cdn/shop/t/41/assets/charity-selector.js.map?v=29862886279050495761788005493
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.