Search results for: 'pdq 002 pdq tpms anod 20038 20008'
`;
return loader;
}
function addAjaxLoader(button, loader) {
button.setAttribute('data-label', button.getAttribute('aria-label'));
button.removeAttribute('aria-label');
button.prepend(loader);
button.classList.add('relative', '[&>:not(.loader)]:invisible');
button.disabled = true;
}
function removeAjaxLoader(button, loader) {
button.setAttribute('aria-label', button.getAttribute('data-label'));
button.removeAttribute('data-label');
loader.remove();
button.classList.remove('[&>:not(.loader)]:invisible');
button.disabled = false;
}
window.setAjaxCart = ({
formSelectors = ['#product_addtocart_form', '.product_addtocart_form'],
extraDelay = 500 } = {}) => {
const forms = document.querySelectorAll(formSelectors.toString());
forms.forEach(form => {
form.addEventListener('submit', async (event) => {
event.preventDefault();
const formData = new FormData(form);
const button = form.querySelector('button')
? form.querySelector('button')
: document.getElementById('product-addtocart-button');
const loader = createAjaxLoader(button);
const postUrl = event.target.action;
let bodyUrl = new URLSearchParams(formData);
bodyUrl.append("uenc", hyva.getUenc());
addAjaxLoader(button, loader);
try {
const response = await fetch(postUrl, {
method: 'POST',
body: bodyUrl.toString(),
mode: "cors",
credentials: "include",
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
}
});
if (!response.ok) {
return form.submit();
}
if (response.redirected) {
return window.location.href = response.url;
}
window.dispatchEvent(new CustomEvent("reload-customer-section-data"));
const productFormData = {
productId: formData.get("product"),
options: {}
}
for (const formEntry of formData.entries()) {
if (!formEntry[0].startsWith('super_attribute')) continue;
const formEntryOption = { [formEntry[0].match(/\[(\d+)\]/)[1]]: formEntry[1] };
productFormData.options = { ...productFormData.options, ...formEntryOption };
}
window.dispatchEvent(new CustomEvent('toggle-ajax-cart', { detail: productFormData }));
} catch (err) {
console.warn(err);
window.dispatchEvent(new CustomEvent('product-addtocart-error'));
if (typeof window.dispatchMessages === "undefined") return;
window.dispatchMessages([{
text:'There\u0020was\u0020a\u0020problem\u0020adding\u0020your\u0020item\u0020to\u0020the\u0020cart.',
type: 'error'
}], 5000);
} finally {
setTimeout(() => removeAjaxLoader(button, loader), extraDelay);
}
});
});
}
window.setAjaxCart();
function initAjaxCartModal() {
return {
open: false,
cart: {},
totalCartAmount: 0,
cartItem: null,
showSku: true,
sortObjectByNumberKey(obj) {
let keys = Object.keys(obj).map(Number).sort((a, b) => a - b);
let sortedObj = {};
keys.forEach(key => sortedObj[key] = obj[key]);
return sortedObj;
},
getCurrentProductFromCart(productData) {
if (!this.cart.items) return;
this.cartItem = this.cart.items.filter((item) => {
if (item.product_id !== productData.productId) return;
if (item.product_type !== "configurable") {
return item;
}
let cartItemOptions = {};
for (const option of item.options) {
const filterOptions = { [option.option_id]: option.option_value };
cartItemOptions = { ...cartItemOptions, ...filterOptions }
}
const cartItemOptionsCompare = JSON.stringify(this.sortObjectByNumberKey(cartItemOptions));
const productDataOptionsCompare = JSON.stringify(this.sortObjectByNumberKey(productData.options));
if (cartItemOptionsCompare === productDataOptionsCompare) {
return item;
}
});
if (this.cartItem > 1) {
this.cartItem = this.cartItem.slice(0, 1);
}
this.cartItem = this.cartItem[0];
},
getData(data, productData) {
if (!data.cart) return;
this.cart = data.cart;
this.totalCartAmount = this.cart.summary_count;
this.getCurrentProductFromCart(productData);
},
getItemCountText() {
if (this.totalCartAmount > 1) {
return hyva.strf('(\u00250\u0020items)', this.totalCartAmount);
}
return hyva.strf('(\u00250\u0020item)', this.totalCartAmount);
},
openDialog() {
if (!this.cartItem) return;
this.$root.showModal();
this.open = true;
this.scrollLock(this.open);
},
closeDialog() {
this.$root.close();
this.open = false;
this.scrollLock(this.open);
},
scrollLock(use = true) {
document.body.style.overflow = use ? "hidden" : "";
},
onDialogBackdropClickHelper(event, target) {
const rect = target.getBoundingClientRect();
const isInDialog =
rect.top <= event.clientY &&
event.clientY <= rect.top + rect.height &&
rect.left <= event.clientX &&
event.clientX <= rect.left + rect.width;
return isInDialog;
},
dialogListeners: {
['@keydown.window.escape']() {
this.closeDialog();
},
['@click'](event) {
if (this.onDialogBackdropClickHelper(event, this.$root)) return;
this.closeDialog();
},
['@toggle-ajax-cart.window'](event) {
if (!event.detail) return;
const productData = event.detail;
window.addEventListener('private-content-loaded', (event) => {
this.getData(event.detail.data, productData);
if (!this.open) {
this.openDialog();
} else {
this.closeDialog();
}
}, { once: true });
}
}
}
}
-->