Skip to content

Commit

Permalink
Merge pull request #1 from helloextend/SESERV-11-dawn-theme-zip-file
Browse files Browse the repository at this point in the history
Seserv 11 dawn theme zip file
  • Loading branch information
whereispolaris authored Nov 14, 2022
2 parents 7ad0682 + ff4344c commit c33e774
Show file tree
Hide file tree
Showing 247 changed files with 104,718 additions and 0 deletions.
2,888 changes: 2,888 additions & 0 deletions assets/base.css

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions assets/cart-drawer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
class CartDrawer extends HTMLElement {
constructor() {
super();

this.addEventListener('keyup', (evt) => evt.code === 'Escape' && this.close());
this.querySelector('#CartDrawer-Overlay').addEventListener('click', this.close.bind(this));
this.setHeaderCartIconAccessibility();
}

setHeaderCartIconAccessibility() {
const cartLink = document.querySelector('#cart-icon-bubble');
cartLink.setAttribute('role', 'button');
cartLink.setAttribute('aria-haspopup', 'dialog');
cartLink.addEventListener('click', (event) => {
event.preventDefault();
this.open(cartLink)
});
cartLink.addEventListener('keydown', (event) => {
if (event.code.toUpperCase() === 'SPACE') {
event.preventDefault();
this.open(cartLink);
}
});
}

open(triggeredBy) {
if (triggeredBy) this.setActiveElement(triggeredBy);
const cartDrawerNote = this.querySelector('[id^="Details-"] summary');
if (cartDrawerNote && !cartDrawerNote.hasAttribute('role')) this.setSummaryAccessibility(cartDrawerNote);
// here the animation doesn't seem to always get triggered. A timeout seem to help
setTimeout(() => {this.classList.add('animate', 'active')});

this.addEventListener('transitionend', () => {
const containerToTrapFocusOn = this.classList.contains('is-empty') ? this.querySelector('.drawer__inner-empty') : document.getElementById('CartDrawer');
const focusElement = this.querySelector('.drawer__inner') || this.querySelector('.drawer__close');
trapFocus(containerToTrapFocusOn, focusElement);
}, { once: true });

document.body.classList.add('overflow-hidden');
}

close() {
this.classList.remove('active');
removeTrapFocus(this.activeElement);
document.body.classList.remove('overflow-hidden');
}

setSummaryAccessibility(cartDrawerNote) {
cartDrawerNote.setAttribute('role', 'button');
cartDrawerNote.setAttribute('aria-expanded', 'false');

if(cartDrawerNote.nextElementSibling.getAttribute('id')) {
cartDrawerNote.setAttribute('aria-controls', cartDrawerNote.nextElementSibling.id);
}

cartDrawerNote.addEventListener('click', (event) => {
event.currentTarget.setAttribute('aria-expanded', !event.currentTarget.closest('details').hasAttribute('open'));
});

cartDrawerNote.parentElement.addEventListener('keyup', onKeyUpEscape);
}

renderContents(parsedState) {
this.querySelector('.drawer__inner').classList.contains('is-empty') && this.querySelector('.drawer__inner').classList.remove('is-empty');
this.productId = parsedState.id;
this.getSectionsToRender().forEach((section => {
const sectionElement = section.selector ? document.querySelector(section.selector) : document.getElementById(section.id);
sectionElement.innerHTML =
this.getSectionInnerHTML(parsedState.sections[section.id], section.selector);
}));

setTimeout(() => {
this.querySelector('#CartDrawer-Overlay').addEventListener('click', this.close.bind(this));
this.open();
});
}

getSectionInnerHTML(html, selector = '.shopify-section') {
return new DOMParser()
.parseFromString(html, 'text/html')
.querySelector(selector).innerHTML;
}

getSectionsToRender() {
return [
{
id: 'cart-drawer',
selector: '#CartDrawer'
},
{
id: 'cart-icon-bubble'
}
];
}

getSectionDOM(html, selector = '.shopify-section') {
return new DOMParser()
.parseFromString(html, 'text/html')
.querySelector(selector);
}

setActiveElement(element) {
this.activeElement = element;
}
}

customElements.define('cart-drawer', CartDrawer);

