mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
correct message bus regression
implement automatically updating dates in list
This commit is contained in:
@@ -141,6 +141,10 @@ Discourse = Ember.Application.createWithMixins({
|
||||
xhr.setRequestHeader('X-CSRF-Token', csrfToken);
|
||||
}
|
||||
});
|
||||
|
||||
setInterval(function(){
|
||||
Discourse.Formatter.updateRelativeAge($('.relative-date'));
|
||||
},60 * 1000);
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
68
app/assets/javascripts/discourse/components/formatter.js
Normal file
68
app/assets/javascripts/discourse/components/formatter.js
Normal file
@@ -0,0 +1,68 @@
|
||||
Discourse.Formatter = (function(){
|
||||
var updateRelativeAge, autoUpdatingRelativeAge, relativeAge;
|
||||
|
||||
updateRelativeAge = function(elems) {
|
||||
elems.each(function(){
|
||||
var $this = $(this);
|
||||
$this.html(relativeAge(new Date($this.data('time')), $this.data('format')));
|
||||
});
|
||||
};
|
||||
|
||||
autoUpdatingRelativeAge = function(date,options) {
|
||||
options = options || {};
|
||||
var format = options.format || "tiny";
|
||||
|
||||
return "<span class='relative-date' data-time='" + date.getTime() + "' data-format='" + format + "'>" + relativeAge(date, options) + "</span>";
|
||||
};
|
||||
|
||||
// mostly lifted from rails with a few amendments
|
||||
relativeAge = function(date, options) {
|
||||
options = options || {};
|
||||
var format = options.format || "tiny";
|
||||
|
||||
var distance = Math.round((new Date() - date) / 1000);
|
||||
var distance_in_minutes = Math.round(distance / 60.0);
|
||||
|
||||
var formatted;
|
||||
var t = function(key,opts){
|
||||
return Ember.String.i18n("dates." + format + "." + key, opts);
|
||||
};
|
||||
|
||||
switch(true){
|
||||
|
||||
case(distance_in_minutes < 1):
|
||||
formatted = t("less_than_x_minutes", {count: 1});
|
||||
break;
|
||||
case(distance_in_minutes >= 1 && distance_in_minutes <= 44):
|
||||
formatted = t("x_minutes", {count: distance_in_minutes});
|
||||
break;
|
||||
case(distance_in_minutes >= 45 && distance_in_minutes <= 89):
|
||||
formatted = t("about_x_hours", {count: 1});
|
||||
break;
|
||||
case(distance_in_minutes >= 90 && distance_in_minutes <= 1439):
|
||||
formatted = t("about_x_hours", {count: Math.round(distance_in_minutes / 60.0)});
|
||||
break;
|
||||
case(distance_in_minutes >= 1440 && distance_in_minutes <= 2519):
|
||||
formatted = t("x_days", {count: 1});
|
||||
break;
|
||||
case(distance_in_minutes >= 2520 && distance_in_minutes <= 129599):
|
||||
formatted = t("x_days", {count: Math.round(distance_in_minutes / 1440.0)});
|
||||
break;
|
||||
case(distance_in_minutes >= 129600 && distance_in_minutes <= 525599):
|
||||
formatted = t("x_months", {count: Math.round(distance_in_minutes / 43200.0)});
|
||||
break;
|
||||
default:
|
||||
var months = Math.round(distance_in_minutes / 43200.0);
|
||||
if (months < 24) {
|
||||
formatted = t("x_months", {count: months});
|
||||
} else {
|
||||
formatted = t("over_x_years", {count: Math.round(months / 12.0)});
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return formatted;
|
||||
};
|
||||
|
||||
return {relativeAge: relativeAge, autoUpdatingRelativeAge: autoUpdatingRelativeAge, updateRelativeAge: updateRelativeAge};
|
||||
})();
|
||||
@@ -173,11 +173,21 @@ Handlebars.registerHelper('avatar', function(user, options) {
|
||||
@for Handlebars
|
||||
**/
|
||||
Handlebars.registerHelper('unboundDate', function(property, options) {
|
||||
var dt;
|
||||
dt = new Date(Ember.Handlebars.get(this, property, options));
|
||||
var dt = new Date(Ember.Handlebars.get(this, property, options));
|
||||
return dt.format("long");
|
||||
});
|
||||
|
||||
/**
|
||||
Live refreshing age helper
|
||||
|
||||
@method unboundDate
|
||||
@for Handlebars
|
||||
**/
|
||||
Handlebars.registerHelper('unboundAge', function(property, options) {
|
||||
var dt = new Date(Ember.Handlebars.get(this, property, options));
|
||||
return new Handlebars.SafeString(Discourse.Formatter.autoUpdatingRelativeAge(dt));
|
||||
});
|
||||
|
||||
/**
|
||||
Display a date related to an edit of a post
|
||||
|
||||
@@ -285,7 +295,7 @@ Handlebars.registerHelper('date', function(property, options) {
|
||||
}
|
||||
displayDate = humanized;
|
||||
if (!leaveAgo) {
|
||||
displayDate = (dt.millisecondsAgo()).duration();
|
||||
displayDate = (dt.millisecondsAgo()).duration();
|
||||
}
|
||||
}
|
||||
return new Handlebars.SafeString("<span class='date' title='" + fullReadable + "'>" + displayDate + "</span>");
|
||||
|
||||
@@ -63,14 +63,14 @@
|
||||
|
||||
{{#if bumped}}
|
||||
<td class='num activity'>
|
||||
<a href="{{url}}" {{{bindAttr class=":age ageCold"}}} title='{{i18n first_post}}: {{{unboundDate created_at}}}' >{{{age}}}</a>
|
||||
<a href="{{url}}" {{{bindAttr class=":age ageCold"}}} title='{{i18n first_post}}: {{{unboundDate created_at}}}' >{{unboundAge created_at}}</a>
|
||||
</td>
|
||||
<td class='num activity last'>
|
||||
<a href="{{lastPostUrl}}" class='age' title='{{i18n last_post}}: {{{unboundDate bumped_at}}}'>{{{bumped_age}}}</a>
|
||||
<a href="{{lastPostUrl}}" class='age' title='{{i18n last_post}}: {{{unboundDate bumped_at}}}'>{{unboundAge bumped_at}}</a>
|
||||
</td>
|
||||
{{else}}
|
||||
<td class='num activity'>
|
||||
<a href="{{url}}" class='age' title='{{i18n first_post}}: {{{unboundDate created_at}}}'>{{{age}}}</a>
|
||||
<a href="{{url}}" class='age' title='{{i18n first_post}}: {{{unboundDate created_at}}}'>{{unboundAge created_at}}</a>
|
||||
</td>
|
||||
<td></td>
|
||||
{{/if}}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
require_dependency 'age_words'
|
||||
require_dependency 'pinned_check'
|
||||
|
||||
class ListableTopicSerializer < BasicTopicSerializer
|
||||
@@ -10,8 +9,6 @@ class ListableTopicSerializer < BasicTopicSerializer
|
||||
:last_posted_at,
|
||||
:bumped,
|
||||
:bumped_at,
|
||||
:bumped_age,
|
||||
:age,
|
||||
:unseen,
|
||||
:last_read_post_number,
|
||||
:unread,
|
||||
@@ -23,20 +20,10 @@ class ListableTopicSerializer < BasicTopicSerializer
|
||||
:closed,
|
||||
:archived
|
||||
|
||||
def age
|
||||
AgeWords.age_words(Time.now - (object.created_at || Time.now))
|
||||
end
|
||||
|
||||
def bumped
|
||||
object.created_at < object.bumped_at
|
||||
end
|
||||
|
||||
def bumped_age
|
||||
return nil if object.bumped_at.blank?
|
||||
AgeWords.age_words(Time.now - object.bumped_at)
|
||||
end
|
||||
alias include_bumped_age? :bumped
|
||||
|
||||
def seen
|
||||
object.user_data.present?
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user