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

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-01-14 20:27:40 +01:00
import TabAwareWidget from "./tab_aware_widget.js";
export default class TabCachingWidget extends TabAwareWidget {
constructor(appContext, widgetFactory) {
super(appContext);
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-08 17:44:34 +01:00
activeTabChangedListener(param) {
super.activeTabChangedListener(param);
// stop propagation of the event to the children, individual tab widget should not know about tab switching
// since they are per-tab
return false;
}
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) {
widget = this.widgets[this.tabContext.tabId] = this.widgetFactory();
2020-02-08 18:25:07 +01:00
this.children.push(widget);
this.$widget.after(widget.render());
2020-01-18 18:01:16 +01:00
widget.eventReceived('setTabContext', {tabContext: this.tabContext});
2020-01-14 20:27:40 +01:00
}
widget.toggle(true);
}
2020-01-19 11:37:24 +01:00
2020-01-19 21:12:53 +01:00
tabRemovedListener({tabId}) {
const widget = this.widgets[tabId];
if (widget) {
widget.remove();
}
}
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
&& this.tabContext && tabId === this.tabContext.tabId
&& this.widgets[tabId].isEnabled());
2020-01-19 11:37:24 +01:00
}
}
2020-01-14 20:27:40 +01:00
}