mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 05:05:15 +08:00
When links have thousands of clicks, display them like 3.3K
This commit is contained in:
parent
e276045112
commit
15c9c90533
|
@ -291,18 +291,14 @@ Handlebars.registerHelper('number', function(property, options) {
|
||||||
title = I18n.t(options.hash.numberKey, { number: orig });
|
title = I18n.t(options.hash.numberKey, { number: orig });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Round off the thousands to one decimal place
|
|
||||||
var n = orig;
|
|
||||||
if (orig > 999 && !options.hash.noTitle) {
|
|
||||||
n = (orig / 1000).toFixed(1) + "K";
|
|
||||||
}
|
|
||||||
|
|
||||||
var classNames = 'number';
|
var classNames = 'number';
|
||||||
if (options.hash['class']) {
|
if (options.hash['class']) {
|
||||||
classNames += ' ' + Ember.Handlebars.get(this, options.hash['class'], options);
|
classNames += ' ' + Ember.Handlebars.get(this, options.hash['class'], options);
|
||||||
}
|
}
|
||||||
var result = "<span class='" + classNames + "'";
|
var result = "<span class='" + classNames + "'";
|
||||||
|
|
||||||
|
// Round off the thousands to one decimal place
|
||||||
|
var n = Discourse.Formatter.number(orig);
|
||||||
if (n !== title) {
|
if (n !== title) {
|
||||||
result += " title='" + Handlebars.Utils.escapeExpression(title) + "'";
|
result += " title='" + Handlebars.Utils.escapeExpression(title) + "'";
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
Discourse.Formatter = (function(){
|
|
||||||
|
|
||||||
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny,
|
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge, relativeAgeTiny,
|
||||||
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
|
||||||
shortDate, shortDateNoYear, tinyDateYear, breakUp, relativeAgeTinyShowsYear;
|
shortDate, shortDateNoYear, tinyDateYear, breakUp, relativeAgeTinyShowsYear;
|
||||||
|
@ -287,7 +285,17 @@ Discourse.Formatter = (function(){
|
||||||
return "UNKNOWN FORMAT";
|
return "UNKNOWN FORMAT";
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
var number = function(val) {
|
||||||
|
val = parseInt(val, 10);
|
||||||
|
if (isNaN(val)) val = 0;
|
||||||
|
|
||||||
|
if (val > 999) {
|
||||||
|
return (val / 1000).toFixed(1) + "K";
|
||||||
|
}
|
||||||
|
return val.toString();
|
||||||
|
};
|
||||||
|
|
||||||
|
Discourse.Formatter = {
|
||||||
longDate: longDate,
|
longDate: longDate,
|
||||||
relativeAge: relativeAge,
|
relativeAge: relativeAge,
|
||||||
autoUpdatingRelativeAge: autoUpdatingRelativeAge,
|
autoUpdatingRelativeAge: autoUpdatingRelativeAge,
|
||||||
|
@ -295,6 +303,6 @@ Discourse.Formatter = (function(){
|
||||||
toTitleCase: toTitleCase,
|
toTitleCase: toTitleCase,
|
||||||
shortDate: shortDate,
|
shortDate: shortDate,
|
||||||
breakUp: breakUp,
|
breakUp: breakUp,
|
||||||
cappedMemoize: cappedMemoize
|
cappedMemoize: cappedMemoize,
|
||||||
|
number: number
|
||||||
};
|
};
|
||||||
})();
|
|
||||||
|
|
|
@ -123,24 +123,23 @@ Discourse.PostView = Discourse.GroupedView.extend(Ember.Evented, {
|
||||||
var self = this,
|
var self = this,
|
||||||
link_counts = this.get('post.link_counts');
|
link_counts = this.get('post.link_counts');
|
||||||
|
|
||||||
if (link_counts) {
|
if (!link_counts) return;
|
||||||
_.each(link_counts, function(lc) {
|
|
||||||
if (lc.clicks > 0) {
|
link_counts.forEach(function(lc) {
|
||||||
|
if (!lc.clicks || lc.clicks < 1) return;
|
||||||
|
|
||||||
self.$(".cooked a[href]").each(function() {
|
self.$(".cooked a[href]").each(function() {
|
||||||
var link = $(this);
|
var link = $(this);
|
||||||
if (link.attr('href') === lc.url) {
|
if (link.attr('href') === lc.url) {
|
||||||
// don't display badge counts on category badge
|
// don't display badge counts on category badge
|
||||||
if (link.closest('.badge-category').length === 0) {
|
if (link.closest('.badge-category').length === 0 && (link.closest(".onebox-result").length === 0 || link.hasClass("track-link"))) {
|
||||||
// nor in oneboxes (except when we force it)
|
link.append("<span class='badge badge-notification clicks' title='" +
|
||||||
if (link.closest(".onebox-result").length === 0 || link.hasClass("track-link")) {
|
I18n.t("topic_map.clicks") +
|
||||||
link.append("<span class='badge badge-notification clicks' title='" + I18n.t("topic_map.clicks") + "'>" + lc.clicks + "</span>");
|
"'>" + Discourse.Formatter.number(lc.clicks) + "</span>");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
actions: {
|
actions: {
|
||||||
|
|
|
@ -199,3 +199,10 @@ test("breakUp", function(){
|
||||||
equal(b("bobmarleytoo","Bob Marley Too"), "bob<wbr>​marley<wbr>​too");
|
equal(b("bobmarleytoo","Bob Marley Too"), "bob<wbr>​marley<wbr>​too");
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("number", function() {
|
||||||
|
equal(Discourse.Formatter.number(123), "123", "it returns a string version of the number");
|
||||||
|
equal(Discourse.Formatter.number("123"), "123", "it works with a string command");
|
||||||
|
equal(Discourse.Formatter.number(NaN), "0", "it reeturns 0 for NaN");
|
||||||
|
equal(Discourse.Formatter.number(3333), "3.3K", "it abbreviates thousands");
|
||||||
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user