import NoteContextAwareWidget from "./note_context_aware_widget.js"; import linkService from "../services/link.js"; import server from "../services/server.js"; import froca from "../services/froca.js"; const TPL = `
`; export default class BacklinksWidget extends NoteContextAwareWidget { doRender() { this.$widget = $(TPL); this.$count = this.$widget.find('.backlinks-count'); this.$items = this.$widget.find('.backlinks-items'); this.$ticker = this.$widget.find('.backlinks-ticker'); this.$count.on("click", () => { this.$items.toggle(); this.$items.css("max-height", $(window).height() - this.$items.offset().top - 10); if (this.$items.is(":visible")) { this.renderBacklinks(); } }); this.$closeTickerButton = this.$widget.find('.backlinks-close-ticker'); this.$closeTickerButton.on("click", () => { this.$ticker.hide(); this.clearItems(); }); this.contentSized(); } async refreshWithNote(note) { this.clearItems(); // can't use froca since that would count only relations from loaded notes const resp = await server.get(`notes/${this.noteId}/backlink-count`); if (!resp || !resp.count) { this.$ticker.hide(); return; } this.$ticker.show(); this.$count.text( `${resp.count} backlink` + (resp.count === 1 ? '' : 's') ); } clearItems() { this.$items.empty().hide(); } async renderBacklinks() { if (!this.note) { return; } this.$items.empty(); const backlinks = await server.get(`note-map/${this.noteId}/backlinks`); if (!backlinks.length) { return; } await froca.getNotes(backlinks.map(bl => bl.noteId)); // prefetch all for (const backlink of backlinks) { const $item = $("").text("relation: " + backlink.relationName)); } else { $item.append(...backlink.excerpts); } this.$items.append($item); } } }