]> BookStack Code Mirror - bookstack/blob - resources/js/components/editor-toolbox.js
Simplify ApiAuthException control flow
[bookstack] / resources / js / components / editor-toolbox.js
1 import {Component} from './component';
2
3 export class EditorToolbox extends Component {
4
5     setup() {
6         // Elements
7         this.container = this.$el;
8         this.buttons = this.$manyRefs.tabButton;
9         this.contentElements = this.$manyRefs.tabContent;
10         this.toggleButton = this.$refs.toggle;
11
12         this.setupListeners();
13
14         // Set the first tab as active on load
15         this.setActiveTab(this.contentElements[0].dataset.tabContent);
16     }
17
18     setupListeners() {
19         // Toolbox toggle button click
20         this.toggleButton.addEventListener('click', () => this.toggle());
21         // Tab button click
22         this.container.addEventListener('click', event => {
23             const button = event.target.closest('button');
24             if (this.buttons.includes(button)) {
25                 const name = button.dataset.tab;
26                 this.setActiveTab(name, true);
27             }
28         });
29     }
30
31     toggle() {
32         this.container.classList.toggle('open');
33         const expanded = this.container.classList.contains('open') ? 'true' : 'false';
34         this.toggleButton.setAttribute('aria-expanded', expanded);
35     }
36
37     setActiveTab(tabName, openToolbox = false) {
38         // Set button visibility
39         for (const button of this.buttons) {
40             button.classList.remove('active');
41             const bName = button.dataset.tab;
42             if (bName === tabName) button.classList.add('active');
43         }
44
45         // Set content visibility
46         for (const contentEl of this.contentElements) {
47             contentEl.style.display = 'none';
48             const cName = contentEl.dataset.tabContent;
49             if (cName === tabName) contentEl.style.display = 'block';
50         }
51
52         if (openToolbox && !this.container.classList.contains('open')) {
53             this.toggle();
54         }
55     }
56
57 }