autocomplete for add link dialog, #203

This commit is contained in:
azivner
2018-11-07 17:16:33 +01:00
parent a0f362457e
commit 90dbe637ed
6 changed files with 39 additions and 56 deletions

View File

@@ -15,7 +15,6 @@ const $prefixFormGroup = $("#add-link-prefix-form-group");
const $linkTypeDiv = $("#add-link-type-div");
const $linkTypes = $("input[name='add-link-type']");
const $linkTypeHtml = $linkTypes.filter('input[value="html"]');
const $showRecentNotesButton = $dialog.find(".show-recent-notes-button");
function setLinkType(linkType) {
$linkTypes.each(function () {
@@ -51,50 +50,31 @@ async function showDialog() {
$linkTitle.val(noteTitle);
}
await $autoComplete.autocomplete({
source: noteAutocompleteService.autocompleteSource,
minLength: 0,
change: async (event, ui) => {
if (!ui.item) {
return;
}
noteAutocompleteService.initNoteAutocomplete($autoComplete);
const notePath = linkService.getNotePathFromLabel(ui.item.value);
$autoComplete.on('autocomplete:selected', function(event, suggestion, dataset) {
if (!suggestion.path) {
return false;
}
if (!notePath) {
return;
}
const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
if (noteId) {
await setDefaultLinkTitle(noteId);
}
},
select: function (event, ui) {
if (ui.item.value === 'No results') {
return false;
}
},
// 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: async (event, ui) => {
const notePath = linkService.getNotePathFromLabel(ui.item.value);
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
await setDefaultLinkTitle(noteId);
event.preventDefault();
if (noteId) {
setDefaultLinkTitle(noteId);
}
});
showRecentNotes();
$autoComplete.on('autocomplete:cursorchanged', function(event, suggestion, dataset) {
const noteId = treeUtils.getNoteIdFromNotePath(suggestion.path);
setDefaultLinkTitle(noteId);
});
noteAutocompleteService.showRecentNotes($autoComplete);
}
$form.submit(() => {
const value = $autoComplete.val();
const notePath = linkService.getNotePathFromLabel(value);
const notePath = $autoComplete.getSelectedPath();
const noteId = treeUtils.getNoteIdFromNotePath(notePath);
if (notePath) {
@@ -131,6 +111,9 @@ $form.submit(() => {
$dialog.modal('hide');
}
}
else {
console.error("No path to add link.");
}
return false;
});
@@ -152,14 +135,8 @@ function linkTypeChanged() {
$linkTypeDiv.toggle(!hasSelection());
}
function showRecentNotes() {
$autoComplete.autocomplete("search", "");
}
$linkTypes.change(linkTypeChanged);
$showRecentNotesButton.click(showRecentNotes);
export default {
showDialog
};