2018-12-09 07:34:06 +08:00
|
|
|
|
|
|
|
class TriLayout {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.elem = elem;
|
|
|
|
|
|
|
|
this.lastLayoutType = 'none';
|
|
|
|
this.onDestroy = null;
|
2019-04-14 00:36:27 +08:00
|
|
|
this.scrollCache = {
|
|
|
|
'content': 0,
|
|
|
|
'info': 0,
|
|
|
|
};
|
|
|
|
this.lastTabShown = 'content';
|
2018-12-09 07:34:06 +08:00
|
|
|
|
2019-04-14 00:36:27 +08:00
|
|
|
// Bind any listeners
|
|
|
|
this.mobileTabClick = this.mobileTabClick.bind(this);
|
2018-12-09 07:34:06 +08:00
|
|
|
|
2019-04-14 00:36:27 +08:00
|
|
|
// Watch layout changes
|
2018-12-09 07:34:06 +08:00
|
|
|
this.updateLayout();
|
|
|
|
window.addEventListener('resize', event => {
|
|
|
|
this.updateLayout();
|
2019-04-14 00:36:27 +08:00
|
|
|
}, {passive: true});
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateLayout() {
|
2018-12-09 21:42:35 +08:00
|
|
|
let newLayout = 'tablet';
|
|
|
|
if (window.innerWidth <= 1000) newLayout = 'mobile';
|
|
|
|
if (window.innerWidth >= 1400) newLayout = 'desktop';
|
2018-12-09 07:34:06 +08:00
|
|
|
if (newLayout === this.lastLayoutType) return;
|
|
|
|
|
|
|
|
if (this.onDestroy) {
|
|
|
|
this.onDestroy();
|
|
|
|
this.onDestroy = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newLayout === 'desktop') {
|
|
|
|
this.setupDesktop();
|
2018-12-09 21:42:35 +08:00
|
|
|
} else if (newLayout === 'mobile') {
|
2018-12-09 07:34:06 +08:00
|
|
|
this.setupMobile();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.lastLayoutType = newLayout;
|
|
|
|
}
|
|
|
|
|
|
|
|
setupMobile() {
|
2019-04-14 00:36:27 +08:00
|
|
|
const layoutTabs = document.querySelectorAll('[tri-layout-mobile-tab]');
|
|
|
|
for (let tab of layoutTabs) {
|
|
|
|
tab.addEventListener('click', this.mobileTabClick);
|
|
|
|
}
|
2018-12-09 07:34:06 +08:00
|
|
|
|
|
|
|
this.onDestroy = () => {
|
2019-04-14 00:36:27 +08:00
|
|
|
for (let tab of layoutTabs) {
|
|
|
|
tab.removeEventListener('click', this.mobileTabClick);
|
|
|
|
}
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setupDesktop() {
|
2018-12-09 21:42:35 +08:00
|
|
|
//
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-04-14 00:36:27 +08:00
|
|
|
* Action to run when the mobile info toggle bar is clicked/tapped
|
2018-12-09 07:34:06 +08:00
|
|
|
* @param event
|
|
|
|
*/
|
2019-04-14 00:36:27 +08:00
|
|
|
mobileTabClick(event) {
|
|
|
|
const tab = event.target.getAttribute('tri-layout-mobile-tab');
|
|
|
|
this.scrollCache[this.lastTabShown] = document.documentElement.scrollTop;
|
|
|
|
|
|
|
|
// Set tab status
|
|
|
|
const activeTabs = document.querySelectorAll('.tri-layout-mobile-tab.active');
|
|
|
|
for (let tab of activeTabs) {
|
|
|
|
tab.classList.remove('active');
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
2019-04-14 00:36:27 +08:00
|
|
|
event.target.classList.add('active');
|
|
|
|
|
|
|
|
// Toggle section
|
|
|
|
const showInfo = (tab === 'info');
|
|
|
|
this.elem.classList.toggle('show-info', showInfo);
|
|
|
|
|
|
|
|
// Set the scroll position from cache
|
|
|
|
const pageHeader = document.querySelector('header');
|
|
|
|
const defaultScrollTop = pageHeader.getBoundingClientRect().bottom;
|
|
|
|
document.documentElement.scrollTop = this.scrollCache[tab] || defaultScrollTop;
|
|
|
|
setTimeout(() => {
|
|
|
|
document.documentElement.scrollTop = this.scrollCache[tab] || defaultScrollTop;
|
|
|
|
}, 50);
|
|
|
|
|
|
|
|
this.lastTabShown = tab;
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TriLayout;
|