]> BookStack Code Mirror - bookstack/blob - resources/js/components/tabs.js
Added testing coverage for tag index
[bookstack] / resources / js / components / tabs.js
1 /**
2  * Tabs
3  * Works by matching 'tabToggle<Key>' with 'tabContent<Key>' sections.
4  * @extends {Component}
5  */
6 import {onSelect} from "../services/dom";
7
8 class Tabs {
9
10     setup() {
11         this.tabContentsByName = {};
12         this.tabButtonsByName = {};
13         this.allContents = [];
14         this.allButtons = [];
15
16         for (const [key, elems] of Object.entries(this.$manyRefs || {})) {
17             if (key.startsWith('toggle')) {
18                 const cleanKey = key.replace('toggle', '').toLowerCase();
19                 onSelect(elems, e => this.show(cleanKey));
20                 this.allButtons.push(...elems);
21                 this.tabButtonsByName[cleanKey] = elems;
22             }
23             if (key.startsWith('content')) {
24                 const cleanKey = key.replace('content', '').toLowerCase();
25                 this.tabContentsByName[cleanKey] = elems;
26                 this.allContents.push(...elems);
27             }
28         }
29     }
30
31     show(key) {
32         this.allContents.forEach(c => {
33             c.classList.add('hidden');
34             c.classList.remove('selected');
35         });
36         this.allButtons.forEach(b => b.classList.remove('selected'));
37
38         const contents = this.tabContentsByName[key] || [];
39         const buttons = this.tabButtonsByName[key] || [];
40         if (contents.length > 0) {
41             contents.forEach(c => {
42                 c.classList.remove('hidden')
43                 c.classList.add('selected')
44             });
45             buttons.forEach(b => b.classList.add('selected'));
46         }
47     }
48
49 }
50
51 export default Tabs;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.