Notes/src/public/javascripts/widgets/tab_caching_widget.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-01-14 20:27:40 +01:00
import TabAwareWidget from "./tab_aware_widget.js";
2020-02-16 20:09:59 +01:00
import keyboardActionsService from "../services/keyboard_actions.js";
2020-01-14 20:27:40 +01:00
export default class TabCachingWidget extends TabAwareWidget {
constructor(parent, widgetFactory) {
super(parent);
2020-01-14 20:27:40 +01:00
this.widgetFactory = widgetFactory;
this.widgets = {};
}
isEnabled() {
return this.tabContext && this.tabContext.isActive();
2020-02-08 20:53:07 +01:00
}
doRender() {
return this.$widget = $(`<div class="marker" style="display: none;">`);
2020-01-14 20:27:40 +01:00
}
2020-02-16 19:21:17 +01:00
async handleEventInChildren(name, data) {
2020-02-08 17:44:34 +01:00
// stop propagation of the event to the children, individual tab widget should not know about tab switching
// since they are per-tab
2020-02-12 20:31:31 +01:00
if (name !== 'activeTabChanged') {
2020-02-16 19:21:17 +01:00
await super.handleEventInChildren(name, data);
2020-02-12 20:31:31 +01:00
}
2020-02-08 17:44:34 +01:00
}
refreshWithNote() {
2020-01-14 20:27:40 +01:00
for (const widget of Object.values(this.widgets)) {
widget.toggle(false);
}
2020-01-24 17:54:47 +01:00
if (!this.tabContext) {
console.log(`No tabContext in widget ${this.componentId}.`);
2020-01-24 20:15:53 +01:00
2020-01-24 17:54:47 +01:00
return;
}
2020-01-14 20:27:40 +01:00
let widget = this.widgets[this.tabContext.tabId];
if (!widget) {
2020-02-15 09:43:47 +01:00
widget = this.widgets[this.tabContext.tabId] = this.widgetFactory(this);
2020-02-08 18:25:07 +01:00
this.children.push(widget);
2020-02-16 20:09:59 +01:00
const $renderedWidget = widget.render();
keyboardActionsService.updateDisplayedShortcuts($renderedWidget);
this.$widget.after($renderedWidget);
2020-01-18 18:01:16 +01:00
2020-02-16 19:21:17 +01:00
widget.handleEvent('setTabContext', {tabContext: this.tabContext});
2020-01-14 20:27:40 +01:00
}
2020-02-09 21:53:10 +01:00
widget.toggle(widget.isEnabled());
2020-01-14 20:27:40 +01:00
}
2020-01-19 11:37:24 +01:00
2020-02-16 19:23:49 +01:00
tabRemovedEvent({tabId}) {
2020-01-19 21:12:53 +01:00
const widget = this.widgets[tabId];
if (widget) {
widget.remove();
2020-02-09 21:13:05 +01:00
delete this.widgets[tabId];
this.children = this.children.filter(ch => ch !== widget);
2020-01-19 21:12:53 +01:00
}
}
toggle(show) {
2020-01-19 11:37:24 +01:00
for (const tabId in this.widgets) {
2020-02-08 20:53:07 +01:00
this.widgets[tabId].toggle(
show
2020-02-09 21:53:10 +01:00
&& this.isTab(tabId)
&& this.widgets[tabId].isEnabled());
2020-01-19 11:37:24 +01:00
}
}
2020-01-14 20:27:40 +01:00
}