' +
'Loading your cart...' +
'
';
postAjax('szh_get_cart')
.then(function(response){
if(
response &&
response.success
){
applyCartData(
response.data
);
}else{
miniCartBody.innerHTML =
'' +
'Unable to load cart.' +
'
';
}
})
.catch(function(){
miniCartBody.innerHTML =
'' +
'Unable to load cart.' +
'
';
});
}
/* Load count/subtotal on first page load */
postAjax('szh_get_cart')
.then(function(response){
if(
response &&
response.success
){
applyCartData(
response.data
);
}
})
.catch(function(){});
/* ---------------------------------------------------------
REMOVE FROM MINI CART
--------------------------------------------------------- */
miniCartBody.addEventListener(
'click',
function(event){
const remove =
event.target.closest(
'.remove_from_cart_button'
);
if(!remove){
return;
}
const cartKey =
remove.getAttribute(
'data-cart_item_key'
);
if(!cartKey){
return;
}
event.preventDefault();
remove.style.opacity = '.4';
remove.style.pointerEvents = 'none';
postAjax(
'szh_remove_cart_item',
{
cart_item_key:cartKey
}
)
.then(function(response){
if(
response &&
response.success
){
applyCartData(
response.data
);
if(
window.jQuery
){
window.jQuery(
document.body
).trigger(
'wc_fragment_refresh'
);
}
}else{
loadCart();
}
})
.catch(function(){
loadCart();
});
}
);
/* ---------------------------------------------------------
WOOCOMMERCE ADD-TO-CART EVENTS
--------------------------------------------------------- */
if(window.jQuery){
window.jQuery(
document.body
).on(
'added_to_cart removed_from_cart wc_fragments_refreshed',
function(){
postAjax(
'szh_get_cart'
)
.then(function(response){
if(
response &&
response.success
){
applyCartData(
response.data
);
}
});
}
);
}
/* ---------------------------------------------------------
DYNAMIC CATEGORIES
--------------------------------------------------------- */
const desktopCategories =
document.getElementById(
'szh-desktop-categories'
);
const mobileCategories =
document.getElementById(
'szh-mobile-categories'
);
function renderCategories(categories){
/* DESKTOP */
desktopCategories.innerHTML = '';
categories.forEach(function(category){
const link =
document.createElement('a');
link.className =
'szh-category-row';
link.href =
category.url;
link.innerHTML =
'' +
escapeHtml(category.name) +
'' +
'' +
escapeHtml(String(category.count)) +
'' +
'›';
desktopCategories.appendChild(
link
);
});
const allLink =
document.createElement('a');
allLink.href = '/shop/';
allLink.className =
'szh-category-row';
allLink.innerHTML =
'' +
'View All Books' +
'' +
'→';
desktopCategories.appendChild(
allLink
);
/* MOBILE */
mobileCategories.innerHTML = '';
categories.forEach(function(category){
const wrapper =
document.createElement('div');
wrapper.className =
'szh-mobile-category';
const head =
document.createElement('div');
head.className =
'szh-mobile-category-head';
const link =
document.createElement('a');
link.className =
'szh-mobile-category-link';
link.href =
category.url;
link.textContent =
category.name;
head.appendChild(link);
if(
category.children &&
category.children.length
){
const toggle =
document.createElement(
'button'
);
toggle.type = 'button';
toggle.className =
'szh-mobile-category-toggle';
toggle.setAttribute(
'aria-label',
'Show ' +
category.name +
' subcategories'
);
toggle.innerHTML =
'';
toggle.addEventListener(
'click',
function(){
wrapper.classList.toggle(
'szh-expanded'
);
}
);
head.appendChild(
toggle
);
}
wrapper.appendChild(
head
);
if(
category.children &&
category.children.length
){
const sub =
document.createElement('div');
sub.className =
'szh-mobile-subcategories';
category.children.forEach(
function(child){
const childLink =
document.createElement(
'a'
);
childLink.className =
'szh-mobile-subcategory';
childLink.href =
child.url;
childLink.textContent =
child.name;
sub.appendChild(
childLink
);
}
);
wrapper.appendChild(
sub
);
}
mobileCategories.appendChild(
wrapper
);
});
}
postAjax('szh_categories')
.then(function(response){
if(
response &&
response.success &&
Array.isArray(response.data)
){
renderCategories(
response.data
);
}
})
.catch(function(){
desktopCategories.innerHTML =
'' +
'Categories unavailable.' +
'
';
mobileCategories.innerHTML =
'' +
'Categories unavailable.' +
'
';
});
/* ---------------------------------------------------------
AJAX PRODUCT SEARCH
--------------------------------------------------------- */
document.querySelectorAll(
'[data-szh-search]'
).forEach(function(searchBox){
const input =
searchBox.querySelector(
'.szh-search-input'
);
const results =
searchBox.querySelector(
'.szh-search-results'
);
const form =
searchBox.querySelector(
'.szh-search-form'
);
let timer = null;
let controller = null;
let activeIndex = -1;
function closeResults(){
results.classList.remove(
'szh-is-open'
);
activeIndex = -1;
}
function openResults(){
results.classList.add(
'szh-is-open'
);
}
function getProductLinks(){
return Array.from(
results.querySelectorAll(
'.szh-search-product'
)
);
}
function updateKeyboardSelection(){
const links =
getProductLinks();
links.forEach(
function(link,index){
link.classList.toggle(
'szh-search-active',
index === activeIndex
);
}
);
}
function renderSearchProducts(
products,
term
){
results.innerHTML = '';
if(!products.length){
results.innerHTML =
'' +
'No products found for “' +
escapeHtml(term) +
'”.' +
'
';
openResults();
return;
}
products.forEach(
function(product){
const link =
document.createElement(
'a'
);
link.href =
product.url;
link.className =
'szh-search-product';
link.setAttribute(
'role',
'option'
);
const image =
document.createElement(
'div'
);
image.className =
'szh-search-image';
const img =
document.createElement(
'img'
);
img.src =
product.image;
img.alt = '';
img.loading =
'lazy';
image.appendChild(
img
);
const info =
document.createElement(
'div'
);
info.className =
'szh-search-info';
const title =
document.createElement(
'div'
);
title.className =
'szh-search-title';
title.textContent =
product.name;
info.appendChild(
title
);
const price =
document.createElement(
'div'
);
price.className =
'szh-search-price';
/*
* Price HTML comes from WooCommerce.
*/
price.innerHTML =
product.price;
link.appendChild(
image
);
link.appendChild(
info
);
link.appendChild(
price
);
results.appendChild(
link
);
}
);
const all =
document.createElement(
'a'
);
all.className =
'szh-search-all';
all.href =
'/?s=' +
encodeURIComponent(term) +
'&post_type=product';
all.textContent =
'View all results for “' +
term +
'” →';
results.appendChild(
all
);
activeIndex = -1;
openResults();
}
input.addEventListener(
'input',
function(){
const term =
input.value.trim();
clearTimeout(timer);
activeIndex = -1;
if(term.length < 2){
closeResults();
results.innerHTML = '';
return;
}
results.innerHTML =
'' +
'Searching...' +
'
';
openResults();
timer = setTimeout(
function(){
postAjax(
'szh_product_search',
{
term:term
}
)
.then(function(response){
/*
* Ignore stale result if user
* has changed search text.
*/
if(
input.value.trim()
!== term
){
return;
}
if(
response &&
response.success &&
Array.isArray(
response.data
)
){
renderSearchProducts(
response.data,
term
);
}
})
.catch(function(){
results.innerHTML =
'' +
'Search unavailable.' +
'
';
});
},
280
);
}
);
/* KEYBOARD NAVIGATION */
input.addEventListener(
'keydown',
function(event){
const links =
getProductLinks();
if(
event.key === 'ArrowDown' &&
links.length
){
event.preventDefault();
activeIndex++;
if(
activeIndex >= links.length
){
activeIndex = 0;
}
updateKeyboardSelection();
}
if(
event.key === 'ArrowUp' &&
links.length
){
event.preventDefault();
activeIndex--;
if(activeIndex = 0 &&
links[activeIndex]
){
event.preventDefault();
window.location.href =
links[activeIndex].href;
}
if(event.key === 'Escape'){
closeResults();
input.blur();
}
}
);
/* SEARCH FORM STILL WORKS NORMALLY */
form.addEventListener(
'submit',
function(){
closeResults();
}
);
/* CLOSE WHEN CLICKING OUTSIDE */
document.addEventListener(
'click',
function(event){
if(
!searchBox.contains(
event.target
)
){
closeResults();
}
}
);
});
})();











