forked from Shopify/dawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add featured product section (Shopify#261)
- Loading branch information
1 parent
9789ae1
commit 7a2f34f
Showing
65 changed files
with
3,019 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,100 @@ | ||
class PickupAvailability extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
if(!this.hasAttribute('available')) return; | ||
|
||
this.errorHtml = this.querySelector('template').content.firstElementChild.cloneNode(true); | ||
this.onClickRefreshList = this.onClickRefreshList.bind(this); | ||
this.fetchAvailability(this.dataset.variantId); | ||
} | ||
|
||
fetchAvailability(variantId) { | ||
const variantSectionUrl = `${this.dataset.baseUrl}variants/${variantId}/?section_id=pickup-availability`; | ||
|
||
fetch(variantSectionUrl) | ||
.then(response => response.text()) | ||
.then(text => { | ||
const sectionInnerHTML = new DOMParser() | ||
.parseFromString(text, 'text/html') | ||
.querySelector('.shopify-section'); | ||
this.renderPreview(sectionInnerHTML); | ||
}) | ||
.catch(e => { | ||
this.querySelector('button')?.removeEventListener('click', this.onClickRefreshList); | ||
this.renderError(); | ||
}); | ||
} | ||
|
||
onClickRefreshList(evt) { | ||
this.fetchAvailability(this.dataset.variantId); | ||
} | ||
|
||
renderError() { | ||
this.innerHTML = ''; | ||
this.appendChild(this.errorHtml); | ||
|
||
this.querySelector('button').addEventListener('click', this.onClickRefreshList); | ||
} | ||
|
||
renderPreview(sectionInnerHTML) { | ||
const drawer = document.querySelector('pickup-availability-drawer'); | ||
if (drawer) drawer.remove(); | ||
if (!sectionInnerHTML.querySelector('pickup-availability-preview')) { | ||
this.innerHTML = ""; | ||
this.removeAttribute('available'); | ||
return; | ||
if (!customElements.get('pickup-availability')) { | ||
customElements.define('pickup-availability', class PickupAvailability extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
if(!this.hasAttribute('available')) return; | ||
|
||
this.errorHtml = this.querySelector('template').content.firstElementChild.cloneNode(true); | ||
this.onClickRefreshList = this.onClickRefreshList.bind(this); | ||
this.fetchAvailability(this.dataset.variantId); | ||
} | ||
|
||
fetchAvailability(variantId) { | ||
const variantSectionUrl = `${this.dataset.baseUrl}variants/${variantId}/?section_id=pickup-availability`; | ||
|
||
fetch(variantSectionUrl) | ||
.then(response => response.text()) | ||
.then(text => { | ||
const sectionInnerHTML = new DOMParser() | ||
.parseFromString(text, 'text/html') | ||
.querySelector('.shopify-section'); | ||
this.renderPreview(sectionInnerHTML); | ||
}) | ||
.catch(e => { | ||
this.querySelector('button')?.removeEventListener('click', this.onClickRefreshList); | ||
this.renderError(); | ||
}); | ||
} | ||
|
||
onClickRefreshList(evt) { | ||
this.fetchAvailability(this.dataset.variantId); | ||
} | ||
|
||
renderError() { | ||
this.innerHTML = ''; | ||
this.appendChild(this.errorHtml); | ||
|
||
this.querySelector('button').addEventListener('click', this.onClickRefreshList); | ||
} | ||
|
||
this.innerHTML = sectionInnerHTML.querySelector('pickup-availability-preview').outerHTML; | ||
this.setAttribute('available', ''); | ||
renderPreview(sectionInnerHTML) { | ||
const drawer = document.querySelector('pickup-availability-drawer'); | ||
if (drawer) drawer.remove(); | ||
if (!sectionInnerHTML.querySelector('pickup-availability-preview')) { | ||
this.innerHTML = ""; | ||
this.removeAttribute('available'); | ||
return; | ||
} | ||
|
||
document.body.appendChild(sectionInnerHTML.querySelector('pickup-availability-drawer')); | ||
this.innerHTML = sectionInnerHTML.querySelector('pickup-availability-preview').outerHTML; | ||
this.setAttribute('available', ''); | ||
|
||
this.querySelector('button').addEventListener('click', (evt) => { | ||
document.querySelector('pickup-availability-drawer').show(evt.target); | ||
}); | ||
} | ||
document.body.appendChild(sectionInnerHTML.querySelector('pickup-availability-drawer')); | ||
|
||
this.querySelector('button').addEventListener('click', (evt) => { | ||
document.querySelector('pickup-availability-drawer').show(evt.target); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
customElements.define('pickup-availability', PickupAvailability); | ||
if (!customElements.get('pickup-availability-drawer')) { | ||
customElements.define('pickup-availability-drawer', class PickupAvailabilityDrawer extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
class PickupAvailabilityDrawer extends HTMLElement { | ||
constructor() { | ||
super(); | ||
this.onBodyClick = this.handleBodyClick.bind(this); | ||
|
||
this.querySelector('button').addEventListener('click', () => { | ||
this.hide(); | ||
}); | ||
|
||
this.onBodyClick = this.handleBodyClick.bind(this); | ||
this.addEventListener('keyup', () => { | ||
if(event.code.toUpperCase() === 'ESCAPE') this.hide(); | ||
}); | ||
} | ||
|
||
this.querySelector('button').addEventListener('click', () => { | ||
this.hide(); | ||
}); | ||
handleBodyClick(evt) { | ||
const target = evt.target; | ||
if (target != this && !target.closest('pickup-availability-drawer') && target.id != 'ShowPickupAvailabilityDrawer') { | ||
this.hide(); | ||
} | ||
} | ||
|
||
this.addEventListener('keyup', () => { | ||
if(event.code.toUpperCase() === 'ESCAPE') this.hide(); | ||
}); | ||
} | ||
hide() { | ||
this.removeAttribute('open'); | ||
document.body.removeEventListener('click', this.onBodyClick); | ||
document.body.classList.remove('overflow-hidden'); | ||
removeTrapFocus(this.focusElement); | ||
} | ||
|
||
handleBodyClick(evt) { | ||
const target = evt.target; | ||
if (target != this && !target.closest('pickup-availability-drawer') && target.id != 'ShowPickupAvailabilityDrawer') { | ||
this.hide(); | ||
show(focusElement) { | ||
this.focusElement = focusElement; | ||
this.setAttribute('open', ''); | ||
document.body.addEventListener('click', this.onBodyClick); | ||
document.body.classList.add('overflow-hidden'); | ||
trapFocus(this); | ||
} | ||
} | ||
|
||
hide() { | ||
this.removeAttribute('open'); | ||
document.body.removeEventListener('click', this.onBodyClick); | ||
document.body.classList.remove('overflow-hidden'); | ||
removeTrapFocus(this.focusElement); | ||
} | ||
|
||
show(focusElement) { | ||
this.focusElement = focusElement; | ||
this.setAttribute('open', ''); | ||
document.body.addEventListener('click', this.onBodyClick); | ||
document.body.classList.add('overflow-hidden'); | ||
trapFocus(this); | ||
} | ||
}); | ||
} | ||
|
||
customElements.define('pickup-availability-drawer', PickupAvailabilityDrawer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
class ProductForm extends HTMLElement { | ||
constructor() { | ||
super(); | ||
if (!customElements.get('product-form')) { | ||
customElements.define('product-form', class ProductForm extends HTMLElement { | ||
constructor() { | ||
super(); | ||
|
||
this.form = this.querySelector('form'); | ||
this.form.addEventListener('submit', this.onSubmitHandler.bind(this)); | ||
this.cartNotification = document.querySelector('cart-notification'); | ||
} | ||
this.form = this.querySelector('form'); | ||
this.form.addEventListener('submit', this.onSubmitHandler.bind(this)); | ||
this.cartNotification = document.querySelector('cart-notification'); | ||
} | ||
|
||
onSubmitHandler(evt) { | ||
evt.preventDefault(); | ||
this.cartNotification.setActiveElement(document.activeElement); | ||
|
||
const submitButton = this.querySelector('[type="submit"]'); | ||
onSubmitHandler(evt) { | ||
evt.preventDefault(); | ||
this.cartNotification.setActiveElement(document.activeElement); | ||
|
||
submitButton.setAttribute('disabled', true); | ||
submitButton.classList.add('loading'); | ||
const submitButton = this.querySelector('[type="submit"]'); | ||
|
||
const body = JSON.stringify({ | ||
...JSON.parse(serializeForm(this.form)), | ||
sections: this.cartNotification.getSectionsToRender().map((section) => section.id), | ||
sections_url: window.location.pathname | ||
}); | ||
submitButton.setAttribute('disabled', true); | ||
submitButton.classList.add('loading'); | ||
|
||
fetch(`${routes.cart_add_url}`, { ...fetchConfig('javascript'), body }) | ||
.then((response) => response.json()) | ||
.then((parsedState) => { | ||
this.cartNotification.renderContents(parsedState); | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
}) | ||
.finally(() => { | ||
submitButton.classList.remove('loading'); | ||
submitButton.removeAttribute('disabled'); | ||
const body = JSON.stringify({ | ||
...JSON.parse(serializeForm(this.form)), | ||
sections: this.cartNotification.getSectionsToRender().map((section) => section.id), | ||
sections_url: window.location.pathname | ||
}); | ||
} | ||
} | ||
|
||
customElements.define('product-form', ProductForm); | ||
fetch(`${routes.cart_add_url}`, { ...fetchConfig('javascript'), body }) | ||
.then((response) => response.json()) | ||
.then((parsedState) => { | ||
this.cartNotification.renderContents(parsedState); | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
}) | ||
.finally(() => { | ||
submitButton.classList.remove('loading'); | ||
submitButton.removeAttribute('disabled'); | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.