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

87 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-01-12 19:05:09 +01:00
import BasicWidget from "./basic_widget.js";
export default class TabAwareWidget extends BasicWidget {
setTabContextListener({tabContext}) {
2020-01-12 19:05:09 +01:00
/** @var {TabContext} */
2020-01-16 22:44:36 +01:00
this.tabContext = tabContext;
2020-01-18 18:01:16 +01:00
this.noteSwitched();
2020-01-12 19:05:09 +01:00
}
isTab(tabId) {
return this.tabContext && this.tabContext.tabId === tabId;
}
isNote(noteId) {
2020-01-27 22:58:03 +01:00
return this.noteId === noteId;
}
get note() {
return this.tabContext && this.tabContext.note;
}
get noteId() {
return this.note && this.note.noteId;
}
2020-01-28 21:54:28 +01:00
get notePath() {
return this.tabContext && this.tabContext.notePath;
}
2020-02-02 18:46:50 +01:00
tabNoteSwitchedListener({tabId, notePath}) {
// if notePath does not match then the tabContext has been switched to another note in the mean time
if (this.isTab(tabId) && this.notePath === notePath) {
2020-01-18 18:01:16 +01:00
this.noteSwitched();
}
}
noteSwitched() {
this.refresh();
}
2020-01-18 18:01:16 +01:00
activeTabChanged() {
this.refresh();
}
2020-02-02 20:02:08 +01:00
async isEnabled() {
return !!this.note && this.tabContext.isActive();
2020-02-02 20:02:08 +01:00
}
async refresh() {
if (await this.isEnabled()) {
const start = Date.now();
2020-01-19 11:37:24 +01:00
this.toggle(true);
2020-02-02 20:02:08 +01:00
await this.refreshWithNote(this.note, this.notePath);
const end = Date.now();
if (glob.PROFILING_LOG && end - start > 10) {
2020-02-02 20:02:08 +01:00
console.log(`Refresh of ${this.componentId} took ${end-start}ms`);
}
2020-01-19 11:37:24 +01:00
}
else {
this.toggle(false);
}
}
2020-01-28 21:54:28 +01:00
refreshWithNote(note, notePath) {}
2020-01-12 19:05:09 +01:00
activeTabChangedListener() {
2020-02-07 21:43:02 +01:00
this.tabContext = this.tabManager.getActiveTabContext();
2020-01-12 19:05:09 +01:00
this.activeTabChanged();
}
treeCacheReloadedListener() {
this.refresh();
}
lazyLoadedListener() {
2020-02-06 20:04:43 +01:00
if (!this.tabContext) { // has not been loaded yet
2020-02-07 21:43:02 +01:00
this.tabContext = this.tabManager.getActiveTabContext();
2020-02-06 20:04:43 +01:00
}
this.refresh();
}
2020-01-12 19:05:09 +01:00
}