PERF: move activity-column to handlebars

This commit is contained in:
Sam 2014-10-28 17:20:43 +11:00
parent 93af6107e7
commit f5379cee89
10 changed files with 50 additions and 62 deletions

View file

@ -1,43 +0,0 @@
import { daysSinceEpoch } from "discourse/helpers/cold-age-class";
export default Ember.Component.extend({
tagName: 'td',
classNameBindings: [':activity', 'coldness'],
attributeBindings: ['title'],
// returns createdAt if there's no bumped date
bumpedAt: function() {
var bumpedAt = this.get('topic.bumped_at');
if (bumpedAt) {
return new Date(bumpedAt);
} else {
return this.get('createdAt');
}
}.property('topic.bumped_at', 'createdAt'),
createdAt: function() {
return new Date(this.get('topic.created_at'));
}.property('topic.created_at'),
coldness: function() {
var bumpedAt = this.get('bumpedAt'),
createdAt = this.get('createdAt');
if (!bumpedAt) { return; }
var delta = daysSinceEpoch(bumpedAt) - daysSinceEpoch(createdAt);
if (delta > Discourse.SiteSettings.cold_age_days_high) { return 'coldmap-high'; }
if (delta > Discourse.SiteSettings.cold_age_days_medium) { return 'coldmap-med'; }
if (delta > Discourse.SiteSettings.cold_age_days_low) { return 'coldmap-low'; }
}.property('bumpedAt', 'createdAt'),
title: function() {
return I18n.t('first_post') + ": " + Discourse.Formatter.longDate(this.get('createdAt')) + "\n" +
I18n.t('last_post') + ": " + Discourse.Formatter.longDate(this.get('bumpedAt'));
}.property('bumpedAt', 'createdAt'),
render: function(buffer) {
buffer.push("<a href='" + this.get('topic.lastPostUrl') +"'>" + Discourse.Formatter.autoUpdatingRelativeAge(this.get('bumpedAt')) + "</a>");
}
});

View file

