import NoteContextAwareWidget from "../note_context_aware_widget.js"; const TPL = `
`; export default class RibbonContainer extends NoteContextAwareWidget { constructor() { super(); this.ribbonWidgets = []; this.buttonWidgets = []; } ribbon(widget) { super.child(widget); this.ribbonWidgets.push(widget); return this; } button(widget) { super.child(widget); this.buttonWidgets.push(widget); return this; } doRender() { this.$widget = $(TPL); this.$tabContainer = this.$widget.find('.ribbon-tab-container'); this.$buttonContainer = this.$widget.find('.ribbon-button-container'); this.$bodyContainer = this.$widget.find('.ribbon-body-container'); for (const ribbonWidget of this.ribbonWidgets) { this.$bodyContainer.append( $('
') .attr('data-ribbon-component-id', ribbonWidget.componentId) .append(ribbonWidget.render()) ); } for (const buttonWidget of this.buttonWidgets) { this.$buttonContainer.append(buttonWidget.render()); } this.$tabContainer.on('click', '.ribbon-tab-title', e => { const $ribbonTitle = $(e.target).closest('.ribbon-tab-title'); const activate = !$ribbonTitle.hasClass("active"); this.$tabContainer.find('.ribbon-tab-title').removeClass("active"); this.$bodyContainer.find('.ribbon-body').removeClass("active"); if (activate) { const ribbonComponendId = $ribbonTitle.attr('data-ribbon-component-id'); this.lastActiveComponentId = ribbonComponendId; this.$tabContainer.find(`.ribbon-tab-title[data-ribbon-component-id="${ribbonComponendId}"]`).addClass("active"); this.$bodyContainer.find(`.ribbon-body[data-ribbon-component-id="${ribbonComponendId}"]`).addClass("active"); const activeChild = this.getActiveRibbonWidget(); if (activeChild) { activeChild.handleEvent('noteSwitched', {noteContext: this.noteContext, notePath: this.notePath}); } } else { this.lastActiveComponentId = null; } }); } async refreshWithNote(note, noExplicitActivation = false) { this.lastNoteType = note.type; let $ribbonTabToActivate, $lastActiveRibbon; this.$tabContainer.empty(); for (const ribbonWidget of this.ribbonWidgets) { const ret = ribbonWidget.getTitle(note); if (!ret.show) { continue; } const $ribbonTitle = $('
') .attr('data-ribbon-component-id', ribbonWidget.componentId) .append($('') .addClass(ret.icon) .attr("title", ret.title)) .append(" ") .append($('').text(ret.title)); this.$tabContainer.append($ribbonTitle); this.$tabContainer.append('
'); if (ret.activate && !this.lastActiveComponentId && !$ribbonTabToActivate && !noExplicitActivation) { $ribbonTabToActivate = $ribbonTitle; } if (this.lastActiveComponentId === ribbonWidget.componentId) { $lastActiveRibbon = $ribbonTitle; } } this.$tabContainer.find('.ribbon-tab-title-icon').tooltip(); if (!$ribbonTabToActivate) { $ribbonTabToActivate = $lastActiveRibbon; } if ($ribbonTabToActivate) { $ribbonTabToActivate.trigger('click'); } else { this.$bodyContainer.find('.ribbon-body').removeClass("active"); } } refreshRibbonContainerCommand() { this.refreshWithNote(this.note, true); } async handleEventInChildren(name, data) { if (['activeContextChanged', 'setNoteContext'].includes(name)) { // won't trigger .refresh(); await super.handleEventInChildren('setNoteContext', data); } else { const activeRibbonWidget = this.getActiveRibbonWidget(); // forward events only to active ribbon tab, inactive ones don't need to be updated if (activeRibbonWidget) { await activeRibbonWidget.handleEvent(name, data); } for (const buttonWidget of this.buttonWidgets) { await buttonWidget.handleEvent(name, data); } } } entitiesReloadedEvent({loadResults}) { if (loadResults.isNoteReloaded(this.noteId) && this.lastNoteType !== this.note.type) { // note type influences the list of available ribbon tabs the most // check for type is so that we don't update on each title rename this.lastNoteType = this.note.type; this.refresh(); } } getActiveRibbonWidget() { return this.ribbonWidgets.find(ch => ch.componentId === this.lastActiveComponentId) } }