prefer incoming over outgoing links

This commit is contained in:
Régis Hanol 2016-06-08 11:34:36 +02:00
parent 7b6fb17bbb
commit ec7443b021
1 changed files with 40 additions and 33 deletions

View File

@ -11,38 +11,42 @@ export default createWidget('post-links', {
return { collapsed: true };
},
linkHtml(link) {
const linkBody = [new RawHtml({html: `<span>${Discourse.Emoji.unescape(Handlebars.Utils.escapeExpression(link.title))}</span>`})];
if (link.clicks) {
linkBody.push(h('span.badge.badge-notification.clicks', link.clicks.toString()));
}
return h('li',
h('a.track-link', {
className: link.reflection ? 'inbound' : 'outbound',
attributes: {href: link.url}
}, [linkBody, iconNode(link.reflection ? 'arrow-left' : 'arrow-right')])
);
},
html(attrs, state) {
const links = this.attrs.links || [];
const dedupedLinks = _.uniq(links, true, l => l.title);
const incomingLinks = dedupedLinks.filter(l => l.reflection);
// if all links are outgoing, don't show any
if (incomingLinks.length === 0) { return; }
const result = [];
if (links.length) {
const seenTitles = {};
let titleCount = 0;
let hasMore = links.any((l) => {
if (state.collapsed && titleCount === 5) { return true; }
let title = l.title;
if (title && !seenTitles[title]) {
seenTitles[title] = true;
titleCount++;
const linkBody = [new RawHtml({html: `<span>${Discourse.Emoji.unescape(Handlebars.Utils.escapeExpression(title))}</span>`})];
if (l.clicks) {
linkBody.push(h('span.badge.badge-notification.clicks', l.clicks.toString()));
if (dedupedLinks.length <= 5) {
// show all links
_.each(dedupedLinks, l => result.push(this.linkHtml(l)));
} else {
// show up to 5 *incoming* links when collapsed
if (state.collapsed) {
const max = Math.min(5, incomingLinks.length);
for (let i = 0; i < max; i++) {
result.push(this.linkHtml(incomingLinks[i]));
}
result.push(h('li',
h('a.track-link', {
className: l.reflection ? 'inbound' : 'outbound',
attributes: {href: l.url}
}, [linkBody, iconNode(l.reflection ? 'arrow-left' : 'arrow-right')])
));
}
});
if (hasMore) {
// 'show more' link
if (dedupedLinks.length > 5) {
result.push(h('li', this.attach('link', {
labelCount: `post_links.title`,
title: "post_links.about",
@ -51,6 +55,9 @@ export default createWidget('post-links', {
className: 'expand-links'
})));
}
} else {
_.each(dedupedLinks, l => result.push(this.linkHtml(l)));
}
}
if (result.length) {