Skip to content

Build your personal knowledge base with Trilium Notes

merged tree utils into tree service

zadam3254b551d8f7
diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.js
index d4bc8f7952..12a5f99a7e 100644
--- a/src/public/javascripts/desktop.js
+++ b/src/public/javascripts/desktop.js
@@ -12,7 +12,6 @@ import ScriptContext from './services/script_context.js';
import sync from './services/sync.js';
import treeService from './services/tree.js';
import treeChanges from './services/branches.js';
-import treeUtils from './services/tree_utils.js';
import utils from './services/utils.js';
import server from './services/server.js';
import Entrypoints from './services/entrypoints.js';
diff --git a/src/public/javascripts/dialogs/add_link.js b/src/public/javascripts/dialogs/add_link.js
index be8e8f2440..a4e59d87cb 100644
--- a/src/public/javascripts/dialogs/add_link.js
+++ b/src/public/javascripts/dialogs/add_link.js
@@ -1,6 +1,6 @@
import linkService from '../services/link.js';
import noteDetailService from '../services/note_detail.js';
-import treeUtils from '../services/tree_utils.js';
+import treeService from '../services/tree.js';
import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
@@ -23,7 +23,7 @@ export async function showDialog() {
$linkTitle.val('');
async function setDefaultLinkTitle(noteId) {
- const noteTitle = await treeUtils.getNoteTitle(noteId);
+ const noteTitle = await treeService.getNoteTitle(noteId);
$linkTitle.val(noteTitle);
}
@@ -35,7 +35,7 @@ export async function showDialog() {
return false;
}
- const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path);
+ const noteId = treeService.getNoteIdFromNotePath(suggestion.path);
if (noteId) {
setDefaultLinkTitle(noteId);
@@ -43,7 +43,7 @@ export async function showDialog() {
});
$autoComplete.on('autocomplete:cursorchanged', function(event, suggestion, dataset) {
- const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path);
+ const noteId = treeService.getNoteIdFromNotePath(suggestion.path);
setDefaultLinkTitle(noteId);
});
diff --git a/src/public/javascripts/dialogs/attributes.js b/src/public/javascripts/dialogs/attributes.js
index 939eb6316c..add45be9d0 100644
--- a/src/public/javascripts/dialogs/attributes.js
+++ b/src/public/javascripts/dialogs/attributes.js
@@ -1,7 +1,7 @@
import noteDetailService from '../services/note_detail.js';
import server from '../services/server.js';
import toastService from "../services/toast.js";
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import attributeAutocompleteService from "../services/attribute_autocomplete.js";
import utils from "../services/utils.js";
import linkService from "../services/link.js";
@@ -66,7 +66,7 @@ function AttributesModel() {
for (const attr of ownedAttributes) {
attr.labelValue = attr.type === 'label' ? attr.value : '';
- attr.relationValue = attr.type === 'relation' ? (await treeUtils.getNoteTitle(attr.value)) : '';
+ attr.relationValue = attr.type === 'relation' ? (await treeService.getNoteTitle(attr.value)) : '';
attr.selectedPath = attr.type === 'relation' ? attr.value : '';
attr.labelDefinition = (attr.type === 'label-definition' && attr.value) ? attr.value : {
labelType: "text",
@@ -151,7 +151,7 @@ function AttributesModel() {
attr.value = attr.labelValue;
}
else if (attr.type === 'relation') {
- attr.value = treeUtils.getNoteIdFromNotePath(attr.selectedPath);
+ attr.value = treeService.getNoteIdFromNotePath(attr.selectedPath);
}
else if (attr.type === 'label-definition') {
attr.value = attr.labelDefinition;
diff --git a/src/public/javascripts/dialogs/branch_prefix.js b/src/public/javascripts/dialogs/branch_prefix.js
index e9aa1cd649..0b6112b8da 100644
--- a/src/public/javascripts/dialogs/branch_prefix.js
+++ b/src/public/javascripts/dialogs/branch_prefix.js
@@ -1,7 +1,7 @@
import treeService from '../services/tree.js';
import server from '../services/server.js';
import treeCache from "../services/tree_cache.js";
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import toastService from "../services/toast.js";
import utils from "../services/utils.js";
@@ -34,7 +34,7 @@ export async function showDialog(node) {
$treePrefixInput.val(branch.prefix);
- const noteTitle = await treeUtils.getNoteTitle(node.data.noteId);
+ const noteTitle = await treeService.getNoteTitle(node.data.noteId);
$noteTitle.text(" - " + noteTitle);
}
diff --git a/src/public/javascripts/dialogs/clone_to.js b/src/public/javascripts/dialogs/clone_to.js
index 147e5c8bd2..30bfc65c17 100644
--- a/src/public/javascripts/dialogs/clone_to.js
+++ b/src/public/javascripts/dialogs/clone_to.js
@@ -1,7 +1,7 @@
import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
import cloningService from "../services/cloning.js";
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import toastService from "../services/toast.js";
import treeCache from "../services/tree_cache.js";
@@ -43,7 +43,7 @@ export async function showDialog(noteIds) {
}
async function cloneNotesTo(notePath) {
- const targetNoteId = treeUtils.getNoteIdFromNotePath(notePath);
+ const targetNoteId = treeService.getNoteIdFromNotePath(notePath);
for (const cloneNoteId of clonedNoteIds) {
await cloningService.cloneNoteTo(cloneNoteId, targetNoteId, $clonePrefix.val());
diff --git a/src/public/javascripts/dialogs/export.js b/src/public/javascripts/dialogs/export.js
index 895eb46ea5..1cf094e6aa 100644
--- a/src/public/javascripts/dialogs/export.js
+++ b/src/public/javascripts/dialogs/export.js
@@ -1,4 +1,4 @@
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import utils from "../services/utils.js";
import ws from "../services/ws.js";
import toastService from "../services/toast.js";
@@ -43,11 +43,11 @@ export async function showDialog(notePath, defaultType) {
$dialog.modal();
- const {noteId, parentNoteId} = treeUtils.getNoteIdAndParentIdFromNotePath(notePath);
+ const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
branchId = await treeCache.getBranchId(parentNoteId, noteId);
- const noteTitle = await treeUtils.getNoteTitle(noteId);
+ const noteTitle = await treeService.getNoteTitle(noteId);
$noteTitle.html(noteTitle);
}
diff --git a/src/public/javascripts/dialogs/import.js b/src/public/javascripts/dialogs/import.js
index 828eb1d594..150f1b748a 100644
--- a/src/public/javascripts/dialogs/import.js
+++ b/src/public/javascripts/dialogs/import.js
@@ -1,5 +1,5 @@
import utils from '../services/utils.js';
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import importService from "../services/import.js";
const $dialog = $("#import-dialog");
@@ -30,7 +30,7 @@ export async function showDialog(noteId) {
parentNoteId = noteId;
- $noteTitle.text(await treeUtils.getNoteTitle(parentNoteId));
+ $noteTitle.text(await treeService.getNoteTitle(parentNoteId));
$dialog.modal();
}
diff --git a/src/public/javascripts/dialogs/include_note.js b/src/public/javascripts/dialogs/include_note.js
index bf89179b50..ec337518d9 100644
--- a/src/public/javascripts/dialogs/include_note.js
+++ b/src/public/javascripts/dialogs/include_note.js
@@ -1,4 +1,4 @@
-import treeUtils from '../services/tree_utils.js';
+import treeService from '../services/tree.js';
import noteAutocompleteService from '../services/note_autocomplete.js';
import utils from "../services/utils.js";
@@ -29,7 +29,7 @@ $form.on('submit', () => {
$dialog.modal('hide');
if (callback) {
- callback(treeUtils.getNoteIdFromNotePath(notePath));
+ callback(treeService.getNoteIdFromNotePath(notePath));
}
}
else {
diff --git a/src/public/javascripts/dialogs/move_to.js b/src/public/javascripts/dialogs/move_to.js
index 78e3318fb5..125eb36f6e 100644
--- a/src/public/javascripts/dialogs/move_to.js
+++ b/src/public/javascripts/dialogs/move_to.js
@@ -1,7 +1,7 @@
import noteAutocompleteService from "../services/note_autocomplete.js";
import utils from "../services/utils.js";
import cloningService from "../services/cloning.js";
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import toastService from "../services/toast.js";
import treeCache from "../services/tree_cache.js";
import treeChangesService from "../services/branches.js";
diff --git a/src/public/javascripts/mobile.js b/src/public/javascripts/mobile.js
index 0c0b091f73..2b645575c7 100644
--- a/src/public/javascripts/mobile.js
+++ b/src/public/javascripts/mobile.js
@@ -5,7 +5,7 @@ import treeBuilder from "./services/tree_builder.js";
import contextMenuWidget from "./services/context_menu.js";
import treeChangesService from "./services/branches.js";
import utils from "./services/utils.js";
-import treeUtils from "./services/tree_utils.js";
+import treeService from "./services/tree.js";
import appContext from "./services/app_context.js";
window.glob.isDesktop = utils.isDesktop;
@@ -60,7 +60,7 @@ async function showTree() {
showDetailPane();
- const notePath = await treeUtils.getNotePath(node);
+ const notePath = await treeService.getNotePath(node);
},
expand: (event, data) => treeService.setExpandedToServer(data.node.data.branchId, true),
@@ -109,7 +109,7 @@ $detail.on("click", ".note-menu-button", async e => {
selectContextMenuItem: async (event, cmd) => {
if (cmd === "insertNoteAfter") {
const parentNoteId = node.data.parentNoteId;
- const isProtected = await treeUtils.getParentProtectedStatus(node);
+ const isProtected = await treeService.getParentProtectedStatus(node);
treeService.createNote(node, parentNoteId, 'after', { isProtected: isProtected });
}
diff --git a/src/public/javascripts/services/app_context.js b/src/public/javascripts/services/app_context.js
index ea95fd7d14..d532683187 100644
--- a/src/public/javascripts/services/app_context.js
+++ b/src/public/javascripts/services/app_context.js
@@ -2,8 +2,6 @@ import GlobalButtonsWidget from "../widgets/global_buttons.js";
import SearchBoxWidget from "../widgets/search_box.js";
import SearchResultsWidget from "../widgets/search_results.js";
import NoteTreeWidget from "../widgets/note_tree.js";
-import treeService from "./tree.js";
-import noteDetailService from "./note_detail.js";
import TabContext from "./tab_context.js";
import server from "./server.js";
import TabRowWidget from "../widgets/tab_row.js";
@@ -22,7 +20,7 @@ import GlobalMenuWidget from "../widgets/global_menu.js";
import RowFlexContainer from "../widgets/row_flex_container.js";
import StandardTopWidget from "../widgets/standard_top_widget.js";
import treeCache from "./tree_cache.js";
-import treeUtils from "./tree_utils.js";
+import treeService from "./tree.js";
import NotePathsWidget from "../widgets/note_paths.js";
import RunScriptButtonsWidget from "../widgets/run_script_buttons.js";
import ProtectedNoteSwitchWidget from "../widgets/protected_note_switch.js";
@@ -163,7 +161,7 @@ class AppContext {
const activeTabContext = this.getActiveTabContext();
if (activeTabContext && activeTabContext.notePath) {
- const note = await treeCache.getNote(treeUtils.getNoteIdFromNotePath(activeTabContext.notePath));
+ const note = await treeCache.getNote(treeService.getNoteIdFromNotePath(activeTabContext.notePath));
// it helps navigating in history if note title is included in the title
document.title += " - " + note.title;
diff --git a/src/public/javascripts/services/attributes.js b/src/public/javascripts/services/attributes.js
index 54f8702efe..0b0d9f1d48 100644
--- a/src/public/javascripts/services/attributes.js
+++ b/src/public/javascripts/services/attributes.js
@@ -1,6 +1,6 @@
import server from "./server.js";
import ws from "./ws.js";
-import treeUtils from "./tree_utils.js";
+import treeService from "./tree.js";
import noteAutocompleteService from "./note_autocomplete.js";
import Component from "../widgets/component.js";
import utils from "./utils.js";
diff --git a/src/public/javascripts/services/branches.js b/src/public/javascripts/services/branches.js
index cef7655b1c..b197ced7db 100644
--- a/src/public/javascripts/services/branches.js
+++ b/src/public/javascripts/services/branches.js
@@ -3,7 +3,6 @@ import utils from './utils.js';
import server from './server.js';
import toastService from "./toast.js";
import treeCache from "./tree_cache.js";
-import treeUtils from "./tree_utils.js";
import hoistedNoteService from "./hoisted_note.js";
import noteDetailService from "./note_detail.js";
import ws from "./ws.js";
@@ -83,7 +82,7 @@ async function getNextNode(nodes) {
next = nodes[0].getParent();
}
- return treeUtils.getNotePath(next);
+ return treeService.getNotePath(next);
}
async function deleteNodes(branchIdsToDelete) {
diff --git a/src/public/javascripts/services/link.js b/src/public/javascripts/services/link.js
index 8a21a4040b..bc4f758907 100644
--- a/src/public/javascripts/services/link.js
+++ b/src/public/javascripts/services/link.js
@@ -1,7 +1,5 @@
import treeService from './tree.js';
-import treeUtils from './tree_utils.js';
import contextMenuService from "./context_menu.js";
-import noteDetailService from "./note_detail.js";
import appContext from "./app_context.js";
function getNotePathFromUrl(url) {
@@ -22,9 +20,9 @@ async function createNoteLink(notePath, options = {}) {
const showNotePath = options.showNotePath === undefined ? false : options.showNotePath;
if (!noteTitle) {
- const {noteId, parentNoteId} = treeUtils.getNoteIdAndParentIdFromNotePath(notePath);
+ const {noteId, parentNoteId} = treeService.getNoteIdAndParentIdFromNotePath(notePath);
- noteTitle = await treeUtils.getNoteTitle(noteId, parentNoteId);
+ noteTitle = await treeService.getNoteTitle(noteId, parentNoteId);
}
const $noteLink = $("<a>", {
@@ -49,7 +47,7 @@ async function createNoteLink(notePath, options = {}) {
const parentNotePath = noteIds.join("/").trim();
if (parentNotePath) {
- $container.append($("<small>").text(" (" + await treeUtils.getNotePathTitle(parentNotePath) + ")"));
+ $container.append($("<small>").text(" (" + await treeService.getNotePathTitle(parentNotePath) + ")"));
}
}
}
diff --git a/src/public/javascripts/services/note_tooltip.js b/src/public/javascripts/services/note_tooltip.js
index f998c5f02f..25bec7e886 100644
--- a/src/public/javascripts/services/note_tooltip.js
+++ b/src/public/javascripts/services/note_tooltip.js
@@ -1,5 +1,5 @@
import noteDetailService from "./note_detail.js";
-import treeUtils from "./tree_utils.js";
+import treeService from "./tree.js";
import linkService from "./link.js";
import server from "./server.js";
@@ -40,7 +40,7 @@ async function mouseEnterHandler() {
return;
}
- const noteId = treeUtils.getNoteIdFromNotePath(notePath);
+ const noteId = treeService.getNoteIdFromNotePath(notePath);
const notePromise = noteDetailService.loadNote(noteId);
const attributePromise = server.get(`notes/${noteId}/attributes`);
diff --git a/src/public/javascripts/services/tab_context.js b/src/public/javascripts/services/tab_context.js
index 8230fc01ab..eed5f2dc2d 100644
--- a/src/public/javascripts/services/tab_context.js
+++ b/src/public/javascripts/services/tab_context.js
@@ -5,10 +5,9 @@ import Attributes from "./attributes.js";
import utils from "./utils.js";
import optionsService from "./options.js";
import appContext from "./app_context.js";
-import treeUtils from "./tree_utils.js";
+import treeService from "./tree.js";
import noteDetailService from "./note_detail.js";
import Component from "../widgets/component.js";
-import treeService from "./tree.js";
let showSidebarInNewTab = true;
@@ -52,7 +51,7 @@ class TabContext extends Component {
await this.trigger('beforeNoteSwitch', {tabId: this.tabId}, true);
this.notePath = notePath;
- const noteId = treeUtils.getNoteIdFromNotePath(notePath);
+ const noteId = treeService.getNoteIdFromNotePath(notePath);
/** @property {NoteFull} */
this.note = await noteDetailService.loadNote(noteId);
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index ba2ae9b677..e80d3fcde4 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -1,7 +1,6 @@
import ws from './ws.js';
import noteDetailService from './note_detail.js';
import protectedSessionHolder from './protected_session_holder.js';
-import treeUtils from './tree_utils.js';
import utils from './utils.js';
import server from './server.js';
import treeCache from './tree_cache.js';
@@ -28,7 +27,7 @@ async function setPrefix(branchId, prefix) {
}
async function setNodeTitleWithPrefix(node) {
- const noteTitle = await treeUtils.getNoteTitle(node.data.noteId);
+ const noteTitle = await getNoteTitle(node.data.noteId);
const branch = treeCache.getBranch(node.data.branchId);
const prefix = branch.prefix;
@@ -214,14 +213,14 @@ async function treeInitialized() {
// (useful, among others, for opening clipped notes from clipper)
if (location.hash) {
const notePath = location.hash.substr(1);
- const noteId = treeUtils.getNoteIdFromNotePath(notePath);
+ const noteId = getNoteIdFromNotePath(notePath);
if (noteId && await treeCache.noteExists(noteId)) {
for (const tab of openTabs) {
tab.active = false;
}
- const foundTab = openTabs.find(tab => noteId === treeUtils.getNoteIdFromNotePath(tab.notePath));
+ const foundTab = openTabs.find(tab => noteId === getNoteIdFromNotePath(tab.notePath));
if (foundTab) {
foundTab.active = true;
@@ -238,7 +237,7 @@ async function treeInitialized() {
let filteredTabs = [];
for (const openTab of openTabs) {
- const noteId = treeUtils.getNoteIdFromNotePath(openTab.notePath);
+ const noteId = getNoteIdFromNotePath(openTab.notePath);
if (await treeCache.noteExists(noteId)) {
// note doesn't exist so don't try to open tab for it
@@ -500,6 +499,119 @@ async function duplicateNote(noteId, parentNoteId) {
toastService.showMessage(`Note "${origNote.title}" has been duplicated`);
}
+async function getParentProtectedStatus(node) {
+ return await hoistedNoteService.isRootNode(node) ? 0 : node.getParent().data.isProtected;
+}
+
+function getNoteIdFromNotePath(notePath) {
+ if (!notePath) {
+ return null;
+ }
+
+ const path = notePath.split("/");
+
+ const lastSegment = path[path.length - 1];
+
+ // path could have also tabId suffix
+ return lastSegment.split("-")[0];
+}
+
+function getNoteIdAndParentIdFromNotePath(notePath) {
+ let parentNoteId = 'root';
+ let noteId = '';
+
+ if (notePath) {
+ const path = notePath.split("/");
+
+ const lastSegment = path[path.length - 1];
+
+ // path could have also tabId suffix
+ noteId = lastSegment.split("-")[0];
+
+ if (path.length > 1) {
+ parentNoteId = path[path.length - 2];
+ }
+ }
+
+ return {
+ parentNoteId,
+ noteId
+ }
+}
+
+async function getNotePath(node) {
+ if (!node) {
+ console.error("Node is null");
+ return "";
+ }
+
+ const path = [];
+
+ while (node && !await hoistedNoteService.isRootNode(node)) {
+ if (node.data.noteId) {
+ path.push(node.data.noteId);
+ }
+
+ node = node.getParent();
+ }
+
+ if (node) { // null node can happen directly after unhoisting when tree is still hoisted but option has been changed already
+ path.push(node.data.noteId); // root or hoisted noteId
+ }
+
+ return path.reverse().join("/");
+}
+
+async function getNoteTitle(noteId, parentNoteId = null) {
+ utils.assertArguments(noteId);
+
+ const note = await treeCache.getNote(noteId);
+ if (!note) {
+ return "[not found]";
+ }
+
+ let {title} = note;
+
+ if (parentNoteId !== null) {
+ const branchId = note.parentToBranch[parentNoteId];
+
+ if (branchId) {
+ const branch = treeCache.getBranch(branchId);
+
+ if (branch && branch.prefix) {
+ title = branch.prefix + ' - ' + title;
+ }
+ }
+ }
+
+ return title;
+}
+
+async function getNotePathTitle(notePath) {
+ utils.assertArguments(notePath);
+
+ const titlePath = [];
+
+ if (notePath.startsWith('root/')) {
+ notePath = notePath.substr(5);
+ }
+
+ // special case when we want just root's title
+ if (notePath === 'root') {
+ return await getNoteTitle(notePath);
+ }
+
+ let parentNoteId = 'root';
+
+ for (const noteId of notePath.split('/')) {
+ titlePath.push(await getNoteTitle(noteId, parentNoteId));
+
+ parentNoteId = noteId;
+ }
+
+ return titlePath.join(' / ');
+}
+
frontendLoaded.then(bundle.executeStartupBundles);
export default {
@@ -515,5 +627,11 @@ export default {
createNewTopLevelNote,
duplicateNote,
getRunPath,
- setNodeTitleWithPrefix
+ setNodeTitleWithPrefix,
+ getParentProtectedStatus,
+ getNotePath,
+ getNoteIdFromNotePath,
+ getNoteIdAndParentIdFromNotePath,
+ getNoteTitle,
+ getNotePathTitle
};
\ No newline at end of file
diff --git a/src/public/javascripts/services/tree_context_menu.js b/src/public/javascripts/services/tree_context_menu.js
index 90d3e18d57..4cb66dd72f 100644
--- a/src/public/javascripts/services/tree_context_menu.js
+++ b/src/public/javascripts/services/tree_context_menu.js
@@ -2,14 +2,11 @@ import treeService from './tree.js';
import ws from './ws.js';
import protectedSessionService from './protected_session.js';
import treeChangesService from './branches.js';
-import treeUtils from './tree_utils.js';
import treeCache from "./tree_cache.js";
import syncService from "./sync.js";
import hoistedNoteService from './hoisted_note.js';
-import noteDetailService from './note_detail.js';
import clipboard from './clipboard.js';
import protectedSessionHolder from "./protected_session_holder.js";
-import searchNotesService from "./search_notes.js";
import appContext from "./app_context.js";
class TreeContextMenu {
@@ -101,7 +98,7 @@ class TreeContextMenu {
async selectContextMenuItem(event, cmd) {
const noteId = this.node.data.noteId;
- const notePath = await treeUtils.getNotePath(this.node);
+ const notePath = await treeService.getNotePath(this.node);
if (cmd === 'openInTab') {
const tabContext = appContext.openEmptyTab();
@@ -110,7 +107,7 @@ class TreeContextMenu {
}
else if (cmd.startsWith("insertNoteAfter")) {
const parentNoteId = this.node.data.parentNoteId;
- const isProtected = await treeUtils.getParentProtectedStatus(this.node);
+ const isProtected = await treeService.getParentProtectedStatus(this.node);
const type = cmd.split("_")[1];
treeService.createNote(this.node, parentNoteId, 'after', {
diff --git a/src/public/javascripts/services/tree_utils.js b/src/public/javascripts/services/tree_utils.js
deleted file mode 100644
index 454c3c30b5..0000000000
--- a/src/public/javascripts/services/tree_utils.js
+++ /dev/null
@@ -1,125 +0,0 @@
-import utils from './utils.js';
-import hoistedNoteService from './hoisted_note.js';
-import treeCache from "./tree_cache.js";
-
-async function getParentProtectedStatus(node) {
- return await hoistedNoteService.isRootNode(node) ? 0 : node.getParent().data.isProtected;
-}
-
-function getNoteIdFromNotePath(notePath) {
- if (!notePath) {
- return null;
- }
-
- const path = notePath.split("/");
-
- const lastSegment = path[path.length - 1];
-
- // path could have also tabId suffix
- return lastSegment.split("-")[0];
-}
-
-function getNoteIdAndParentIdFromNotePath(notePath) {
- let parentNoteId = 'root';
- let noteId = '';
-
- if (notePath) {
- const path = notePath.split("/");
-
- const lastSegment = path[path.length - 1];
-
- // path could have also tabId suffix
- noteId = lastSegment.split("-")[0];
-
- if (path.length > 1) {
- parentNoteId = path[path.length - 2];
- }
- }
-
- return {
- parentNoteId,
- noteId
- }
-}
-
-async function getNotePath(node) {
- if (!node) {
- console.error("Node is null");
- return "";
- }
-
- const path = [];
-
- while (node && !await hoistedNoteService.isRootNode(node)) {
- if (node.data.noteId) {
- path.push(node.data.noteId);
- }
-
- node = node.getParent();
- }
-
- if (node) { // null node can happen directly after unhoisting when tree is still hoisted but option has been changed already
- path.push(node.data.noteId); // root or hoisted noteId
- }
-
- return path.reverse().join("/");
-}
-
-async function getNoteTitle(noteId, parentNoteId = null) {
- utils.assertArguments(noteId);
-
- const note = await treeCache.getNote(noteId);
- if (!note) {
- return "[not found]";
- }
-
- let {title} = note;
-
- if (parentNoteId !== null) {
- const branchId = note.parentToBranch[parentNoteId];
-
- if (branchId) {
- const branch = treeCache.getBranch(branchId);
-
- if (branch && branch.prefix) {
- title = branch.prefix + ' - ' + title;
- }
- }
- }
-
- return title;
-}
-
-async function getNotePathTitle(notePath) {
- utils.assertArguments(notePath);
-
- const titlePath = [];
-
- if (notePath.startsWith('root/')) {
- notePath = notePath.substr(5);
- }
-
- // special case when we want just root's title
- if (notePath === 'root') {
- return await getNoteTitle(notePath);
- }
-
- let parentNoteId = 'root';
-
- for (const noteId of notePath.split('/')) {
- titlePath.push(await getNoteTitle(noteId, parentNoteId));
-
- parentNoteId = noteId;
- }
-
- return titlePath.join(' / ');
-}
-
-export default {
- getParentProtectedStatus,
- getNotePath,
- getNoteIdFromNotePath,
- getNoteIdAndParentIdFromNotePath,
- getNoteTitle,
- getNotePathTitle,
-};
\ No newline at end of file
diff --git a/src/public/javascripts/widgets/note_paths.js b/src/public/javascripts/widgets/note_paths.js
index 7b70bb9ca8..5351a8771f 100644
--- a/src/public/javascripts/widgets/note_paths.js
+++ b/src/public/javascripts/widgets/note_paths.js
@@ -1,6 +1,5 @@
import TabAwareWidget from "./tab_aware_widget.js";
import treeService from "../services/tree.js";
-import treeUtils from "../services/tree_utils.js";
import linkService from "../services/link.js";
const TPL = `
@@ -71,7 +70,7 @@ export default class NotePathsWidget extends TabAwareWidget {
}
async addPath(notePath, isCurrent) {
- const title = await treeUtils.getNotePathTitle(notePath);
+ const title = await treeService.getNotePathTitle(notePath);
const noteLink = await linkService.createNoteLink(notePath, {title});
diff --git a/src/public/javascripts/widgets/note_tree.js b/src/public/javascripts/widgets/note_tree.js
index e7a2ba8a85..d62c0f6b9c 100644
--- a/src/public/javascripts/widgets/note_tree.js
+++ b/src/public/javascripts/widgets/note_tree.js
@@ -1,8 +1,6 @@
import hoistedNoteService from "../services/hoisted_note.js";
import searchNotesService from "../services/search_notes.js";
import treeService from "../services/tree.js";
-import treeUtils from "../services/tree_utils.js";
-import noteDetailService from "../services/note_detail.js";
import utils from "../services/utils.js";
import contextMenuWidget from "../services/context_menu.js";
import treeKeyBindingService from "../services/tree_keybindings.js";
@@ -51,7 +49,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
if (e.which === 2) {
const node = $.ui.fancytree.getNode(e);
- treeUtils.getNotePath(node).then(notePath => {
+ treeService.getNotePath(node).then(notePath => {
if (notePath) {
const tabContext = appContext.openEmptyTab();
tabContext.setNote(notePath);
@@ -89,7 +87,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
}
else if (event.ctrlKey) {
const tabContext = appContext.openEmptyTab();
- treeUtils.getNotePath(node).then(notePath => tabContext.setNote(notePath));
+ treeService.getNotePath(node).then(notePath => tabContext.setNote(notePath));
appContext.activateTab(tabContext.tabId);
}
else {
@@ -105,7 +103,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
// click event won't propagate so let's close context menu manually
contextMenuWidget.hideContextMenu();
- const notePath = await treeUtils.getNotePath(data.node);
+ const notePath = await treeService.getNotePath(data.node);
const activeTabContext = this.appContext.getActiveTabContext();
await activeTabContext.setNote(notePath);
@@ -463,7 +461,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
async createNoteAfterListener() {
const node = this.getActiveNode();
const parentNoteId = node.data.parentNoteId;
- const isProtected = await treeUtils.getParentProtectedStatus(node);
+ const isProtected = await treeService.getParentProtectedStatus(node);
if (node.data.noteId === 'root' || node.data.noteId === await hoistedNoteService.getHoistedNoteId()) {
return;
@@ -524,7 +522,7 @@ export default class NoteTreeWidget extends TabAwareWidget {
const activeNode = this.getActiveNode();
- const activeNotePath = activeNode !== null ? await treeUtils.getNotePath(activeNode) : null;
+ const activeNotePath = activeNode !== null ? await treeService.getNotePath(activeNode) : null;
await this.reload(notes);
diff --git a/src/public/javascripts/widgets/promoted_attributes.js b/src/public/javascripts/widgets/promoted_attributes.js
index f99d536c31..afc057c0a4 100644
--- a/src/public/javascripts/widgets/promoted_attributes.js
+++ b/src/public/javascripts/widgets/promoted_attributes.js
@@ -1,6 +1,6 @@
import server from "../services/server.js";
import ws from "../services/ws.js";
-import treeUtils from "../services/tree_utils.js";
+import treeService from "../services/tree.js";
import noteAutocompleteService from "../services/note_autocomplete.js";
import TabAwareWidget from "./tab_aware_widget.js";
@@ -185,7 +185,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
}
else if (valueAttr.type === 'relation') {
if (valueAttr.value) {
- $input.val(await treeUtils.getNoteTitle(valueAttr.value));
+ $input.val(await treeService.getNoteTitle(valueAttr.value));
}
// no need to wait for this
@@ -247,7 +247,7 @@ export default class PromotedAttributesWidget extends TabAwareWidget {
else if ($attr.prop("attribute-type") === "relation") {
const selectedPath = $attr.getSelectedPath();
- value = selectedPath ? treeUtils.getNoteIdFromNotePath(selectedPath) : "";
+ value = selectedPath ? treeService.getNoteIdFromNotePath(selectedPath) : "";
}
else {
value = $attr.val();