2019-05-02 05:06:18 +08:00
|
|
|
import protectedSessionHolder from "./protected_session_holder.js";
|
|
|
|
import server from "./server.js";
|
|
|
|
import bundleService from "./bundle.js";
|
2019-05-05 04:44:25 +08:00
|
|
|
import Attributes from "./attributes.js";
|
2019-05-02 05:06:18 +08:00
|
|
|
import utils from "./utils.js";
|
2019-08-25 23:36:13 +08:00
|
|
|
import optionsService from "./options.js";
|
2020-01-12 19:30:30 +08:00
|
|
|
import appContext from "./app_context.js";
|
2020-01-16 04:36:01 +08:00
|
|
|
import treeUtils from "./tree_utils.js";
|
|
|
|
import noteDetailService from "./note_detail.js";
|
|
|
|
import Component from "../widgets/component.js";
|
2019-05-02 05:06:18 +08:00
|
|
|
|
2019-08-20 05:29:42 +08:00
|
|
|
let showSidebarInNewTab = true;
|
|
|
|
|
2019-08-25 23:36:13 +08:00
|
|
|
optionsService.addLoadListener(options => {
|
2019-08-23 05:31:02 +08:00
|
|
|
showSidebarInNewTab = options.is('showSidebarInNewTab');
|
2019-08-20 05:29:42 +08:00
|
|
|
});
|
|
|
|
|
2020-01-16 04:36:01 +08:00
|
|
|
class TabContext extends Component {
|
2019-05-12 01:44:58 +08:00
|
|
|
/**
|
2020-01-16 04:36:01 +08:00
|
|
|
* @param {AppContext} appContext
|
2020-01-13 02:05:09 +08:00
|
|
|
* @param {TabRowWidget} tabRow
|
2019-08-17 03:29:44 +08:00
|
|
|
* @param {object} state
|
2019-05-12 01:44:58 +08:00
|
|
|
*/
|
2020-01-16 04:36:01 +08:00
|
|
|
constructor(appContext, tabRow, state = {}) {
|
|
|
|
super(appContext);
|
|
|
|
|
2019-05-12 01:44:58 +08:00
|
|
|
this.tabRow = tabRow;
|
2019-08-17 03:29:44 +08:00
|
|
|
this.tabId = state.tabId || utils.randomString(4);
|
2019-09-05 03:30:11 +08:00
|
|
|
this.state = state;
|
2019-09-05 04:13:22 +08:00
|
|
|
|
2020-01-16 04:36:01 +08:00
|
|
|
this.attributes = new Attributes(this.appContext, this);
|
|
|
|
|
|
|
|
this.children.push(this.attributes);
|
2020-01-21 03:51:22 +08:00
|
|
|
|
|
|
|
this.trigger('tabOpened', {tabId: this.tabId});
|
2019-05-03 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2020-01-16 04:36:01 +08:00
|
|
|
async setNote(notePath) {
|
2020-01-20 03:18:02 +08:00
|
|
|
await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true);
|
|
|
|
|
2019-09-05 04:45:12 +08:00
|
|
|
this.notePath = notePath;
|
2020-01-16 04:36:01 +08:00
|
|
|
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
|
|
|
|
|
|
|
|
/** @property {NoteFull} */
|
|
|
|
this.note = await noteDetailService.loadNote(noteId);
|
|
|
|
|
2020-01-19 01:01:16 +08:00
|
|
|
//this.cleanup(); // esp. on windows autocomplete is not getting closed automatically
|
2019-06-27 03:08:54 +08:00
|
|
|
|
2019-05-15 04:29:47 +08:00
|
|
|
setTimeout(async () => {
|
|
|
|
// we include the note into recent list only if the user stayed on the note at least 5 seconds
|
2019-05-22 03:47:28 +08:00
|
|
|
if (notePath && notePath === this.notePath) {
|
|
|
|
await server.post('recent-notes', {
|
2019-09-05 04:45:12 +08:00
|
|
|
noteId: this.note.noteId,
|
2019-05-22 03:47:28 +08:00
|
|
|
notePath: this.notePath
|
|
|
|
});
|
2019-05-15 04:29:47 +08:00
|
|
|
}
|
|
|
|
}, 5000);
|
|
|
|
|
2019-09-05 04:13:22 +08:00
|
|
|
bundleService.executeRelationBundles(this.note, 'runOnNoteView', this);
|
2020-01-03 02:03:54 +08:00
|
|
|
|
2020-01-19 18:03:34 +08:00
|
|
|
if (this.note.isProtected && protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
// FIXME: there are probably more places where this should be done
|
|
|
|
protectedSessionHolder.touchProtectedSession();
|
|
|
|
}
|
|
|
|
|
2020-01-19 01:01:16 +08:00
|
|
|
this.trigger('tabNoteSwitched', {tabId: this.tabId});
|
2019-05-14 04:08:06 +08:00
|
|
|
}
|
|
|
|
|
2020-01-20 04:12:53 +08:00
|
|
|
async remove() {
|
|
|
|
await this.trigger('beforeTabRemove', {tabId: this.tabId}, true);
|
|
|
|
|
|
|
|
this.trigger('tabRemoved', {tabId: this.tabId});
|
|
|
|
}
|
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
async saveNote() {
|
2020-01-14 04:48:44 +08:00
|
|
|
return; // FIXME
|
|
|
|
|
2019-05-02 04:19:29 +08:00
|
|
|
if (this.note.isProtected && !protectedSessionHolder.isProtectedSessionAvailable()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.note.title = this.$noteTitle.val();
|
2019-05-22 02:24:40 +08:00
|
|
|
this.note.content = this.getComponent().getContent();
|
2019-05-02 04:19:29 +08:00
|
|
|
|
|
|
|
// it's important to set the flag back to false immediatelly after retrieving title and content
|
|
|
|
// otherwise we might overwrite another change (especially async code)
|
|
|
|
this.isNoteChanged = false;
|
|
|
|
|
2019-08-07 04:39:27 +08:00
|
|
|
const resp = await server.put('notes/' + this.note.noteId, this.note.dto);
|
|
|
|
|
|
|
|
this.note.dateModified = resp.dateModified;
|
|
|
|
this.note.utcDateModified = resp.utcDateModified;
|
2019-05-02 04:19:29 +08:00
|
|
|
|
|
|
|
if (this.note.isProtected) {
|
|
|
|
protectedSessionHolder.touchProtectedSession();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 16:04:03 +08:00
|
|
|
getTabState() {
|
|
|
|
if (!this.notePath) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
tabId: this.tabId,
|
|
|
|
notePath: this.notePath,
|
2020-01-16 05:27:52 +08:00
|
|
|
active: this.tabRow.activeTabId === this.tabId
|
2019-08-15 16:04:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stateChanged() {
|
2020-01-12 19:48:17 +08:00
|
|
|
appContext.openTabsChanged();
|
2019-08-15 16:04:03 +08:00
|
|
|
}
|
2019-05-02 04:19:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
export default TabContext;
|