PageSourceSearch

https://simonpearce.com/cdn/shop/t/527/assets/cart-discount.js?v=81860812426183110041790181760

js simonpearce.com collected 2026-09-24 20:26:11 UTC 4,957 bytes, 2 lines download raw bytes

1import{Component}from"@theme/component";import{morphSection}from"@theme/section-renderer";import{DiscountUpdateEvent}from"@theme/events";import{fetchConfig}from"@theme/utilities";import{cartPerformance}from"@theme/performance";class CartDiscount extends Component{requiredRefs=["cartDiscountError","cartDiscountErrorDiscountCode","cartDiscountErrorShipping","cartDiscountInput"];#activeFetch=null;#lastApplyTime=0;#createAbortController(){this.#activeFetch&&this.#activeFetch.abort();const abortController=new AbortController;return this.#activeFetch=abortController,abortController}applyDiscount=async event=>{const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping}=this.refs;event.preventDefault(),event.stopPropagation();const eventTarget=event.target,form=eventTarget instanceof HTMLFormElement?eventTarget:eventTarget instanceof Element?eventTarget.closest("form"):null;if(!(form instanceof HTMLFormElement))return;const now=Date.now();if(now-this.#lastApplyTime<400)return;this.#lastApplyTime=now;const discountCode=form.querySelector('input[name="discount"]');if(!(discountCode instanceof HTMLInputElement)||typeof this.dataset.sectionId!="string")return;const discountCodeValue=discountCode.value.trim();if(cartDiscountError.classList.add("hidden"),cartDiscountErrorDiscountCode.classList.add("hidden"),cartDiscountErrorShipping.classList.add("hidden"),discountCode.setAttribute("aria-invalid","false"),!discountCodeValue){this.#handleDiscountError("discount_code");return}const abortController=this.#createAbortController();try{const existingDiscounts=this.#existingDiscounts();if(existingDiscounts.includes(discountCodeValue))return;const config=fetchConfig("json",{body:JSON.stringify({discount:[...existingDiscounts,discountCodeValue].join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(Theme.routes.cart_update_url,{...config,signal:abortController.signal})).json(),responseDiscountCodes=Array.isArray(data.discount_codes)?data.discount_codes:[];if(responseDiscountCodes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!1)){this.#handleDiscountError("discount_code");return}const newHtml=data.sections?.[this.dataset.sectionId];if(typeof newHtml!="string"){this.#handleDiscountError("discount_code");return}const section=new DOMParser().parseFromString(newHtml,"text/html").getElementById(`shopify-section-${this.dataset.sectionId}`),discountCodes=section?.querySelectorAll(".cart-discount__pill")||[];if(section){const codes=Array.from(discountCodes).map(element=>element instanceof HTMLLIElement?element.dataset.discountCode:null).filter(Boolean);if(codes.length===existingDiscounts.length&&codes.every(code=>existingDiscounts.includes(code))&&responseDiscountCodes.find(discount=>discount.code===discountCodeValue&&discount.applicable===!0)){this.#handleDiscountError("shipping");return}}document.dispatchEvent(new DiscountUpdateEvent(data,this.id)),morphSection(this.dataset.sectionId,newHtml)}catch{}finally{this.#activeFetch=null,cartPerformance.measureFromEvent("discount-update:user-action",event)}};removeDiscount=async event=>{if(event.preventDefault(),event.stopPropagation(),event instanceof KeyboardEvent&&event.key!=="Enter"||!(event instanceof MouseEvent)||!(event.target instanceof HTMLElement)||typeof this.dataset.sectionId!="string")return;const pill=event.target.closest(".cart-discount__pill");if(!(pill instanceof HTMLLIElement))return;const discountCode=pill.dataset.discountCode;if(!discountCode)return;const existingDiscounts=this.#existingDiscounts(),index=existingDiscounts.indexOf(discountCode);if(index===-1)return;existingDiscounts.splice(index,1);const abortController=this.#createAbortController();try{const config=fetchConfig("json",{body:JSON.stringify({discount:existingDiscounts.join(","),sections:[this.dataset.sectionId]})}),data=await(await fetch(Theme.routes.cart_update_url,{...config,signal:abortController.signal})).json();document.dispatchEvent(new DiscountUpdateEvent(data,this.id)),morphSection(this.dataset.sectionId,data.sections[this.dataset.sectionId])}catch{}finally{this.#activeFetch=null}};#handleDiscountError(type){const{cartDiscountError,cartDiscountErrorDiscountCode,cartDiscountErrorShipping}=this.refs,target=type==="discount_code"?cartDiscountErrorDiscountCode:cartDiscountErrorShipping;cartDiscountError.classList.remove("hidden"),target.classList.remove("hidden"),this.refs.cartDiscountInput.setAttribute("aria-invalid","true")}#existingDiscounts(){const discountCodes=[],discountPills=this.querySelectorAll(".cart-discount__pill");for(const pill of discountPills)pill instanceof HTMLLIElement&&typeof pill.dataset.discountCode=="string"&&discountCodes.push(pill.dataset.discountCode);return discountCodes}}customElements.get("cart-discount-component")||customElements.define("cart-discount-component",CartDiscount);
2//# sourceMappingURL=/cdn/shop/t/527/assets/cart-discount.js.map?v=81860812426183110041790181760

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.