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C,CAAmB9C,GAnIRA,KACvB,MACM+C,GADInH,EAAAA,EAAAA,OAAM,cACOuF,WAEvB,GAAI4B,EAAc,CACd,MAAMC,EAAeD,EAAaE,UAElC,GAAID,EAAc,CACd,MAAME,EAAsBF,EAAaG,QAAQ,KAC3CC,EAAuBJ,EAAaG,QAAQ,KAElD,IAA6B,IAAzBD,EACAH,EAAaE,UAAY,kBAAkBjD,EAAO6C,cAC/C,CACH,MAAMQ,EAAeL,EAAaM,UAAUJ,EAAsB,EAAGE,GAE/DG,EADiBX,SAASS,GACGrD,EAAO6C,OAC1CE,EAAaE,UAAY,kBAAkBM,I,KAmH3CC,CAAkBxD,IAlKZ,IAAInB,GACZS,gBA4EF,IAAIiB,EAuFoBwB,GAtF9BzC,gBAuFUwB,EAAcd,GAAQ,E,CAE5B,MAAOA,GACL7B,GAAW,EACP6B,EAAOa,QAtEfZ,EAAYW,EAFQC,EAyEOb,MAnEtBC,EAAUyC,YACXzC,EAAUyC,UAAY7B,EAAMA,OAE3BZ,EAAUR,UAAUiB,SAAS,oCAC9BT,EAAUR,UAAUC,IAAI,oCAiExBoB,EAAcd,GAAQ,E,EA3ENa,MAEpBZ,CA4ER,IAEA,MACI,IAAK,MAAMwD,KAjNJnH,EAAAA,EAAAA,KAAI,6CAkNPmH,EAAQC,iBAAiB,QAASrC,EAEzC,EAJD,E","sources":["webpack://bm3giraffe/./WebRoot/js/util.ts","webpack://bm3giraffe/webpack/bootstrap","webpack://bm3giraffe/webpack/runtime/define property getters","webpack://bm3giraffe/webpack/runtime/hasOwnProperty shorthand","webpack://bm3giraffe/webpack/runtime/make namespace object","webpack://bm3giraffe/./WebRoot/js/add-to-cart.ts"],"sourcesContent":["function first(selector: string, root?: Element) : T {\r\n const foundElement = (root || document).querySelector(selector);\r\n if (!foundElement) {\r\n throw new Error(`element '${selector}' was not found.`);\r\n } else {\r\n return foundElement as T;\r\n }\r\n}\r\n\r\nfunction tryFirst(selector: string, root?: Element): T | null {\r\n const foundElement = (root || document).querySelector(selector);\r\n if (!foundElement) {\r\n console.warn(`element '${selector}' was not found.`);\r\n }\r\n return foundElement;\r\n}\r\n\r\nfunction all(selector: string, root?: Element): Array {\r\n return Array.from((root || document).querySelectorAll(selector));\r\n}\r\n\r\n\r\nexport {\r\n first,\r\n tryFirst,\r\n all\r\n}","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","// define getter functions for harmony exports\n__webpack_require__.d = function(exports, definition) {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.o = function(obj, prop) { return Object.prototype.hasOwnProperty.call(obj, prop); }","// define __esModule on exports\n__webpack_require__.r = function(exports) {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","import { first, all } from \"./util\"\r\n\r\ntype AddToCartResult = {\n productId: number\n variantName: string\n amount: number\n error: string\n}\n\nlet canFetch = true;\n\nconst isMobileView = () => {\n return window.innerWidth <= 1000;\n}\n\nfunction getCartAnimationContainer() {\n const animationContainer = isMobileView() ? first(\".mobile-nav\") : first(\".cart-text\");\n if (!animationContainer) {\n throw new Error('no animation container found');\r\n } else {\n return animationContainer;\r\n }\n}\n\nconst randomIntFromRange = (min: number, max: number) => {\n return Math.floor(Math.random() * (max - min + 1) + min)\n}\n\nconst getAddToCartSections = () => {\n return all(\".product-components__add-to-cart__section\");\n}\n\nclass BoxAnimation {\n \n private readonly img: HTMLImageElement\n private readonly animationTarget: HTMLElement\n\n constructor() {\n this.img = document.createElement(\"img\");\n this.img.setAttribute(\"src\", \"/img/cart/bg-effect.png\");\n this.img.setAttribute(\"class\", \"cart-text__animation-img\");\n this.animationTarget = getCartAnimationContainer();\n }\n\n playAnimation() {\n this.animationTarget.appendChild(this.img);\n\n setTimeout(() => {\n this.img.classList.add(isMobileView() ? \"fly-down\" : \"fly-up\");\n const x = randomIntFromRange(1, 80) * (randomIntFromRange(1, 10) < 5 ? 1 : -1);\n this.img.style.transform = `rotate(${x}deg)`;\n }, 10);\n\n setTimeout(() => {\n this.animationTarget.removeChild(this.img);\n }, 2000);\n }\n}\n\nconst playAddToCartAnimation = () => {\n const animation = new BoxAnimation();\n animation.playAnimation();\n}\n\nconst variantOk = (result: AddToCartResult, container: HTMLElement) => {\n return container.getAttribute(\"data-variant\") === result.variantName;\n}\n\nconst productIdOk = (result: AddToCartResult, container: HTMLElement) => {\n return container.getAttribute(\"data-product-id\") === result.productId.toString()\n}\n\nconst updateButtonAmount = (result: AddToCartResult) => {\n\n const existingItemElements = all(\".btn--add-to-cart__existing-items\");\n\n for (const existingItemsElement of existingItemElements) {\n if (variantOk(result, existingItemsElement) && productIdOk(result, existingItemsElement)) {\n var buttonText = existingItemsElement.innerText;\n if (buttonText) {\n const before = parseInt(existingItemsElement.innerText, 10);\n const after = before + result.amount;\n existingItemsElement.innerText = after.toString();\n } else {\n existingItemsElement.innerText = result.amount.toString();\n }\n }\n }\n}\n\nconst updateNavCartText = (result: AddToCartResult) => {\n const e = first(\".cart-text\");\n const existingNode = e.firstChild;\n\n if (existingNode) {\n const existingText = existingNode.nodeValue;\n\n if (existingText) {\n const leftBracketPosition = existingText.indexOf(\"(\");\n const rightBracketPosition = existingText.indexOf(\")\");\n\n if (leftBracketPosition === -1) {\n existingNode.nodeValue = `Einkaufswagen (${result.amount})`;\n } else {\n const amountAsText = existingText.substring(leftBracketPosition + 1, rightBracketPosition);\n const existingAmount = parseInt(amountAsText);\n const newAmount = existingAmount + result.amount;\n existingNode.nodeValue = `Einkaufswagen (${newAmount})`;\n }\n }\n }\n}\n\nclass ButtonAnimation {\n\n private readonly btn: HTMLElement\n\n constructor(btn: HTMLElement) {\n this.btn = btn;\n }\n\n playAnimation() {\n const div = first(\".btn--add-to-cart__existing-items\", this.btn);\n\n if (!div.classList.contains('bump')) {\n div.classList.add('bump');\n }\n\n setTimeout(() => {\n if (div.classList.contains('bump')) {\n div.classList.remove('bump');\n }\n }, 500);\n }\n}\n\nconst playButtonAnimation = (btn: HTMLElement) => {\n var a = new ButtonAnimation(btn);\n a.playAnimation();\n}\n\nconst clearPrevErrors = () => {\n for (const div of all(\".errors\")) {\n if (div.parentElement) {\n div.parentElement.removeChild(div);\n }\n }\n}\n\nconst getContainer = (error: AddToCartResult) => {\n for (const container of all('.hover-content')) {\n if (container.getAttribute(\"data-variant\") === error.variantName && container.getAttribute(\"data-product-id\") === error.productId.toString()) {\n return container;\n }\n }\n}\n\nconst createHoverElement = (error: AddToCartResult) => {\n // find the hover content element\n var container = getContainer(error);\n\n // put the error message in there, give it the 'hover effect' class\n if (container) {\n if (!container.innerText) {\n container.innerText = error.error;\n }\n if (!container.classList.contains('btn--add-to-cart__hover-content')) {\n container.classList.add('btn--add-to-cart__hover-content');\n }\n }\n}\n\nconst disableButton = (result: AddToCartResult, state: boolean) => {\n // find the hover content element\n var container = getContainer(result);\n\n // ..which is a child of the button\n if (container) {\n const btn = container.parentElement;\n\n if (btn instanceof HTMLButtonElement) {\n btn.disabled = state;\n\n // gray out the image also\n if (state) {\n var img = btn.firstChild;\n if (img && img instanceof HTMLImageElement && !img.classList.contains('btn--add-to-cart__image-gray')) {\n img.classList.add('btn--add-to-cart__image-gray');\n }\n }\n }\n }\n}\n\nconst addToCartEventListener = async (e: MouseEvent) => {\n e.stopPropagation();\n e.preventDefault();\n\n if (canFetch && e?.currentTarget instanceof Element) {\n const form = first('form', e.currentTarget);\n const formData = new FormData(form);\n\n const button = first('button', form);\n\n if (button.hasAttribute('disabled')) {\n return;\n }\n\n button.setAttribute('disabled', 'true');\n\n canFetch = false;\n\n try {\n const response = await fetch(\"/Api/AddProduct\", { method: 'POST', body: formData, credentials: 'include' })\n const result: AddToCartResult = await response.json();\n canFetch = true;\n clearPrevErrors();\n\n if (result.error) {\n throw result;\n } else {\n updateButtonAmount(result);\n updateNavCartText(result);\n playAddToCartAnimation();\n playButtonAnimation(button);\n disableButton(result, false);\n }\r\n } catch (result) {\n canFetch = true;\n if (result.error) {\n createHoverElement(result);\n }\n disableButton(result, true);\r\n }\n }\n}\n\n(() => {\n for (const element of getAddToCartSections()) {\n element.addEventListener('click', addToCartEventListener);\n }\n})()"],"names":["first","selector","root","foundElement","document","querySelector","Error","tryFirst","console","warn","all","Array","from","querySelectorAll","__webpack_module_cache__","__webpack_require__","moduleId","cachedModule","undefined","exports","module","__webpack_modules__","d","definition","key","o","Object","defineProperty","enumerable","get","obj","prop","prototype","hasOwnProperty","call","r","Symbol","toStringTag","value","canFetch","isMobileView","window","innerWidth","randomIntFromRange","min","max","Math","floor","random","BoxAnimation","constructor","this","img","createElement","setAttribute","animationTarget","animationContainer","getCartAnimationContainer","playAnimation","appendChild","setTimeout","classList","add","x","style","transform","removeChild","variantOk","result","container","getAttribute","variantName","productIdOk","productId","toString","ButtonAnimation","btn","div","contains","remove","getContainer","error","disableButton","state","parentElement","HTMLButtonElement","disabled","firstChild","HTMLImageElement","addToCartEventListener","e","__awaiter","stopPropagation","preventDefault","currentTarget","Element","form","formData","FormData","button","hasAttribute","response","fetch","method","body","credentials","json","clearPrevErrors","existingItemElements","existingItemsElement","innerText","after","parseInt","amount","updateButtonAmount","existingNode","existingText","nodeValue","leftBracketPosition","indexOf","rightBracketPosition","amountAsText","substring","newAmount","updateNavCartText","element","addEventListener"],"sourceRoot":""}