@ -8,18 +8,19 @@ export function daysSinceEpoch(dt) {
**/
function coldAgeClass(property, options) {
var dt = Em.Handlebars.get(this, property, options);
var className = (options && options.hash && options.hash.class !== undefined) ? options.hash.class : 'age';
if (!dt) { return 'age'; }
if (!dt) { return className; }
// Show heat on age
var nowDays = daysSinceEpoch(new Date()),
epochDays = daysSinceEpoch(new Date(dt));
if (nowDays - epochDays > Discourse.SiteSettings.cold_age_days_high) return 'age coldmap-high';
if (nowDays - epochDays > Discourse.SiteSettings.cold_age_days_medium) return 'age coldmap-med';
if (nowDays - epochDays > Discourse.SiteSettings.cold_age_days_low) return 'age coldmap-low';
if (nowDays - epochDays > Discourse.SiteSettings.cold_age_days_high) return className + ' coldmap-high';
if (nowDays - epochDays > Discourse.SiteSettings.cold_age_days_medium) return className + ' coldmap-med';
if (nowDays - epochDays > Discourse.SiteSettings.cold_age_days_low) return className + ' coldmap-low';
return 'age';
return className;
}
Handlebars.registerHelper('cold-age-class', coldAgeClass);

View file

@ -3,19 +3,27 @@
update the dates on a regular interval.
**/
Handlebars.registerHelper('format-date', function(property, options) {
var leaveAgo;
if (property.hash) {
if (property.hash.leaveAgo) {
leaveAgo = property.hash.leaveAgo === "true";
var leaveAgo, format = 'medium', title = true;
var hash = property.hash || (options && options.hash);
if (hash) {
if (hash.leaveAgo) {
leaveAgo = hash.leaveAgo === "true";
}
if (property.hash.path) {
property = property.hash.path;
if (hash.path) {
property = hash.path;
}
if (hash.format) {
format = hash.format;
}
if (hash.noTitle) {
title = false;
}
}
var val = Ember.Handlebars.get(this, property, options);
if (val) {
var date = new Date(val);
return new Handlebars.SafeString(Discourse.Formatter.autoUpdatingRelativeAge(date, {format: 'medium', title: true, leaveAgo: leaveAgo}));
return new Handlebars.SafeString(Discourse.Formatter.autoUpdatingRelativeAge(date, {format: format, title: title, leaveAgo: leaveAgo}));
}
});

View file

@ -5,7 +5,9 @@ Handlebars.registerHelper('handlebars', function(property, options) {
if(params) {
for(var prop in params){
params[prop] = Em.Handlebars.get(this, params[prop]);
if(options.hashTypes[prop] === "ID") {
params[prop] = Em.Handlebars.get(this, params[prop], options);
}
}
}

View file

@ -1,5 +1,24 @@
Discourse.Topic = Discourse.Model.extend({
// returns createdAt if there's no bumped date
bumpedAt: function() {
var bumpedAt = this.get('bumped_at');
if (bumpedAt) {
return new Date(bumpedAt);
} else {
return this.get('createdAt');
}
}.property('bumped_at', 'createdAt'),
bumpedAtTitle: function() {
return I18n.t('first_post') + ": " + Discourse.Formatter.longDate(this.get('createdAt')) + "\n" +
I18n.t('last_post') + ": " + Discourse.Formatter.longDate(this.get('bumpedAt'));
}.property('bumpedAt'),
createdAt: function() {
return new Date(this.get('created_at'));
}.property('created_at'),
postStream: function() {
return Discourse.PostStream.create({topic: this});
}.property(),

View file

@ -48,7 +48,7 @@
{{number topic.views numberKey="views_long"}}
</td>
{{activity-column topic=topic class="num"}}
{{handlebars "list/activity_column" topic=topic class="num" tagName="td"}}
</tr>
{{/grouped-each}}
</tbody>

View file

@ -0,0 +1 @@
<{{this.tagName}} class="{{cold-age-class "topic.bumpedAt" class=this.class}} activity" title="{{get "topic.bumpedAtTitle"}}"><a href="{{get "topic.lastPostUrl"}}">{{format-date "topic.bumpedAt" format="tiny" noTitle=true}}</a></{{this.tagName}}>

View file

@ -27,10 +27,10 @@
{{/if}}
</td>
{{handlebars 'list/category_column' hideCategory=hideCategory category=category}}
{{handlebars 'list/posters_column' posters=posters}}
{{handlebars "list/category_column" hideCategory=hideCategory category=category}}
{{handlebars "list/posters_column" posters=posters}}
{{posts-count-column topic=model class="num" action="showTopicEntrance"}}
<td class="num views {{unbound viewsHeat}}">{{number views numberKey="views_long"}}</td>
{{activity-column topic=model class="num"}}
{{handlebars "list/activity_column" topic=model class="num" tagName="td"}}

View file

@ -27,7 +27,7 @@
<div class="topic-item-stats clearfix">
<div class="pull-right">
{{posts-count-column topic=topic tagName="div" class="num posts" action="clickedPosts"}}
{{activity-column topic=topic tagName="div" class="num activity last"}}
{{handlebars "list/activity_column" topic=topic tagName="div" class="num activity last"}}
</div>
{{#unless controller.hideCategory}}
<div class='category'>

View file

@ -31,7 +31,7 @@
{{posts-count-column topic=this tagName="div" class="num posts" action="showTopicEntrance"}}
<div class='num activity last'>
<a href="{{lastPostUrl}}" title='{{i18n last_post}}: {{{raw-date bumped_at}}}'>{{last_poster_username}}</a>
{{activity-column topic=this tagName="span" class="age"}}
{{handlebars "list/activity_column" topic=this tagName="span" class="age"}}
</div>
</div>
<div class="clearfix"></div>