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,45 +11,52 @@ export default createWidget('post-links', {
return { collapsed: true }; 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) { html(attrs, state) {
const links = this.attrs.links || []; 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 = []; const result = [];
if (links.length) {
const seenTitles = {}; if (dedupedLinks.length <= 5) {
// show all links
let titleCount = 0; _.each(dedupedLinks, l => result.push(this.linkHtml(l)));
} else {
let hasMore = links.any((l) => { // show up to 5 *incoming* links when collapsed
if (state.collapsed && titleCount === 5) { return true; } if (state.collapsed) {
const max = Math.min(5, incomingLinks.length);
let title = l.title; for (let i = 0; i < max; i++) {
if (title && !seenTitles[title]) { result.push(this.linkHtml(incomingLinks[i]));
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()));
}
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')])
));
} }
}); // 'show more' link
if (dedupedLinks.length > 5) {
if (hasMore) { result.push(h('li', this.attach('link', {
result.push(h('li', this.attach('link', { labelCount: `post_links.title`,
labelCount: `post_links.title`, title: "post_links.about",
title: "post_links.about", count: links.length,
count: links.length, action: 'expandLinks',
action: 'expandLinks', className: 'expand-links'
className: 'expand-links' })));
}))); }
} else {
_.each(dedupedLinks, l => result.push(this.linkHtml(l)));
} }
} }