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-14 18:17:20 +08:00
|
|
|
import treeService from './tree.js';
|
2018-08-17 03:02:42 +08:00
|
|
|
|
2018-11-15 02:33:48 +08:00
|
|
|
// this key needs to have this value so it's hit by the tooltip
|
|
|
|
const SELECTED_PATH_KEY = "data-note-path";
|
2018-11-14 07:05:09 +08:00
|
|
|
|
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-14 06:16:26 +08:00
|
|
|
function clearText($el) {
|
2018-11-14 18:17:20 +08:00
|
|
|
$el.setSelectedPath("");
|
2018-11-14 07:05:09 +08:00
|
|
|
$el.autocomplete("val", "").change();
|
2018-11-14 06:16:26 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 16:51:14 +08:00
|
|
|
function showRecentNotes($el) {
|
2018-11-14 18:17:20 +08:00
|
|
|
$el.setSelectedPath("");
|
2018-11-07 16:51:14 +08:00
|
|
|
$el.autocomplete("val", "");
|
2019-01-11 04:04:06 +08:00
|
|
|
$el.focus();
|
2018-11-07 16:51:14 +08:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:28:52 +08:00
|
|
|
function initNoteAutocomplete($el, options) {
|
2018-11-14 18:17:20 +08:00
|
|
|
if (!$el.hasClass("note-autocomplete-input")) {
|
2018-11-14 18:28:52 +08:00
|
|
|
options = options || {};
|
|
|
|
|
2018-11-14 18:17:20 +08:00
|
|
|
$el.addClass("note-autocomplete-input");
|
|
|
|
|
2018-11-15 02:33:48 +08:00
|
|
|
const $clearTextButton = $("<a>")
|
2018-11-14 06:16:26 +08:00
|
|
|
.addClass("input-group-text input-clearer-button jam jam-close")
|
|
|
|
.prop("title", "Clear text field");
|
|
|
|
|
2018-11-15 02:33:48 +08:00
|
|
|
const $showRecentNotesButton = $("<a>")
|
2018-11-09 18:57:52 +08:00
|
|
|
.addClass("input-group-text show-recent-notes-button jam jam-clock")
|
2018-11-14 06:16:26 +08:00
|
|
|
.prop("title", "Show recent notes");
|
|
|
|
|
2018-11-15 02:33:48 +08:00
|
|
|
const $goToSelectedNoteButton = $("<a>")
|
|
|
|
.addClass("input-group-text go-to-selected-note-button jam jam-arrow-right");
|
2018-11-14 18:17:20 +08:00
|
|
|
|
2018-11-14 18:28:52 +08:00
|
|
|
const $sideButtons = $("<div>")
|
2018-11-14 06:16:26 +08:00
|
|
|
.addClass("input-group-append")
|
|
|
|
.append($clearTextButton)
|
2018-11-14 18:28:52 +08:00
|
|
|
.append($showRecentNotesButton);
|
|
|
|
|
|
|
|
if (!options.hideGoToSelectedNoteButton) {
|
|
|
|
$sideButtons.append($goToSelectedNoteButton);
|
|
|
|
}
|
|
|
|
|
|
|
|
$el.after($sideButtons);
|
2018-08-07 04:29:03 +08:00
|
|
|
|
2018-11-14 06:16:26 +08:00
|
|
|
$clearTextButton.click(() => clearText($el));
|
2018-08-07 04:29:03 +08:00
|
|
|
|
2019-01-11 04:04:06 +08:00
|
|
|
$showRecentNotesButton.click(e => {
|
|
|
|
showRecentNotes($el);
|
|
|
|
|
|
|
|
// this will cause the click not give focus to the "show recent notes" button
|
|
|
|
// this is important because otherwise input will lose focus immediatelly and not show the results
|
|
|
|
return false;
|
|
|
|
});
|
2018-11-07 16:51:14 +08:00
|
|
|
|
2018-11-14 18:17:20 +08:00
|
|
|
$goToSelectedNoteButton.click(() => {
|
|
|
|
if ($el.hasClass("disabled")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
treeService.activateNote($el.getSelectedPath());
|
|
|
|
});
|
|
|
|
|
2018-11-07 16:51:14 +08:00
|
|
|
$el.autocomplete({
|
|
|
|
appendTo: document.querySelector('body'),
|
|
|
|
hint: false,
|
|
|
|
autoselect: true,
|
|
|
|
openOnFocus: true,
|
2018-11-27 20:13:06 +08:00
|
|
|
minLength: 0,
|
|
|
|
tabAutocomplete: false
|
2018-11-07 16:51:14 +08:00
|
|
|
}, [
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2018-11-14 07:05:09 +08:00
|
|
|
$el.on('autocomplete:selected', (event, suggestion) => $el.setSelectedPath(suggestion.path));
|
2018-11-13 06:34:22 +08:00
|
|
|
$el.on('autocomplete:closed', () => {
|
2018-11-14 07:05:09 +08:00
|
|
|
if (!$el.val().trim()) {
|
2018-11-20 04:58:52 +08:00
|
|
|
clearText($el);
|
2018-11-14 07:05:09 +08:00
|
|
|
}
|
2018-11-13 06:34:22 +08:00
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
function init() {
|
|
|
|
$.fn.getSelectedPath = function () {
|
|
|
|
if (!$(this).val().trim()) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return $(this).attr(SELECTED_PATH_KEY);
|
|
|
|
}
|
|
|
|
};
|
2018-11-13 06:34:22 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
$.fn.setSelectedPath = function (path) {
|
|
|
|
path = path || "";
|
2018-11-14 18:17:20 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
$(this).attr(SELECTED_PATH_KEY, path);
|
2018-11-14 18:17:20 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
$(this)
|
|
|
|
.closest(".input-group")
|
|
|
|
.find(".go-to-selected-note-button")
|
|
|
|
.toggleClass("disabled", !path.trim())
|
|
|
|
.attr(SELECTED_PATH_KEY, path); // we also set attr here so tooltip can be displayed
|
|
|
|
};
|
2018-11-14 07:05:09 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
ko.bindingHandlers.noteAutocomplete = {
|
|
|
|
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
|
|
|
|
initNoteAutocomplete($(element));
|
2018-11-08 03:32:11 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
$(element).setSelectedPath(bindingContext.$data.selectedPath);
|
2018-11-14 18:17:20 +08:00
|
|
|
|
2018-12-24 17:10:36 +08:00
|
|
|
$(element).on('autocomplete:selected', function (event, suggestion, dataset) {
|
|
|
|
bindingContext.$data.selectedPath = $(element).val().trim() ? suggestion.path : '';
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2018-08-07 04:29:03 +08:00
|
|
|
|
|
|
|
export default {
|
2018-08-17 03:02:42 +08:00
|
|
|
initNoteAutocomplete,
|
2018-12-24 17:10:36 +08:00
|
|
|
showRecentNotes,
|
|
|
|
init
|
2018-08-07 04:29:03 +08:00
|
|
|
}
|