Skip to content

Commit

Permalink
removed relativeTime util method, utils.js better be a C now.
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Jan 19, 2015
1 parent d2938ea commit 99dc37b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 39 deletions.
8 changes: 7 additions & 1 deletion public/src/modules/notifications.js
Expand Up @@ -25,11 +25,14 @@ define('notifications', ['sounds'], function(sound) {
image = '';
}

return '<li class="' + (notification.readClass || '') + '"><a href="' + (notification.path || '#') + '">' + image + '<span class="pull-right relTime">' + utils.relativeTime(notification.datetime, true) + '</span><span class="text">' + notification.bodyShort + '</span></a></li>';
return '<li class="' + (notification.readClass || '') + '"><a href="' + (notification.path || '#') + '">' + image + '<span class="pull-right relTime">' + $.timeago(new Date(parseInt(notification.datetime, 10))) + '</span><span class="text">' + notification.bodyShort + '</span></a></li>';
}

var x, html = '';

// Switch to shorthand
translator.toggleTimeagoShorthand();

if (!err && (data.read.length + data.unread.length) > 0) {
var image = '';
for (x = 0; x < data.unread.length; x++) {
Expand All @@ -43,6 +46,9 @@ define('notifications', ['sounds'], function(sound) {
html += '<li class="no-notifs"><a>[[notifications:no_notifs]]</a></li>';
}

// Switch back to original timeago strings
translator.toggleTimeagoShorthand();

html += '<li class="pagelink"><a href="' + config.relative_path + '/notifications">[[notifications:see_all]]</a></li>';

notifList.translateHtml(html);
Expand Down
28 changes: 28 additions & 0 deletions public/src/translator.js
Expand Up @@ -85,6 +85,34 @@
}
};

translator.toggleTimeagoShorthand = function() {
if (!translator.timeagoStrings) {
translator.timeagoStrings = $.extend({}, jQuery.timeago.settings.strings);
jQuery.timeago.settings.strings = {
prefixAgo: null,
prefixFromNow: null,
suffixAgo: "",
suffixFromNow: "",
seconds: "1m",
minute: "1m",
minutes: "%dm",
hour: "1h",
hours: "%dh",
day: "1d",
days: "%dd",
month: "1mo",
months: "%dmo",
year: "1yr",
years: "%dyr",
wordSeparator: " ",
numbers: []
};
} else {
jQuery.timeago.settings.strings = $.extend({}, translator.timeagoStrings);
delete translator.timeagoStrings;
}
};

translator.translate = function (text, language, callback) {
if (typeof language === 'function') {
callback = language;
Expand Down
38 changes: 0 additions & 38 deletions public/src/utils.js
Expand Up @@ -63,44 +63,6 @@
});
},

relativeTime: function(timestamp, min) {
var now = +new Date(),
difference = now - Math.floor(parseFloat(timestamp));

if(difference < 0) {
difference = 0;
}

difference = Math.floor(difference / 1000);

if (difference < 60) {
return difference + (min ? 's' : ' second') + (difference !== 1 && !min ? 's' : '');
}

difference = Math.floor(difference / 60);
if (difference < 60) {
return difference + (min ? 'm' : ' minute') + (difference !== 1 && !min ? 's' : '');
}

difference = Math.floor(difference / 60);
if (difference < 24) {
return difference + (min ? 'h' : ' hour') + (difference !== 1 && !min ? 's' : '');
}

difference = Math.floor(difference / 24);
if (difference < 30) {
return difference + (min ? 'd' : ' day') + (difference !== 1 && !min ? 's' : '');
}

difference = Math.floor(difference / 30);
if (difference < 12) {
return difference + (min ? 'mon' : ' month') + (difference !== 1 && !min ? 's' : '');
}

difference = Math.floor(difference / 12);
return difference + (min ? 'y' : ' year') + (difference !== 1 && !min ? 's' : '');
},

invalidUnicodeChars: XRegExp('[^\\p{L}\\s\\d\\-_]', 'g'),
invalidLatinChars: /[^\w\s\d\-_]/g,
trimRegex: /^\s+|\s+$/g,
Expand Down

0 comments on commit 99dc37b

Please sign in to comment.