Skip to content

Commit 0dcb2ec

Browse files
committed
Layout: Converted tri-layout component to ts
1 parent 9186e77 commit 0dcb2ec

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

resources/js/components/tri-layout.js renamed to resources/js/components/tri-layout.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import {Component} from './component';
22

33
export class TriLayout extends Component {
4-
5-
setup() {
4+
private container!: HTMLElement;
5+
private tabs!: HTMLElement[];
6+
private sidebarScrollContainers!: HTMLElement[];
7+
8+
private lastLayoutType = 'none';
9+
private onDestroy: (()=>void)|null = null;
10+
private scrollCache: Record<string, number> = {
11+
content: 0,
12+
info: 0,
13+
};
14+
private lastTabShown = 'content';
15+
16+
setup(): void {
617
this.container = this.$refs.container;
718
this.tabs = this.$manyRefs.tab;
819
this.sidebarScrollContainers = this.$manyRefs.sidebarScrollContainer;
920

10-
this.lastLayoutType = 'none';
11-
this.onDestroy = null;
12-
this.scrollCache = {
13-
content: 0,
14-
info: 0,
15-
};
16-
this.lastTabShown = 'content';
17-
1821
// Bind any listeners
1922
this.mobileTabClick = this.mobileTabClick.bind(this);
2023

@@ -27,7 +30,7 @@ export class TriLayout extends Component {
2730
this.setupSidebarScrollHandlers();
2831
}
2932

30-
updateLayout() {
33+
updateLayout(): void {
3134
let newLayout = 'tablet';
3235
if (window.innerWidth <= 1000) newLayout = 'mobile';
3336
if (window.innerWidth > 1400) newLayout = 'desktop';
@@ -59,33 +62,30 @@ export class TriLayout extends Component {
5962
};
6063
}
6164

62-
setupDesktop() {
65+
setupDesktop(): void {
6366
//
6467
}
6568

6669
/**
6770
* Action to run when the mobile info toggle bar is clicked/tapped
68-
* @param event
6971
*/
70-
mobileTabClick(event) {
71-
const {tab} = event.target.dataset;
72+
mobileTabClick(event: MouseEvent): void {
73+
const tab = (event.target as HTMLElement).dataset.tab || '';
7274
this.showTab(tab);
7375
}
7476

7577
/**
7678
* Show the content tab.
7779
* Used by the page-display component.
7880
*/
79-
showContent() {
81+
showContent(): void {
8082
this.showTab('content', false);
8183
}
8284

8385
/**
8486
* Show the given tab
85-
* @param {String} tabName
86-
* @param {Boolean }scroll
8787
*/
88-
showTab(tabName, scroll = true) {
88+
showTab(tabName: string, scroll: boolean = true): void {
8989
this.scrollCache[this.lastTabShown] = document.documentElement.scrollTop;
9090

9191
// Set tab status
@@ -100,7 +100,7 @@ export class TriLayout extends Component {
100100

101101
// Set the scroll position from cache
102102
if (scroll) {
103-
const pageHeader = document.querySelector('header');
103+
const pageHeader = document.querySelector('header') as HTMLElement;
104104
const defaultScrollTop = pageHeader.getBoundingClientRect().bottom;
105105
document.documentElement.scrollTop = this.scrollCache[tabName] || defaultScrollTop;
106106
setTimeout(() => {
@@ -111,7 +111,7 @@ export class TriLayout extends Component {
111111
this.lastTabShown = tabName;
112112
}
113113

114-
setupSidebarScrollHandlers() {
114+
setupSidebarScrollHandlers(): void {
115115
for (const sidebar of this.sidebarScrollContainers) {
116116
sidebar.addEventListener('scroll', () => this.handleSidebarScroll(sidebar), {
117117
passive: true,
@@ -126,13 +126,15 @@ export class TriLayout extends Component {
126126
});
127127
}
128128

129-
handleSidebarScroll(sidebar) {
129+
handleSidebarScroll(sidebar: HTMLElement): void {
130130
const scrollable = sidebar.clientHeight !== sidebar.scrollHeight;
131131
const atTop = sidebar.scrollTop === 0;
132132
const atBottom = (sidebar.scrollTop + sidebar.clientHeight) === sidebar.scrollHeight;
133133

134-
sidebar.parentElement.classList.toggle('scroll-away-from-top', !atTop && scrollable);
135-
sidebar.parentElement.classList.toggle('scroll-away-from-bottom', !atBottom && scrollable);
134+
if (sidebar.parentElement) {
135+
sidebar.parentElement.classList.toggle('scroll-away-from-top', !atTop && scrollable);
136+
sidebar.parentElement.classList.toggle('scroll-away-from-bottom', !atBottom && scrollable);
137+
}
136138
}
137139

138140
}

0 commit comments

Comments
 (0)