From f5379cee89d3c45fd358133abe3919e61d597cd6 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 28 Oct 2014 17:20:43 +1100 Subject: [PATCH] PERF: move activity-column to handlebars --- .../components/activity-column.js.es6 | 43 ------------------- .../discourse/helpers/cold-age-class.js.es6 | 11 ++--- .../discourse/helpers/format-date.js.es6 | 22 +++++++--- .../discourse/helpers/handlebars.js.es6 | 4 +- .../javascripts/discourse/models/topic.js | 19 ++++++++ .../templates/components/basic-topic-list.hbs | 2 +- .../templates/list/activity_column.raw.hbs | 1 + .../templates/list/topic_list_item.hbs | 6 +-- .../mobile/components/basic-topic-list.hbs | 2 +- .../templates/mobile/list/topic_list_item.hbs | 2 +- 10 files changed, 50 insertions(+), 62 deletions(-) delete mode 100644 app/assets/javascripts/discourse/components/activity-column.js.es6 create mode 100644 app/assets/javascripts/discourse/templates/list/activity_column.raw.hbs diff --git a/app/assets/javascripts/discourse/components/activity-column.js.es6 b/app/assets/javascripts/discourse/components/activity-column.js.es6 deleted file mode 100644 index 8f6a20aee..000000000 --- a/app/assets/javascripts/discourse/components/activity-column.js.es6 +++ /dev/null @@ -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("" + Discourse.Formatter.autoUpdatingRelativeAge(this.get('bumpedAt')) + ""); - } - -}); diff --git a/app/assets/javascripts/discourse/helpers/cold-age-class.js.es6 b/app/assets/javascripts/discourse/helpers/cold-age-class.js.es6 index d7d644a79..0fa515ea7 100644 --- a/app/assets/javascripts/discourse/helpers/cold-age-class.js.es6 +++ b/app/assets/javascripts/discourse/helpers/cold-age-class.js.es6 @@ -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); diff --git a/app/assets/javascripts/discourse/helpers/format-date.js.es6 b/app/assets/javascripts/discourse/helpers/format-date.js.es6 index 1de71f4c2..312d8947b 100644 --- a/app/assets/javascripts/discourse/helpers/format-date.js.es6 +++ b/app/assets/javascripts/discourse/helpers/format-date.js.es6 @@ -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})); } }); diff --git a/app/assets/javascripts/discourse/helpers/handlebars.js.es6 b/app/assets/javascripts/discourse/helpers/handlebars.js.es6 index ce021cc2a..18931346b 100644 --- a/app/assets/javascripts/discourse/helpers/handlebars.js.es6 +++ b/app/assets/javascripts/discourse/helpers/handlebars.js.es6 @@ -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); + } } } diff --git a/app/assets/javascripts/discourse/models/topic.js b/app/assets/javascripts/discourse/models/topic.js index 40c0d9eb8..f748e2b33 100644 --- a/app/assets/javascripts/discourse/models/topic.js +++ b/app/assets/javascripts/discourse/models/topic.js @@ -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(), diff --git a/app/assets/javascripts/discourse/templates/components/basic-topic-list.hbs b/app/assets/javascripts/discourse/templates/components/basic-topic-list.hbs index 706b2ce3b..1cdf72cd3 100644 --- a/app/assets/javascripts/discourse/templates/components/basic-topic-list.hbs +++ b/app/assets/javascripts/discourse/templates/components/basic-topic-list.hbs @@ -48,7 +48,7 @@ {{number topic.views numberKey="views_long"}} - {{activity-column topic=topic class="num"}} + {{handlebars "list/activity_column" topic=topic class="num" tagName="td"}} {{/grouped-each}} diff --git a/app/assets/javascripts/discourse/templates/list/activity_column.raw.hbs b/app/assets/javascripts/discourse/templates/list/activity_column.raw.hbs new file mode 100644 index 000000000..1eba13241 --- /dev/null +++ b/app/assets/javascripts/discourse/templates/list/activity_column.raw.hbs @@ -0,0 +1 @@ +<{{this.tagName}} class="{{cold-age-class "topic.bumpedAt" class=this.class}} activity" title="{{get "topic.bumpedAtTitle"}}">{{format-date "topic.bumpedAt" format="tiny" noTitle=true}} diff --git a/app/assets/javascripts/discourse/templates/list/topic_list_item.hbs b/app/assets/javascripts/discourse/templates/list/topic_list_item.hbs index 8201a6dc8..3e108d328 100644 --- a/app/assets/javascripts/discourse/templates/list/topic_list_item.hbs +++ b/app/assets/javascripts/discourse/templates/list/topic_list_item.hbs @@ -27,10 +27,10 @@ {{/if}} -{{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"}} {{number views numberKey="views_long"}} -{{activity-column topic=model class="num"}} +{{handlebars "list/activity_column" topic=model class="num" tagName="td"}} diff --git a/app/assets/javascripts/discourse/templates/mobile/components/basic-topic-list.hbs b/app/assets/javascripts/discourse/templates/mobile/components/basic-topic-list.hbs index 3c03c8777..2b5f07508 100644 --- a/app/assets/javascripts/discourse/templates/mobile/components/basic-topic-list.hbs +++ b/app/assets/javascripts/discourse/templates/mobile/components/basic-topic-list.hbs @@ -27,7 +27,7 @@
{{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"}}
{{#unless controller.hideCategory}}
diff --git a/app/assets/javascripts/discourse/templates/mobile/list/topic_list_item.hbs b/app/assets/javascripts/discourse/templates/mobile/list/topic_list_item.hbs index 670c9f5b1..7ba927d93 100644 --- a/app/assets/javascripts/discourse/templates/mobile/list/topic_list_item.hbs +++ b/app/assets/javascripts/discourse/templates/mobile/list/topic_list_item.hbs @@ -31,7 +31,7 @@ {{posts-count-column topic=this tagName="div" class="num posts" action="showTopicEntrance"}}
{{last_poster_username}} - {{activity-column topic=this tagName="span" class="age"}} + {{handlebars "list/activity_column" topic=this tagName="span" class="age"}}