1import{Component}from"@theme/component";import{fetchConfig,preloadImage,onAnimationEnd,yieldToMainThread}from"@theme/utilities";import{ThemeEvents,CartAddEvent,CartErrorEvent}from"@theme/events";import{cartPerformance}from"@theme/performance";import{morph}from"@theme/morph";const ERROR_MESSAGE_DISPLAY_DURATION=1e4,ERROR_BUTTON_REENABLE_DELAY=1e3,SUCCESS_MESSAGE_DISPLAY_DURATION=5e3;export class AddToCartComponent extends Component{requiredRefs=["addToCartButton"];#resetTimeouts=[];connectedCallback(){super.connectedCallback(),this.addEventListener("pointerenter",this.#preloadImage)}disconnectedCallback(){super.disconnectedCallback(),this.#resetTimeouts&&this.#resetTimeouts.forEach(timeoutId=>clearTimeout(timeoutId)),this.removeEventListener("pointerenter",this.#preloadImage)}disable(){this.refs.addToCartButton.disabled=!0}enable(){this.refs.addToCartButton.disabled=!1}handleClick(event){if(!this.closest("form")?.checkValidity())return;const quantitySelector=this.closest("product-form-component")?.refs.quantitySelector;quantitySelector?.canAddToCart&&!quantitySelector.canAddToCart().canAdd||this.refs.addToCartButton.dataset.puppet!=="true"&&(this.dataset.addToCartAnimation==="true"&&!event.target.closest(".quick-add-modal")&&this.#animateFlyToCart(),this.animateAddToCart())}#preloadImage=()=>{const image=this.dataset.productVariantMedia;image&&preloadImage(image)};#animateFlyToCart(){const{addToCartButton}=this.refs,cartIcon=document.querySelector(".header-actions__cart-icon"),image=this.dataset.productVariantMedia;if(!cartIcon||!addToCartButton||!image)return;const flyToCartElement=document.createElement("fly-to-cart");let flyToCartClass=addToCartButton.classList.contains("quick-add__button")?"fly-to-cart--quick":"fly-to-cart--main";flyToCartElement.classList.add(flyToCartClass),flyToCartElement.style.setProperty("background-image",`url(${image})`),flyToCartElement.style.setProperty("--start-opacity","0"),flyToCartElement.source=addToCartButton,flyToCartElement.destination=cartIcon,document.body.appendChild(flyToCartElement)}animateAddToCart=async function(){const{addToCartButton}=this.refs;this.#resetTimeouts||(this.#resetTimeouts=[]),this.#resetTimeouts.forEach(timeoutId2=>clearTimeout(timeoutId2)),this.#resetTimeouts=[],addToCartButton.dataset.added!=="true"&&(addToCartButton.dataset.added="true"),await yieldToMainThread(),await onAnimationEnd(addToCartButton);const timeoutId=setTimeout(()=>{addToCartButton.removeAttribute("data-added");const index=this.#resetTimeouts.indexOf(timeoutId);index>-1&&this.#resetTimeouts.splice(index,1)},800);this.#resetTimeouts.push(timeoutId)}}customElements.get("add-to-cart-component")||customElements.define("add-to-cart-component",AddToCartComponent);class ProductFormComponent extends Component{requiredRefs=["variantId","liveRegion"];#abortController=new AbortController;#timeout;connectedCallback(){super.connectedCallback();const{signal}=this.#abortController,target=this.closest(".shopify-section, dialog, product-card");target?.addEventListener(ThemeEvents.variantUpdate,this.#onVariantUpdate,{signal}),target?.addEventListener(ThemeEvents.variantSelected,this.#onVariantSelected,{signal}),document.addEventListener(ThemeEvents.cartUpdate,this.#onCartUpdate,{signal})}disconnectedCallback(){super.disconnectedCallback(),this.#abortController.abort()}#updateCartQuantityFromData(cart){const variantIdInput=this.querySelector('input[name="id"]');if(!variantIdInput?.value||!cart?.items)return 0;const cartItem=cart.items.find(item=>item.variant_id.toString()===variantIdInput.value.toString()),cartQty=cartItem?cartItem.quantity:0,quantitySelector=this.querySelector("quantity-selector-component");return quantitySelector?.setCartQuantity&&quantitySelector.setCartQuantity(cartQty),this.#updateQuantityLabel(cartQty),cartQty}async#fetchAndUpdateCartQuantity(){if(!this.querySelector('input[name="id"]')?.value)return 0;try{const cart=await(await fetch("/cart.js")).json();return this.#updateCartQuantityFromData(cart)}catch(error){return console.error("Failed to fetch cart quantity:",error),0}}#onCartUpdate=async event=>{if(event.detail?.sourceId===this.id||event.detail?.data?.source==="product-form-component")return;const cart=event.detail?.resource;cart?.items?this.#updateCartQuantityFromData(cart):await this.#fetchAndUpdateCartQuantity()};handleSubmit(event){const{addToCartTextError}=this.refs;event.preventDefault(),this.#timeout&&clearTimeout(this.#timeout);const allAddToCartContainers=this.querySelectorAll("add-to-cart-component");
1if(Array.from(allAddToCartContainers).some(container=>container.refs.addToCartButton?.disabled))return;const form=this.querySelector("form");if(!form)throw new Error("Product form element missing");if(this.refs.quantitySelector?.canAddToCart){const validation=this.refs.quantitySelector.canAddToCart();if(!validation.canAdd){for(const container of allAddToCartContainers)container.disable();const errorMessage=(this.dataset.quantityErrorMax||"").replace("{{ maximum }}",validation.maxQuantity?.toString()||"");if(addToCartTextError){addToCartTextError.classList.remove("hidden");const textNode=addToCartTextError.childNodes[2];if(textNode)textNode.textContent=errorMessage;else{const newTextNode=document.createTextNode(errorMessage);addToCartTextError.appendChild(newTextNode)}this.#setLiveRegionText(errorMessage),this.#timeout&&clearTimeout(this.#timeout),this.#timeout=setTimeout(()=>{addToCartTextError&&(addToCartTextError.classList.add("hidden"),this.#clearLiveRegionText())},ERROR_MESSAGE_DISPLAY_DURATION)}setTimeout(()=>{for(const container of allAddToCartContainers)container.enable()},ERROR_BUTTON_REENABLE_DELAY);return}}const formData=new FormData(form),cartItemsComponents=document.querySelectorAll("cart-items-component");let cartItemComponentsSectionIds=[];cartItemsComponents.forEach(item=>{item instanceof HTMLElement&&item.dataset.sectionId&&cartItemComponentsSectionIds.push(item.dataset.sectionId),formData.append("sections",cartItemComponentsSectionIds.join(","))});const fetchCfg=fetchConfig("javascript",{body:formData});fetch(Theme.routes.cart_add_url,{...fetchCfg,headers:{...fetchCfg.headers,Accept:"text/html"}}).then(response=>response.json()).then(async response=>{if(response.status){if(this.dispatchEvent(new CartErrorEvent(form.getAttribute("id")||"",response.message,response.description,response.errors)),!addToCartTextError)return;addToCartTextError.classList.remove("hidden");const textNode=addToCartTextError.childNodes[2];if(textNode)textNode.textContent=response.message;else{const newTextNode=document.createTextNode(response.message);addToCartTextError.appendChild(newTextNode)}this.#setLiveRegionText(response.message),this.#timeout=setTimeout(()=>{addToCartTextError&&(addToCartTextError.classList.add("hidden"),this.#clearLiveRegionText())},ERROR_MESSAGE_DISPLAY_DURATION),this.dispatchEvent(new CartAddEvent({},this.id,{didError:!0,source:"product-form-component",itemCount:Number(formData.get("quantity"))||Number(this.dataset.quantityDefault),productId:this.dataset.productId}));return}else{const id=formData.get("id");if(addToCartTextError&&(addToCartTextError.classList.add("hidden"),addToCartTextError.removeAttribute("aria-live")),!id)throw new Error("Form ID is required");const anyAddToCartButton=allAddToCartContainers[0]?.refs.addToCartButton;if(anyAddToCartButton){const addedText=anyAddToCartButton.querySelector(".add-to-cart-text--added")?.textContent?.trim()||Theme.translations.added;this.#setLiveRegionText(addedText),setTimeout(()=>{this.#clearLiveRegionText()},SUCCESS_MESSAGE_DISPLAY_DURATION)}await this.#fetchAndUpdateCartQuantity(),this.dispatchEvent(new CartAddEvent({},id.toString(),{source:"product-form-component",itemCount:Number(formData.get("quantity"))||Number(this.dataset.quantityDefault),productId:this.dataset.productId,sections:response.sections}))}}).catch(error=>{console.error(error)}).finally(()=>{cartPerformance.measureFromEvent("add:user-action",event)})}#updateQuantityLabel(cartQty){const quantityLabel=this.refs.quantityLabelCartCount;if(quantityLabel){const inCartText=quantityLabel.textContent?.match(/\((\d+)\s+(.+)\)/);inCartText&&inCartText[2]&&(quantityLabel.textContent=`(${cartQty} ${inCartText[2]})`),quantityLabel.classList.toggle("hidden",cartQty===0)}}#setLiveRegionText(text){const liveRegion=this.refs.liveRegion;liveRegion.textContent=text}#clearLiveRegionText(){const liveRegion=this.refs.liveRegion;liveRegion.textContent=""}#morphOrUpdateElement(currentElement,newElement,insertReferenceElement=null){currentElement&&newElement?morph(currentElement,newElement):currentElement&&!newElement?currentElement.remove():!currentElement&&newElement&&insertReferenceElement&&insertReferenceElement.insertAdjacentElement("beforebegin",newElement.cloneNode(!0))}#onVariantUpdate=async event=>{if(event.detail.data.newProduct)this.dataset.productId=event.detail.data.newProduct.id;else if(event.detail.data.productId!==this.dataset.productId)return;const{variantId}=this.refs;variantId.value=event.detail.resource?.id??"";const{addToCartButtonContainer:currentAddToCartButtonContainer,acceleratedCheckoutButtonContainer}=this.refs,currentAddToCartButton=currentAddToCartButtonContainer?.refs.addToCartButton;if(!currentAddToCartButtonContainer||!currentAddToCartButton&&!acceleratedCheckoutButtonContainer)return;event.detail.resource==null||event.detail.resource.available==!1?currentAddToCartButtonContainer.disable():currentAddToCartButtonContainer.enable();const newAddToCartButton=event.detail.data.html.querySelector('product-form-component [ref="addToCartButton"]');if(newAddToCartButton&¤tAddToCartButton&&morph(currentAddToCartButton,newAddToCartButton),acceleratedCheckoutButtonContainer&&(event.detail.resource==null||event.detail.resource.available==!1?acceleratedCheckoutButtonContainer?.setAttribute("hidden","true"):acceleratedCheckoutButtonContainer?.removeAttribute("hidden")),event.detail.resource){const productVariantMedia=event.detail.resource.featured_media?.preview_image?.src;productVariantMedia&&this.refs.addToCartButtonContainer?.setAttribute("data-product-variant-media",productVariantMedia+"&width=100")}const{quantityRules,pricePerItem,quantitySelector,productFormButtons,quantityLabel,quantitySelectorWrapper}=this.refs,newQuantityInput=event.detail.data.html.querySelector('quantity-selector-component input[ref="quantityInput"]');quantitySelector?.updateConstraints&&newQuantityInput&&quantitySelector.updateConstraints(newQuantityInput.min,newQuantityInput.max||null,newQuantityInput.step);const newQuantityRules=event.detail.data.html.querySelector(".quantity-rules"),isQuantityRulesChanging=!!quantityRules!=!!newQuantityRules,newPricePerItem=event.detail.data.html.querySelector("price-per-item");if((isQuantityRulesChanging||!!pricePerItem!=!!newPricePerItem)&&quantitySelector){const currentQuantityValue=quantitySelector.getValue?.(),newProductFormButtons=event.detail.data.html.querySelector(".product-form-buttons");if(productFormButtons&&newProductFormButtons){morph(productFormButtons,newProductFormButtons);const newQuantityInputElement=event.detail.data.html.querySelector('quantity-selector-component input[ref="quantityInput"]');this.refs.quantitySelector?.updateConstraints&&newQuantityInputElement&¤tQuantityValue&&(this.refs.quantitySelector.setValue(currentQuantityValue),this.refs.quantitySelector.updateConstraints(newQuantityInputElement.min,newQuantityInputElement.max||null,newQuantityInputElement.step))}}else{const morphTargets=[[".quantity-label",quantityLabel,quantitySelector],[".quantity-rules",quantityRules,this.refs.productFormButtons],["price-per-item",pricePerItem,quantitySelectorWrapper]];for(const[selector,currentElement,fallback]of morphTargets)this.#morphOrUpdateElement(currentElement,event.detail.data.html.querySelector(selector),fallback)}const currentVolumePricing=this.refs.volumePricing,newVolumePricing=event.detail.data.html.querySelector("volume-pricing");this.#morphOrUpdateElement(currentVolumePricing,newVolumePricing,this.refs.productFormButtons),(quantityRules||newQuantityRules||pricePerItem||newPricePerItem||currentVolumePricing||newVolumePricing)&&await this.#fetchAndUpdateCartQuantity()};#onVariantSelected=()=>{this.refs.addToCartButtonContainer?.disable()}}customElements.get("product-form-component")||customElements.define("product-form-component",ProductFormComponent); 2//# sourceMappingURL=/cdn/shop/t/22/assets/product-form.js.map?v=128867569059604095961775475694
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.