automatically updating times for posts on topic

moved moment.js into localization file (we need to localize it)
added helpers for date formatting use, moment().shortDate() moment().longDate() moment().shortDateNoYear()
This commit is contained in:
Sam
2013-06-11 17:25:50 +10:00
parent 6d85dc1724
commit c2cfbce9ce
8 changed files with 65 additions and 22 deletions

View File

@@ -4,15 +4,10 @@ Discourse.Formatter = (function(){
relativeAgeMedium, relativeAgeMediumSpan, longDate, toTitleCase,
shortDate;
var shortDateNoYearFormat = Ember.String.i18n("dates.short_date_no_year");
var longDateFormat = Ember.String.i18n("dates.long_date");
var shortDateFormat = Ember.String.i18n("dates.short_date");
shortDate = function(date){
return moment(date).format(shortDateFormat);
return moment(date).shortDate();
};
// http://stackoverflow.com/questions/196972/convert-string-to-title-case-with-javascript
// TODO: locale support ?
toTitleCase = function toTitleCase(str)
@@ -23,14 +18,14 @@ Discourse.Formatter = (function(){
}
longDate = function(dt) {
return moment(dt).format(longDateFormat);
return moment(dt).longDate();
};
updateRelativeAge = function(elems) {
// jQuery .each
elems.each(function(){
var $this = $(this);
$this.html(relativeAge(new Date($this.data('time')), $this.data('format')));
$this.html(relativeAge(new Date($this.data('time')), {format: $this.data('format'), wrapInSpan: false}));
});
};
@@ -38,7 +33,17 @@ Discourse.Formatter = (function(){
options = options || {};
var format = options.format || "tiny";
return "<span class='relative-date' data-time='" + date.getTime() + "' data-format='" + format + "'>" + relativeAge(date, options) + "</span>";
var append = "";
if(format === 'medium') {
append = " date' title='" + longDate(date);
if(options.leaveAgo) {
format = 'medium-with-ago';
}
options.wrapInSpan = false;
}
return "<span class='relative-date" + append + "' data-time='" + date.getTime() + "' data-format='" + format + "'>" + relativeAge(date, options) + "</span>";
};
@@ -139,7 +144,7 @@ Discourse.Formatter = (function(){
if ((new Date()).getFullYear() !== date.getFullYear()) {
displayDate = shortDate(date);
} else {
displayDate = moment(date).format(shortDateNoYearFormat);
displayDate = moment(date).shortDateNoYear();
}
} else {
displayDate = relativeAgeMediumSpan(distance, leaveAgo);
@@ -160,6 +165,8 @@ Discourse.Formatter = (function(){
return relativeAgeTiny(date, options);
} else if (format === "medium") {
return relativeAgeMedium(date, options);
} else if (format === 'medium-with-ago') {
return relativeAgeMedium(date, _.extend(options, {format: 'medium', leaveAgo: true}));
}
return "UNKNOWN FORMAT";

View File

@@ -212,7 +212,7 @@ Handlebars.registerHelper('unboundAge', function(property, options) {
Handlebars.registerHelper('editDate', function(property, options) {
// autoupdating this is going to be painful
var date = new Date(Ember.Handlebars.get(this, property, options));
return new Handlebars.SafeString(Discourse.Formatter.relativeAge(date, {format: 'medium', leaveAgo: true, wrapInSpan: false}));
return new Handlebars.SafeString(Discourse.Formatter.autoUpdatingRelativeAge(date, {format: 'medium', leaveAgo: true, wrapInSpan: false}));
});
/**
@@ -286,6 +286,6 @@ Handlebars.registerHelper('date', function(property, options) {
if (val) {
date = new Date(val);
}
return new Handlebars.SafeString(Discourse.Formatter.relativeAge(date, {format: 'medium', leaveAgo: leaveAgo}));
return new Handlebars.SafeString(Discourse.Formatter.autoUpdatingRelativeAge(date, {format: 'medium', leaveAgo: leaveAgo}));
});

File diff suppressed because it is too large Load Diff