2018-08-07 04:29:03 +08:00
|
|
|
import server from "./server.js";
|
2018-08-17 03:02:42 +08:00
|
|
|
import noteDetailService from "./note_detail.js";
|
|
|
|
|
2018-11-07 07:23:50 +08:00
|
|
|
async function autocompleteSource(term, cb) {
|
2018-08-17 03:02:42 +08:00
|
|
|
const result = await server.get('autocomplete'
|
2018-11-07 07:23:50 +08:00
|
|
|
+ '?query=' + encodeURIComponent(term)
|
2018-08-17 03:02:42 +08:00
|
|
|
+ '¤tNoteId=' + noteDetailService.getCurrentNoteId());
|
|
|
|
|
2018-11-07 16:35:29 +08:00
|
|
|
if (result.length === 0) {
|
|
|
|
result.push({
|
|
|
|
title: "No results",
|
|
|
|
path: ""
|
|
|
|
});
|
2018-08-17 03:02:42 +08:00
|
|
|
}
|
2018-11-07 16:35:29 +08:00
|
|
|
|
|
|
|
cb(result);
|
2018-08-17 03:02:42 +08:00
|
|
|
}
|
2018-08-07 04:29:03 +08:00
|
|
|
|
2018-11-07 16:51:14 +08:00
|
|
|
function showRecentNotes($el) {
|
|
|
|
$el.autocomplete("val", "");
|
|
|
|
$el.autocomplete("open");
|
|
|
|
}
|
|
|
|
|
|
|
|
function initNoteAutocomplete($el) {
|
|
|
|
if (!$el.hasClass("aa-input")) {
|
2018-11-06 04:12:03 +08:00
|
|
|
const $showRecentNotesButton = $("<div>").addClass("input-group-append").append(
|
|
|
|
$("<span>")
|
|
|
|
.addClass("input-group-text show-recent-notes-button")
|
|
|
|
.prop("title", "Show recent notes"));
|
2018-08-07 04:29:03 +08:00
|
|
|
|
|
|
|
$el.after($showRecentNotesButton);
|
|
|
|
|
2018-11-07 16:51:14 +08:00
|
|
|
$showRecentNotesButton.click(() => showRecentNotes($el));
|
|
|
|
|
|
|
|
$el.autocomplete({
|
|
|
|
appendTo: document.querySelector('body'),
|
|
|
|
hint: false,
|
|
|
|
autoselect: true,
|
|
|
|
openOnFocus: true,
|
|
|
|
minLength: 0
|
|
|
|
}, [
|
|
|
|
{
|
|
|
|
source: autocompleteSource,
|
|
|
|
displayKey: 'title',
|
|
|
|
templates: {
|
|
|
|
suggestion: function(suggestion) {
|
2018-11-08 00:16:33 +08:00
|
|
|
return suggestion.highlighted;
|
2018-11-07 16:51:14 +08:00
|
|
|
}
|
2018-08-07 04:29:03 +08:00
|
|
|
}
|
|
|
|
}
|
2018-11-07 16:51:14 +08:00
|
|
|
]);
|
2018-11-08 00:16:33 +08:00
|
|
|
|
|
|
|
$el.on('autocomplete:selected', function(event, suggestion, dataset) {
|
|
|
|
$el.prop("data-selected-path", suggestion.path);
|
|
|
|
});
|
|
|
|
|
|
|
|
$el.getSelectedPath = () => $el.prop("data-selected-path");
|
2018-08-07 04:29:03 +08:00
|
|
|
}
|
2018-11-07 16:51:14 +08:00
|
|
|
|
|
|
|
return $el;
|
2018-08-07 04:29:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ko.bindingHandlers.noteAutocomplete = {
|
|
|
|
init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
|
|
|
|
initNoteAutocomplete($(element));
|
2018-11-08 03:32:11 +08:00
|
|
|
|
|
|
|
$(element).on('autocomplete:selected', function(event, suggestion, dataset) {
|
|
|
|
bindingContext.$data.selectedPath = suggestion.path;
|
|
|
|
});
|
2018-08-07 04:29:03 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
2018-08-17 03:02:42 +08:00
|
|
|
initNoteAutocomplete,
|
2018-11-07 16:51:14 +08:00
|
|
|
autocompleteSource,
|
|
|
|
showRecentNotes
|
2018-08-07 04:29:03 +08:00
|
|
|
}
|