class CartDrawerItems extends CartItems {
getSectionsToRender() {
return [
{
id: 'CartDrawer',
section: 'cart-drawer',
selector: '.drawer__inner'
},
{
id: 'cart-icon-bubble',
section: 'cart-icon-bubble',
selector: '.shopify-section'
}
];
}
}

customElements.define('cart-drawer-items', CartDrawerItems);
80 changes: 80 additions & 0 deletions assets/cart-notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
class CartNotification extends HTMLElement {
constructor() {
super();

this.notification = document.getElementById('cart-notification');
this.header = document.querySelector('sticky-header');
this.onBodyClick = this.handleBodyClick.bind(this);

this.notification.addEventListener('keyup', (evt) => evt.code === 'Escape' && this.close());
this.querySelectorAll('button[type="button"]').forEach((closeButton) =>
closeButton.addEventListener('click', this.close.bind(this))
);
}

open() {
this.notification.classList.add('animate', 'active');

this.notification.addEventListener('transitionend', () => {
this.notification.focus();
trapFocus(this.notification);
}, { once: true });

document.body.addEventListener('click', this.onBodyClick);
}

close() {
this.notification.classList.remove('active');

document.body.removeEventListener('click', this.onBodyClick);

removeTrapFocus(this.activeElement);
}

renderContents(parsedState) {
this.cartItemKey = parsedState.key;
this.getSectionsToRender().forEach((section => {
document.getElementById(section.id).innerHTML =
this.getSectionInnerHTML(parsedState.sections[section.id], section.selector);
}));

if (this.header) this.header.reveal();
this.open();
}

getSectionsToRender() {
return [
{
id: 'cart-notification-product',
selector: `[id="cart-notification-product-${this.cartItemKey}"]`,
},
{
id: 'cart-notification-button'
},
{
id: 'cart-icon-bubble'
}
];
}

getSectionInnerHTML(html, selector = '.shopify-section') {
return new DOMParser()
.parseFromString(html, 'text/html')
.querySelector(selector).innerHTML;
}

handleBodyClick(evt) {
const target = evt.target;
if (target !== this.notification && !target.closest('cart-notification')) {
const disclosure = target.closest('details-disclosure, header-menu');
this.activeElement = disclosure ? disclosure.querySelector('summary') : null;
this.close();
}
}

setActiveElement(element) {
this.activeElement = element;
}
}

customElements.define('cart-notification', CartNotification);
169 changes: 169 additions & 0 deletions assets/cart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
class CartRemoveButton extends HTMLElement {
constructor() {
super();
this.addEventListener('click', (event) => {
event.preventDefault();
const cartItems = this.closest('cart-items') || this.closest('cart-drawer-items');
cartItems.updateQuantity(this.dataset.index, 0);
});
}
}

customElements.define('cart-remove-button', CartRemoveButton);

