@magic-spells/gift-with-purchase 0.2.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +123 -26
- package/dist/gift-with-purchase.cjs.css +3 -0
- package/dist/gift-with-purchase.cjs.css.map +1 -1
- package/dist/gift-with-purchase.cjs.js +276 -186
- package/dist/gift-with-purchase.cjs.js.map +1 -1
- package/dist/gift-with-purchase.esm.css +3 -0
- package/dist/gift-with-purchase.esm.css.map +1 -1
- package/dist/gift-with-purchase.esm.js +276 -186
- package/dist/gift-with-purchase.esm.js.map +1 -1
- package/dist/gift-with-purchase.min.css +1 -1
- package/dist/gift-with-purchase.min.css.map +1 -1
- package/dist/gift-with-purchase.min.js +1 -1
- package/dist/gift-with-purchase.min.js.map +1 -1
- package/dist/gift-with-purchase.scss +5 -0
- package/package.json +4 -4
- package/src/gift-with-purchase.js +276 -186
- package/src/gift-with-purchase.scss +5 -0
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gift-with-purchase.esm.js","sources":["../src/gift-with-purchase.js"],"sourcesContent":["import './gift-with-purchase.scss';\n\n/**\n * Gift With Purchase Component - automatically adds/removes gift when cart threshold is met\n * Emits gwp:added/gwp:removed/gwp:error events and broadcasts cart updates\n */\nclass GiftWithPurchase extends HTMLElement {\n\t// private fields\n\t#threshold = 0;\n\t#currentAmount = 0;\n\t#variantId = null;\n\t#isActive = false;\n\t#isAdded = false;\n\t#promoEnded = false;\n\t#cartDialog = null;\n\t#boundHandleCartDataChange = null; // pre-bound listener ref for clean-up\n\t#debounceTimer = null; // debouncing cart updates\n\t#messageAbove = null; // message when threshold is met\n\t#messageBelow = null; // message when below threshold\n\n\tstatic get observedAttributes() {\n\t\treturn ['threshold', 'current', 'variant-id', 'promo-ended', 'message-above', 'message-below'];\n\t}\n\n\tconstructor() {\n\t\tsuper();\n\t\t// read initial attributes once\n\t\tthis.#threshold = parseFloat(this.getAttribute('threshold')) || 0;\n\t\tthis.#currentAmount = parseFloat(this.getAttribute('current')) || 0;\n\t\tthis.#variantId = this.getAttribute('variant-id');\n\t\tthis.#promoEnded = this.hasAttribute('promo-ended');\n\t\tthis.#messageAbove = this.getAttribute('message-above');\n\t\tthis.#messageBelow = this.getAttribute('message-below');\n\t\tthis.#boundHandleCartDataChange = this.#handleCartDataChange.bind(this);\n\t}\n\n\tconnectedCallback() {\n\t\tthis.#render();\n\t\tthis.#attachListeners();\n\t}\n\n\tdisconnectedCallback() {\n\t\tif (this.#debounceTimer) clearTimeout(this.#debounceTimer);\n\t\tif (this.#cartDialog)\n\t\t\tthis.#cartDialog.removeEventListener(\n\t\t\t\t'cart-dialog:data-changed',\n\t\t\t\tthis.#boundHandleCartDataChange\n\t\t\t);\n\t}\n\n\t#render() {\n\t\tthis.classList.add('gift-with-purchase');\n\t\tthis.#renderMessages();\n\t}\n\n\t#renderMessages() {\n\t\t// Look for existing message element with data-content-gwp-message\n\t\tthis.#updateMessages();\n\t}\n\n\t#updateMessages() {\n\t\tconst messageEl = this.querySelector('[data-content-gwp-message]');\n\t\tif (!messageEl) return;\n\n\t\tlet message = '';\n\n\t\t// console.log('updateMessages - this.#isActive', this.#isActive);\n\n\t\tif (this.#isActive && this.#messageAbove) {\n\t\t\t// set message to above threshold message\n\t\t\tmessage = this.#messageAbove;\n\t\t} else if (!this.#isActive && this.#messageBelow) {\n\t\t\t// set message to below threshold message\n\t\t\tconst remaining = this.#threshold - this.#currentAmount;\n\t\t\tconst formattedAmount = remaining.toFixed(2).replace(/\\.00$/, '');\n\t\t\tmessage = this.#messageBelow\n\t\t\t\t.replace(/\\{\\s*amount\\s*\\}/g, formattedAmount)\n\t\t\t\t.replace(/\\{amount\\}/g, formattedAmount);\n\t\t}\n\n\t\tmessageEl.textContent = message;\n\t\tmessageEl.style.display = message ? 'block' : 'none';\n\t}\n\n\t#attachListeners() {\n\t\t// Look for cart-dialog element when attaching listeners (more reliable timing)\n\t\tthis.#cartDialog = this.closest('cart-dialog');\n\n\t\tif (this.#cartDialog) {\n\t\t\t// console.log('cartDialog exists and is attaching events');\n\t\t\tthis.#cartDialog.addEventListener(\n\t\t\t\t'cart-dialog:data-changed',\n\t\t\t\tthis.#boundHandleCartDataChange\n\t\t\t);\n\t\t} else {\n\t\t\t// Try again after a short delay in case the DOM isn't fully ready\n\t\t\tsetTimeout(() => {\n\t\t\t\t// console.log('cartDialog DIDNT exist and we waited to attach events');\n\t\t\t\tthis.#cartDialog = this.closest('cart-dialog');\n\t\t\t\tif (this.#cartDialog) {\n\t\t\t\t\tthis.#cartDialog.addEventListener(\n\t\t\t\t\t\t'cart-dialog:data-changed',\n\t\t\t\t\t\tthis.#boundHandleCartDataChange\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconsole.error('GWP - cart-dialog still not found after delay');\n\t\t\t\t}\n\t\t\t}, 100);\n\t\t}\n\t}\n\n\t#handleCartDataChange(event) {\n\t\tconst cart = event.detail;\n\t\t// console.log('GWP - handleCartDataChange cart: ', cart.calculated_subtotal, cart);\n\n\t\tif (!cart || typeof cart.calculated_subtotal === 'undefined') return;\n\t\tif (this.#debounceTimer) clearTimeout(this.#debounceTimer);\n\n\t\tthis.#debounceTimer = setTimeout(() => {\n\t\t\tthis.#debounceTimer = null;\n\t\t\tthis.#currentAmount = parseFloat(cart.calculated_subtotal / 100) || 0;\n\t\t\tthis.#checkGiftInCart(cart);\n\t\t\tthis.#updateState(cart);\n\t\t}, 300);\n\t}\n\n\t// checks to see if the gift is already in the cart\n\t#checkGiftInCart(cart) {\n\t\tif (!cart.items || !this.#variantId) {\n\t\t\tthis.#isAdded = false;\n\t\t\treturn;\n\t\t}\n\t\tconst giftLines = cart.items.filter(\n\t\t\t(lineItem) =>\n\t\t\t\tlineItem.variant_id.toString() === this.#variantId.toString() &&\n\t\t\t\tlineItem.properties?._gwp_item === 'true'\n\t\t);\n\t\tthis.#isAdded = giftLines.length > 0;\n\t\tif (this.#promoEnded && giftLines.length) this.#removeAllGiftItems(giftLines);\n\t}\n\n\t#updateState(cart) {\n\t\t// console.log('********** ---------- Updating state....');\n\t\tconst wasActive = this.#isActive;\n\t\tthis.#isActive = this.#currentAmount >= this.#threshold && !this.#promoEnded;\n\n\t\t// console.log('********** ---------- this.#isActive', this.#isActive);\n\n\t\tif (this.#promoEnded) {\n\t\t\t// remove GWP from cart\n\t\t\tthis.#removeGiftFromCart(cart);\n\t\t}\n\n\t\tif (this.#isActive && !wasActive && !this.#isAdded && this.#variantId) {\n\t\t\tthis.#addGiftToCart();\n\t\t} else if (!this.#isActive && wasActive && this.#isAdded && this.#variantId) {\n\t\t\tthis.#removeGiftFromCart(cart);\n\t\t}\n\n\t\tthis.#updateVisualState();\n\t\tthis.#updateMessages();\n\t}\n\n\t#updateVisualState() {\n\t\tif (this.#promoEnded) {\n\t\t\tthis.setAttribute('state', 'ended');\n\t\t\tthis.style.display = 'none';\n\t\t\treturn;\n\t\t}\n\n\t\tthis.style.display = '';\n\t\tif (this.#isAdded) {\n\t\t\tthis.setAttribute('state', 'added');\n\t\t} else if (this.#isActive) {\n\t\t\tthis.setAttribute('state', 'active');\n\t\t}\n\t\t// Note: no 'inactive' state since component wouldn't be loaded if inactive\n\t}\n\n\tasync #addGiftToCart() {\n\t\ttry {\n\t\t\tconst res = await fetch('/cart/add.js', {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tcredentials: 'same-origin',\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t'X-Requested-With': 'XMLHttpRequest',\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\titems: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tid: this.#variantId,\n\t\t\t\t\t\t\tquantity: 1,\n\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t_gwp_item: 'true',\n\t\t\t\t\t\t\t\t_hide_in_cart: 'true',\n\t\t\t\t\t\t\t\t_ignore_price_in_subtotal: 'true',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\t});\n\t\t\tif (!res.ok) throw new Error(`http ${res.status}`);\n\t\t\tawait res.json();\n\t\t\tthis.#isAdded = true;\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:added', {\n\t\t\t\t\tdetail: { variantId: this.#variantId },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: add error', err);\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:error', {\n\t\t\t\t\tdetail: { action: 'add', error: err.message },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tasync #removeGiftFromCart(cart) {\n\t\ttry {\n\t\t\t// get all GWP items in the cart\n\t\t\tconst giftLines = cart.items.filter(\n\t\t\t\t(lineItem) =>\n\t\t\t\t\tlineItem.variant_id.toString() === this.#variantId.toString() &&\n\t\t\t\t\tlineItem.properties?._gwp_item === 'true'\n\t\t\t);\n\n\t\t\t// exit if no items in the cart\n\t\t\tif (!giftLines.length) {\n\t\t\t\tthis.#isAdded = false;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// remove all GWP items from the cart\n\t\t\tawait this.#removeAllGiftItems(giftLines);\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: remove error', err);\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:error', {\n\t\t\t\t\tdetail: { action: 'remove', error: err.message },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tasync #removeAllGiftItems(giftLines) {\n\t\ttry {\n\t\t\tawait Promise.all(\n\t\t\t\tgiftLines.map((giftItem) =>\n\t\t\t\t\tfetch('/cart/change.js', {\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\tcredentials: 'same-origin',\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t\t\t'X-Requested-With': 'XMLHttpRequest',\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify({ id: giftItem.key, quantity: 0 }),\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t);\n\t\t\tthis.#isAdded = false;\n\t\t\t// note: you can broadcast cart again here if you want real-time re-render after remove\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:removed', {\n\t\t\t\t\tdetail: { variantId: this.#variantId },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: bulk remove error', err);\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:error', {\n\t\t\t\t\tdetail: { action: 'remove', error: err.message },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tgetState() {\n\t\treturn {\n\t\t\tcurrentAmount: this.#currentAmount,\n\t\t\tthreshold: this.#threshold,\n\t\t\tvariantId: this.#variantId,\n\t\t\tisActive: this.#isActive,\n\t\t\tisAdded: this.#isAdded,\n\t\t\tpromoEnded: this.#promoEnded,\n\t\t\tremainingAmount: Math.max(0, this.#threshold - this.#currentAmount),\n\t\t};\n\t}\n\n\tget currentAmount() {\n\t\treturn this.#currentAmount;\n\t}\n\tget threshold() {\n\t\treturn this.#threshold;\n\t}\n\tget variantId() {\n\t\treturn this.#variantId;\n\t}\n\tget isActive() {\n\t\treturn this.#isActive;\n\t}\n\tget isAdded() {\n\t\treturn this.#isAdded;\n\t}\n\tget promoEnded() {\n\t\treturn this.#promoEnded;\n\t}\n\n\t// Public setter methods for programmatic control\n\tsetCurrentAmount(amount) {\n\t\tthis.#currentAmount = parseFloat(amount) || 0;\n\t\tthis.#updateState({ items: [] }); // Pass empty cart to avoid cart operations\n\t\tthis.#updateMessages();\n\t}\n\n\tsetThreshold(threshold) {\n\t\tthis.#threshold = parseFloat(threshold) || 0;\n\t\tthis.#updateState({ items: [] }); // Pass empty cart to avoid cart operations\n\t\tthis.#updateMessages();\n\t}\n\n\tsetVariantId(variantId) {\n\t\tthis.#variantId = variantId;\n\t}\n}\n\nif (!customElements.get('gift-with-purchase')) {\n\tcustomElements.define('gift-with-purchase', GiftWithPurchase);\n}\n\nexport { GiftWithPurchase };\n"],"names":[],"mappings":"AAEA;AACA;AACA;AACA;AACA,MAAM,gBAAgB,SAAS,WAAW,CAAC;AAC3C;AACA,CAAC,UAAU,GAAG,CAAC,CAAC;AAChB,CAAC,cAAc,GAAG,CAAC,CAAC;AACpB,CAAC,UAAU,GAAG,IAAI,CAAC;AACnB,CAAC,SAAS,GAAG,KAAK,CAAC;AACnB,CAAC,QAAQ,GAAG,KAAK,CAAC;AAClB,CAAC,WAAW,GAAG,KAAK,CAAC;AACrB,CAAC,WAAW,GAAG,IAAI,CAAC;AACpB,CAAC,0BAA0B,GAAG,IAAI,CAAC;AACnC,CAAC,cAAc,GAAG,IAAI,CAAC;AACvB,CAAC,aAAa,GAAG,IAAI,CAAC;AACtB,CAAC,aAAa,GAAG,IAAI,CAAC;AACtB;AACA,CAAC,WAAW,kBAAkB,GAAG;AACjC,EAAE,OAAO,CAAC,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,eAAe,EAAE,eAAe,CAAC,CAAC;AACjG,EAAE;AACF;AACA,CAAC,WAAW,GAAG;AACf,EAAE,KAAK,EAAE,CAAC;AACV;AACA,EAAE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,WAAW,CAAC,CAAC,IAAI,CAAC,CAAC;AACpE,EAAE,IAAI,CAAC,cAAc,GAAG,UAAU,CAAC,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC;AACtE,EAAE,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC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K,EAAE,EAAE,EAAE,CAAC,CAAC;AACnC,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC;AACzB,EAAE;AACF;AACA,CAAC,YAAY,CAAC,SAAS,EAAE;AACzB,EAAE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;AAC/C,EAAE,IAAI,CAAC,YAAY,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,CAAC;AACnC,EAAE,IAAI,CAAC,eAAe,EAAE,CAAC;AACzB,EAAE;AACF;AACA,CAAC,YAAY,CAAC,SAAS,EAAE;AACzB,EAAE,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;AAC9B,EAAE;AACF,CAAC;AACD;AACA,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,oBAAoB,CAAC,EAAE;AAC/C,CAAC,cAAc,CAAC,MAAM,CAAC,oBAAoB,EAAE,gBAAgB,CAAC,CAAC;AAC/D;;;;"}
|
|
1
|
+
{"version":3,"file":"gift-with-purchase.esm.js","sources":["../src/gift-with-purchase.js"],"sourcesContent":["import './gift-with-purchase.scss';\n\n/**\n * Gift With Purchase Component - automatically adds/removes gift when cart threshold is met\n * Emits gwp:added/gwp:removed/gwp:error events and broadcasts cart updates\n */\nclass GiftWithPurchase extends HTMLElement {\n\t#threshold = 0;\n\t#currentAmount = 0;\n\t#variantId = null;\n\t#isActive = false;\n\t#isAdded = false;\n\t#promoEnded = false;\n\t#productAvailable = true;\n\t#isDisabled = false;\n\t#cartPanel = null;\n\t#handlers = {};\n\t#debounceTimer = null;\n\t#attachRetryTimer = null;\n\t#isMutating = false; // prevents overlapping cart mutations\n\t#pendingCart = null; // stores cart snapshot during mutation for recheck\n\t#messageAbove = null;\n\t#messageBelow = null;\n\t#moneyFormat = null;\n\n\tstatic get observedAttributes() {\n\t\treturn [\n\t\t\t'threshold',\n\t\t\t'current',\n\t\t\t'variant-id',\n\t\t\t'promo-ended',\n\t\t\t'product-available',\n\t\t\t'message-above',\n\t\t\t'message-below',\n\t\t\t'money-format',\n\t\t];\n\t}\n\n\tconstructor() {\n\t\tsuper();\n\t\tconst _ = this;\n\t\t_.#threshold = parseFloat(_.getAttribute('threshold')) || 0;\n\t\t_.#currentAmount = parseFloat(_.getAttribute('current')) || 0;\n\t\t_.#variantId = _.getAttribute('variant-id');\n\t\t_.#promoEnded = _.hasAttribute('promo-ended');\n\t\t_.#productAvailable = _.#parseBooleanValue(_.getAttribute('product-available'), true);\n\t\t_.#messageAbove = _.getAttribute('message-above');\n\t\t_.#messageBelow = _.getAttribute('message-below');\n\t\t_.#moneyFormat = _.getAttribute('money-format');\n\t\t_.#handlers = { cartDataChange: _.#handleCartDataChange.bind(_) };\n\t}\n\n\tconnectedCallback() {\n\t\tconst _ = this;\n\n\t\t_.#calculateInitialState();\n\t\t_.#render();\n\t\t_.#updateVisualState();\n\t\t_.#attachListeners();\n\t}\n\n\t#calculateInitialState() {\n\t\tconst _ = this;\n\t\t// Calculate initial active state based on attributes (before any cart events)\n\t\tconst convertedThreshold = _.#getConvertedThreshold();\n\t\t_.#isDisabled = _.#promoEnded || !_.#productAvailable;\n\t\t_.#isActive = _.#currentAmount >= convertedThreshold && !_.#isDisabled;\n\t}\n\n\tdisconnectedCallback() {\n\t\tconst _ = this;\n\t\tif (_.#debounceTimer) clearTimeout(_.#debounceTimer);\n\t\tif (_.#attachRetryTimer) clearTimeout(_.#attachRetryTimer);\n\t\tif (_.#cartPanel) _.#cartPanel.removeEventListener('cart-panel:data-changed', _.#handlers.cartDataChange);\n\t}\n\n\tattributeChangedCallback(name, oldValue, newValue) {\n\t\tconst _ = this;\n\t\tif (oldValue === newValue) return;\n\n\t\tswitch (name) {\n\t\t\tcase 'threshold':\n\t\t\t\t_.#threshold = parseFloat(newValue) || 0;\n\t\t\t\tbreak;\n\t\t\tcase 'current':\n\t\t\t\t_.#currentAmount = parseFloat(newValue) || 0;\n\t\t\t\tbreak;\n\t\t\tcase 'variant-id':\n\t\t\t\t_.#variantId = newValue;\n\t\t\t\tbreak;\n\t\t\tcase 'promo-ended':\n\t\t\t\t_.#promoEnded = newValue !== null;\n\t\t\t\tbreak;\n\t\t\tcase 'product-available':\n\t\t\t\t_.#productAvailable = _.#parseBooleanValue(newValue, true);\n\t\t\t\tbreak;\n\t\t\tcase 'message-above':\n\t\t\t\t_.#messageAbove = newValue;\n\t\t\t\tbreak;\n\t\t\tcase 'message-below':\n\t\t\t\t_.#messageBelow = newValue;\n\t\t\t\tbreak;\n\t\t\tcase 'money-format':\n\t\t\t\t_.#moneyFormat = newValue;\n\t\t\t\tbreak;\n\t\t}\n\n\t\t// Recalculate state and update UI if component is connected\n\t\tif (_.isConnected) {\n\t\t\t_.#calculateInitialState();\n\t\t\t_.#updateVisualState();\n\t\t\t_.#updateMessages();\n\t\t}\n\t}\n\n\t#render() {\n\t\tthis.classList.add('gift-with-purchase');\n\t\tthis.#renderMessages();\n\t}\n\n\t#renderMessages() {\n\t\t// Look for existing message element with data-content-gwp-message\n\t\tthis.#updateMessages();\n\t}\n\n\t#formatMoney(amount) {\n\t\tif (!this.#moneyFormat) return amount.toFixed(2).replace(/\\.00$/, '');\n\n\t\tconst amountFixed = amount.toFixed(2);\n\t\tconst amountNoDecimals = Math.round(amount).toString();\n\t\tconst amountWithComma = amountFixed.replace('.', ',');\n\t\tconst amountNoDecimalsWithComma = amountNoDecimals.replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');\n\n\t\treturn this.#moneyFormat\n\t\t\t.replace(/\\{\\{\\s*amount_no_decimals_with_comma_separator\\s*\\}\\}/g, amountNoDecimalsWithComma)\n\t\t\t.replace(/\\{\\{\\s*amount_with_comma_separator\\s*\\}\\}/g, amountWithComma)\n\t\t\t.replace(/\\{\\{\\s*amount_no_decimals\\s*\\}\\}/g, amountNoDecimals)\n\t\t\t.replace(/\\{\\{\\s*amount\\s*\\}\\}/g, amountFixed);\n\t}\n\n\t#getConvertedThreshold() {\n\t\t// Convert threshold using Shopify currency rate if available (for multi-currency stores)\n\t\tconst rate = parseFloat(window.Shopify?.currency?.rate) || 1;\n\t\treturn this.#threshold * rate;\n\t}\n\n\t#parseBooleanValue(value, defaultValue = true) {\n\t\tif (value === null || typeof value === 'undefined') return defaultValue;\n\t\tif (value === '') return true;\n\t\tconst normalized = String(value).trim().toLowerCase();\n\t\tif (normalized === 'false' || normalized === '0') return false;\n\t\tif (normalized === 'true' || normalized === '1') return true;\n\t\treturn defaultValue;\n\t}\n\n\t#updateMessages() {\n\t\tconst _ = this;\n\t\tconst messageEl = _.querySelector('[data-content-gwp-message]');\n\t\tif (!messageEl) return;\n\n\t\tlet message = '';\n\t\tif (_.#isActive && _.#messageAbove) {\n\t\t\tmessage = _.#messageAbove;\n\t\t} else if (!_.#isActive && _.#messageBelow) {\n\t\t\tconst remaining = _.#getConvertedThreshold() - _.#currentAmount;\n\t\t\tconst formattedAmount = _.#formatMoney(remaining);\n\t\t\tmessage = _.#messageBelow\n\t\t\t\t.replace(/\\[\\s*amount\\s*\\]/g, formattedAmount)\n\t\t\t\t.replace(/\\[amount\\]/g, formattedAmount);\n\t\t}\n\n\t\tmessageEl.textContent = message;\n\t\tmessageEl.style.display = message ? 'block' : 'none';\n\t}\n\n\t#attachListeners() {\n\t\tconst _ = this;\n\t\t_.#cartPanel = _.closest('cart-panel');\n\n\t\tif (_.#cartPanel) {\n\t\t\t_.#cartPanel.addEventListener('cart-panel:data-changed', _.#handlers.cartDataChange);\n\t\t} else {\n\t\t\t_.#attachRetryTimer = setTimeout(() => {\n\t\t\t\t_.#attachRetryTimer = null;\n\t\t\t\t_.#cartPanel = _.closest('cart-panel');\n\t\t\t\tif (_.#cartPanel) _.#cartPanel.addEventListener('cart-panel:data-changed', _.#handlers.cartDataChange);\n\t\t\t\telse console.error('GWP - cart-panel still not found after delay');\n\t\t\t}, 100);\n\t\t}\n\t}\n\n\t#handleCartDataChange(event) {\n\t\tconst _ = this;\n\t\tconst cart = event.detail;\n\t\tif (!cart || typeof cart.calculated_subtotal === 'undefined') return;\n\t\tif (_.#debounceTimer) clearTimeout(_.#debounceTimer);\n\n\t\t_.#debounceTimer = setTimeout(() => {\n\t\t\t_.#debounceTimer = null;\n\t\t\t_.#currentAmount = parseFloat(cart.calculated_subtotal / 100) || 0;\n\t\t\t_.#checkGiftInCart(cart);\n\t\t\t_.#updateState(cart);\n\t\t}, 300);\n\t}\n\n\t#checkGiftInCart(cart) {\n\t\tconst _ = this;\n\t\tconst giftLines = _.#getGiftLines(cart, true);\n\t\t_.#isAdded = giftLines.length > 0;\n\t}\n\n\t#getGiftLines(cart, matchVariantId = true) {\n\t\tconst _ = this;\n\t\tif (!cart?.items) return [];\n\t\treturn cart.items.filter((item) => {\n\t\t\tif (item.properties?._gwp_item !== 'true') return false;\n\t\t\tif (!matchVariantId) return true;\n\t\t\tif (!_.#variantId) return false;\n\t\t\treturn item.variant_id?.toString() === _.#variantId.toString();\n\t\t});\n\t}\n\n\t#recheckIfPending() {\n\t\tconst _ = this;\n\t\tif (_.#pendingCart) {\n\t\t\tconst cart = _.#pendingCart;\n\t\t\t_.#pendingCart = null;\n\t\t\t_.#checkGiftInCart(cart);\n\t\t\t_.#updateState(cart);\n\t\t}\n\t}\n\n\t#updateState(cart) {\n\t\tconst _ = this;\n\n\t\t// If mutation in progress, queue this cart for recheck later\n\t\tif (_.#isMutating) {\n\t\t\t_.#pendingCart = cart;\n\t\t\treturn;\n\t\t}\n\n\t\tconst wasActive = _.#isActive;\n\t\tconst convertedThreshold = _.#getConvertedThreshold();\n\t\t_.#isDisabled = _.#promoEnded || !_.#productAvailable;\n\t\t_.#isActive = _.#currentAmount >= convertedThreshold && !_.#isDisabled;\n\n\t\tif (_.#isDisabled) _.#removeAllGiftsFromCart(cart);\n\t\telse if (_.#isActive && !wasActive && !_.#isAdded && _.#variantId) _.#addGiftToCart();\n\t\telse if (!_.#isActive && _.#isAdded && _.#variantId) _.#removeGiftFromCart(cart);\n\n\t\t_.#updateVisualState();\n\t\t_.#updateMessages();\n\t}\n\n\t#updateVisualState() {\n\t\tconst _ = this;\n\t\tif (_.#promoEnded) {\n\t\t\t_.setAttribute('state', 'ended');\n\t\t\t_.style.display = 'none';\n\t\t\treturn;\n\t\t}\n\n\t\tif (!_.#productAvailable) {\n\t\t\t_.setAttribute('state', 'disabled');\n\t\t\t_.style.display = 'none';\n\t\t\treturn;\n\t\t}\n\n\t\t_.style.display = '';\n\t\tif (_.#isAdded) _.setAttribute('state', 'added');\n\t\telse if (_.#isActive) _.setAttribute('state', 'active');\n\t\telse _.setAttribute('state', 'inactive');\n\t}\n\n\tasync #addGiftToCart() {\n\t\tconst _ = this;\n\t\t_.#isMutating = true;\n\t\ttry {\n\t\t\tconst res = await fetch('/cart/add.js', {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tcredentials: 'same-origin',\n\t\t\t\theaders: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' },\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\titems: [{ id: _.#variantId, quantity: 1, properties: { _gwp_item: 'true', _hide_in_cart: 'true', _ignore_price_in_subtotal: 'true' } }],\n\t\t\t\t}),\n\t\t\t});\n\t\t\tif (!res.ok) throw new Error(`http ${res.status}`);\n\t\t\tawait res.json();\n\t\t\t_.#isAdded = true;\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:added', { detail: { variantId: _.#variantId }, bubbles: true }));\n\t\t\t_.#cartPanel?.getCartAndRefresh();\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: add error', err);\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:error', { detail: { action: 'add', error: err.message }, bubbles: true }));\n\t\t} finally {\n\t\t\t_.#isMutating = false;\n\t\t\t_.#recheckIfPending();\n\t\t}\n\t}\n\n\tasync #removeGiftFromCart(cart) {\n\t\tconst _ = this;\n\t\tif (!cart?.items) return;\n\n\t\tconst giftLines = _.#getGiftLines(cart, true);\n\t\tif (!giftLines.length) {\n\t\t\t_.#isAdded = false;\n\t\t\treturn;\n\t\t}\n\n\t\t_.#isMutating = true;\n\t\ttry {\n\t\t\tawait _.#removeAllGiftItems(giftLines);\n\t\t} finally {\n\t\t\t_.#isMutating = false;\n\t\t\t_.#recheckIfPending();\n\t\t}\n\t}\n\n\tasync #removeAllGiftsFromCart(cart) {\n\t\tconst _ = this;\n\t\tif (!cart?.items) return;\n\n\t\tconst giftLines = _.#getGiftLines(cart, false);\n\t\tif (!giftLines.length) {\n\t\t\t_.#isAdded = false;\n\t\t\treturn;\n\t\t}\n\n\t\t_.#isMutating = true;\n\t\ttry {\n\t\t\tawait _.#removeAllGiftItems(giftLines);\n\t\t} finally {\n\t\t\t_.#isMutating = false;\n\t\t\t_.#recheckIfPending();\n\t\t}\n\t}\n\n\tasync #removeAllGiftItems(giftLines) {\n\t\tconst _ = this;\n\t\ttry {\n\t\t\tawait Promise.all(\n\t\t\t\tgiftLines.map(async (item) => {\n\t\t\t\t\tconst res = await fetch('/cart/change.js', {\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\tcredentials: 'same-origin',\n\t\t\t\t\t\theaders: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' },\n\t\t\t\t\t\tbody: JSON.stringify({ id: item.key, quantity: 0 }),\n\t\t\t\t\t});\n\t\t\t\t\tif (!res.ok) throw new Error(`http ${res.status}`);\n\t\t\t\t})\n\t\t\t);\n\t\t\t_.#isAdded = false;\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:removed', { detail: { variantId: _.#variantId }, bubbles: true }));\n\t\t\t_.#cartPanel?.getCartAndRefresh();\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: bulk remove error', err);\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:error', { detail: { action: 'remove', error: err.message }, bubbles: true }));\n\t\t}\n\t}\n\n\tgetState() {\n\t\tconst _ = this;\n\t\tconst convertedThreshold = _.#getConvertedThreshold();\n\t\treturn {\n\t\t\tcurrentAmount: _.#currentAmount,\n\t\t\tthreshold: _.#threshold,\n\t\t\tconvertedThreshold,\n\t\t\tvariantId: _.#variantId,\n\t\t\tisActive: _.#isActive,\n\t\t\tisAdded: _.#isAdded,\n\t\t\tpromoEnded: _.#promoEnded,\n\t\t\tproductAvailable: _.#productAvailable,\n\t\t\tisDisabled: _.#isDisabled,\n\t\t\tremainingAmount: Math.max(0, convertedThreshold - _.#currentAmount),\n\t\t\tcurrencyRate: parseFloat(window.Shopify?.currency?.rate) || 1,\n\t\t};\n\t}\n\n\tget currentAmount() {\n\t\treturn this.#currentAmount;\n\t}\n\tget threshold() {\n\t\treturn this.#threshold;\n\t}\n\tget variantId() {\n\t\treturn this.#variantId;\n\t}\n\tget isActive() {\n\t\treturn this.#isActive;\n\t}\n\tget isAdded() {\n\t\treturn this.#isAdded;\n\t}\n\tget promoEnded() {\n\t\treturn this.#promoEnded;\n\t}\n\tget productAvailable() {\n\t\treturn this.#productAvailable;\n\t}\n\tget isDisabled() {\n\t\treturn this.#isDisabled;\n\t}\n\n\tsetCurrentAmount(amount) {\n\t\tconst _ = this;\n\t\t_.#currentAmount = parseFloat(amount) || 0;\n\t\t_.#updateState({ items: [] });\n\t\t_.#updateMessages();\n\t}\n\n\tsetThreshold(threshold) {\n\t\tconst _ = this;\n\t\t_.#threshold = parseFloat(threshold) || 0;\n\t\t_.#updateState({ items: [] });\n\t\t_.#updateMessages();\n\t}\n\n\tsetVariantId(variantId) {\n\t\tthis.#variantId = variantId;\n\t}\n}\n\nif (!customElements.get('gift-with-purchase')) {\n\tcustomElements.define('gift-with-purchase', GiftWithPurchase);\n}\n\nexport { GiftWithPurchase };\n"],"names":[],"mappings":"AAEA;AACA;AACA;AACA;AACA,MAAM,gBAAgB,SAAS,WAAW,CAAC;AAC3C,CAAC,UAAU,GAAG,CAAC,CAAC;AAChB,CAAC,cAAc,GAAG,CAAC,CAAC;AACpB,CAAC,UAAU,GAAG,IAAI,CAAC;AACnB,CAAC,SAAS,GAAG,KAAK,CAAC;AACnB,CAAC,QAA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E;AACzB,EAAE,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;AAC9B,EAAE;AACF,CAAC;AACD;AACA,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,oBAAoB,CAAC,EAAE;AAC/C,CAAC,cAAc,CAAC,MAAM,CAAC,oBAAoB,EAAE,gBAAgB,CAAC,CAAC;AAC/D;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
gift-with-purchase{--gwp-border-radius:8px;--gwp-padding:1rem;--gwp-gap:1rem;--gwp-bg-active:#e8f5e8;--gwp-bg-added:#d4edda;--gwp-border-active:#28a745;--gwp-border-added:#155724;--gwp-text-active:#155724;--gwp-text-added:#155724;background-color:var(--gwp-bg-active);border:2px solid var(--gwp-border-active);border-radius:var(--gwp-border-radius);color:var(--gwp-text-active);display:block;padding:var(--gwp-padding)}gift-with-purchase .gwp-product{align-items:flex-start;display:flex;gap:var(--gwp-gap)}gift-with-purchase [data-gwp-image]{flex-shrink:0}gift-with-purchase .gwp-content{flex:1;min-width:0}gift-with-purchase[state=active]{background-color:var(--gwp-bg-active);border-color:var(--gwp-border-active);color:var(--gwp-text-active)}gift-with-purchase[state=added]{background-color:var(--gwp-bg-added);border-color:var(--gwp-border-added);color:var(--gwp-text-added)}gift-with-purchase[state=ended]{display:none}
|
|
1
|
+
gift-with-purchase{--gwp-border-radius:8px;--gwp-padding:1rem;--gwp-gap:1rem;--gwp-bg-active:#e8f5e8;--gwp-bg-added:#d4edda;--gwp-border-active:#28a745;--gwp-border-added:#155724;--gwp-text-active:#155724;--gwp-text-added:#155724;background-color:var(--gwp-bg-active);border:2px solid var(--gwp-border-active);border-radius:var(--gwp-border-radius);color:var(--gwp-text-active);display:block;padding:var(--gwp-padding)}gift-with-purchase .gwp-product{align-items:flex-start;display:flex;gap:var(--gwp-gap)}gift-with-purchase [data-gwp-image]{flex-shrink:0}gift-with-purchase .gwp-content{flex:1;min-width:0}gift-with-purchase[state=active]{background-color:var(--gwp-bg-active);border-color:var(--gwp-border-active);color:var(--gwp-text-active)}gift-with-purchase[state=added]{background-color:var(--gwp-bg-added);border-color:var(--gwp-border-added);color:var(--gwp-text-added)}gift-with-purchase[state=disabled],gift-with-purchase[state=ended]{display:none}
|
|
2
2
|
/*# sourceMappingURL=gift-with-purchase.min.css.map */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["gift-with-purchase.scss"],"names":[],"mappings":"AAAA,mBACE,uBAAwB,CACxB,kBAAmB,CACnB,cAAe,CACf,uBAAwB,CACxB,sBAAuB,CACvB,2BAA4B,CAC5B,0BAA2B,CAC3B,yBAA0B,CAC1B,wBAAyB,CAKzB,qCAAsC,CAHtC,yCAA0C,CAC1C,sCAAuC,CAGvC,4BAA6B,CAL7B,aAAc,CAGd,0BAGF,CACA,gCAEE,sBAAuB,CADvB,YAAa,CAEb,kBACF,CACA,oCACE,aACF,CACA,gCACE,MAAO,CACP,WACF,CACA,iCACE,qCAAsC,CACtC,qCAAsC,CACtC,4BACF,CACA,gCACE,oCAAqC,CACrC,oCAAqC,CACrC,2BACF,
|
|
1
|
+
{"version":3,"sources":["gift-with-purchase.scss"],"names":[],"mappings":"AAAA,mBACE,uBAAwB,CACxB,kBAAmB,CACnB,cAAe,CACf,uBAAwB,CACxB,sBAAuB,CACvB,2BAA4B,CAC5B,0BAA2B,CAC3B,yBAA0B,CAC1B,wBAAyB,CAKzB,qCAAsC,CAHtC,yCAA0C,CAC1C,sCAAuC,CAGvC,4BAA6B,CAL7B,aAAc,CAGd,0BAGF,CACA,gCAEE,sBAAuB,CADvB,YAAa,CAEb,kBACF,CACA,oCACE,aACF,CACA,gCACE,MAAO,CACP,WACF,CACA,iCACE,qCAAsC,CACtC,qCAAsC,CACtC,4BACF,CACA,gCACE,oCAAqC,CACrC,oCAAqC,CACrC,2BACF,CAIA,mEACE,YACF","file":"gift-with-purchase.min.css","sourcesContent":["gift-with-purchase {\n --gwp-border-radius: 8px;\n --gwp-padding: 1rem;\n --gwp-gap: 1rem;\n --gwp-bg-active: #e8f5e8;\n --gwp-bg-added: #d4edda;\n --gwp-border-active: #28a745;\n --gwp-border-added: #155724;\n --gwp-text-active: #155724;\n --gwp-text-added: #155724;\n display: block;\n border: 2px solid var(--gwp-border-active);\n border-radius: var(--gwp-border-radius);\n padding: var(--gwp-padding);\n background-color: var(--gwp-bg-active);\n color: var(--gwp-text-active);\n}\ngift-with-purchase .gwp-product {\n display: flex;\n align-items: flex-start;\n gap: var(--gwp-gap);\n}\ngift-with-purchase [data-gwp-image] {\n flex-shrink: 0;\n}\ngift-with-purchase .gwp-content {\n flex: 1;\n min-width: 0;\n}\ngift-with-purchase[state=active] {\n background-color: var(--gwp-bg-active);\n border-color: var(--gwp-border-active);\n color: var(--gwp-text-active);\n}\ngift-with-purchase[state=added] {\n background-color: var(--gwp-bg-added);\n border-color: var(--gwp-border-added);\n color: var(--gwp-text-added);\n}\ngift-with-purchase[state=ended] {\n display: none;\n}\ngift-with-purchase[state=disabled] {\n display: none;\n}"]}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(t
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).GiftWithPurchase={})}(this,function(e){"use strict";class t extends HTMLElement{#e=0;#t=0;#a=null;#r=!1;#s=!1;#i=!1;#n=!0;#o=!1;#d=null;#l={};#c=null;#u=null;#h=!1;#m=null;#p=null;#g=null;#b=null;static get observedAttributes(){return["threshold","current","variant-id","promo-ended","product-available","message-above","message-below","money-format"]}constructor(){super();const e=this;e.#e=parseFloat(e.getAttribute("threshold"))||0,e.#t=parseFloat(e.getAttribute("current"))||0,e.#a=e.getAttribute("variant-id"),e.#i=e.hasAttribute("promo-ended"),e.#n=e.#v(e.getAttribute("product-available"),!0),e.#p=e.getAttribute("message-above"),e.#g=e.getAttribute("message-below"),e.#b=e.getAttribute("money-format"),e.#l={cartDataChange:e.#A.bind(e)}}connectedCallback(){const e=this;e.#f(),e.#y(),e.#C(),e.#w()}#f(){const e=this,t=e.#T();e.#o=e.#i||!e.#n,e.#r=e.#t>=t&&!e.#o}disconnectedCallback(){const e=this;e.#c&&clearTimeout(e.#c),e.#u&&clearTimeout(e.#u),e.#d&&e.#d.removeEventListener("cart-panel:data-changed",e.#l.cartDataChange)}attributeChangedCallback(e,t,a){const r=this;if(t!==a){switch(e){case"threshold":r.#e=parseFloat(a)||0;break;case"current":r.#t=parseFloat(a)||0;break;case"variant-id":r.#a=a;break;case"promo-ended":r.#i=null!==a;break;case"product-available":r.#n=r.#v(a,!0);break;case"message-above":r.#p=a;break;case"message-below":r.#g=a;break;case"money-format":r.#b=a}r.isConnected&&(r.#f(),r.#C(),r.#I())}}#y(){this.classList.add("gift-with-purchase"),this.#E()}#E(){this.#I()}#S(e){if(!this.#b)return e.toFixed(2).replace(/\.00$/,"");const t=e.toFixed(2),a=Math.round(e).toString(),r=t.replace(".",","),s=a.replace(/\B(?=(\d{3})+(?!\d))/g,",");return this.#b.replace(/\{\{\s*amount_no_decimals_with_comma_separator\s*\}\}/g,s).replace(/\{\{\s*amount_with_comma_separator\s*\}\}/g,r).replace(/\{\{\s*amount_no_decimals\s*\}\}/g,a).replace(/\{\{\s*amount\s*\}\}/g,t)}#T(){const e=parseFloat(window.Shopify?.currency?.rate)||1;return this.#e*e}#v(e,t=!0){if(null==e)return t;if(""===e)return!0;const a=String(e).trim().toLowerCase();return"false"!==a&&"0"!==a&&("true"===a||"1"===a||t)}#I(){const e=this,t=e.querySelector("[data-content-gwp-message]");if(!t)return;let a="";if(e.#r&&e.#p)a=e.#p;else if(!e.#r&&e.#g){const t=e.#T()-e.#t,r=e.#S(t);a=e.#g.replace(/\[\s*amount\s*\]/g,r).replace(/\[amount\]/g,r)}t.textContent=a,t.style.display=a?"block":"none"}#w(){const e=this;e.#d=e.closest("cart-panel"),e.#d?e.#d.addEventListener("cart-panel:data-changed",e.#l.cartDataChange):e.#u=setTimeout(()=>{e.#u=null,e.#d=e.closest("cart-panel"),e.#d?e.#d.addEventListener("cart-panel:data-changed",e.#l.cartDataChange):console.error("GWP - cart-panel still not found after delay")},100)}#A(e){const t=this,a=e.detail;a&&void 0!==a.calculated_subtotal&&(t.#c&&clearTimeout(t.#c),t.#c=setTimeout(()=>{t.#c=null,t.#t=parseFloat(a.calculated_subtotal/100)||0,t.#_(a),t.#M(a)},300))}#_(e){const t=this.#k(e,!0);this.#s=t.length>0}#k(e,t=!0){const a=this;return e?.items?e.items.filter(e=>"true"===e.properties?._gwp_item&&(!t||!!a.#a&&e.variant_id?.toString()===a.#a.toString())):[]}#P(){const e=this;if(e.#m){const t=e.#m;e.#m=null,e.#_(t),e.#M(t)}}#M(e){const t=this;if(t.#h)return void(t.#m=e);const a=t.#r,r=t.#T();t.#o=t.#i||!t.#n,t.#r=t.#t>=r&&!t.#o,t.#o?t.#F(e):t.#r&&!a&&!t.#s&&t.#a?t.#G():!t.#r&&t.#s&&t.#a&&t.#D(e),t.#C(),t.#I()}#C(){const e=this;return e.#i?(e.setAttribute("state","ended"),void(e.style.display="none")):e.#n?(e.style.display="",void(e.#s?e.setAttribute("state","added"):e.#r?e.setAttribute("state","active"):e.setAttribute("state","inactive"))):(e.setAttribute("state","disabled"),void(e.style.display="none"))}async#G(){const e=this;e.#h=!0;try{const t=await fetch("/cart/add.js",{method:"POST",credentials:"same-origin",headers:{"Content-Type":"application/json","X-Requested-With":"XMLHttpRequest"},body:JSON.stringify({items:[{id:e.#a,quantity:1,properties:{_gwp_item:"true",_hide_in_cart:"true",_ignore_price_in_subtotal:"true"}}]})});if(!t.ok)throw new Error(`http ${t.status}`);await t.json(),e.#s=!0,e.dispatchEvent(new CustomEvent("gwp:added",{detail:{variantId:e.#a},bubbles:!0})),e.#d?.getCartAndRefresh()}catch(t){console.error("giftwithpurchase: add error",t),e.dispatchEvent(new CustomEvent("gwp:error",{detail:{action:"add",error:t.message},bubbles:!0}))}finally{e.#h=!1,e.#P()}}async#D(e){const t=this;if(!e?.items)return;const a=t.#k(e,!0);if(a.length){t.#h=!0;try{await t.#L(a)}finally{t.#h=!1,t.#P()}}else t.#s=!1}async#F(e){const t=this;if(!e?.items)return;const a=t.#k(e,!1);if(a.length){t.#h=!0;try{await t.#L(a)}finally{t.#h=!1,t.#P()}}else t.#s=!1}async#L(e){const t=this;try{await Promise.all(e.map(async e=>{const t=await fetch("/cart/change.js",{method:"POST",credentials:"same-origin",headers:{"Content-Type":"application/json","X-Requested-With":"XMLHttpRequest"},body:JSON.stringify({id:e.key,quantity:0})});if(!t.ok)throw new Error(`http ${t.status}`)})),t.#s=!1,t.dispatchEvent(new CustomEvent("gwp:removed",{detail:{variantId:t.#a},bubbles:!0})),t.#d?.getCartAndRefresh()}catch(e){console.error("giftwithpurchase: bulk remove error",e),t.dispatchEvent(new CustomEvent("gwp:error",{detail:{action:"remove",error:e.message},bubbles:!0}))}}getState(){const e=this,t=e.#T();return{currentAmount:e.#t,threshold:e.#e,convertedThreshold:t,variantId:e.#a,isActive:e.#r,isAdded:e.#s,promoEnded:e.#i,productAvailable:e.#n,isDisabled:e.#o,remainingAmount:Math.max(0,t-e.#t),currencyRate:parseFloat(window.Shopify?.currency?.rate)||1}}get currentAmount(){return this.#t}get threshold(){return this.#e}get variantId(){return this.#a}get isActive(){return this.#r}get isAdded(){return this.#s}get promoEnded(){return this.#i}get productAvailable(){return this.#n}get isDisabled(){return this.#o}setCurrentAmount(e){const t=this;t.#t=parseFloat(e)||0,t.#M({items:[]}),t.#I()}setThreshold(e){const t=this;t.#e=parseFloat(e)||0,t.#M({items:[]}),t.#I()}setVariantId(e){this.#a=e}}customElements.get("gift-with-purchase")||customElements.define("gift-with-purchase",t),e.GiftWithPurchase=t});
|
|
2
2
|
//# sourceMappingURL=gift-with-purchase.min.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gift-with-purchase.min.js","sources":["../src/gift-with-purchase.js"],"sourcesContent":["import './gift-with-purchase.scss';\n\n/**\n * Gift With Purchase Component - automatically adds/removes gift when cart threshold is met\n * Emits gwp:added/gwp:removed/gwp:error events and broadcasts cart updates\n */\nclass GiftWithPurchase extends HTMLElement {\n\t// private fields\n\t#threshold = 0;\n\t#currentAmount = 0;\n\t#variantId = null;\n\t#isActive = false;\n\t#isAdded = false;\n\t#promoEnded = false;\n\t#cartDialog = null;\n\t#boundHandleCartDataChange = null; // pre-bound listener ref for clean-up\n\t#debounceTimer = null; // debouncing cart updates\n\t#messageAbove = null; // message when threshold is met\n\t#messageBelow = null; // message when below threshold\n\n\tstatic get observedAttributes() {\n\t\treturn ['threshold', 'current', 'variant-id', 'promo-ended', 'message-above', 'message-below'];\n\t}\n\n\tconstructor() {\n\t\tsuper();\n\t\t// read initial attributes once\n\t\tthis.#threshold = parseFloat(this.getAttribute('threshold')) || 0;\n\t\tthis.#currentAmount = parseFloat(this.getAttribute('current')) || 0;\n\t\tthis.#variantId = this.getAttribute('variant-id');\n\t\tthis.#promoEnded = this.hasAttribute('promo-ended');\n\t\tthis.#messageAbove = this.getAttribute('message-above');\n\t\tthis.#messageBelow = this.getAttribute('message-below');\n\t\tthis.#boundHandleCartDataChange = this.#handleCartDataChange.bind(this);\n\t}\n\n\tconnectedCallback() {\n\t\tthis.#render();\n\t\tthis.#attachListeners();\n\t}\n\n\tdisconnectedCallback() {\n\t\tif (this.#debounceTimer) clearTimeout(this.#debounceTimer);\n\t\tif (this.#cartDialog)\n\t\t\tthis.#cartDialog.removeEventListener(\n\t\t\t\t'cart-dialog:data-changed',\n\t\t\t\tthis.#boundHandleCartDataChange\n\t\t\t);\n\t}\n\n\t#render() {\n\t\tthis.classList.add('gift-with-purchase');\n\t\tthis.#renderMessages();\n\t}\n\n\t#renderMessages() {\n\t\t// Look for existing message element with data-content-gwp-message\n\t\tthis.#updateMessages();\n\t}\n\n\t#updateMessages() {\n\t\tconst messageEl = this.querySelector('[data-content-gwp-message]');\n\t\tif (!messageEl) return;\n\n\t\tlet message = '';\n\n\t\t// console.log('updateMessages - this.#isActive', this.#isActive);\n\n\t\tif (this.#isActive && this.#messageAbove) {\n\t\t\t// set message to above threshold message\n\t\t\tmessage = this.#messageAbove;\n\t\t} else if (!this.#isActive && this.#messageBelow) {\n\t\t\t// set message to below threshold message\n\t\t\tconst remaining = this.#threshold - this.#currentAmount;\n\t\t\tconst formattedAmount = remaining.toFixed(2).replace(/\\.00$/, '');\n\t\t\tmessage = this.#messageBelow\n\t\t\t\t.replace(/\\{\\s*amount\\s*\\}/g, formattedAmount)\n\t\t\t\t.replace(/\\{amount\\}/g, formattedAmount);\n\t\t}\n\n\t\tmessageEl.textContent = message;\n\t\tmessageEl.style.display = message ? 'block' : 'none';\n\t}\n\n\t#attachListeners() {\n\t\t// Look for cart-dialog element when attaching listeners (more reliable timing)\n\t\tthis.#cartDialog = this.closest('cart-dialog');\n\n\t\tif (this.#cartDialog) {\n\t\t\t// console.log('cartDialog exists and is attaching events');\n\t\t\tthis.#cartDialog.addEventListener(\n\t\t\t\t'cart-dialog:data-changed',\n\t\t\t\tthis.#boundHandleCartDataChange\n\t\t\t);\n\t\t} else {\n\t\t\t// Try again after a short delay in case the DOM isn't fully ready\n\t\t\tsetTimeout(() => {\n\t\t\t\t// console.log('cartDialog DIDNT exist and we waited to attach events');\n\t\t\t\tthis.#cartDialog = this.closest('cart-dialog');\n\t\t\t\tif (this.#cartDialog) {\n\t\t\t\t\tthis.#cartDialog.addEventListener(\n\t\t\t\t\t\t'cart-dialog:data-changed',\n\t\t\t\t\t\tthis.#boundHandleCartDataChange\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconsole.error('GWP - cart-dialog still not found after delay');\n\t\t\t\t}\n\t\t\t}, 100);\n\t\t}\n\t}\n\n\t#handleCartDataChange(event) {\n\t\tconst cart = event.detail;\n\t\t// console.log('GWP - handleCartDataChange cart: ', cart.calculated_subtotal, cart);\n\n\t\tif (!cart || typeof cart.calculated_subtotal === 'undefined') return;\n\t\tif (this.#debounceTimer) clearTimeout(this.#debounceTimer);\n\n\t\tthis.#debounceTimer = setTimeout(() => {\n\t\t\tthis.#debounceTimer = null;\n\t\t\tthis.#currentAmount = parseFloat(cart.calculated_subtotal / 100) || 0;\n\t\t\tthis.#checkGiftInCart(cart);\n\t\t\tthis.#updateState(cart);\n\t\t}, 300);\n\t}\n\n\t// checks to see if the gift is already in the cart\n\t#checkGiftInCart(cart) {\n\t\tif (!cart.items || !this.#variantId) {\n\t\t\tthis.#isAdded = false;\n\t\t\treturn;\n\t\t}\n\t\tconst giftLines = cart.items.filter(\n\t\t\t(lineItem) =>\n\t\t\t\tlineItem.variant_id.toString() === this.#variantId.toString() &&\n\t\t\t\tlineItem.properties?._gwp_item === 'true'\n\t\t);\n\t\tthis.#isAdded = giftLines.length > 0;\n\t\tif (this.#promoEnded && giftLines.length) this.#removeAllGiftItems(giftLines);\n\t}\n\n\t#updateState(cart) {\n\t\t// console.log('********** ---------- Updating state....');\n\t\tconst wasActive = this.#isActive;\n\t\tthis.#isActive = this.#currentAmount >= this.#threshold && !this.#promoEnded;\n\n\t\t// console.log('********** ---------- this.#isActive', this.#isActive);\n\n\t\tif (this.#promoEnded) {\n\t\t\t// remove GWP from cart\n\t\t\tthis.#removeGiftFromCart(cart);\n\t\t}\n\n\t\tif (this.#isActive && !wasActive && !this.#isAdded && this.#variantId) {\n\t\t\tthis.#addGiftToCart();\n\t\t} else if (!this.#isActive && wasActive && this.#isAdded && this.#variantId) {\n\t\t\tthis.#removeGiftFromCart(cart);\n\t\t}\n\n\t\tthis.#updateVisualState();\n\t\tthis.#updateMessages();\n\t}\n\n\t#updateVisualState() {\n\t\tif (this.#promoEnded) {\n\t\t\tthis.setAttribute('state', 'ended');\n\t\t\tthis.style.display = 'none';\n\t\t\treturn;\n\t\t}\n\n\t\tthis.style.display = '';\n\t\tif (this.#isAdded) {\n\t\t\tthis.setAttribute('state', 'added');\n\t\t} else if (this.#isActive) {\n\t\t\tthis.setAttribute('state', 'active');\n\t\t}\n\t\t// Note: no 'inactive' state since component wouldn't be loaded if inactive\n\t}\n\n\tasync #addGiftToCart() {\n\t\ttry {\n\t\t\tconst res = await fetch('/cart/add.js', {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tcredentials: 'same-origin',\n\t\t\t\theaders: {\n\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t'X-Requested-With': 'XMLHttpRequest',\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\titems: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tid: this.#variantId,\n\t\t\t\t\t\t\tquantity: 1,\n\t\t\t\t\t\t\tproperties: {\n\t\t\t\t\t\t\t\t_gwp_item: 'true',\n\t\t\t\t\t\t\t\t_hide_in_cart: 'true',\n\t\t\t\t\t\t\t\t_ignore_price_in_subtotal: 'true',\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\t\t});\n\t\t\tif (!res.ok) throw new Error(`http ${res.status}`);\n\t\t\tawait res.json();\n\t\t\tthis.#isAdded = true;\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:added', {\n\t\t\t\t\tdetail: { variantId: this.#variantId },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: add error', err);\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:error', {\n\t\t\t\t\tdetail: { action: 'add', error: err.message },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tasync #removeGiftFromCart(cart) {\n\t\ttry {\n\t\t\t// get all GWP items in the cart\n\t\t\tconst giftLines = cart.items.filter(\n\t\t\t\t(lineItem) =>\n\t\t\t\t\tlineItem.variant_id.toString() === this.#variantId.toString() &&\n\t\t\t\t\tlineItem.properties?._gwp_item === 'true'\n\t\t\t);\n\n\t\t\t// exit if no items in the cart\n\t\t\tif (!giftLines.length) {\n\t\t\t\tthis.#isAdded = false;\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// remove all GWP items from the cart\n\t\t\tawait this.#removeAllGiftItems(giftLines);\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: remove error', err);\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:error', {\n\t\t\t\t\tdetail: { action: 'remove', error: err.message },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tasync #removeAllGiftItems(giftLines) {\n\t\ttry {\n\t\t\tawait Promise.all(\n\t\t\t\tgiftLines.map((giftItem) =>\n\t\t\t\t\tfetch('/cart/change.js', {\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\tcredentials: 'same-origin',\n\t\t\t\t\t\theaders: {\n\t\t\t\t\t\t\t'Content-Type': 'application/json',\n\t\t\t\t\t\t\t'X-Requested-With': 'XMLHttpRequest',\n\t\t\t\t\t\t},\n\t\t\t\t\t\tbody: JSON.stringify({ id: giftItem.key, quantity: 0 }),\n\t\t\t\t\t})\n\t\t\t\t)\n\t\t\t);\n\t\t\tthis.#isAdded = false;\n\t\t\t// note: you can broadcast cart again here if you want real-time re-render after remove\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:removed', {\n\t\t\t\t\tdetail: { variantId: this.#variantId },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: bulk remove error', err);\n\t\t\tthis.dispatchEvent(\n\t\t\t\tnew CustomEvent('gwp:error', {\n\t\t\t\t\tdetail: { action: 'remove', error: err.message },\n\t\t\t\t\tbubbles: true,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tgetState() {\n\t\treturn {\n\t\t\tcurrentAmount: this.#currentAmount,\n\t\t\tthreshold: this.#threshold,\n\t\t\tvariantId: this.#variantId,\n\t\t\tisActive: this.#isActive,\n\t\t\tisAdded: this.#isAdded,\n\t\t\tpromoEnded: this.#promoEnded,\n\t\t\tremainingAmount: Math.max(0, this.#threshold - this.#currentAmount),\n\t\t};\n\t}\n\n\tget currentAmount() {\n\t\treturn this.#currentAmount;\n\t}\n\tget threshold() {\n\t\treturn this.#threshold;\n\t}\n\tget variantId() {\n\t\treturn this.#variantId;\n\t}\n\tget isActive() {\n\t\treturn this.#isActive;\n\t}\n\tget isAdded() {\n\t\treturn this.#isAdded;\n\t}\n\tget promoEnded() {\n\t\treturn this.#promoEnded;\n\t}\n\n\t// Public setter methods for programmatic control\n\tsetCurrentAmount(amount) {\n\t\tthis.#currentAmount = parseFloat(amount) || 0;\n\t\tthis.#updateState({ items: [] }); // Pass empty cart to avoid cart operations\n\t\tthis.#updateMessages();\n\t}\n\n\tsetThreshold(threshold) {\n\t\tthis.#threshold = parseFloat(threshold) || 0;\n\t\tthis.#updateState({ items: [] }); // Pass empty cart to avoid cart operations\n\t\tthis.#updateMessages();\n\t}\n\n\tsetVariantId(variantId) {\n\t\tthis.#variantId = variantId;\n\t}\n}\n\nif (!customElements.get('gift-with-purchase')) {\n\tcustomElements.define('gift-with-purchase', GiftWithPurchase);\n}\n\nexport { GiftWithPurchase };\n"],"names":["GiftWithPurchase","HTMLElement","threshold","currentAmount","variantId","isActive","isAdded","promoEnded","cartDialog","boundHandleCartDataChange","debounceTimer","messageAbove","messageBelow","observedAttributes","constructor","super","this","parseFloat","getAttribute","hasAttribute","handleCartDataChange","bind","connectedCallback","render","attachListeners","disconnectedCallback","clearTimeout","removeEventListener","classList","add","renderMessages","updateMessages","messageEl","querySelector","message","formattedAmount","toFixed","replace","textContent","style","display","closest","addEventListener","setTimeout","console","error","event","cart","detail","calculated_subtotal","checkGiftInCart","updateState","items","giftLines","filter","lineItem","variant_id","toString","properties","_gwp_item","length","removeAllGiftItems","wasActive","removeGiftFromCart","addGiftToCart","updateVisualState","setAttribute","res","fetch","method","credentials","headers","body","JSON","stringify","id","quantity","_hide_in_cart","_ignore_price_in_subtotal","ok","Error","status","json","dispatchEvent","CustomEvent","bubbles","err","action","Promise","all","map","giftItem","key","getState","remainingAmount","Math","max","setCurrentAmount","amount","setThreshold","setVariantId","customElements","get","define"],"mappings":"uPAMA,MAAMA,UAAyBC,YAE9BC,GAAa,EACbC,GAAiB,EACjBC,GAAa,KACbC,IAAY,EACZC,IAAW,EACXC,IAAc,EACdC,GAAc,KACdC,GAA6B,KAC7BC,GAAiB,KACjBC,GAAgB,KAChBC,GAAgB,KAEhB,6BAAWC,GACV,MAAO,CAAC,YAAa,UAAW,aAAc,cAAe,gBAAiB,gBAC9E,CAED,WAAAC,GACCC,QAEAC,MAAKd,EAAae,WAAWD,KAAKE,aAAa,eAAiB,EAChEF,MAAKb,EAAiBc,WAAWD,KAAKE,aAAa,aAAe,EAClEF,MAAKZ,EAAaY,KAAKE,aAAa,cACpCF,MAAKT,EAAcS,KAAKG,aAAa,eACrCH,MAAKL,EAAgBK,KAAKE,aAAa,iBACvCF,MAAKJ,EAAgBI,KAAKE,aAAa,iBACvCF,MAAKP,EAA6BO,MAAKI,EAAsBC,KAAKL,KAClE,CAED,iBAAAM,GACCN,MAAKO,IACLP,MAAKQ,GACL,CAED,oBAAAC,GACKT,MAAKN,GAAgBgB,aAAaV,MAAKN,GACvCM,MAAKR,GACRQ,MAAKR,EAAYmB,oBAChB,2BACAX,MAAKP,EAEP,CAED,EAAAc,GACCP,KAAKY,UAAUC,IAAI,sBACnBb,MAAKc,GACL,CAED,EAAAA,GAECd,MAAKe,GACL,CAED,EAAAA,GACC,MAAMC,EAAYhB,KAAKiB,cAAc,8BACrC,IAAKD,EAAW,OAEhB,IAAIE,EAAU,GAId,GAAIlB,MAAKX,GAAaW,MAAKL,EAE1BuB,EAAUlB,MAAKL,OACT,IAAKK,MAAKX,GAAaW,MAAKJ,EAAe,CAEjD,MACMuB,GADYnB,MAAKd,EAAac,MAAKb,GACPiC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oE,CAAajG,GACZc,MAAKd,EAAae,WAAWf,IAAc,EAC3Cc,MAAKmC,EAAa,CAAEC,MAAO,KAC3BpC,MAAKe,GACL,CAED,YAAAqE,CAAahG,GACZY,MAAKZ,EAAaA,CAClB,EAGGiG,eAAeC,IAAI,uBACvBD,eAAeE,OAAO,qBAAsBvG"}
|
|
1
|
+
{"version":3,"file":"gift-with-purchase.min.js","sources":["../src/gift-with-purchase.js"],"sourcesContent":["import './gift-with-purchase.scss';\n\n/**\n * Gift With Purchase Component - automatically adds/removes gift when cart threshold is met\n * Emits gwp:added/gwp:removed/gwp:error events and broadcasts cart updates\n */\nclass GiftWithPurchase extends HTMLElement {\n\t#threshold = 0;\n\t#currentAmount = 0;\n\t#variantId = null;\n\t#isActive = false;\n\t#isAdded = false;\n\t#promoEnded = false;\n\t#productAvailable = true;\n\t#isDisabled = false;\n\t#cartPanel = null;\n\t#handlers = {};\n\t#debounceTimer = null;\n\t#attachRetryTimer = null;\n\t#isMutating = false; // prevents overlapping cart mutations\n\t#pendingCart = null; // stores cart snapshot during mutation for recheck\n\t#messageAbove = null;\n\t#messageBelow = null;\n\t#moneyFormat = null;\n\n\tstatic get observedAttributes() {\n\t\treturn [\n\t\t\t'threshold',\n\t\t\t'current',\n\t\t\t'variant-id',\n\t\t\t'promo-ended',\n\t\t\t'product-available',\n\t\t\t'message-above',\n\t\t\t'message-below',\n\t\t\t'money-format',\n\t\t];\n\t}\n\n\tconstructor() {\n\t\tsuper();\n\t\tconst _ = this;\n\t\t_.#threshold = parseFloat(_.getAttribute('threshold')) || 0;\n\t\t_.#currentAmount = parseFloat(_.getAttribute('current')) || 0;\n\t\t_.#variantId = _.getAttribute('variant-id');\n\t\t_.#promoEnded = _.hasAttribute('promo-ended');\n\t\t_.#productAvailable = _.#parseBooleanValue(_.getAttribute('product-available'), true);\n\t\t_.#messageAbove = _.getAttribute('message-above');\n\t\t_.#messageBelow = _.getAttribute('message-below');\n\t\t_.#moneyFormat = _.getAttribute('money-format');\n\t\t_.#handlers = { cartDataChange: _.#handleCartDataChange.bind(_) };\n\t}\n\n\tconnectedCallback() {\n\t\tconst _ = this;\n\n\t\t_.#calculateInitialState();\n\t\t_.#render();\n\t\t_.#updateVisualState();\n\t\t_.#attachListeners();\n\t}\n\n\t#calculateInitialState() {\n\t\tconst _ = this;\n\t\t// Calculate initial active state based on attributes (before any cart events)\n\t\tconst convertedThreshold = _.#getConvertedThreshold();\n\t\t_.#isDisabled = _.#promoEnded || !_.#productAvailable;\n\t\t_.#isActive = _.#currentAmount >= convertedThreshold && !_.#isDisabled;\n\t}\n\n\tdisconnectedCallback() {\n\t\tconst _ = this;\n\t\tif (_.#debounceTimer) clearTimeout(_.#debounceTimer);\n\t\tif (_.#attachRetryTimer) clearTimeout(_.#attachRetryTimer);\n\t\tif (_.#cartPanel) _.#cartPanel.removeEventListener('cart-panel:data-changed', _.#handlers.cartDataChange);\n\t}\n\n\tattributeChangedCallback(name, oldValue, newValue) {\n\t\tconst _ = this;\n\t\tif (oldValue === newValue) return;\n\n\t\tswitch (name) {\n\t\t\tcase 'threshold':\n\t\t\t\t_.#threshold = parseFloat(newValue) || 0;\n\t\t\t\tbreak;\n\t\t\tcase 'current':\n\t\t\t\t_.#currentAmount = parseFloat(newValue) || 0;\n\t\t\t\tbreak;\n\t\t\tcase 'variant-id':\n\t\t\t\t_.#variantId = newValue;\n\t\t\t\tbreak;\n\t\t\tcase 'promo-ended':\n\t\t\t\t_.#promoEnded = newValue !== null;\n\t\t\t\tbreak;\n\t\t\tcase 'product-available':\n\t\t\t\t_.#productAvailable = _.#parseBooleanValue(newValue, true);\n\t\t\t\tbreak;\n\t\t\tcase 'message-above':\n\t\t\t\t_.#messageAbove = newValue;\n\t\t\t\tbreak;\n\t\t\tcase 'message-below':\n\t\t\t\t_.#messageBelow = newValue;\n\t\t\t\tbreak;\n\t\t\tcase 'money-format':\n\t\t\t\t_.#moneyFormat = newValue;\n\t\t\t\tbreak;\n\t\t}\n\n\t\t// Recalculate state and update UI if component is connected\n\t\tif (_.isConnected) {\n\t\t\t_.#calculateInitialState();\n\t\t\t_.#updateVisualState();\n\t\t\t_.#updateMessages();\n\t\t}\n\t}\n\n\t#render() {\n\t\tthis.classList.add('gift-with-purchase');\n\t\tthis.#renderMessages();\n\t}\n\n\t#renderMessages() {\n\t\t// Look for existing message element with data-content-gwp-message\n\t\tthis.#updateMessages();\n\t}\n\n\t#formatMoney(amount) {\n\t\tif (!this.#moneyFormat) return amount.toFixed(2).replace(/\\.00$/, '');\n\n\t\tconst amountFixed = amount.toFixed(2);\n\t\tconst amountNoDecimals = Math.round(amount).toString();\n\t\tconst amountWithComma = amountFixed.replace('.', ',');\n\t\tconst amountNoDecimalsWithComma = amountNoDecimals.replace(/\\B(?=(\\d{3})+(?!\\d))/g, ',');\n\n\t\treturn this.#moneyFormat\n\t\t\t.replace(/\\{\\{\\s*amount_no_decimals_with_comma_separator\\s*\\}\\}/g, amountNoDecimalsWithComma)\n\t\t\t.replace(/\\{\\{\\s*amount_with_comma_separator\\s*\\}\\}/g, amountWithComma)\n\t\t\t.replace(/\\{\\{\\s*amount_no_decimals\\s*\\}\\}/g, amountNoDecimals)\n\t\t\t.replace(/\\{\\{\\s*amount\\s*\\}\\}/g, amountFixed);\n\t}\n\n\t#getConvertedThreshold() {\n\t\t// Convert threshold using Shopify currency rate if available (for multi-currency stores)\n\t\tconst rate = parseFloat(window.Shopify?.currency?.rate) || 1;\n\t\treturn this.#threshold * rate;\n\t}\n\n\t#parseBooleanValue(value, defaultValue = true) {\n\t\tif (value === null || typeof value === 'undefined') return defaultValue;\n\t\tif (value === '') return true;\n\t\tconst normalized = String(value).trim().toLowerCase();\n\t\tif (normalized === 'false' || normalized === '0') return false;\n\t\tif (normalized === 'true' || normalized === '1') return true;\n\t\treturn defaultValue;\n\t}\n\n\t#updateMessages() {\n\t\tconst _ = this;\n\t\tconst messageEl = _.querySelector('[data-content-gwp-message]');\n\t\tif (!messageEl) return;\n\n\t\tlet message = '';\n\t\tif (_.#isActive && _.#messageAbove) {\n\t\t\tmessage = _.#messageAbove;\n\t\t} else if (!_.#isActive && _.#messageBelow) {\n\t\t\tconst remaining = _.#getConvertedThreshold() - _.#currentAmount;\n\t\t\tconst formattedAmount = _.#formatMoney(remaining);\n\t\t\tmessage = _.#messageBelow\n\t\t\t\t.replace(/\\[\\s*amount\\s*\\]/g, formattedAmount)\n\t\t\t\t.replace(/\\[amount\\]/g, formattedAmount);\n\t\t}\n\n\t\tmessageEl.textContent = message;\n\t\tmessageEl.style.display = message ? 'block' : 'none';\n\t}\n\n\t#attachListeners() {\n\t\tconst _ = this;\n\t\t_.#cartPanel = _.closest('cart-panel');\n\n\t\tif (_.#cartPanel) {\n\t\t\t_.#cartPanel.addEventListener('cart-panel:data-changed', _.#handlers.cartDataChange);\n\t\t} else {\n\t\t\t_.#attachRetryTimer = setTimeout(() => {\n\t\t\t\t_.#attachRetryTimer = null;\n\t\t\t\t_.#cartPanel = _.closest('cart-panel');\n\t\t\t\tif (_.#cartPanel) _.#cartPanel.addEventListener('cart-panel:data-changed', _.#handlers.cartDataChange);\n\t\t\t\telse console.error('GWP - cart-panel still not found after delay');\n\t\t\t}, 100);\n\t\t}\n\t}\n\n\t#handleCartDataChange(event) {\n\t\tconst _ = this;\n\t\tconst cart = event.detail;\n\t\tif (!cart || typeof cart.calculated_subtotal === 'undefined') return;\n\t\tif (_.#debounceTimer) clearTimeout(_.#debounceTimer);\n\n\t\t_.#debounceTimer = setTimeout(() => {\n\t\t\t_.#debounceTimer = null;\n\t\t\t_.#currentAmount = parseFloat(cart.calculated_subtotal / 100) || 0;\n\t\t\t_.#checkGiftInCart(cart);\n\t\t\t_.#updateState(cart);\n\t\t}, 300);\n\t}\n\n\t#checkGiftInCart(cart) {\n\t\tconst _ = this;\n\t\tconst giftLines = _.#getGiftLines(cart, true);\n\t\t_.#isAdded = giftLines.length > 0;\n\t}\n\n\t#getGiftLines(cart, matchVariantId = true) {\n\t\tconst _ = this;\n\t\tif (!cart?.items) return [];\n\t\treturn cart.items.filter((item) => {\n\t\t\tif (item.properties?._gwp_item !== 'true') return false;\n\t\t\tif (!matchVariantId) return true;\n\t\t\tif (!_.#variantId) return false;\n\t\t\treturn item.variant_id?.toString() === _.#variantId.toString();\n\t\t});\n\t}\n\n\t#recheckIfPending() {\n\t\tconst _ = this;\n\t\tif (_.#pendingCart) {\n\t\t\tconst cart = _.#pendingCart;\n\t\t\t_.#pendingCart = null;\n\t\t\t_.#checkGiftInCart(cart);\n\t\t\t_.#updateState(cart);\n\t\t}\n\t}\n\n\t#updateState(cart) {\n\t\tconst _ = this;\n\n\t\t// If mutation in progress, queue this cart for recheck later\n\t\tif (_.#isMutating) {\n\t\t\t_.#pendingCart = cart;\n\t\t\treturn;\n\t\t}\n\n\t\tconst wasActive = _.#isActive;\n\t\tconst convertedThreshold = _.#getConvertedThreshold();\n\t\t_.#isDisabled = _.#promoEnded || !_.#productAvailable;\n\t\t_.#isActive = _.#currentAmount >= convertedThreshold && !_.#isDisabled;\n\n\t\tif (_.#isDisabled) _.#removeAllGiftsFromCart(cart);\n\t\telse if (_.#isActive && !wasActive && !_.#isAdded && _.#variantId) _.#addGiftToCart();\n\t\telse if (!_.#isActive && _.#isAdded && _.#variantId) _.#removeGiftFromCart(cart);\n\n\t\t_.#updateVisualState();\n\t\t_.#updateMessages();\n\t}\n\n\t#updateVisualState() {\n\t\tconst _ = this;\n\t\tif (_.#promoEnded) {\n\t\t\t_.setAttribute('state', 'ended');\n\t\t\t_.style.display = 'none';\n\t\t\treturn;\n\t\t}\n\n\t\tif (!_.#productAvailable) {\n\t\t\t_.setAttribute('state', 'disabled');\n\t\t\t_.style.display = 'none';\n\t\t\treturn;\n\t\t}\n\n\t\t_.style.display = '';\n\t\tif (_.#isAdded) _.setAttribute('state', 'added');\n\t\telse if (_.#isActive) _.setAttribute('state', 'active');\n\t\telse _.setAttribute('state', 'inactive');\n\t}\n\n\tasync #addGiftToCart() {\n\t\tconst _ = this;\n\t\t_.#isMutating = true;\n\t\ttry {\n\t\t\tconst res = await fetch('/cart/add.js', {\n\t\t\t\tmethod: 'POST',\n\t\t\t\tcredentials: 'same-origin',\n\t\t\t\theaders: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' },\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\titems: [{ id: _.#variantId, quantity: 1, properties: { _gwp_item: 'true', _hide_in_cart: 'true', _ignore_price_in_subtotal: 'true' } }],\n\t\t\t\t}),\n\t\t\t});\n\t\t\tif (!res.ok) throw new Error(`http ${res.status}`);\n\t\t\tawait res.json();\n\t\t\t_.#isAdded = true;\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:added', { detail: { variantId: _.#variantId }, bubbles: true }));\n\t\t\t_.#cartPanel?.getCartAndRefresh();\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: add error', err);\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:error', { detail: { action: 'add', error: err.message }, bubbles: true }));\n\t\t} finally {\n\t\t\t_.#isMutating = false;\n\t\t\t_.#recheckIfPending();\n\t\t}\n\t}\n\n\tasync #removeGiftFromCart(cart) {\n\t\tconst _ = this;\n\t\tif (!cart?.items) return;\n\n\t\tconst giftLines = _.#getGiftLines(cart, true);\n\t\tif (!giftLines.length) {\n\t\t\t_.#isAdded = false;\n\t\t\treturn;\n\t\t}\n\n\t\t_.#isMutating = true;\n\t\ttry {\n\t\t\tawait _.#removeAllGiftItems(giftLines);\n\t\t} finally {\n\t\t\t_.#isMutating = false;\n\t\t\t_.#recheckIfPending();\n\t\t}\n\t}\n\n\tasync #removeAllGiftsFromCart(cart) {\n\t\tconst _ = this;\n\t\tif (!cart?.items) return;\n\n\t\tconst giftLines = _.#getGiftLines(cart, false);\n\t\tif (!giftLines.length) {\n\t\t\t_.#isAdded = false;\n\t\t\treturn;\n\t\t}\n\n\t\t_.#isMutating = true;\n\t\ttry {\n\t\t\tawait _.#removeAllGiftItems(giftLines);\n\t\t} finally {\n\t\t\t_.#isMutating = false;\n\t\t\t_.#recheckIfPending();\n\t\t}\n\t}\n\n\tasync #removeAllGiftItems(giftLines) {\n\t\tconst _ = this;\n\t\ttry {\n\t\t\tawait Promise.all(\n\t\t\t\tgiftLines.map(async (item) => {\n\t\t\t\t\tconst res = await fetch('/cart/change.js', {\n\t\t\t\t\t\tmethod: 'POST',\n\t\t\t\t\t\tcredentials: 'same-origin',\n\t\t\t\t\t\theaders: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' },\n\t\t\t\t\t\tbody: JSON.stringify({ id: item.key, quantity: 0 }),\n\t\t\t\t\t});\n\t\t\t\t\tif (!res.ok) throw new Error(`http ${res.status}`);\n\t\t\t\t})\n\t\t\t);\n\t\t\t_.#isAdded = false;\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:removed', { detail: { variantId: _.#variantId }, bubbles: true }));\n\t\t\t_.#cartPanel?.getCartAndRefresh();\n\t\t} catch (err) {\n\t\t\tconsole.error('giftwithpurchase: bulk remove error', err);\n\t\t\t_.dispatchEvent(new CustomEvent('gwp:error', { detail: { action: 'remove', error: err.message }, bubbles: true }));\n\t\t}\n\t}\n\n\tgetState() {\n\t\tconst _ = this;\n\t\tconst convertedThreshold = _.#getConvertedThreshold();\n\t\treturn {\n\t\t\tcurrentAmount: _.#currentAmount,\n\t\t\tthreshold: _.#threshold,\n\t\t\tconvertedThreshold,\n\t\t\tvariantId: _.#variantId,\n\t\t\tisActive: _.#isActive,\n\t\t\tisAdded: _.#isAdded,\n\t\t\tpromoEnded: _.#promoEnded,\n\t\t\tproductAvailable: _.#productAvailable,\n\t\t\tisDisabled: _.#isDisabled,\n\t\t\tremainingAmount: Math.max(0, convertedThreshold - _.#currentAmount),\n\t\t\tcurrencyRate: parseFloat(window.Shopify?.currency?.rate) || 1,\n\t\t};\n\t}\n\n\tget currentAmount() {\n\t\treturn this.#currentAmount;\n\t}\n\tget threshold() {\n\t\treturn this.#threshold;\n\t}\n\tget variantId() {\n\t\treturn this.#variantId;\n\t}\n\tget isActive() {\n\t\treturn this.#isActive;\n\t}\n\tget isAdded() {\n\t\treturn this.#isAdded;\n\t}\n\tget promoEnded() {\n\t\treturn this.#promoEnded;\n\t}\n\tget productAvailable() {\n\t\treturn this.#productAvailable;\n\t}\n\tget isDisabled() {\n\t\treturn this.#isDisabled;\n\t}\n\n\tsetCurrentAmount(amount) {\n\t\tconst _ = this;\n\t\t_.#currentAmount = parseFloat(amount) || 0;\n\t\t_.#updateState({ items: [] });\n\t\t_.#updateMessages();\n\t}\n\n\tsetThreshold(threshold) {\n\t\tconst _ = this;\n\t\t_.#threshold = parseFloat(threshold) || 0;\n\t\t_.#updateState({ items: [] });\n\t\t_.#updateMessages();\n\t}\n\n\tsetVariantId(variantId) {\n\t\tthis.#variantId = variantId;\n\t}\n}\n\nif (!customElements.get('gift-with-purchase')) {\n\tcustomElements.define('gift-with-purchase', GiftWithPurchase);\n}\n\nexport { GiftWithPurchase };\n"],"names":["GiftWithPurchase","HTMLElement","threshold","currentAmount","variantId","isActive","isAdded","promoEnded","productAvailable","isDisabled","cartPanel","handlers","debounceTimer","attachRetryTimer","isMutating","pendingCart","messageAbove","messageBelow","moneyFormat","observedAttributes","constructor","super","_","this","parseFloat","getAttribute","hasAttribute","parseBooleanValue","cartDataChange","handleCartDataChange","bind","connectedCallback","calculateInitialState","render","updateVisualState","attachListeners","convertedThreshold","getConvertedThreshold","disconnectedCallback","clearTimeout","removeEventListener","attributeChangedCallback","name","oldValue","newValue","isConnected","updateMessages","classList","add","renderMessages","formatMoney","amount","toFixed","replace","amountFixed","amountNoDecimals","Math","round","toString","amountWithComma","amountNoDecimalsWithComma","rate","window","Shopify","currency","value","defaultValue","normalized","String","trim","toLowerCase","messageEl","querySelector","message","remaining","formattedAmount","textContent","style","display","closest","addEventListener","setTimeout","console","error","event","cart","detail","calculated_subtotal","checkGiftInCart","updateState","giftLines","getGiftLines","length","matchVariantId","items","filter","item","properties","_gwp_item","variant_id","recheckIfPending","wasActive","removeAllGiftsFromCart","addGiftToCart","removeGiftFromCart","setAttribute","res","fetch","method","credentials","headers","body","JSON","stringify","id","quantity","_hide_in_cart","_ignore_price_in_subtotal","ok","Error","status","json","dispatchEvent","CustomEvent","bubbles","getCartAndRefresh","err","action","removeAllGiftItems","Promise","all","map","async","key","getState","remainingAmount","max","currencyRate","setCurrentAmount","setThreshold","setVariantId","customElements","get","define"],"mappings":"uPAMA,MAAMA,UAAyBC,YAC9BC,GAAa,EACbC,GAAiB,EACjBC,GAAa,KACbC,IAAY,EACZC,IAAW,EACXC,IAAc,EACdC,IAAoB,EACpBC,IAAc,EACdC,GAAa,KACbC,GAAY,CAAA,EACZC,GAAiB,KACjBC,GAAoB,KACpBC,IAAc,EACdC,GAAe,KACfC,GAAgB,KAChBC,GAAgB,KAChBC,GAAe,KAEf,6BAAWC,GACV,MAAO,CACN,YACA,UACA,aACA,cACA,oBACA,gBACA,gBACA,eAED,CAED,WAAAC,GACCC,QACA,MAAMC,EAAIC,KACVD,GAAEpB,EAAasB,WAAWF,EAAEG,aAAa,eAAiB,EAC1DH,GAAEnB,EAAiBqB,WAAWF,EAAEG,aAAa,aAAe,EAC5DH,GAAElB,EAAakB,EAAEG,aAAa,cAC9BH,GAAEf,EAAce,EAAEI,aAAa,eAC/BJ,GAAEd,EAAoBc,GAAEK,EAAmBL,EAAEG,aAAa,sBAAsB,GAChFH,GAAEN,EAAgBM,EAAEG,aAAa,iBACjCH,GAAEL,EAAgBK,EAAEG,aAAa,iBACjCH,GAAEJ,EAAeI,EAAEG,aAAa,gBAChCH,GAAEX,EAAY,CAAEiB,eAAgBN,GAAEO,EAAsBC,KAAKR,GAC7D,CAED,iBAAAS,GACC,MAAMT,EAAIC,KAEVD,GAAEU,IACFV,GAAEW,IACFX,GAAEY,IACFZ,GAAEa,GACF,CAED,EAAAH,GACC,MAAMV,EAAIC,KAEJa,EAAqBd,GAAEe,IAC7Bf,GAAEb,EAAca,GAAEf,IAAgBe,GAAEd,EAC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,GAAEd,EACpBC,WAAYa,GAAEb,EACd8H,gBAAiB/E,KAAKgF,IAAI,EAAGpG,EAAqBd,GAAEnB,GACpDsI,aAAcjH,WAAWsC,OAAOC,SAASC,UAAUH,OAAS,EAE7D,CAED,iBAAI1D,GACH,OAAOoB,MAAKpB,CACZ,CACD,aAAID,GACH,OAAOqB,MAAKrB,CACZ,CACD,aAAIE,GACH,OAAOmB,MAAKnB,CACZ,CACD,YAAIC,GACH,OAAOkB,MAAKlB,CACZ,CACD,WAAIC,GACH,OAAOiB,MAAKjB,CACZ,CACD,cAAIC,GACH,OAAOgB,MAAKhB,CACZ,CACD,oBAAIC,GACH,OAAOe,MAAKf,CACZ,CACD,cAAIC,GACH,OAAOc,MAAKd,CACZ,CAED,gBAAAiI,CAAiBvF,GAChB,MAAM7B,EAAIC,KACVD,GAAEnB,EAAiBqB,WAAW2B,IAAW,EACzC7B,GAAEmE,EAAa,CAAEK,MAAO,KACxBxE,GAAEwB,GACF,CAED,YAAA6F,CAAazI,GACZ,MAAMoB,EAAIC,KACVD,GAAEpB,EAAasB,WAAWtB,IAAc,EACxCoB,GAAEmE,EAAa,CAAEK,MAAO,KACxBxE,GAAEwB,GACF,CAED,YAAA8F,CAAaxI,GACZmB,MAAKnB,EAAaA,CAClB,EAGGyI,eAAeC,IAAI,uBACvBD,eAAeE,OAAO,qBAAsB/I"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@magic-spells/gift-with-purchase",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Gift with purchase web component for e-commerce threshold promotions.",
|
|
5
5
|
"author": "Cory Schulz",
|
|
6
6
|
"license": "MIT",
|
|
@@ -60,11 +60,11 @@
|
|
|
60
60
|
"not ie <= 11"
|
|
61
61
|
],
|
|
62
62
|
"devDependencies": {
|
|
63
|
-
"@eslint/js": "^
|
|
63
|
+
"@eslint/js": "^9.38.0",
|
|
64
64
|
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
65
65
|
"@rollup/plugin-terser": "^0.4.4",
|
|
66
|
-
"eslint": "^
|
|
67
|
-
"globals": "^
|
|
66
|
+
"eslint": "^9.38.0",
|
|
67
|
+
"globals": "^15.15.0",
|
|
68
68
|
"prettier": "^3.3.3",
|
|
69
69
|
"rollup": "^3.0.0",
|
|
70
70
|
"rollup-plugin-copy": "^3.5.0",
|