2019-09-01 08:58:13 +02:00
|
|
|
import StandardWidget from "./standard_widget.js";
|
|
|
|
import linkService from "../services/link.js";
|
|
|
|
import server from "../services/server.js";
|
|
|
|
import treeCache from "../services/tree_cache.js";
|
|
|
|
|
|
|
|
class SimilarNotesWidget extends StandardWidget {
|
|
|
|
getWidgetTitle() { return "Similar notes"; }
|
|
|
|
|
2019-09-09 21:23:04 +02:00
|
|
|
getHelp() {
|
|
|
|
return {
|
|
|
|
title: "This list contains notes which might be similar to the current note based on textual similarity of note title."
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-01 08:58:13 +02:00
|
|
|
getMaxHeight() { return "200px"; }
|
|
|
|
|
|
|
|
async doRenderBody() {
|
2019-09-03 21:31:39 +02:00
|
|
|
// remember which title was when we found the similar notes
|
|
|
|
this.title = this.ctx.note.title;
|
|
|
|
|
2019-11-19 20:53:04 +01:00
|
|
|
const similarNotes = await server.get('similar-notes/' + this.ctx.note.noteId);
|
2019-09-01 08:58:13 +02:00
|
|
|
|
2019-09-01 11:33:45 +02:00
|
|
|
if (similarNotes.length === 0) {
|
2019-09-01 08:58:13 +02:00
|
|
|
this.$body.text("No similar notes found ...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-02 21:36:24 +02:00
|
|
|
const noteIds = similarNotes.flatMap(note => note.notePath);
|
|
|
|
|
2019-09-08 11:25:57 +02:00
|
|
|
await treeCache.getNotes(noteIds, true); // preload all at once
|
2019-09-01 08:58:13 +02:00
|
|
|
|
2019-09-01 13:42:10 +02:00
|
|
|
const $list = $('<ul>');
|
2019-09-01 11:33:45 +02:00
|
|
|
|
|
|
|
for (const similarNote of similarNotes) {
|
2019-09-08 11:25:57 +02:00
|
|
|
const note = await treeCache.getNote(similarNote.noteId, true);
|
2019-09-01 08:58:13 +02:00
|
|
|
|
2019-09-01 13:42:10 +02:00
|
|
|
if (!note) {
|
|
|
|
continue;
|
2019-09-01 11:37:43 +02:00
|
|
|
}
|
2019-09-01 08:58:13 +02:00
|
|
|
|
2019-09-01 13:42:10 +02:00
|
|
|
const $item = $("<li>")
|
2019-12-28 21:10:02 +01:00
|
|
|
.append(await linkService.createNoteLink(similarNote.notePath.join("/"), {showNotePath: true}));
|
2019-09-01 13:42:10 +02:00
|
|
|
|
2019-09-01 08:58:13 +02:00
|
|
|
$list.append($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$body.empty().append($list);
|
|
|
|
}
|
2019-09-03 21:31:39 +02:00
|
|
|
|
|
|
|
eventReceived(name, data) {
|
|
|
|
if (name === 'noteSaved') {
|
|
|
|
if (this.title !== this.ctx.note.title) {
|
|
|
|
this.rendered = false;
|
|
|
|
|
|
|
|
this.renderBody();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 08:58:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default SimilarNotesWidget;
|