1 import {Component} from './component';
3 export class TriLayout extends Component {
6 this.container = this.$refs.container;
7 this.tabs = this.$manyRefs.tab;
9 this.lastLayoutType = 'none';
10 this.onDestroy = null;
15 this.lastTabShown = 'content';
18 this.mobileTabClick = this.mobileTabClick.bind(this);
20 // Watch layout changes
22 window.addEventListener('resize', () => {
28 let newLayout = 'tablet';
29 if (window.innerWidth <= 1000) newLayout = 'mobile';
30 if (window.innerWidth >= 1400) newLayout = 'desktop';
31 if (newLayout === this.lastLayoutType) return;
35 this.onDestroy = null;
38 if (newLayout === 'desktop') {
40 } else if (newLayout === 'mobile') {
44 this.lastLayoutType = newLayout;
48 for (const tab of this.tabs) {
49 tab.addEventListener('click', this.mobileTabClick);
52 this.onDestroy = () => {
53 for (const tab of this.tabs) {
54 tab.removeEventListener('click', this.mobileTabClick);
64 * Action to run when the mobile info toggle bar is clicked/tapped
67 mobileTabClick(event) {
68 const {tab} = event.target.dataset;
73 * Show the content tab.
74 * Used by the page-display component.
77 this.showTab('content', false);
82 * @param {String} tabName
83 * @param {Boolean }scroll
85 showTab(tabName, scroll = true) {
86 this.scrollCache[this.lastTabShown] = document.documentElement.scrollTop;
89 for (const tab of this.tabs) {
90 const isActive = (tab.dataset.tab === tabName);
91 tab.setAttribute('aria-selected', isActive ? 'true' : 'false');
95 const showInfo = (tabName === 'info');
96 this.container.classList.toggle('show-info', showInfo);
98 // Set the scroll position from cache
100 const pageHeader = document.querySelector('header');
101 const defaultScrollTop = pageHeader.getBoundingClientRect().bottom;
102 document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
104 document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
108 this.lastTabShown = tabName;