2022-11-16 21:04:22 +08:00
|
|
|
import {Component} from "./component";
|
2017-09-25 01:30:21 +08:00
|
|
|
|
2022-11-16 21:04:22 +08:00
|
|
|
export class EditorToolbox extends Component {
|
|
|
|
|
|
|
|
setup() {
|
2017-09-25 01:30:21 +08:00
|
|
|
// Elements
|
2022-11-16 21:04:22 +08:00
|
|
|
this.container = this.$el;
|
|
|
|
this.buttons = this.$manyRefs.tabButton;
|
|
|
|
this.contentElements = this.$manyRefs.tabContent;
|
|
|
|
this.toggleButton = this.$refs.toggle;
|
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
|
|
|
|
// Set the first tab as active on load
|
|
|
|
this.setActiveTab(this.contentElements[0].dataset.tabContent);
|
|
|
|
}
|
2017-09-25 01:30:21 +08:00
|
|
|
|
2022-11-16 21:04:22 +08:00
|
|
|
setupListeners() {
|
2017-09-25 01:30:21 +08:00
|
|
|
// Toolbox toggle button click
|
2022-11-16 21:04:22 +08:00
|
|
|
this.toggleButton.addEventListener('click', () => this.toggle());
|
2017-09-25 01:30:21 +08:00
|
|
|
// Tab button click
|
2022-11-16 21:04:22 +08:00
|
|
|
this.container.addEventListener('click', event => {
|
|
|
|
const button = event.target.closest('button');
|
|
|
|
if (this.buttons.includes(button)) {
|
|
|
|
const name = button.dataset.tab;
|
|
|
|
this.setActiveTab(name, true);
|
|
|
|
}
|
2017-09-25 01:30:21 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
2022-11-16 21:04:22 +08:00
|
|
|
this.container.classList.toggle('open');
|
|
|
|
const expanded = this.container.classList.contains('open') ? 'true' : 'false';
|
2019-08-26 19:47:04 +08:00
|
|
|
this.toggleButton.setAttribute('aria-expanded', expanded);
|
2017-09-25 01:30:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setActiveTab(tabName, openToolbox = false) {
|
2022-11-16 21:04:22 +08:00
|
|
|
|
2017-09-25 01:30:21 +08:00
|
|
|
// Set button visibility
|
2022-11-16 21:04:22 +08:00
|
|
|
for (const button of this.buttons) {
|
|
|
|
button.classList.remove('active');
|
|
|
|
const bName = button.dataset.tab;
|
|
|
|
if (bName === tabName) button.classList.add('active');
|
2017-09-25 01:30:21 +08:00
|
|
|
}
|
2022-11-16 21:04:22 +08:00
|
|
|
|
2017-09-25 01:30:21 +08:00
|
|
|
// Set content visibility
|
2022-11-16 21:04:22 +08:00
|
|
|
for (const contentEl of this.contentElements) {
|
|
|
|
contentEl.style.display = 'none';
|
|
|
|
const cName = contentEl.dataset.tabContent;
|
|
|
|
if (cName === tabName) contentEl.style.display = 'block';
|
2017-09-25 01:30:21 +08:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:04:22 +08:00
|
|
|
if (openToolbox && !this.container.classList.contains('open')) {
|
2022-01-25 06:08:36 +08:00
|
|
|
this.toggle();
|
|
|
|
}
|
2017-09-25 01:30:21 +08:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:04:22 +08:00
|
|
|
}
|