2023-04-19 05:20:02 +08:00
|
|
|
import {Component} from './component';
|
2018-12-09 07:34:06 +08:00
|
|
|
|
2022-11-16 00:04:46 +08:00
|
|
|
export class TriLayout extends Component {
|
2018-12-09 07:34:06 +08:00
|
|
|
|
2021-04-20 04:41:13 +08:00
|
|
|
setup() {
|
|
|
|
this.container = this.$refs.container;
|
|
|
|
this.tabs = this.$manyRefs.tab;
|
2018-12-09 07:34:06 +08:00
|
|
|
|
|
|
|
this.lastLayoutType = 'none';
|
|
|
|
this.onDestroy = null;
|
2019-04-14 00:36:27 +08:00
|
|
|
this.scrollCache = {
|
2023-04-19 05:20:02 +08:00
|
|
|
content: 0,
|
|
|
|
info: 0,
|
2019-04-14 00:36:27 +08:00
|
|
|
};
|
|
|
|
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();
|
2023-04-19 22:20:04 +08:00
|
|
|
window.addEventListener('resize', () => {
|
2018-12-09 07:34:06 +08:00
|
|
|
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';
|
2023-04-19 05:20:02 +08:00
|
|
|
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() {
|
2021-04-20 04:41:13 +08:00
|
|
|
for (const tab of this.tabs) {
|
2019-04-14 00:36:27 +08:00
|
|
|
tab.addEventListener('click', this.mobileTabClick);
|
|
|
|
}
|
2018-12-09 07:34:06 +08:00
|
|
|
|
|
|
|
this.onDestroy = () => {
|
2021-04-20 04:41:13 +08:00
|
|
|
for (const tab of this.tabs) {
|
2019-04-14 00:36:27 +08:00
|
|
|
tab.removeEventListener('click', this.mobileTabClick);
|
|
|
|
}
|
2023-04-19 05:20:02 +08:00
|
|
|
};
|
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) {
|
2023-04-19 05:20:02 +08:00
|
|
|
const {tab} = event.target.dataset;
|
2019-05-25 22:37:49 +08:00
|
|
|
this.showTab(tab);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the content tab.
|
|
|
|
* Used by the page-display component.
|
|
|
|
*/
|
|
|
|
showContent() {
|
2019-06-16 19:46:23 +08:00
|
|
|
this.showTab('content', false);
|
2019-05-25 22:37:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the given tab
|
2021-04-20 04:41:13 +08:00
|
|
|
* @param {String} tabName
|
|
|
|
* @param {Boolean }scroll
|
2019-05-25 22:37:49 +08:00
|
|
|
*/
|
2019-06-16 19:46:23 +08:00
|
|
|
showTab(tabName, scroll = true) {
|
2019-04-14 00:36:27 +08:00
|
|
|
this.scrollCache[this.lastTabShown] = document.documentElement.scrollTop;
|
|
|
|
|
|
|
|
// Set tab status
|
2021-04-20 04:41:13 +08:00
|
|
|
for (const tab of this.tabs) {
|
|
|
|
const isActive = (tab.dataset.tab === tabName);
|
|
|
|
tab.setAttribute('aria-selected', isActive ? 'true' : 'false');
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
2019-04-14 00:36:27 +08:00
|
|
|
|
|
|
|
// Toggle section
|
2019-05-25 22:37:49 +08:00
|
|
|
const showInfo = (tabName === 'info');
|
2021-04-20 04:41:13 +08:00
|
|
|
this.container.classList.toggle('show-info', showInfo);
|
2019-04-14 00:36:27 +08:00
|
|
|
|
|
|
|
// Set the scroll position from cache
|
2019-06-16 19:46:23 +08:00
|
|
|
if (scroll) {
|
|
|
|
const pageHeader = document.querySelector('header');
|
|
|
|
const defaultScrollTop = pageHeader.getBoundingClientRect().bottom;
|
2019-05-25 22:37:49 +08:00
|
|
|
document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
|
2019-06-16 19:46:23 +08:00
|
|
|
setTimeout(() => {
|
|
|
|
document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
|
|
|
|
}, 50);
|
|
|
|
}
|
2019-04-14 00:36:27 +08:00
|
|
|
|
2019-05-25 22:37:49 +08:00
|
|
|
this.lastTabShown = tabName;
|
2018-12-09 07:34:06 +08:00
|
|
|
}
|
|
|
|
|
2023-04-19 05:20:02 +08:00
|
|
|
}
|