]> BookStack Code Mirror - bookstack/blob - resources/assets/js/components/tri-layout.js
Update maintenance.php
[bookstack] / resources / assets / js / components / tri-layout.js
1
2 class TriLayout {
3
4     constructor(elem) {
5         this.elem = elem;
6
7         this.lastLayoutType = 'none';
8         this.onDestroy = null;
9         this.scrollCache = {
10             'content': 0,
11             'info': 0,
12         };
13         this.lastTabShown = 'content';
14
15         // Bind any listeners
16         this.mobileTabClick = this.mobileTabClick.bind(this);
17
18         // Watch layout changes
19         this.updateLayout();
20         window.addEventListener('resize', event => {
21             this.updateLayout();
22         }, {passive: true});
23     }
24
25     updateLayout() {
26         let newLayout = 'tablet';
27         if (window.innerWidth <= 1000) newLayout =  'mobile';
28         if (window.innerWidth >= 1400) newLayout =  'desktop';
29         if (newLayout === this.lastLayoutType) return;
30
31         if (this.onDestroy) {
32             this.onDestroy();
33             this.onDestroy = null;
34         }
35
36         if (newLayout === 'desktop') {
37             this.setupDesktop();
38         } else if (newLayout === 'mobile') {
39             this.setupMobile();
40         }
41
42         this.lastLayoutType = newLayout;
43     }
44
45     setupMobile() {
46         const layoutTabs = document.querySelectorAll('[tri-layout-mobile-tab]');
47         for (let tab of layoutTabs) {
48             tab.addEventListener('click', this.mobileTabClick);
49         }
50
51         this.onDestroy = () => {
52             for (let tab of layoutTabs) {
53                 tab.removeEventListener('click', this.mobileTabClick);
54             }
55         }
56     }
57
58     setupDesktop() {
59         //
60     }
61
62
63     /**
64      * Action to run when the mobile info toggle bar is clicked/tapped
65      * @param event
66      */
67     mobileTabClick(event) {
68         const tab = event.target.getAttribute('tri-layout-mobile-tab');
69         this.scrollCache[this.lastTabShown] = document.documentElement.scrollTop;
70
71         // Set tab status
72         const activeTabs = document.querySelectorAll('.tri-layout-mobile-tab.active');
73         for (let tab of activeTabs) {
74             tab.classList.remove('active');
75         }
76         event.target.classList.add('active');
77
78         // Toggle section
79         const showInfo = (tab === 'info');
80         this.elem.classList.toggle('show-info', showInfo);
81
82         // Set the scroll position from cache
83         const pageHeader = document.querySelector('header');
84         const defaultScrollTop = pageHeader.getBoundingClientRect().bottom;
85         document.documentElement.scrollTop = this.scrollCache[tab] || defaultScrollTop;
86         setTimeout(() => {
87             document.documentElement.scrollTop = this.scrollCache[tab] || defaultScrollTop;
88         }, 50);
89
90         this.lastTabShown = tab;
91     }
92
93 }
94
95 export default TriLayout;