2019-05-02 05:06:18 +08:00
|
|
|
import treeService from "./tree.js";
|
|
|
|
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";
|
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
|
|
|
});
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
class TabContext {
|
2019-05-12 01:44:58 +08:00
|
|
|
/**
|
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
|
|
|
*/
|
2019-08-17 03:29:44 +08:00
|
|
|
constructor(tabRow, state = {}) {
|
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-05-15 04:29:47 +08:00
|
|
|
this.$tab = $(this.tabRow.addTab(this.tabId));
|
2019-09-05 03:30:11 +08:00
|
|
|
this.initialized = false;
|
|
|
|
this.state = state;
|
|
|
|
}
|
|
|
|
|
2019-09-05 04:13:22 +08:00
|
|
|
async initTabContent() {
|
2019-09-05 03:30:11 +08:00
|
|
|
if (this.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-05 16:59:34 +08:00
|
|
|
|
2019-09-05 04:13:22 +08:00
|
|
|
this.initialized = true;
|
|
|
|
|
2020-01-14 04:48:44 +08:00
|
|
|
this.$tabContent = $("<div>"); // FIXME
|
2019-05-05 16:59:34 +08:00
|
|
|
|
2019-05-04 20:34:03 +08:00
|
|
|
this.noteChangeDisabled = false;
|
2019-05-02 04:19:29 +08:00
|
|
|
this.isNoteChanged = false;
|
2019-05-05 04:44:25 +08:00
|
|
|
this.attributes = new Attributes(this);
|
2019-05-03 04:24:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 01:49:19 +08:00
|
|
|
async setNote(note, notePath) {
|
2019-08-17 17:28:36 +08:00
|
|
|
/** @property {NoteFull} */
|
2019-05-03 04:24:43 +08:00
|
|
|
this.note = note;
|
2019-09-05 04:45:12 +08:00
|
|
|
this.notePath = notePath;
|
|
|
|
this.tabRow.updateTab(this.$tab[0], {title: this.note.title});
|
2019-05-05 04:44:25 +08:00
|
|
|
|
2019-09-05 03:30:11 +08:00
|
|
|
if (!this.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-05 16:59:34 +08:00
|
|
|
|
2019-09-07 19:09:32 +08:00
|
|
|
if (utils.isDesktop()) {
|
|
|
|
this.attributes.refreshAttributes();
|
|
|
|
} else {
|
|
|
|
// mobile usually doesn't need attributes so we just invalidate
|
|
|
|
this.attributes.invalidateAttributes();
|
|
|
|
}
|
|
|
|
|
2019-05-14 04:08:06 +08:00
|
|
|
this.setupClasses();
|
|
|
|
|
2019-05-15 04:29:47 +08:00
|
|
|
this.setCurrentNotePathToHash();
|
|
|
|
|
2019-09-05 03:30:11 +08:00
|
|
|
this.noteChangeDisabled = true;
|
|
|
|
|
|
|
|
try {
|
2020-01-14 04:48:44 +08:00
|
|
|
|
2019-09-05 03:30:11 +08:00
|
|
|
} finally {
|
|
|
|
this.noteChangeDisabled = false;
|
|
|
|
}
|
|
|
|
|
2019-05-30 01:55:05 +08:00
|
|
|
this.setTitleBar();
|
|
|
|
|
2019-10-02 03:11:11 +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
|
|
|
|
|
|
|
// after loading new note make sure editor is scrolled to the top
|
2020-01-14 04:48:44 +08:00
|
|
|
// FIXME
|
|
|
|
//this.getComponent().scrollToTop();
|
2020-01-13 06:03:55 +08:00
|
|
|
|
|
|
|
appContext.trigger('activeNoteChanged');
|
2019-05-14 04:08:06 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 03:30:11 +08:00
|
|
|
async show() {
|
|
|
|
if (!this.initialized) {
|
2019-09-05 04:13:22 +08:00
|
|
|
await this.initTabContent();
|
2019-09-05 03:30:11 +08:00
|
|
|
|
|
|
|
if (this.note) {
|
|
|
|
await this.setNote(this.note, this.notePath);
|
|
|
|
}
|
2019-09-05 04:45:12 +08:00
|
|
|
else {
|
2020-01-14 04:48:44 +08:00
|
|
|
// FIXME
|
2019-09-05 04:45:12 +08:00
|
|
|
await this.renderComponent(); // render empty page
|
|
|
|
}
|
2019-09-05 03:30:11 +08:00
|
|
|
}
|
|
|
|
|
2019-05-15 04:29:47 +08:00
|
|
|
this.setCurrentNotePathToHash();
|
2019-05-30 01:55:05 +08:00
|
|
|
this.setTitleBar();
|
|
|
|
}
|
|
|
|
|
2019-10-06 02:27:30 +08:00
|
|
|
async renderComponent(disableAutoBook = false) {
|
2020-01-14 04:48:44 +08:00
|
|
|
// FIXME
|
2019-09-05 03:30:11 +08:00
|
|
|
}
|
|
|
|
|
2019-05-30 01:55:05 +08:00
|
|
|
setTitleBar() {
|
|
|
|
if (!this.$tabContent.is(":visible")) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-15 04:29:47 +08:00
|
|
|
|
|
|
|
document.title = "Trilium Notes";
|
|
|
|
|
|
|
|
if (this.note) {
|
|
|
|
// it helps navigating in history if note title is included in the title
|
|
|
|
document.title += " - " + this.note.title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hide() {
|
2019-09-05 03:30:11 +08:00
|
|
|
if (this.initialized) {
|
|
|
|
this.$tabContent.hide();
|
|
|
|
}
|
2019-05-15 04:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
setCurrentNotePathToHash() {
|
2019-11-27 03:42:34 +08:00
|
|
|
if (this.isActive()) {
|
2019-05-15 04:29:47 +08:00
|
|
|
document.location.hash = (this.notePath || "") + "-" + this.tabId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-27 03:42:34 +08:00
|
|
|
isActive() {
|
|
|
|
return this.$tab[0] === this.tabRow.activeTabEl;
|
|
|
|
}
|
|
|
|
|
2019-05-14 04:08:06 +08:00
|
|
|
setupClasses() {
|
|
|
|
for (const clazz of Array.from(this.$tab[0].classList)) { // create copy to safely iterate over while removing classes
|
|
|
|
if (clazz !== 'note-tab') {
|
|
|
|
this.$tab.removeClass(clazz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 01:55:24 +08:00
|
|
|
for (const clazz of Array.from(this.$tabContent[0].classList)) { // create copy to safely iterate over while removing classes
|
2019-05-14 04:08:06 +08:00
|
|
|
if (clazz !== 'note-tab-content') {
|
2019-05-09 01:55:24 +08:00
|
|
|
this.$tabContent.removeClass(clazz);
|
2019-05-06 02:45:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-14 04:08:06 +08:00
|
|
|
this.$tab.addClass(this.note.cssClass);
|
|
|
|
this.$tab.addClass(utils.getNoteTypeClass(this.note.type));
|
|
|
|
this.$tab.addClass(utils.getMimeTypeClass(this.note.mime));
|
|
|
|
|
|
|
|
this.$tabContent.addClass(this.note.cssClass);
|
2019-05-09 01:55:24 +08:00
|
|
|
this.$tabContent.addClass(utils.getNoteTypeClass(this.note.type));
|
|
|
|
this.$tabContent.addClass(utils.getMimeTypeClass(this.note.mime));
|
2019-05-23 04:26:55 +08:00
|
|
|
|
|
|
|
this.$tabContent.toggleClass("protected", this.note.isProtected);
|
2019-05-02 04:19:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-06 01:48:30 +08:00
|
|
|
getComponent() {
|
2020-01-14 04:48:44 +08:00
|
|
|
// FIXME
|
2019-08-27 01:49:19 +08:00
|
|
|
}
|
|
|
|
|
2019-10-06 02:27:30 +08:00
|
|
|
getComponentType(disableAutoBook) {
|
2020-01-14 04:48:44 +08:00
|
|
|
// FIXME
|
2019-05-02 04:19:29 +08:00
|
|
|
}
|
|
|
|
|
2019-05-22 02:24:40 +08:00
|
|
|
async activate() {
|
|
|
|
await this.tabRow.activateTab(this.$tab[0]);
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
treeService.setNoteTitle(this.note.noteId, this.note.title);
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2020-01-13 02:05:09 +08:00
|
|
|
// FIXME trigger "noteSaved" event so that title indicator is triggered
|
|
|
|
this.eventReceived('noteSaved');
|
2019-05-02 04:19:29 +08:00
|
|
|
|
|
|
|
// run async
|
2019-05-30 03:48:48 +08:00
|
|
|
bundleService.executeRelationBundles(this.note, 'runOnNoteChange', this);
|
2019-05-02 04:19:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async saveNoteIfChanged() {
|
|
|
|
if (this.isNoteChanged) {
|
|
|
|
await this.saveNote();
|
2019-05-21 04:25:04 +08:00
|
|
|
|
2020-01-12 19:30:30 +08:00
|
|
|
appContext.refreshTabs(this.tabId, this.note.noteId);
|
2019-05-02 04:19:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
noteChanged() {
|
2019-05-04 20:34:03 +08:00
|
|
|
if (this.noteChangeDisabled) {
|
2019-05-02 04:19:29 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.isNoteChanged = true;
|
|
|
|
|
2020-01-14 04:48:44 +08:00
|
|
|
// FIXME: trigger noteChanged event
|
|
|
|
//this.$savedIndicator.fadeOut();
|
2019-05-02 04:19:29 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 04:45:12 +08:00
|
|
|
async remove() {
|
2019-10-22 03:22:53 +08:00
|
|
|
if (this.$tabContent) {
|
|
|
|
// sometimes there are orphan autocompletes after closing the tab
|
|
|
|
this.cleanup();
|
2019-09-05 04:45:12 +08:00
|
|
|
|
2019-10-22 03:22:53 +08:00
|
|
|
await this.saveNoteIfChanged();
|
|
|
|
this.$tabContent.remove();
|
|
|
|
}
|
2019-09-05 04:45:12 +08:00
|
|
|
}
|
|
|
|
|
2019-10-02 03:11:11 +08:00
|
|
|
cleanup() {
|
2019-10-16 01:42:39 +08:00
|
|
|
if (this.$tabContent && utils.isDesktop()) {
|
2019-07-01 02:14:57 +08:00
|
|
|
this.$tabContent.find('.aa-input').autocomplete('close');
|
2019-10-02 03:11:11 +08:00
|
|
|
|
|
|
|
$('.note-tooltip').remove();
|
2019-07-01 02:14:57 +08:00
|
|
|
}
|
2019-06-27 03:08:54 +08:00
|
|
|
}
|
2019-08-07 04:39:27 +08:00
|
|
|
|
2019-09-04 03:31:39 +08:00
|
|
|
eventReceived(name, data) {
|
2019-09-05 03:30:11 +08:00
|
|
|
if (!this.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-04 03:31:39 +08:00
|
|
|
this.attributes.eventReceived(name, data);
|
2019-08-07 04:39:27 +08:00
|
|
|
}
|
2019-08-15 16:04:03 +08:00
|
|
|
|
|
|
|
getTabState() {
|
|
|
|
if (!this.notePath) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
tabId: this.tabId,
|
|
|
|
notePath: this.notePath,
|
2020-01-15 04:23:32 +08:00
|
|
|
active: this.tabRow.activeTabEl === this.$tab[0]
|
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;
|