2023-04-18 17:20:02 -04:00
|
|
|
import {Component} from './component';
|
2017-09-24 13:30:21 -04:00
|
|
|
|
2022-11-16 08:04:22 -05:00
|
|
|
export class EditorToolbox extends Component {
|
|
|
|
|
|
|
|
setup() {
|
2017-09-24 13:30:21 -04:00
|
|
|
// Elements
|
2022-11-16 08:04:22 -05:00
|
|
|
this.container = this.$el;
|
|
|
|
this.buttons = this.$manyRefs.tabButton;
|
|
|
|
this.contentElements = this.$manyRefs.tabContent;
|
|
|
|
this.toggleButton = this.$refs.toggle;
|
2023-10-14 12:10:07 -04:00
|
|
|
this.editorWrapEl = this.container.closest('.page-editor');
|
2022-11-16 08:04:22 -05:00
|
|
|
|
|
|
|
this.setupListeners();
|
|
|
|
|
|
|
|
// Set the first tab as active on load
|
|
|
|
this.setActiveTab(this.contentElements[0].dataset.tabContent);
|
|
|
|
}
|
2017-09-24 13:30:21 -04:00
|
|
|
|
2022-11-16 08:04:22 -05:00
|
|
|
setupListeners() {
|
2017-09-24 13:30:21 -04:00
|
|
|
// Toolbox toggle button click
|
2022-11-16 08:04:22 -05:00
|
|
|
this.toggleButton.addEventListener('click', () => this.toggle());
|
2017-09-24 13:30:21 -04:00
|
|
|
// Tab button click
|
2022-11-16 08:04:22 -05: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-24 13:30:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
toggle() {
|
2022-11-16 08:04:22 -05:00
|
|
|
this.container.classList.toggle('open');
|
2023-10-14 12:10:07 -04:00
|
|
|
const isOpen = this.container.classList.contains('open');
|
|
|
|
this.toggleButton.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
|
|
|
|
this.editorWrapEl.classList.toggle('toolbox-open', isOpen);
|
2017-09-24 13:30:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
setActiveTab(tabName, openToolbox = false) {
|
|
|
|
// Set button visibility
|
2022-11-16 08:04:22 -05:00
|
|
|
for (const button of this.buttons) {
|
|
|
|
button.classList.remove('active');
|
2023-04-18 17:20:02 -04:00
|
|
|
const bName = button.dataset.tab;
|
2022-11-16 08:04:22 -05:00
|
|
|
if (bName === tabName) button.classList.add('active');
|
2017-09-24 13:30:21 -04:00
|
|
|
}
|
2022-11-16 08:04:22 -05:00
|
|
|
|
2017-09-24 13:30:21 -04:00
|
|
|
// Set content visibility
|
2022-11-16 08:04:22 -05: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-24 13:30:21 -04:00
|
|
|
}
|
|
|
|
|
2022-11-16 08:04:22 -05:00
|
|
|
if (openToolbox && !this.container.classList.contains('open')) {
|
2022-01-24 17:08:36 -05:00
|
|
|
this.toggle();
|
|
|
|
}
|
2017-09-24 13:30:21 -04:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:20:02 -04:00
|
|
|
}
|