diff --git a/public/javascripts/dialogs/add_link.js b/public/javascripts/dialogs/add_link.js index 23a178b28..02f3c43c2 100644 --- a/public/javascripts/dialogs/add_link.js +++ b/public/javascripts/dialogs/add_link.js @@ -27,7 +27,7 @@ const addLink = (function() { } autoCompleteEl.autocomplete({ - source: getAutocompleteItems(glob.allNoteIds), + source: noteTree.getAutocompleteItems(), minLength: 0, change: () => { const val = autoCompleteEl.val(); diff --git a/public/javascripts/dialogs/jump_to_note.js b/public/javascripts/dialogs/jump_to_note.js index dac1f3803..8051fd8d2 100644 --- a/public/javascripts/dialogs/jump_to_note.js +++ b/public/javascripts/dialogs/jump_to_note.js @@ -16,7 +16,7 @@ const jumpToNote = (function() { }); autoCompleteEl.autocomplete({ - source: getAutocompleteItems(glob.allNoteIds), + source: noteTree.getAutocompleteItems(), minLength: 0 }); } diff --git a/public/javascripts/init.js b/public/javascripts/init.js index 66efd2e79..c96fea726 100644 --- a/public/javascripts/init.js +++ b/public/javascripts/init.js @@ -57,10 +57,10 @@ $.ui.autocomplete.filter = (array, terms) => { for (const item of array) { let found = true; - const lcValue = item.value.toLowerCase(); + const lcLabel = item.label.toLowerCase(); for (const token of tokens) { - if (lcValue.indexOf(token) === -1) { + if (lcLabel.indexOf(token) === -1) { found = false; break; } diff --git a/public/javascripts/note_tree.js b/public/javascripts/note_tree.js index 7dc9e101b..05bf3b325 100644 --- a/public/javascripts/note_tree.js +++ b/public/javascripts/note_tree.js @@ -418,8 +418,8 @@ const noteTree = (function() { tree.clearFilter(); } - function getByNoteId(noteId) { - return notesMap[noteId]; + function getByNoteTreeId(noteTreeId) { + return notesMap[noteTreeId]; } // note that if you want to access data like note_id or is_protected, you need to go into "data" property @@ -444,6 +444,44 @@ const noteTree = (function() { node.toggleClass("protected", !!node.data.is_protected); } + function getAutocompleteItems(parentNoteId, notePath, titlePath) { + if (!parentNoteId) { + parentNoteId = 'root'; + } + + if (!parentToChildren[parentNoteId]) { + return []; + } + + if (!notePath) { + notePath = ''; + } + + if (!titlePath) { + titlePath = ''; + } + + const autocompleteItems = []; + + for (const childNoteId of parentToChildren[parentNoteId]) { + const childNotePath = (notePath ? (notePath + '/') : '') + childNoteId; + const childTitlePath = (titlePath ? (titlePath + ' / ') : '') + getNoteTitle(childNoteId); + + autocompleteItems.push({ + value: childNotePath, + label: childTitlePath + }); + + const childItems = getAutocompleteItems(childNoteId, childNotePath, childTitlePath); + + for (const childItem of childItems) { + autocompleteItems.push(childItem); + } + } + + return autocompleteItems; + } + $("button#reset-search-button").click(resetSearch); $("input[name=search]").keyup(e => { @@ -477,7 +515,7 @@ const noteTree = (function() { collapseTree, scrollToCurrentNote, toggleSearch, - getByNoteId, + getByNoteTreeId, getKeyFromNoteTreeId, getNoteTreeIdFromKey, setCurrentNoteTreeBasedOnProtectedStatus, @@ -486,6 +524,7 @@ const noteTree = (function() { activateNode, getCurrentNotePath, getNoteTitle, - setCurrentNotePathToHash + setCurrentNotePathToHash, + getAutocompleteItems }; })(); \ No newline at end of file diff --git a/public/javascripts/tree_utils.js b/public/javascripts/tree_utils.js index 2b0a0c4ad..916b95044 100644 --- a/public/javascripts/tree_utils.js +++ b/public/javascripts/tree_utils.js @@ -35,7 +35,7 @@ const treeUtils = (function() { } function getFullName(noteTreeId) { - let note = noteTree.getByNoteId(noteTreeId); + let note = noteTree.getByNoteTreeId(noteTreeId); if (note === null) { return "[unknown]"; @@ -46,7 +46,7 @@ const treeUtils = (function() { while (note) { path.push(note.note_title); - note = noteTree.getByNoteId(note.note_pid); + note = noteTree.getByNoteTreeId(note.note_pid); } return path.reverse().join(" > "); diff --git a/public/javascripts/utils.js b/public/javascripts/utils.js index a1178c806..ebd9463dd 100644 --- a/public/javascripts/utils.js +++ b/public/javascripts/utils.js @@ -20,25 +20,6 @@ function showError(str) { error.fadeOut(10000); } -function getAutocompleteItems(noteIds) { - const autocompleteItems = []; - - for (const noteId of noteIds) { - const fullName = treeUtils.getFullName(noteId); - - if (fullName !== null) { - autocompleteItems.push({ - value: fullName + " (" + noteId + ")", - label: fullName - }); - } - } - - autocompleteItems.sort((a, b) => a.value < b.value ? -1 : 1); - - return autocompleteItems; -} - function getDateFromTS(timestamp) { // Date accepts number of milliseconds since epoch so UTC timestamp works without any extra handling // see https://stackoverflow.com/questions/4631928/convert-utc-epoch-to-local-date-with-javascript