From 129cc92ec96c75bef1185b99535f3a9847da4dc2 Mon Sep 17 00:00:00 2001 From: Andre Pereira Date: Sun, 13 Mar 2016 13:54:03 +0000 Subject: [PATCH 1/4] Fixes the link count on the sidebar by only counting unique titles. --- app/assets/javascripts/discourse/widgets/post-gutter.js.es6 | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 index 177d1032134..f6bcbf94a8c 100644 --- a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 @@ -41,7 +41,10 @@ export default createWidget('post-gutter', { } if (state.collapsed) { - const remaining = links.length - MAX_GUTTER_LINKS; + var differentTitles = new Set(); + links.forEach(function(x) { differentTitles.add(x.title); }); + const remaining = differentTitles.size - MAX_GUTTER_LINKS; + if (remaining > 0) { result.push(h('li', h('a.toggle-more', I18n.t('post.more_links', {count: remaining})))); } From e7a4900baf745a3a6f5b883cf561a2724e14e573 Mon Sep 17 00:00:00 2001 From: Andre Pereira Date: Sun, 13 Mar 2016 14:31:03 +0000 Subject: [PATCH 2/4] Removed use of ES6 Set. --- .../javascripts/discourse/widgets/post-gutter.js.es6 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 index f6bcbf94a8c..8a0d5acc961 100644 --- a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 @@ -41,9 +41,10 @@ export default createWidget('post-gutter', { } if (state.collapsed) { - var differentTitles = new Set(); - links.forEach(function(x) { differentTitles.add(x.title); }); - const remaining = differentTitles.size - MAX_GUTTER_LINKS; + var differentTitles = [], allTitles = []; + links.forEach(function(x) { allTitles.push(x.title); }); + differentTitles = allTitles.filter(function(item, i, ar){ return ar.indexOf(item) === i; }); + const remaining = differentTitles.length - MAX_GUTTER_LINKS; if (remaining > 0) { result.push(h('li', h('a.toggle-more', I18n.t('post.more_links', {count: remaining})))); From da4c96fad28c0db68f8e0e1ae8fa1ce82438cc9a Mon Sep 17 00:00:00 2001 From: Andre Pereira Date: Sun, 13 Mar 2016 14:37:15 +0000 Subject: [PATCH 3/4] Fixes scope problem. --- app/assets/javascripts/discourse/widgets/post-gutter.js.es6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 index 8a0d5acc961..f580b9aea53 100644 --- a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 @@ -43,7 +43,7 @@ export default createWidget('post-gutter', { if (state.collapsed) { var differentTitles = [], allTitles = []; links.forEach(function(x) { allTitles.push(x.title); }); - differentTitles = allTitles.filter(function(item, i, ar){ return ar.indexOf(item) === i; }); + differentTitles = allTitles.filter(function(item, iter, ar){ return ar.indexOf(item) === iter; }); const remaining = differentTitles.length - MAX_GUTTER_LINKS; if (remaining > 0) { From 3e3aa919342eb71f88da8eec781661f054292b82 Mon Sep 17 00:00:00 2001 From: Andre Pereira Date: Mon, 14 Mar 2016 12:25:00 +0000 Subject: [PATCH 4/4] Refactor to use a single piece of logic --- .../discourse/widgets/post-gutter.js.es6 | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 index f580b9aea53..61d7f973e0f 100644 --- a/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-gutter.js.es6 @@ -21,30 +21,27 @@ export default createWidget('post-gutter', { const seenTitles = {}; - - let i = 0; - while (i < links.length && result.length < toShow) { - const l = links[i++]; - + let titleCount = 0; + links.forEach(function(l) { let title = l.title; if (title && !seenTitles[title]) { seenTitles[title] = true; - const linkBody = [new RawHtml({ html: `${Discourse.Emoji.unescape(title)}` })]; - if (l.clicks) { - linkBody.push(h('span.badge.badge-notification.clicks', l.clicks.toString())); - } + titleCount++; + if (result.length < toShow) { + const linkBody = [new RawHtml({html: `${Discourse.Emoji.unescape(title)}`})]; + if (l.clicks) { + linkBody.push(h('span.badge.badge-notification.clicks', l.clicks.toString())); + } - const className = l.reflection ? 'inbound' : 'outbound'; - const link = h('a.track-link', { className, attributes: { href: l.url } }, linkBody); - result.push(h('li', link)); + const className = l.reflection ? 'inbound' : 'outbound'; + const link = h('a.track-link', {className, attributes: {href: l.url}}, linkBody); + result.push(h('li', link)); + } } - } + }); if (state.collapsed) { - var differentTitles = [], allTitles = []; - 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; + const remaining = titleCount - MAX_GUTTER_LINKS; if (remaining > 0) { result.push(h('li', h('a.toggle-more', I18n.t('post.more_links', {count: remaining}))));