Skip to content

Build your personal knowledge base with Trilium Notes

fix #223 plus some refactorings

azivner295800881b01
diff --git a/src/public/javascripts/services/attributes.js b/src/public/javascripts/services/attributes.js
index e0de78f140..3e606e2925 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 bc859964ae..62c47d2aed 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));