{ "version": 3, "sources": ["../../../Vettvangur.Frontend/src/scripts/components/cart.ts"], "sourcesContent": ["import axios from 'axios'\nimport { breakpoint } from 'utility/breakpoints'\nimport { debounce } from 'throttle-debounce'\nimport gsap from 'gsap'\nimport increment from './increment'\n// import { lenisScroll } from 'utility/smooth-scroll'\n\nconst cart = {\n el: {\n addToCartForm: document.querySelector('.product-page__add-to-cart'),\n cart: {\n // All this elements need to be updated after each renderCart call\n increments: document.querySelectorAll('.cart__udpate-form .increment'),\n modal: document.querySelector('.cart__modal'),\n products: document.querySelectorAll('.cart__product'),\n removeForms: document.querySelectorAll('.cart__remove-form'),\n root: document.querySelector('.cart'),\n updateForms: document.querySelectorAll('.cart__update-form'),\n },\n trigger: {\n desktop: document.querySelector('.header__cart-trigger'),\n mobile: document.querySelector('.mobile-header__cart-trigger'),\n },\n },\n\n count: Number.parseFloat(sessionStorage.getItem('cartCount')) || 0,\n\n init(): void {\n this.el.addToCartForm?.addEventListener('submit', this.addToCart.bind(this))\n\n this.el.trigger.desktop.addEventListener('click', this.toggleCart.bind(this))\n this.el.trigger.mobile.addEventListener('click', this.toggleCart.bind(this))\n\n if (this.count > 0) {\n this.updateCount(this.count, false)\n this.getCart()\n }\n },\n\n async addToCart(event): Promise {\n event.preventDefault()\n\n const action = event.target.action\n const button = event.target.querySelector('.product-page__add-to-cart-button')\n const data = new FormData(event.target)\n\n button.setAttribute('disabled', 'true')\n button.classList.add('button--loading')\n\n await axios\n .post(action, data)\n .then((response) => {\n if (response.status === 200) {\n this.getCart().then(() => {\n this.toggleCart(null)\n button.removeAttribute('disabled')\n button.classList.remove('button--loading')\n })\n }\n })\n .catch((error) => {\n console.log(`addToCart error: ${error}`)\n })\n },\n\n async getCart(): Promise {\n const action = '/umbraco/surface/shopify/getcart'\n\n await axios\n .get(action)\n .then((response) => {\n if (response.status === 200) {\n this.updateCount(response.data.quantity)\n this.renderCart(response.data.view)\n }\n })\n .catch((error) => {\n if (error.response.status === 404) {\n this.toggleCart(null)\n this.updateCount(0)\n }\n\n console.log(`getCart error: ${error}`)\n })\n },\n\n async removeFromCart(event): Promise {\n event.preventDefault()\n\n const lineId = event.target.querySelector(`input[name='lineId']`).value\n const action = `/umbraco/surface/shopify/removefromcart/?lineId=${lineId}`\n const data = new FormData(event.target)\n\n this.el.cart.root.classList.add('cart--processing')\n\n await axios\n .post(action, data)\n .then((response) => {\n if (response.status === 200) {\n this.getCart().then(() => {\n this.toggleCart(null, false)\n this.el.cart.root.classList.remove('cart--processing')\n })\n }\n })\n .catch((error) => {\n console.log(`removeFromCart error: ${error}`)\n })\n },\n\n async updateCartQuantity(event, form): Promise {\n event.preventDefault()\n\n const lineId = form.querySelector(`input[name='lineId']`).value\n const quantity = form.querySelector(`.increment input[name='quantity']`).value\n const action = `/umbraco/surface/shopify/updatequantity/?lineId=${lineId}&quantity=${quantity}`\n\n this.el.cart.root.classList.add('cart--processing')\n\n await axios\n .post(action)\n .then((response) => {\n if (response.status === 200) {\n this.getCart().then(() => {\n this.toggleCart(null, false)\n this.el.cart.root.classList.remove('cart--processing')\n })\n }\n })\n .catch((error) => {\n console.log(`updateCartQuantity error: ${error}`)\n })\n },\n\n closeOnEscPress(event): void {\n if (event.key.match('Escape') && cart.isActive()) {\n cart.toggleCart(event)\n }\n },\n\n closeOnOutsideClick(event): void {\n if (\n !event.target.closest('.cart') &&\n !event.target.closest('.header .header__cart-trigger') &&\n !event.target.closest('.mobile-header .mobile-header__cart-trigger') &&\n cart.isActive()\n ) {\n cart.toggleCart(event)\n }\n },\n\n closeOnScroll(): void {\n if (cart.isActive() && window.scrollY > 123 && window.innerWidth >= breakpoint.tablet) {\n cart.toggleCart(null)\n }\n },\n\n isActive(): boolean {\n return this.el.cart.root?.classList.contains('cart--active')\n },\n\n renderCart(markup): void {\n this.el.cart.root && this.el.cart.root.remove()\n document.body.insertAdjacentHTML('beforeend', markup)\n\n this.el.cart.increments = document.querySelectorAll('.cart__update-form .increment')\n this.el.cart.modal = document.querySelector('.cart__modal')\n this.el.cart.products = document.querySelectorAll('.cart__product')\n this.el.cart.removeForms = document.querySelectorAll('.cart__remove-form')\n this.el.cart.root = document.querySelector('.cart')\n this.el.cart.updateForms = document.querySelectorAll('.cart__update-form')\n\n this.el.cart.increments.forEach((target) => {\n increment.handleIncrement(target)\n })\n\n this.el.cart.removeForms.forEach((form) => {\n form.addEventListener('submit', (event) => {\n this.removeFromCart(event)\n })\n })\n\n this.el.cart.updateForms.forEach((form) => {\n form.querySelectorAll('button').forEach((button) => {\n button.addEventListener(\n 'click',\n debounce(210, (event) => {\n this.updateCartQuantity(event, form)\n })\n )\n })\n\n form.querySelector('.increment input').addEventListener(\n 'keyup',\n debounce(66, (event) => {\n this.updateCartQuantity(event, form)\n })\n )\n })\n },\n\n runAnimation(action): void {\n const props = {\n clearProps: 'all',\n duration: 0.543,\n ease: 'power3.inOut',\n }\n\n gsap.fromTo(\n this.el.cart.modal,\n {\n height: action === 'open' ? 0 : this.el.cart.modal.offsetHeight,\n opacity: action === 'open' ? 0 : 1,\n scale: action === 'open' ? 0.77 : 1,\n },\n {\n height: action === 'open' ? 'auto' : 0,\n // TODO: Decide later if it makes sense to pause scroll when cart is open on mobile\n // onComplete: () => {\n // if (action === 'open') {\n // window.innerWidth < breakpoint.tablet && lenisScroll.stop()\n // } else {\n // lenisScroll.isStopped && lenisScroll.start()\n // }\n // },\n opacity: action === 'open' ? 1 : 0,\n scale: action === 'open' ? 1 : 0.77,\n ...props,\n }\n )\n\n gsap.fromTo(\n this.el.cart.products,\n {\n opacity: action === 'open' ? 0 : 1,\n },\n {\n delay: action === 'open' ? 0.15 : 0,\n opacity: action === 'open' ? 1 : 0,\n overwrite: true,\n stagger: action === 'open' ? 0.06 : 0,\n ...props,\n duration: action === 'open' ? 0.543 : 0.321,\n }\n )\n },\n\n toggleCart(event, animate = true): void {\n event && event.preventDefault()\n\n if (this.count === 0) {\n return\n }\n\n if (animate) {\n this.runAnimation(this.isActive() ? 'close' : 'open')\n }\n\n if (!this.isActive()) {\n this.el.cart.root.classList.add('cart--active')\n this.el.trigger.desktop.classList.add('header__cart-trigger--active')\n this.el.trigger.desktop.ariaExpanded = 'true'\n this.el.trigger.mobile.classList.add('mobile-header__cart-trigger--active')\n this.el.trigger.mobile.ariaExpanded = 'true'\n document.body.addEventListener('click', cart.closeOnOutsideClick)\n window.addEventListener('keydown', cart.closeOnEscPress)\n window.addEventListener('scroll', cart.closeOnScroll)\n } else {\n this.el.cart.root.classList.remove('cart--active')\n this.el.trigger.desktop.classList.remove('header__cart-trigger--active')\n this.el.trigger.desktop.ariaExpanded = 'false'\n this.el.trigger.mobile.classList.remove('mobile-header__cart-trigger--active')\n this.el.trigger.mobile.ariaExpanded = 'false'\n document.body.removeEventListener('click', cart.closeOnOutsideClick)\n window.removeEventListener('keydown', cart.closeOnEscPress)\n window.removeEventListener('scroll', cart.closeOnScroll)\n }\n },\n\n updateCount(count, animateBubble = true): void {\n this.count = count\n sessionStorage.setItem('cartCount', count.toString())\n\n if (this.count !== 0) {\n this.el.trigger.desktop.classList.add('header__cart-trigger--count')\n this.el.trigger.mobile.classList.add('mobile-header__cart-trigger--count')\n } else {\n this.el.trigger.desktop.classList.remove('header__cart-trigger--count')\n this.el.trigger.mobile.classList.remove('mobile-header__cart-trigger--count')\n }\n\n this.el.trigger.desktop.dataset.count = this.count.toString()\n this.el.trigger.mobile.dataset.count = this.count.toString()\n\n if (!animateBubble) {\n this.el.trigger.desktop.classList.add('header__cart-trigger--no-transition')\n this.el.trigger.mobile.classList.add('mobile-header__cart-trigger--no-transition')\n setTimeout(() => {\n this.el.trigger.desktop.classList.remove('header__cart-trigger--no-transition')\n this.el.trigger.mobile.classList.remove('mobile-header__cart-trigger--no-transition')\n }, 10)\n }\n },\n}\n\nexport default cart\n"], "mappings": "+QAOA,IAAMA,EAAO,CACX,GAAI,CACF,cAAe,SAAS,cAA+B,4BAA4B,EACnF,KAAM,CAEJ,WAAY,SAAS,iBAA8B,+BAA+B,EAClF,MAAO,SAAS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EAC9E,KAAK,GAAG,QAAQ,OAAO,UAAU,OAAO,4CAA4C,CACtF,EAAG,EAAE,EAET,CACF,EAEOC,EAAQxB", "names": ["cart", "_a", "event", "action", "button", "data", "axios_default", "response", "error", "form", "lineId", "quantity", "breakpoint", "markup", "target", "increment_default", "debounce", "props", "gsapWithCSS", "__spreadValues", "__spreadProps", "animate", "count", "animateBubble", "cart_default"] }