|
@@ -1,70 +1,99 @@
|
|
|
|
+// Misago dynamic timestamps via Moment.js
|
|
$(function() {
|
|
$(function() {
|
|
- moment.lang($('html').attr('lang'))
|
|
|
|
|
|
|
|
- if (lang_time_units === undefined) {
|
|
|
|
- var units_formats = "smhd";
|
|
|
|
- } else {
|
|
|
|
- var units_formats = lang_time_units;
|
|
|
|
- }
|
|
|
|
|
|
+ function MisagoTimestamps() {
|
|
|
|
|
|
- var moment_now = moment();
|
|
|
|
- var time_format = moment_now.lang()._longDateFormat.LT;
|
|
|
|
|
|
+ var _this = this;
|
|
|
|
|
|
- var moments = {};
|
|
|
|
|
|
+ moment.lang($('html').attr('lang'))
|
|
|
|
|
|
- // Initialize moment.js for dates
|
|
|
|
- function discover_dates() {
|
|
|
|
- $('.dynamic').each(function() {
|
|
|
|
- var timestamp = $(this).data('timestamp');
|
|
|
|
- var rel_moment = moment(timestamp);
|
|
|
|
|
|
+ if (lang_time_units === undefined) {
|
|
|
|
+ this.units_formats = "smhd";
|
|
|
|
+ } else {
|
|
|
|
+ this.units_formats = lang_time_units;
|
|
|
|
+ }
|
|
|
|
|
|
- moments[timestamp] = {
|
|
|
|
- rel_moment: rel_moment,
|
|
|
|
- original: $(this).text()
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
- discover_dates();
|
|
|
|
-
|
|
|
|
- // Update function
|
|
|
|
- function update_times() {
|
|
|
|
- $('.time-ago').each(function() {
|
|
|
|
- var timestamp = $(this).data('timestamp');
|
|
|
|
-
|
|
|
|
- if (moment_now.diff(moments[timestamp].rel_moment, 'minutes') <= 6 && moment_now.diff(moments[timestamp].rel_moment, 'hours') >= -6) {
|
|
|
|
- $(this).text(moments[timestamp].rel_moment.fromNow());
|
|
|
|
- } else if (moment_now.diff(moments[timestamp].rel_moment, 'days') < 1 && moment_now.diff(moments[timestamp].rel_moment, 'days') > -1) {
|
|
|
|
- $(this).text(moments[timestamp].rel_moment.calendar());
|
|
|
|
- } else if (moment_now.diff(moments[timestamp].rel_moment, 'days') < 7 && moment_now.diff(moments[timestamp].rel_moment, 'days') > -7) {
|
|
|
|
- $(this).text(moments[timestamp].rel_moment.format("dddd, " + time_format));
|
|
|
|
|
|
+ this.moment_now = moment();
|
|
|
|
+ this.time_format = this.moment_now.lang()._longDateFormat.LT;
|
|
|
|
+
|
|
|
|
+ this.moments = {};
|
|
|
|
+
|
|
|
|
+ this.time_ago = function(timestamp) {
|
|
|
|
+ var rel_moment = _this.moments[timestamp].rel_moment;
|
|
|
|
+
|
|
|
|
+ if (_this.moment_now.diff(rel_moment, 'minutes') <= 6 && _this.moment_now.diff(rel_moment, 'hours') >= -6) {
|
|
|
|
+ return _this.moments[timestamp].rel_moment.fromNow();
|
|
|
|
+ } else if (_this.moment_now.diff(rel_moment, 'days') < 1 && _this.moment_now.diff(rel_moment, 'days') > -1) {
|
|
|
|
+ return _this.moments[timestamp].rel_moment.calendar();
|
|
|
|
+ } else if (_this.moment_now.diff(rel_moment, 'days') < 7 && _this.moment_now.diff(rel_moment, 'days') > -7) {
|
|
|
|
+ return _this.moments[timestamp].rel_moment.format("dddd, " + _this.time_format);
|
|
} else {
|
|
} else {
|
|
- $(this).text(moments[timestamp].original);
|
|
|
|
|
|
+ return _this.moments[timestamp].original;
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+ }
|
|
|
|
|
|
- $('.time-ago-compact').each(function() {
|
|
|
|
- var timestamp = $(this).data('timestamp');
|
|
|
|
- var diff_seconds = moment_now.diff(moments[timestamp].rel_moment, 'seconds');
|
|
|
|
|
|
+ this.time_ago_compact = function(timestamp) {
|
|
|
|
+ var rel_moment = _this.moments[timestamp].rel_moment;
|
|
|
|
+ var diff_seconds = _this.moment_now.diff(rel_moment, 'seconds');
|
|
|
|
|
|
if (diff_seconds < 60) {
|
|
if (diff_seconds < 60) {
|
|
- $(this).text(diff_seconds + units_formats[0]);
|
|
|
|
|
|
+ return diff_seconds + _this.units_formats[0];
|
|
} else if (diff_seconds < (60 * 56)) {
|
|
} else if (diff_seconds < (60 * 56)) {
|
|
var diff_minutes = Math.floor(diff_seconds / 60);
|
|
var diff_minutes = Math.floor(diff_seconds / 60);
|
|
- $(this).text(diff_minutes + units_formats[1]);
|
|
|
|
|
|
+ return diff_minutes + _this.units_formats[1];
|
|
} else if (diff_seconds <= (3600 * 23)) {
|
|
} else if (diff_seconds <= (3600 * 23)) {
|
|
var diff_hours = Math.floor(diff_seconds / 3600);
|
|
var diff_hours = Math.floor(diff_seconds / 3600);
|
|
- $(this).text(diff_hours + units_formats[2]);
|
|
|
|
|
|
+ return diff_hours + _this.units_formats[2];
|
|
} else if (true | diff_seconds < (3600 * 24 * 15)) {
|
|
} else if (true | diff_seconds < (3600 * 24 * 15)) {
|
|
var diff_days = Math.floor(diff_seconds / (3600 * 24));
|
|
var diff_days = Math.floor(diff_seconds / (3600 * 24));
|
|
- $(this).text(diff_days + units_formats[3]);
|
|
|
|
|
|
+ return diff_days + _this.units_formats[3];
|
|
} else {
|
|
} else {
|
|
- $(this).text(moments[timestamp].original);
|
|
|
|
|
|
+ return _this.moments[timestamp].original;
|
|
}
|
|
}
|
|
- });
|
|
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.discover = function() {
|
|
|
|
+ $('.dynamic').each(function() {
|
|
|
|
+ if ($(this).data('misago-timestamp') == undefined) {
|
|
|
|
+ $(this).data('misago-timestamp', true);
|
|
|
|
+
|
|
|
|
+ var timestamp = $(this).data('timestamp');
|
|
|
|
+ var rel_moment = moment(timestamp);
|
|
|
|
+
|
|
|
|
+ if (_this.moments[timestamp] == undefined) {
|
|
|
|
+ _this.moments[timestamp] = {
|
|
|
|
+ rel_moment: rel_moment,
|
|
|
|
+ original: $(this).text()
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.update = function() {
|
|
|
|
+ $('.time-ago').each(function() {
|
|
|
|
+ $(this).text(_this.time_ago($(this).data('timestamp')));
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ $('.time-ago-compact').each(function() {
|
|
|
|
+ $(this).text(_this.time_ago_compact($(this).data('timestamp')));
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.discover();
|
|
|
|
+ this.update();
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
- // Run updates
|
|
|
|
- $.misago_dom().change(discover_dates);
|
|
|
|
- $.misago_dom().change(update_times);
|
|
|
|
- window.setInterval(update_times, 5000);
|
|
|
|
|
|
+ // start timestamps controller
|
|
|
|
+ Misago.Timestamps = new MisagoTimestamps();
|
|
|
|
+ window.setInterval(Misago.Timestamps.update, 5000);
|
|
|
|
+
|
|
|
|
+ // keep document updated
|
|
|
|
+ Misago.DOM.on_change(function() {
|
|
|
|
+ Misago.Timestamps.discover();
|
|
|
|
+ Misago.Timestamps.update();
|
|
|
|
+ });
|
|
|
|
+
|
|
});
|
|
});
|