146 lines
3.9 KiB
JavaScript
Raw Normal View History

/**
* !!! Filename is intentionally mangled, because some adblockers don't like the word "backlinks".
*/
2022-06-21 23:27:34 +02:00
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";
2021-12-01 23:12:54 +01:00
const TPL = `
<div class="backlinks-widget">
<style>
.backlinks-widget {
position: relative;
}
.backlinks-ticker {
border-radius: 10px;
border-color: var(--main-border-color);
background-color: var(--more-accented-background-color);
padding: 4px 10px 4px 10px;
2022-01-12 20:13:11 +01:00
opacity: 90%;
2021-12-01 23:12:54 +01:00
display: flex;
justify-content: space-between;
align-items: center;
}
.backlinks-count {
cursor: pointer;
}
2022-06-21 23:27:34 +02:00
2021-12-01 23:12:54 +01:00
.backlinks-items {
z-index: 10;
position: absolute;
top: 50px;
right: 10px;
width: 400px;
border-radius: 10px;
background-color: var(--accented-background-color);
color: var(--main-text-color);
2021-12-01 23:12:54 +01:00
padding: 20px;
overflow-y: auto;
}
.backlink-excerpt {
border-left: 2px solid var(--main-border-color);
padding-left: 10px;
opacity: 80%;
font-size: 90%;
}
2021-12-02 22:00:42 +01:00
.backlink-excerpt .backlink-link { /* the actual backlink */
font-weight: bold;
background-color: yellow;
}
2021-12-01 23:12:54 +01:00
</style>
<div class="backlinks-ticker">
<span class="backlinks-count"></span>
</div>
<div class="backlinks-items" style="display: none;"></div>
</div>
`;
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.contentSized();
}
async refreshWithNote(note) {
this.clearItems();
2021-12-24 21:01:36 +01:00
// 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.toggle(false);
2021-12-24 21:01:36 +01:00
return;
2021-12-01 23:12:54 +01:00
}
2021-12-24 21:01:36 +01:00
2022-09-22 23:29:20 +02:00
this.toggle(true);
2021-12-24 21:01:36 +01:00
this.$count.text(
`${resp.count} backlink`
+ (resp.count === 1 ? '' : 's')
);
2021-12-01 23:12:54 +01:00
}
toggle(show) {
this.$widget.toggleClass("hidden-no-content", !show);
}
2021-12-01 23:12:54 +01:00
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) {
2021-12-24 21:01:36 +01:00
const $item = $("<div>");
$item.append(await linkService.createNoteLink(backlink.noteId, {
2021-12-01 23:12:54 +01:00
showNoteIcon: true,
showNotePath: true,
showTooltip: false
}));
2021-12-02 22:00:42 +01:00
if (backlink.relationName) {
2021-12-24 21:01:36 +01:00
$item.append($("<p>").text("relation: " + backlink.relationName));
2021-12-02 22:00:42 +01:00
}
else {
2021-12-24 21:01:36 +01:00
$item.append(...backlink.excerpts);
2021-12-02 22:00:42 +01:00
}
2021-12-24 21:01:36 +01:00
this.$items.append($item);
2021-12-01 23:12:54 +01:00
}
}
}