]> BookStack Code Mirror - bookstack/blob - resources/js/components/editor-toolbox.js
Finished updating remainder of JS components to new system
[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
39         // Set button visibility
40         for (const button of this.buttons) {
41             button.classList.remove('active');
42             const bName =  button.dataset.tab;
43             if (bName === tabName) button.classList.add('active');
44         }
45
46         // Set content visibility
47         for (const contentEl of this.contentElements) {
48             contentEl.style.display = 'none';
49             const cName = contentEl.dataset.tabContent;
50             if (cName === tabName) contentEl.style.display = 'block';
51         }
52
53         if (openToolbox && !this.container.classList.contains('open')) {
54             this.toggle();
55         }
56     }
57
58 }