2019-09-01 14:58:13 +08: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-10 03:23:04 +08: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 14:58:13 +08:00
|
|
|
getMaxHeight() { return "200px"; }
|
|
|
|
|
|
|
|
async doRenderBody() {
|
2019-09-04 03:31:39 +08:00
|
|
|
// remember which title was when we found the similar notes
|
|
|
|
this.title = this.ctx.note.title;
|
|
|
|
|
2019-09-01 17:33:45 +08:00
|
|
|
const similarNotes = await server.get('similar_notes/' + this.ctx.note.noteId);
|
2019-09-01 14:58:13 +08:00
|
|
|
|
2019-09-01 17:33:45 +08:00
|
|
|
if (similarNotes.length === 0) {
|
2019-09-01 14:58:13 +08:00
|
|
|
this.$body.text("No similar notes found ...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-03 03:36:24 +08:00
|
|
|
const noteIds = similarNotes.flatMap(note => note.notePath);
|
|
|
|
|
2019-09-08 17:25:57 +08:00
|
|
|
await treeCache.getNotes(noteIds, true); // preload all at once
|
2019-09-01 14:58:13 +08:00
|
|
|
|
2019-09-01 19:42:10 +08:00
|
|
|
const $list = $('<ul>');
|
2019-09-01 17:33:45 +08:00
|
|
|
|
|
|
|
for (const similarNote of similarNotes) {
|
2019-09-08 17:25:57 +08:00
|
|
|
const note = await treeCache.getNote(similarNote.noteId, true);
|
2019-09-01 14:58:13 +08:00
|
|
|
|
2019-09-01 19:42:10 +08:00
|
|
|
if (!note) {
|
|
|
|
continue;
|
2019-09-01 17:37:43 +08:00
|
|
|
}
|
2019-09-01 14:58:13 +08:00
|
|
|
|
2019-09-01 19:42:10 +08:00
|
|
|
const $item = $("<li>")
|
2019-09-03 03:36:24 +08:00
|
|
|
.append(await linkService.createNoteLinkWithPath(similarNote.notePath.join("/")));
|
2019-09-01 19:42:10 +08:00
|
|
|
|
2019-09-01 14:58:13 +08:00
|
|
|
$list.append($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$body.empty().append($list);
|
|
|
|
}
|
2019-09-04 03:31:39 +08:00
|
|
|
|
|
|
|
eventReceived(name, data) {
|
|
|
|
if (name === 'noteSaved') {
|
|
|
|
if (this.title !== this.ctx.note.title) {
|
|
|
|
this.rendered = false;
|
|
|
|
|
|
|
|
this.renderBody();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-01 14:58:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default SimilarNotesWidget;
|