]> BookStack Code Mirror - bookstack/blob - resources/js/components/editor-toolbox.js
Comments: Added reference marker to comments
[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         this.editorWrapEl = this.container.closest('.page-editor');
12
13         this.setupListeners();
14
15         // Set the first tab as active on load
16         this.setActiveTab(this.contentElements[0].dataset.tabContent);
17     }
18
19     setupListeners() {
20         // Toolbox toggle button click
21         this.toggleButton.addEventListener('click', () => this.toggle());
22         // Tab button click
23         this.container.addEventListener('click', event => {
24             const button = event.target.closest('button');
25             if (this.buttons.includes(button)) {
26                 const name = button.dataset.tab;
27                 this.setActiveTab(name, true);
28             }
29         });
30     }
31
32     toggle() {
33         this.container.classList.toggle('open');
34         const isOpen = this.container.classList.contains('open');
35         this.toggleButton.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
36         this.editorWrapEl.classList.toggle('toolbox-open', isOpen);
37     }
38
39     setActiveTab(tabName, openToolbox = false) {
40         // Set button visibility
41         for (const button of this.buttons) {
42             button.classList.remove('active');
43             const bName = button.dataset.tab;
44             if (bName === tabName) button.classList.add('active');
45         }
46
47         // Set content visibility
48         for (const contentEl of this.contentElements) {
49             contentEl.style.display = 'none';
50             const cName = contentEl.dataset.tabContent;
51             if (cName === tabName) contentEl.style.display = 'block';
52         }
53
54         if (openToolbox && !this.container.classList.contains('open')) {
55             this.toggle();
56         }
57     }
58
59 }