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 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-01 17:33:45 +08:00
|
|
|
await treeCache.getNotes(similarNotes.map(note => note.noteId)); // preload all at once
|
2019-09-01 14:58:13 +08:00
|
|
|
|
2019-09-01 17:33:45 +08:00
|
|
|
const $list = $('<ul style="padding-left: 20px;">');
|
|
|
|
|
|
|
|
for (const similarNote of similarNotes) {
|
2019-09-01 17:37:43 +08:00
|
|
|
const $item = $("<li>")
|
|
|
|
.append(await linkService.createNoteLink(similarNote.notePath.join("/")));
|
|
|
|
|
2019-09-01 17:33:45 +08:00
|
|
|
similarNote.notePath.pop(); // remove last noteId since it's already in the link
|
2019-09-01 14:58:13 +08:00
|
|
|
|
2019-09-01 17:37:43 +08:00
|
|
|
if (similarNote.notePath.length > 0) {
|
|
|
|
$item.append($("<small>").text(" (" + await treeUtils.getNotePathTitle(similarNote.notePath.join("/")) + ")"));
|
|
|
|
}
|
2019-09-01 14:58:13 +08:00
|
|
|
|
|
|
|
$list.append($item);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$body.empty().append($list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SimilarNotesWidget;
|