class CartItems extends HTMLElement {
constructor() {
super();

this.lineItemStatusElement = document.getElementById('shopping-cart-line-item-status') || document.getElementById('CartDrawer-LineItemStatus');

this.currentItemCount = Array.from(this.querySelectorAll('[name="updates[]"]'))
.reduce((total, quantityInput) => total + parseInt(quantityInput.value), 0);

this.debouncedOnChange = debounce((event) => {
this.onChange(event);
}, 300);

this.addEventListener('change', this.debouncedOnChange.bind(this));
}

onChange(event) {
this.updateQuantity(event.target.dataset.index, event.target.value, document.activeElement.getAttribute('name'));
}

getSectionsToRender() {
return [
{
id: 'main-cart-items',
section: document.getElementById('main-cart-items').dataset.id,
selector: '.js-contents',
},
{
id: 'cart-icon-bubble',
section: 'cart-icon-bubble',
selector: '.shopify-section'
},
{
id: 'cart-live-region-text',
section: 'cart-live-region-text',
selector: '.shopify-section'
},
{
id: 'main-cart-footer',
section: document.getElementById('main-cart-footer').dataset.id,
selector: '.js-contents',
}
];
}

updateQuantity(line, quantity, name) {
this.enableLoading(line);

const body = JSON.stringify({
line,
quantity,
sections: this.getSectionsToRender().map((section) => section.section),
sections_url: window.location.pathname
});

fetch(`${routes.cart_change_url}`, {...fetchConfig(), ...{ body }})
.then((response) => {
return response.text();
})
.then((state) => {
const parsedState = JSON.parse(state);
this.classList.toggle('is-empty', parsedState.item_count === 0);
const cartDrawerWrapper = document.querySelector('cart-drawer');
const cartFooter = document.getElementById('main-cart-footer');

if (cartFooter) cartFooter.classList.toggle('is-empty', parsedState.item_count === 0);
if (cartDrawerWrapper) cartDrawerWrapper.classList.toggle('is-empty', parsedState.item_count === 0);

this.getSectionsToRender().forEach((section => {
const elementToReplace =
document.getElementById(section.id).querySelector(section.selector) || document.getElementById(section.id);
elementToReplace.innerHTML =
this.getSectionInnerHTML(parsedState.sections[section.section], section.selector);
}));

this.updateLiveRegions(line, parsedState.item_count);
const lineItem = document.getElementById(`CartItem-${line}`) || document.getElementById(`CartDrawer-Item-${line}`);
if (lineItem && lineItem.querySelector(`[name="${name}"]`)) {
cartDrawerWrapper ? trapFocus(cartDrawerWrapper, lineItem.querySelector(`[name="${name}"]`)) : lineItem.querySelector(`[name="${name}"]`).focus();
} else if (parsedState.item_count === 0 && cartDrawerWrapper) {
trapFocus(cartDrawerWrapper.querySelector('.drawer__inner-empty'), cartDrawerWrapper.querySelector('a'))
} else if (document.querySelector('.cart-item') && cartDrawerWrapper) {
trapFocus(cartDrawerWrapper, document.querySelector('.cart-item__name'))
}
this.disableLoading();
}).catch(() => {
this.querySelectorAll('.loading-overlay').forEach((overlay) => overlay.classList.add('hidden'));
const errors = document.getElementById('cart-errors') || document.getElementById('CartDrawer-CartErrors');
errors.textContent = window.cartStrings.error;
this.disableLoading();
});
}

updateLiveRegions(line, itemCount) {
if (this.currentItemCount === itemCount) {
const lineItemError = document.getElementById(`Line-item-error-${line}`) || document.getElementById(`CartDrawer-LineItemError-${line}`);
const quantityElement = document.getElementById(`Quantity-${line}`) || document.getElementById(`Drawer-quantity-${line}`);

lineItemError
.querySelector('.cart-item__error-text')
.innerHTML = window.cartStrings.quantityError.replace(
'[quantity]',
quantityElement.value
);
}

this.currentItemCount = itemCount;
this.lineItemStatusElement.setAttribute('aria-hidden', true);

const cartStatus = document.getElementById('cart-live-region-text') || document.getElementById('CartDrawer-LiveRegionText');
cartStatus.setAttribute('aria-hidden', false);

setTimeout(() => {
cartStatus.setAttribute('aria-hidden', true);
}, 1000);
}

getSectionInnerHTML(html, selector) {
return new DOMParser()
.parseFromString(html, 'text/html')
.querySelector(selector).innerHTML;
}

enableLoading(line) {
const mainCartItems = document.getElementById('main-cart-items') || document.getElementById('CartDrawer-CartItems');
mainCartItems.classList.add('cart__items--disabled');

const cartItemElements = this.querySelectorAll(`#CartItem-${line} .loading-overlay`);
const cartDrawerItemElements = this.querySelectorAll(`#CartDrawer-Item-${line} .loading-overlay`);

[...cartItemElements, ...cartDrawerItemElements].forEach((overlay) => overlay.classList.remove('hidden'));

document.activeElement.blur();
this.lineItemStatusElement.setAttribute('aria-hidden', false);
}

disableLoading() {
const mainCartItems = document.getElementById('main-cart-items') || document.getElementById('CartDrawer-CartItems');
mainCartItems.classList.remove('cart__items--disabled');
}
}

customElements.define('cart-items', CartItems);

if (!customElements.get('cart-note')) {
customElements.define('cart-note', class CartNote extends HTMLElement {
constructor() {
super();

this.addEventListener('change', debounce((event) => {
const body = JSON.stringify({ note: event.target.value });
fetch(`${routes.cart_update_url}`, {...fetchConfig(), ...{ body }});
}, 300))
}
});
};
Loading

0 comments on commit c33e774

Please sign in to comment.