1
1
import { Component } from './component' ;
2
2
3
3
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 {
6
17
this . container = this . $refs . container ;
7
18
this . tabs = this . $manyRefs . tab ;
8
19
this . sidebarScrollContainers = this . $manyRefs . sidebarScrollContainer ;
9
20
10
- this . lastLayoutType = 'none' ;
11
- this . onDestroy = null ;
12
- this . scrollCache = {
13
- content : 0 ,
14
- info : 0 ,
15
- } ;
16
- this . lastTabShown = 'content' ;
17
-
18
21
// Bind any listeners
19
22
this . mobileTabClick = this . mobileTabClick . bind ( this ) ;
20
23
@@ -27,7 +30,7 @@ export class TriLayout extends Component {
27
30
this . setupSidebarScrollHandlers ( ) ;
28
31
}
29
32
30
- updateLayout ( ) {
33
+ updateLayout ( ) : void {
31
34
let newLayout = 'tablet' ;
32
35
if ( window . innerWidth <= 1000 ) newLayout = 'mobile' ;
33
36
if ( window . innerWidth > 1400 ) newLayout = 'desktop' ;
@@ -59,33 +62,30 @@ export class TriLayout extends Component {
59
62
} ;
60
63
}
61
64
62
- setupDesktop ( ) {
65
+ setupDesktop ( ) : void {
63
66
//
64
67
}
65
68
66
69
/**
67
70
* Action to run when the mobile info toggle bar is clicked/tapped
68
- * @param event
69
71
*/
70
- mobileTabClick ( event ) {
71
- const { tab} = event . target . dataset ;
72
+ mobileTabClick ( event : MouseEvent ) : void {
73
+ const tab = ( event . target as HTMLElement ) . dataset . tab || '' ;
72
74
this . showTab ( tab ) ;
73
75
}
74
76
75
77
/**
76
78
* Show the content tab.
77
79
* Used by the page-display component.
78
80
*/
79
- showContent ( ) {
81
+ showContent ( ) : void {
80
82
this . showTab ( 'content' , false ) ;
81
83
}
82
84
83
85
/**
84
86
* Show the given tab
85
- * @param {String } tabName
86
- * @param {Boolean }scroll
87
87
*/
88
- showTab ( tabName , scroll = true ) {
88
+ showTab ( tabName : string , scroll : boolean = true ) : void {
89
89
this . scrollCache [ this . lastTabShown ] = document . documentElement . scrollTop ;
90
90
91
91
// Set tab status
@@ -100,7 +100,7 @@ export class TriLayout extends Component {
100
100
101
101
// Set the scroll position from cache
102
102
if ( scroll ) {
103
- const pageHeader = document . querySelector ( 'header' ) ;
103
+ const pageHeader = document . querySelector ( 'header' ) as HTMLElement ;
104
104
const defaultScrollTop = pageHeader . getBoundingClientRect ( ) . bottom ;
105
105
document . documentElement . scrollTop = this . scrollCache [ tabName ] || defaultScrollTop ;
106
106
setTimeout ( ( ) => {
@@ -111,7 +111,7 @@ export class TriLayout extends Component {
111
111
this . lastTabShown = tabName ;
112
112
}
113
113
114
- setupSidebarScrollHandlers ( ) {
114
+ setupSidebarScrollHandlers ( ) : void {
115
115
for ( const sidebar of this . sidebarScrollContainers ) {
116
116
sidebar . addEventListener ( 'scroll' , ( ) => this . handleSidebarScroll ( sidebar ) , {
117
117
passive : true ,
@@ -126,13 +126,15 @@ export class TriLayout extends Component {
126
126
} ) ;
127
127
}
128
128
129
- handleSidebarScroll ( sidebar ) {
129
+ handleSidebarScroll ( sidebar : HTMLElement ) : void {
130
130
const scrollable = sidebar . clientHeight !== sidebar . scrollHeight ;
131
131
const atTop = sidebar . scrollTop === 0 ;
132
132
const atBottom = ( sidebar . scrollTop + sidebar . clientHeight ) === sidebar . scrollHeight ;
133
133
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
+ }
136
138
}
137
139
138
140
}
0 commit comments