Refactor to use a single piece of logic

This commit is contained in:
Andre Pereira 2016-03-14 12:25:00 +00:00
parent da4c96fad2
commit 3e3aa91934
1 changed files with 14 additions and 17 deletions

View File

@ -21,14 +21,13 @@ export default createWidget('post-gutter', {
const seenTitles = {}; const seenTitles = {};
let titleCount = 0;
let i = 0; links.forEach(function(l) {
while (i < links.length && result.length < toShow) {
const l = links[i++];
let title = l.title; let title = l.title;
if (title && !seenTitles[title]) { if (title && !seenTitles[title]) {
seenTitles[title] = true; seenTitles[title] = true;
titleCount++;
if (result.length < toShow) {
const linkBody = [new RawHtml({html: `<span>${Discourse.Emoji.unescape(title)}</span>`})]; const linkBody = [new RawHtml({html: `<span>${Discourse.Emoji.unescape(title)}</span>`})];
if (l.clicks) { if (l.clicks) {
linkBody.push(h('span.badge.badge-notification.clicks', l.clicks.toString())); linkBody.push(h('span.badge.badge-notification.clicks', l.clicks.toString()));
@ -39,12 +38,10 @@ export default createWidget('post-gutter', {
result.push(h('li', link)); result.push(h('li', link));
} }
} }
});
if (state.collapsed) { if (state.collapsed) {
var differentTitles = [], allTitles = []; const remaining = titleCount - MAX_GUTTER_LINKS;
links.forEach(function(x) { allTitles.push(x.title); });
differentTitles = allTitles.filter(function(item, iter, ar){ return ar.indexOf(item) === iter; });
const remaining = differentTitles.length - MAX_GUTTER_LINKS;
if (remaining > 0) { if (remaining > 0) {
result.push(h('li', h('a.toggle-more', I18n.t('post.more_links', {count: remaining})))); result.push(h('li', h('a.toggle-more', I18n.t('post.more_links', {count: remaining}))));