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";
|
2019-09-01 17:33:45 +08:00
|
|
|
import treeUtils from "../services/tree_utils.js";
|
2019-09-01 19:42:10 +08:00
|
|
|
import treeService from "../services/tree.js";
|
2019-09-01 14:58:13 +08:00
|
|
|
|
|
|
|
class SimilarNotesWidget extends StandardWidget {
|
|
|
|
getWidgetTitle() { return "Similar notes"; }
|
|
|
|
|
|
|
|
getMaxHeight() { return "200px"; }
|
|
|
|
|
|
|
|
async doRenderBody() {
|
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);
|
|
|
|
|
|
|
|
await treeCache.getNotes(noteIds); // 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-01 19:42:10 +08:00
|
|
|
const note = await treeCache.getNote(similarNote.noteId);
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SimilarNotesWidget;
|