import BasicWidget from "./basic_widget.js";
import server from "../services/server.js";
import linkService from "../services/link.js";
import treeCache from "../services/tree_cache.js";
import utils from "../services/utils.js";
const TPL = `
`;
const MAX_DISPLAYED_NOTES = 20;
export default class QuickSearchWidget extends BasicWidget {
doRender() {
this.$widget = $(TPL);
this.overflowing();
this.$searchString = this.$widget.find('.search-string');
this.$dropdownMenu = this.$widget.find('.dropdown-menu');
this.$dropdownToggle = this.$widget.find('.search-button');
this.$widget.find('.input-group-append').on('show.bs.dropdown', () => this.search());
utils.bindElShortcut(this.$searchString, 'return', () => {
this.$dropdownToggle.dropdown('show');
this.$searchString.focus();
});
return this.$widget;
}
async search() {
this.$dropdownMenu.empty();
this.$dropdownMenu.append(' Searching ... ');
const resultNoteIds = await server.get('quick-search/' + encodeURIComponent(this.$searchString.val()));
const displayedNoteIds = resultNoteIds.slice(0, Math.min(MAX_DISPLAYED_NOTES, resultNoteIds.length));
this.$dropdownMenu.empty();
this.$dropdownMenu.append('Show in full search
');
if (displayedNoteIds.length === 0) {
this.$dropdownMenu.append('No results found ');
}
for (const note of await treeCache.getNotes(displayedNoteIds)) {
const $link = await linkService.createNoteLink(note.noteId, {showNotePath: true});
$link.addClass('dropdown-item');
this.$dropdownMenu.append($link);
}
if (resultNoteIds.length > MAX_DISPLAYED_NOTES) {
this.$dropdownMenu.append(`... and ${resultNoteIds.length - MAX_DISPLAYED_NOTES} more results. `);
}
this.$dropdownToggle.dropdown('update');
}
}