This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/widgets/post-links.js.es6
2019-05-16 10:34:19 +02:00

84 lines
1.9 KiB
JavaScript

import { iconNode } from "discourse-common/lib/icon-library";
import { createWidget } from "discourse/widgets/widget";
import { h } from "virtual-dom";
import { replaceEmoji } from "discourse/widgets/emoji";
export default createWidget("post-links", {
tagName: "div.post-links-container",
buildKey: attrs => `post-links-${attrs.id}`,
defaultState() {
return { collapsed: true };
},
linkHtml(link) {
const linkBody = replaceEmoji(link.title);
if (link.clicks) {
linkBody.push(
h("span.badge.badge-notification.clicks", link.clicks.toString())
);
}
return h(
"li",
h(
"a.track-link",
{
className: "inbound",
attributes: { href: link.url }
},
[iconNode("link"), linkBody]
)
);
},
html(attrs, state) {
if (!this.attrs.links || this.attrs.links.length === 0) {
// shortcut all work
return;
}
// only show incoming
const links = this.attrs.links.filter(l => l.reflection).uniqBy("title");
if (links.length === 0) {
return;
}
const result = [];
// show all links
if (links.length <= 5 || !state.collapsed) {
links.forEach(l => result.push(this.linkHtml(l)));
} else {
const max = Math.min(5, links.length);
for (let i = 0; i < max; i++) {
result.push(this.linkHtml(links[i]));
}
// 'show more' link
if (links.length > max) {
result.push(
h(
"li",
this.attach("link", {
labelCount: "post_links.title",
title: "post_links.about",
count: links.length - max,
action: "expandLinks",
className: "expand-links"
})
)
);
}
}
if (result.length) {
return h("ul.post-links", result);
}
},
expandLinks() {
this.state.collapsed = false;
}
});