switch timeago to a javascript implementation

This commit is contained in:
Shish 2011-12-31 14:12:05 +00:00
parent 1857075bd9
commit ad2bb8b8b6
5 changed files with 152 additions and 32 deletions

View File

@ -129,34 +129,9 @@ function to_shorthand_int($int) {
* @retval string
*/
function autodate($date, $html=true) {
global $config;
$format = $config->get_string('autodate_format', "F j, Y");
$seconds = time() - strtotime($date);
$ago = seconds_to_time($seconds) . " ago";
$on = "on " . date($format, strtotime($date));
if($config->get_bool('use_autodate', true)) {
return ($html ? "<span title='$on'>$ago</span>" : $ago);
}
else {
return $on;
}
}
/**
* Turn a number of seconds into a vague human timespan, eg 142 -> "2 minutes"
*
* @retval string
*/
function seconds_to_time($seconds) {
if($seconds<60) return $seconds . " second" . plural($seconds); $seconds = round($seconds/60);
if($seconds<60) return $seconds . " minute" . plural($seconds); $seconds = round($seconds/60);
if($seconds<24) return $seconds . " hour" . plural($seconds); $seconds = round($seconds/24);
if($seconds<7) return $seconds . " day" . plural($seconds); $seconds = round($seconds/7);
if($seconds<4) return $seconds . " week" . plural($seconds); $seconds = round($seconds/4);
if($seconds<12) return $seconds . " month" . plural($seconds); $seconds = round($seconds/12);
return $seconds . " year" . plural($seconds);
$cpu = date('c', strtotime($date));
$hum = date('F j, Y', strtotime($date));
return ($html ? "<time datetime='$cpu'>$hum</time>" : $hum);
}

View File

@ -169,10 +169,8 @@ class Setup extends SimpleExtension {
$config->set_default_string("main_page", "post/list");
$config->set_default_string("base_href", "./index.php?q=");
$config->set_default_string("theme", "default");
$config->set_default_bool("use_autodate", true);
$config->set_default_bool("word_wrap", true);
$config->set_default_bool("comment_captcha", false);
$config->set_default_string("autodate_format", "F j, Y");
// Automatic caching is disabled by default
$config->set_default_string("autocache_location", "data/cache");
$config->set_default_bool("autocache_css", false);

View File

@ -166,8 +166,8 @@ class UserPage extends SimpleExtension {
public function onUserPageBuilding(Event $event) {
global $page, $user, $config;
$h_join_date = html_escape($event->display_user->join_date);
$event->add_stats("Join date: $h_join_date", 10);
$h_join_date = autodate($event->display_user->join_date);
$event->add_stats("Joined: $h_join_date", 10);
$av = $event->display_user->get_avatar_html();
if($av) $event->add_stats($av, 0);

146
lib/jquery.timeago.js Normal file
View File

@ -0,0 +1,146 @@
/**
* Timeago is a jQuery plugin that makes it easy to support automatically
* updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
*
* @name timeago
* @version 0.10.0
* @requires jQuery v1.2.3+
* @author Ryan McGeary
* @license MIT License - http://www.opensource.org/licenses/mit-license.php
*
* For usage and examples, visit:
* http://timeago.yarp.com/
*
* Copyright (c) 2008-2011, Ryan McGeary (ryanonjavascript -[at]- mcgeary [*dot*] org)
*/
(function($) {
$.timeago = function(timestamp) {
if (timestamp instanceof Date) {
return inWords(timestamp);
} else if (typeof timestamp === "string") {
return inWords($.timeago.parse(timestamp));
} else {
return inWords($.timeago.datetime(timestamp));
}
};
var $t = $.timeago;
$.extend($.timeago, {
settings: {
refreshMillis: 60000,
allowFuture: false,
strings: {
prefixAgo: null,
prefixFromNow: null,
suffixAgo: "ago",
suffixFromNow: "from now",
seconds: "less than a minute",
minute: "about a minute",
minutes: "%d minutes",
hour: "about an hour",
hours: "about %d hours",
day: "a day",
days: "%d days",
month: "about a month",
months: "%d months",
year: "about a year",
years: "%d years",
numbers: []
}
},
inWords: function(distanceMillis) {
var $l = this.settings.strings;
var prefix = $l.prefixAgo;
var suffix = $l.suffixAgo;
if (this.settings.allowFuture) {
if (distanceMillis < 0) {
prefix = $l.prefixFromNow;
suffix = $l.suffixFromNow;
}
}
var seconds = Math.abs(distanceMillis) / 1000;
var minutes = seconds / 60;
var hours = minutes / 60;
var days = hours / 24;
var years = days / 365;
function substitute(stringOrFunction, number) {
var string = $.isFunction(stringOrFunction) ? stringOrFunction(number, distanceMillis) : stringOrFunction;
var value = ($l.numbers && $l.numbers[number]) || number;
return string.replace(/%d/i, value);
}
var words = seconds < 45 && substitute($l.seconds, Math.round(seconds)) ||
seconds < 90 && substitute($l.minute, 1) ||
minutes < 45 && substitute($l.minutes, Math.round(minutes)) ||
minutes < 90 && substitute($l.hour, 1) ||
hours < 24 && substitute($l.hours, Math.round(hours)) ||
hours < 48 && substitute($l.day, 1) ||
days < 30 && substitute($l.days, Math.floor(days)) ||
days < 60 && substitute($l.month, 1) ||
days < 365 && substitute($l.months, Math.floor(days / 30)) ||
years < 2 && substitute($l.year, 1) ||
substitute($l.years, Math.floor(years));
return $.trim([prefix, words, suffix].join(" "));
},
parse: function(iso8601) {
var s = $.trim(iso8601);
s = s.replace(/\.\d\d\d+/,""); // remove milliseconds
s = s.replace(/-/,"/").replace(/-/,"/");
s = s.replace(/T/," ").replace(/Z/," UTC");
s = s.replace(/([\+\-]\d\d)\:?(\d\d)/," $1$2"); // -04:00 -> -0400
return new Date(s);
},
datetime: function(elem) {
// jQuery's `is()` doesn't play well with HTML5 in IE
var isTime = $(elem).get(0).tagName.toLowerCase() === "time"; // $(elem).is("time");
var iso8601 = isTime ? $(elem).attr("datetime") : $(elem).attr("title");
return $t.parse(iso8601);
}
});
$.fn.timeago = function() {
var self = this;
self.each(refresh);
var $s = $t.settings;
if ($s.refreshMillis > 0) {
setInterval(function() { self.each(refresh); }, $s.refreshMillis);
}
return self;
};
function refresh() {
var data = prepareData(this);
if (!isNaN(data.datetime)) {
$(this).text(inWords(data.datetime));
}
return this;
}
function prepareData(element) {
element = $(element);
if (!element.data("timeago")) {
element.data("timeago", { datetime: $t.datetime(element) });
var text = $.trim(element.text());
if (text.length > 0) {
element.attr("title", text);
}
}
return element.data("timeago");
}
function inWords(date) {
return $t.inWords(distance(date));
}
function distance(date) {
return (new Date().getTime() - date.getTime());
}
// fix for IE6 suckage
document.createElement("abbr");
document.createElement("time");
}(jQuery));

View File

@ -26,6 +26,7 @@ $(document).ready(function() {
$confirm.dialog('open');
});
$("time").timeago();
});
var defaultTexts = new Array();