1import{Component}from"@theme/component";import{fetchConfig,onAnimationEnd,preloadImage}from"@theme/utilities";import{ThemeEvents,CartAddEvent,CartErrorEvent}from"@theme/events";import{cartPerformance}from"@theme/performance";import{morph}from"@theme/morph";export const ADD_TO_CART_TEXT_ANIMATION_DURATION=2e3;const ERROR_MESSAGE_DISPLAY_DURATION=1e4,ERROR_BUTTON_REENABLE_DELAY=1e3,SUCCESS_MESSAGE_DISPLAY_DURATION=5e3;export class AddToCartComponent extends Component{requiredRefs=["addToCartButton"];#animationTimeout;#cleanupTimeout;connectedCallback(){super.connectedCallback(),this.addEventListener("pointerenter",this.#preloadImage)}disconnectedCallback(){super.disconnectedCallback(),this.#animationTimeout&&clearTimeout(this.#animationTimeout),this.#cleanupTimeout&&clearTimeout(this.#cleanupTimeout),this.removeEventListener("pointerenter",this.#preloadImage)}disable(){this.refs.addToCartButton.disabled=!0}enable(){this.refs.addToCartButton.disabled=!1}handleClick(event){const form=this.closest("form");if(!form?.checkValidity())return;const quantitySelector=form.querySelector("quantity-selector-component");if(quantitySelector?.canAddToCart&&!quantitySelector.canAddToCart().canAdd)return;this.animateAddToCart(),this.dataset.addToCartAnimation==="true"&&!event.target.closest(".quick-add-modal")&&this.#animateFlyToCart()}#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");flyToCartElement.style.setProperty("background-image",`url(${image})`),flyToCartElement.source=addToCartButton,flyToCartElement.destination=cartIcon,document.body.appendChild(flyToCartElement)}animateAddToCart(){const{addToCartButton}=this.refs;this.#animationTimeout&&clearTimeout(this.#animationTimeout),this.#cleanupTimeout&&clearTimeout(this.#cleanupTimeout),addToCartButton.classList.contains("atc-added")||addToCartButton.classList.add("atc-added"),this.#animationTimeout=setTimeout(()=>{this.#cleanupTimeout=setTimeout(()=>{this.refs.addToCartButton.classList.remove("atc-added")},10)},ADD_TO_CART_TEXT_ANIMATION_DURATION)}}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()}async#fetchAndUpdateCartQuantity(){const variantIdInput=this.querySelector('input[name="id"]');if(!variantIdInput?.value)return 0;try{const cartItem=(await(await fetch("/cart.js")).json()).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}catch(error){return console.error("Failed to fetch cart quantity:",error),0}}#onCartUpdate=async event=>{event.detail?.sourceId===this.id||event.detail?.data?.source==="product-form-component"||await this.#fetchAndUpdateCartQuantity()};handleSubmit(event){const{addToCartTextError,addToCartButtonContainer}=this.refs;if(event.preventDefault(),this.#timeout&&clearTimeout(this.#timeout),addToCartButtonContainer?.refs.addToCartButton?.disabled)return;const form=this.querySelector("form");if(!form)throw new Error("Product form element missing");const quantitySelector=this.querySelector("quantity-selector-component");if(quantitySelector?.canAddToCart){const validation=quantitySelector.canAddToCart();if(!validation.canAdd){addToCartButtonContainer?.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(()=>{addToCartButtonContainer?.enable()},ERROR_BUTTON_REENABLE_DELAY);return}}const formData=new FormData(form),cartItemsComponents=document.querySelectorAll("cart-items-component");let cartItemComponentsSectionIds=[];
1cartItemsComponents.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(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");if(this.refs.addToCartButtonContainer?.refs.addToCartButton){const addedText=this.refs.addToCartButtonContainer.refs.addToCartButton.querySelector(".add-to-cart-text--added")?.textContent?.trim()||Theme.translations.added;this.#setLiveRegionText(addedText),setTimeout(()=>{this.#clearLiveRegionText()},SUCCESS_MESSAGE_DISPLAY_DURATION)}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,addToCartButtonContainer}=this.refs,currentAddToCartButton=addToCartButtonContainer?.refs.addToCartButton,newAddToCartButton=event.detail.data.html.querySelector('[ref="addToCartButton"]');if(variantId.value=event.detail.resource?.id??"",!currentAddToCartButton&&!this.refs.acceleratedCheckoutButtonContainer)return;if(currentAddToCartButton&&(event.detail.resource==null||event.detail.resource.available==!1?addToCartButtonContainer.disable():addToCartButtonContainer.enable(),newAddToCartButton&&morph(currentAddToCartButton,newAddToCartButton)),this.refs.acceleratedCheckoutButtonContainer&&(event.detail.resource==null||event.detail.resource.available==!1?this.refs.acceleratedCheckoutButtonContainer?.setAttribute("hidden","true"):this.refs.acceleratedCheckoutButtonContainer?.removeAttribute("hidden")),event.detail.resource){const productVariantMedia=event.detail.resource.featured_media?.preview_image?.src;productVariantMedia&&addToCartButtonContainer?.setAttribute("data-product-variant-media",productVariantMedia+"&width=100")}const quantitySelector=this.querySelector("quantity-selector-component"),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 quantityRules=this.refs.quantityRules,newQuantityRules=event.detail.data.html.querySelector(".quantity-rules");if(!!quantityRules!=!!newQuantityRules&&quantitySelector){const currentQuantityValue=quantitySelector.getValue?.(),currentProductFormButtons=this.refs.productFormButtons,newProductFormButtons=event.detail.data.html.querySelector(".product-form-buttons");if(currentProductFormButtons&&newProductFormButtons){morph(currentProductFormButtons,newProductFormButtons);const newQuantitySelector=this.querySelector("quantity-selector-component"),newQuantityInputElement=event.detail.data.html.querySelector('quantity-selector-component input[ref="quantityInput"]');newQuantitySelector?.updateConstraints&&newQuantityInputElement&¤tQuantityValue&&(newQuantitySelector.setValue(currentQuantityValue),newQuantitySelector.updateConstraints(newQuantityInputElement.min,newQuantityInputElement.max||null,newQuantityInputElement.step))}}else{const quantityLabel=this.querySelector(".quantity-label"),newQuantityLabel=event.detail.data.html.querySelector(".quantity-label");this.#morphOrUpdateElement(quantityLabel,newQuantityLabel,quantitySelector);const addToCartButton=this.querySelector('[ref="addToCartButtonContainer"]');this.#morphOrUpdateElement(quantityRules,newQuantityRules,addToCartButton)}await this.#fetchAndUpdateCartQuantity()};#onVariantSelected=()=>{this.refs.addToCartButtonContainer?.disable()}}customElements.get("product-form-component")||customElements.define("product-form-component",ProductFormComponent);class FlyToCart extends HTMLElement{source;destination;connectedCallback(){this.#animate()}#animate(){const rect=this.getBoundingClientRect(),sourceRect=this.source.getBoundingClientRect(),destinationRect=this.destination.getBoundingClientRect(),offset={x:rect.width/2,y:rect.height/2},startPoint={x:sourceRect.left+sourceRect.width/2-offset.x,y:sourceRect.top+sourceRect.height/2-offset.y},endPoint={x:destinationRect.left+destinationRect.width/2-offset.x,y:destinationRect.top+destinationRect.height/2-offset.y},controlPoint1={x:startPoint.x,y:startPoint.y-200},controlPoint2={x:endPoint.x-300,y:endPoint.y-100};let startTime=null;const duration=600;this.style.opacity="1";const animate=currentTime=>{startTime||(startTime=currentTime);const elapsed=currentTime-startTime,progress=Math.min(elapsed/duration,1),position=bezierPoint(progress,startPoint,controlPoint1,controlPoint2,endPoint);this.style.setProperty("--x",`${position.x}px`),this.style.setProperty("--y",`${position.y}px`);const scale=1-progress*.5;this.style.setProperty("--scale",`${scale}`),progress<1?requestAnimationFrame(animate):(this.style.opacity="0",onAnimationEnd(this,()=>this.remove()))};this.style.setProperty("--x",`${startPoint.x}px`),this.style.setProperty("--y",`${startPoint.y}px`),requestAnimationFrame(animate)}}function bezierPoint(t,p0,p1,p2,p3){const cX=3*(p1.x-p0.x),bX=3*(p2.x-p1.x)-cX,aX=p3.x-p0.x-cX-bX,cY=3*(p1.y-p0.y),bY=3*(p2.y-p1.y)-cY,aY=p3.y-p0.y-cY-bY,x=aX*Math.pow(t,3)+bX*Math.pow(t,2)+cX*t+p0.x,y=aY*Math.pow(t,3)+bY*Math.pow(t,2)+cY*t+p0.y;return{x,y}}customElements.get("fly-to-cart")||customElements.define("fly-to-cart",FlyToCart); 2//# sourceMappingURL=/cdn/shop/t/2/assets/product-form.js.map?v=83283661069322333421766499840
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.