From 295800881b013f5ea7011cda2de5d9225745cbff Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 14 Nov 2018 00:05:09 +0100 Subject: [PATCH] fix #223 plus some refactorings --- src/public/javascripts/services/attributes.js | 2 +- .../javascripts/services/note_autocomplete.js | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/public/javascripts/services/attributes.js b/src/public/javascripts/services/attributes.js index e0de78f14..3e606e292 100644 --- a/src/public/javascripts/services/attributes.js +++ b/src/public/javascripts/services/attributes.js @@ -204,7 +204,7 @@ async function createPromotedAttributeRow(definitionAttr, valueAttr) { promotedAttributeChanged(event); }); - $input.prop("data-selected-path", valueAttr.value); + $input.setSelectedPath(valueAttr.value); // ideally we'd use link instead of button which would allow tooltip preview, but // we can't guarantee updating the link in the a element diff --git a/src/public/javascripts/services/note_autocomplete.js b/src/public/javascripts/services/note_autocomplete.js index bc859964a..62c47d2ae 100644 --- a/src/public/javascripts/services/note_autocomplete.js +++ b/src/public/javascripts/services/note_autocomplete.js @@ -1,6 +1,8 @@ import server from "./server.js"; import noteDetailService from "./note_detail.js"; +const SELECTED_PATH_KEY = "selected-path"; + async function autocompleteSource(term, cb) { const result = await server.get('autocomplete' + '?query=' + encodeURIComponent(term) @@ -17,7 +19,7 @@ async function autocompleteSource(term, cb) { } function clearText($el) { - $el.val('').change(); + $el.autocomplete("val", "").change(); } function showRecentNotes($el) { @@ -62,12 +64,11 @@ function initNoteAutocomplete($el) { } ]); - $el.on('autocomplete:selected', function(event, suggestion, dataset) { - $el.prop("data-selected-path", suggestion.path); - }); - + $el.on('autocomplete:selected', (event, suggestion) => $el.setSelectedPath(suggestion.path)); $el.on('autocomplete:closed', () => { - $el.prop("data-selected-path", ""); + if (!$el.val().trim()) { + $el.setSelectedPath(""); + } }); } @@ -79,10 +80,14 @@ $.fn.getSelectedPath = function() { return ""; } else { - return $(this).prop("data-selected-path"); + return $(this).data(SELECTED_PATH_KEY); } }; +$.fn.setSelectedPath = function(path) { + $(this).data(SELECTED_PATH_KEY, path); +}; + ko.bindingHandlers.noteAutocomplete = { init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { initNoteAutocomplete($(element));