Home
/
synthetic-material-rugs
/
size-9x12
(function () {
'use strict';
let activeButton = null;
let resetTimer = null;
const addButtonSelector = [
'form[action*="/cart/add"] button[type="submit"]',
'form[action*="/cart/add"] input[type="submit"]',
'button[name="add"]',
'[name="add"]',
'.product-form__submit',
'.add_to_cart',
'.add-to-cart',
'.ajax-submit',
'[data-add-to-cart]'
].join(',');
function isCartAddUrl(url) {
if (!url) return false;
const value = String(url);
return (
value.indexOf('/cart/add') !== -1 ||
value.indexOf('cart/add.js') !== -1
);
}
function saveOriginalButton(button) {
if (!button || button.dataset.rhOriginalSaved === 'true') return;
button.dataset.rhOriginalSaved = 'true';
if (button.tagName === 'INPUT') {
button.dataset.rhOriginalValue = button.value || 'Add to cart';
} else {
button.dataset.rhOriginalHtml = button.innerHTML;
}
}
function setButtonDisplay(button, html, plainText) {
if (!button) return;
if (button.tagName === 'INPUT') {
button.value = plainText;
} else {
button.innerHTML = html;
}
}
function restoreButton(button) {
if (!button) return;
button.classList.remove('rh-cart-adding', 'rh-cart-added');
button.removeAttribute('aria-busy');
if (button.tagName === 'INPUT') {
if (button.dataset.rhOriginalValue) {
button.value = button.dataset.rhOriginalValue;
}
} else if (button.dataset.rhOriginalHtml) {
button.innerHTML = button.dataset.rhOriginalHtml;
}
if (activeButton === button) {
activeButton = null;
}
}
function showAdding(button) {
if (!button) return;
clearTimeout(resetTimer);
activeButton = button;
saveOriginalButton(button);
button.classList.remove('rh-cart-added');
button.classList.add('rh-cart-adding');
button.setAttribute('aria-busy', 'true');
setButtonDisplay(
button,
'Adding...',
'Adding...'
);
}
function animateCartIcon() {
const icon = document.querySelector([
'.header__icon--cart',
'.site-header__cart',
'.header-cart',
'.cart-container',
'[href="/cart"] svg',
'[href*="/cart"] .icon',
'[data-cart-icon]'
].join(','));
if (!icon) return;
icon.classList.remove('rh-cart-icon-bump');
void icon.offsetWidth;
icon.classList.add('rh-cart-icon-bump');
window.setTimeout(function () {
icon.classList.remove('rh-cart-icon-bump');
}, 600);
}
function showAdded() {
if (!activeButton) return;
const button = activeButton;
button.classList.remove('rh-cart-adding');
button.classList.add('rh-cart-added');
button.removeAttribute('aria-busy');
setButtonDisplay(
button,
'✓ Added!',
'✓ Added!'
);
animateCartIcon();
resetTimer = window.setTimeout(function () {
restoreButton(button);
}, 1600);
}
function showError() {
if (!activeButton) return;
const button = activeButton;
button.classList.remove('rh-cart-adding');
button.removeAttribute('aria-busy');
setButtonDisplay(
button,
'Try Again',
'Try Again'
);
resetTimer = window.setTimeout(function () {
restoreButton(button);
}, 1800);
}
/*
* Capture the exact button the customer clicked.
*/
document.addEventListener('click', function (event) {
const button = event.target.closest(addButtonSelector);
if (!button) return;
if (button.disabled) return;
if (button.getAttribute('aria-disabled') === 'true') return;
showAdding(button);
}, true);
/*
* Capture standard product-form submissions.
*/
document.addEventListener('submit', function (event) {
const form = event.target;
if (!form || !form.matches('form[action*="/cart/add"]')) return;
const button =
event.submitter ||
form.querySelector(addButtonSelector) ||
form.querySelector('button[type="submit"]');
if (button && activeButton !== button) {
showAdding(button);
}
}, true);
/*
* Watch modern Shopify themes using fetch().
*/
const originalFetch = window.fetch;
window.fetch = function () {
const requestUrl =
typeof arguments[0] === 'string'
? arguments[0]
: arguments[0] && arguments[0].url;
const isCartRequest = isCartAddUrl(requestUrl);
return originalFetch.apply(this, arguments)
.then(function (response) {
if (isCartRequest) {
if (response.ok) {
window.setTimeout(showAdded, 100);
} else {
showError();
}
}
return response;
})
.catch(function (error) {
if (isCartRequest) {
showError();
}
throw error;
});
};
/*
* Watch older themes using XMLHttpRequest.
*/
const originalXhrOpen = XMLHttpRequest.prototype.open;
const originalXhrSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function (method, url) {
this.rhIsCartAdd = isCartAddUrl(url);
return originalXhrOpen.apply(this, arguments);
};
XMLHttpRequest.prototype.send = function () {
if (this.rhIsCartAdd) {
this.addEventListener('load', function () {
if (this.status >= 200 && this.status < 300) {
window.setTimeout(showAdded, 100);
} else {
showError();
}
});
this.addEventListener('error', function () {
showError();
});
}
return originalXhrSend.apply(this, arguments);
};
})();