1 import {Component} from './component';
3 export class TriLayout extends Component {
6 this.container = this.$refs.container;
7 this.tabs = this.$manyRefs.tab;
8 this.sidebarScrollContainers = this.$manyRefs.sidebarScrollContainer;
10 this.lastLayoutType = 'none';
11 this.onDestroy = null;
16 this.lastTabShown = 'content';
19 this.mobileTabClick = this.mobileTabClick.bind(this);
21 // Watch layout changes
23 window.addEventListener('resize', () => {
27 this.setupSidebarScrollHandlers();
31 let newLayout = 'tablet';
32 if (window.innerWidth <= 1000) newLayout = 'mobile';
33 if (window.innerWidth > 1400) newLayout = 'desktop';
34 if (newLayout === this.lastLayoutType) return;
38 this.onDestroy = null;
41 if (newLayout === 'desktop') {
43 } else if (newLayout === 'mobile') {
47 this.lastLayoutType = newLayout;
51 for (const tab of this.tabs) {
52 tab.addEventListener('click', this.mobileTabClick);
55 this.onDestroy = () => {
56 for (const tab of this.tabs) {
57 tab.removeEventListener('click', this.mobileTabClick);
67 * Action to run when the mobile info toggle bar is clicked/tapped
70 mobileTabClick(event) {
71 const {tab} = event.target.dataset;
76 * Show the content tab.
77 * Used by the page-display component.
80 this.showTab('content', false);
85 * @param {String} tabName
86 * @param {Boolean }scroll
88 showTab(tabName, scroll = true) {
89 this.scrollCache[this.lastTabShown] = document.documentElement.scrollTop;
92 for (const tab of this.tabs) {
93 const isActive = (tab.dataset.tab === tabName);
94 tab.setAttribute('aria-selected', isActive ? 'true' : 'false');
98 const showInfo = (tabName === 'info');
99 this.container.classList.toggle('show-info', showInfo);
101 // Set the scroll position from cache
103 const pageHeader = document.querySelector('header');
104 const defaultScrollTop = pageHeader.getBoundingClientRect().bottom;
105 document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
107 document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
111 this.lastTabShown = tabName;
114 setupSidebarScrollHandlers() {
115 for (const sidebar of this.sidebarScrollContainers) {
116 sidebar.addEventListener('scroll', () => this.handleSidebarScroll(sidebar), {
119 this.handleSidebarScroll(sidebar);
122 window.addEventListener('resize', () => {
123 for (const sidebar of this.sidebarScrollContainers) {
124 this.handleSidebarScroll(sidebar);
129 handleSidebarScroll(sidebar) {
130 const scrollable = sidebar.clientHeight !== sidebar.scrollHeight;
131 const atTop = sidebar.scrollTop === 0;
132 const atBottom = (sidebar.scrollTop + sidebar.clientHeight) === sidebar.scrollHeight;
134 sidebar.parentElement.classList.toggle('scroll-away-from-top', !atTop && scrollable);
135 sidebar.parentElement.classList.toggle('scroll-away-from-bottom', !atBottom && scrollable);