diff --git a/src/public/javascripts/dialogs/attributes.js b/src/public/javascripts/dialogs/attributes.js index 6fe6fc18b..7671e7333 100644 --- a/src/public/javascripts/dialogs/attributes.js +++ b/src/public/javascripts/dialogs/attributes.js @@ -3,6 +3,7 @@ import server from '../services/server.js'; import infoService from "../services/info.js"; import treeUtils from "../services/tree_utils.js"; import linkService from "../services/link.js"; +import noteAutocompleteService from "../services/note_autocomplete.js"; const $dialog = $("#attributes-dialog"); const $saveAttributesButton = $("#save-attributes-button"); @@ -306,49 +307,6 @@ $dialog.on('focus', '.label-value', async function (e) { $(this).autocomplete("search", $(this).val()); }); -async function initNoteAutocomplete($el) { - if (!$el.hasClass("ui-autocomplete-input")) { - await $el.autocomplete({ - source: async function (request, response) { - const result = await server.get('autocomplete?query=' + encodeURIComponent(request.term)); - - if (result.length > 0) { - response(result.map(row => { - return { - label: row.label, - value: row.label + ' (' + row.value + ')' - } - })); - } - else { - response([{ - label: "No results", - value: "No results" - }]); - } - }, - minLength: 0, - select: function (event, ui) { - if (ui.item.value === 'No results') { - return false; - } - } - }); - } -} - -$dialog.on('focus', '.relation-target-note-id', async function () { - await initNoteAutocomplete($(this)); -}); - -$dialog.on('click', '.relations-show-recent-notes', async function () { - const $autocomplete = $(this).parent().find('.relation-target-note-id'); - - await initNoteAutocomplete($autocomplete); - - $autocomplete.autocomplete("search", ""); -}); - export default { showDialog }; \ No newline at end of file diff --git a/src/public/javascripts/services/note_autocomplete.js b/src/public/javascripts/services/note_autocomplete.js new file mode 100644 index 000000000..ffa4325b4 --- /dev/null +++ b/src/public/javascripts/services/note_autocomplete.js @@ -0,0 +1,54 @@ +import server from "./server.js"; + +async function initNoteAutocomplete($el) { + if (!$el.hasClass("ui-autocomplete-input")) { + const $showRecentNotesButton = $("") + .addClass("input-group-addon show-recent-notes-button") + .prop("title", "Show recent notes"); + + $el.after($showRecentNotesButton); + + $showRecentNotesButton.click(() => $el.autocomplete("search", "")); + + await $el.autocomplete({ + appendTo: $el.parent().parent(), + source: async function (request, response) { + const result = await server.get('autocomplete?query=' + encodeURIComponent(request.term)); + + if (result.length > 0) { + response(result.map(row => { + return { + label: row.label, + value: row.label + ' (' + row.value + ')' + } + })); + } + else { + response([{ + label: "No results", + value: "No results" + }]); + } + }, + minLength: 0, + change: function (event, ui) { + $el.trigger("change"); + }, + select: function (event, ui) { + if (ui.item.value === 'No results') { + return false; + } + } + }); + } +} + +ko.bindingHandlers.noteAutocomplete = { + init: function(element, valueAccessor, allBindings, viewModel, bindingContext) { + initNoteAutocomplete($(element)); + } +}; + +export default { + initNoteAutocomplete +} \ No newline at end of file diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index 31003c32e..a95d9731e 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -16,6 +16,7 @@ import noteDetailFile from './note_detail_file.js'; import noteDetailSearch from './note_detail_search.js'; import noteDetailRender from './note_detail_render.js'; import bundleService from "./bundle.js"; +import noteAutocompleteService from "./note_autocomplete.js"; const $noteTitle = $("#note-title"); @@ -250,7 +251,7 @@ async function loadAttributes() { .addClass("form-control") .addClass("promoted-attribute-input"); - const $inputCell = $("