From 1c5483905df8cebd428014421e282f32a71280c7 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 3 Sep 2017 19:15:50 -0400 Subject: [PATCH] fix autofilling autocomplete label on focus --- static/js/init.js | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/static/js/init.js b/static/js/init.js index 51d93842a..b31004c23 100644 --- a/static/js/init.js +++ b/static/js/init.js @@ -130,27 +130,39 @@ $(document).bind('keydown', 'alt+l', function() { }); } - function setDefaultLinkTitle() { - const val = $("#noteAutocomplete").val(); - const noteId = getNodeIdFromLabel(val); - - if (noteId) { - const note = getNodeByKey(noteId); - let noteTitle = note.title; - - if (noteTitle.endsWith(" (clone)")) { - noteTitle = noteTitle.substr(0, noteTitle.length - 8); - } - - $("#linkTitle").val(noteTitle); + function setDefaultLinkTitle(noteId) { + const note = getNodeByKey(noteId); + if (!note) { + return; } + + let noteTitle = note.title; + + if (noteTitle.endsWith(" (clone)")) { + noteTitle = noteTitle.substr(0, noteTitle.length - 8); + } + + $("#linkTitle").val(noteTitle); } $("#noteAutocomplete").autocomplete({ source: autocompleteItems, minLength: 0, - change: setDefaultLinkTitle, - focus: setDefaultLinkTitle + change: function () { + const val = $("#noteAutocomplete").val(); + const noteId = getNodeIdFromLabel(val); + + if (noteId) { + setDefaultLinkTitle(noteId); + } + }, + // this is called when user goes through autocomplete list with keyboard + // at this point the item isn't selected yet so we use supplied ui.item to see where the cursor is + focus: function (event, ui) { + const noteId = getNodeIdFromLabel(ui.item.value); + + setDefaultLinkTitle(noteId); + } }); });