aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--cgit.h1
-rw-r--r--cgit.js63
-rw-r--r--ui-shared.c2
3 files changed, 65 insertions, 1 deletions
diff --git a/cgit.h b/cgit.h
index bb5e1f5..e2c2604 100644
--- a/cgit.h
+++ b/cgit.h
@@ -25,6 +25,7 @@
#include <utf8.h>
#include <notes.h>
#include <graph.h>
+#include <inttypes.h>
/* Add isgraph(x) to Git's sane ctype support (see git-compat-util.h) */
#undef isgraph
diff --git a/cgit.js b/cgit.js
index b9e8f4c..8e50ae8 100644
--- a/cgit.js
+++ b/cgit.js
@@ -5,3 +5,66 @@
* Licensed under GNU General Public License v2
* (see COPYING for full license text)
*/
+
+(function () {
+
+/* this follows the logic and suffixes used in ui-shared.c */
+
+var age_classes = [ "age-mins", "age-hours", "age-days", "age-weeks", "age-months", "age-years" ];
+var age_suffix = [ "min.", "hours", "days", "weeks", "months", "years", "years" ];
+var age_next = [ 60, 3600, 24 * 3600, 7 * 24 * 3600, 30 * 24 * 3600, 365 * 24 * 3600, 365 * 24 * 3600 ];
+var age_limit = [ 7200, 24 * 7200, 7 * 24 * 7200, 30 * 24 * 7200, 365 * 25 * 7200, 365 * 25 * 7200 ];
+var update_next = [ 10, 5 * 60, 1800, 24 * 3600, 24 * 3600, 24 * 3600, 24 * 3600 ];
+
+function render_age(e, age)
+{
+ var t, n;
+
+ for (n = 0; n < age_classes.length; n++)
+ if (age < age_limit[n])
+ break;
+
+ t = Math.round(age / age_next[n]) + " " + age_suffix[n];
+
+ if (e.textContent != t) {
+ e.textContent = t;
+ if (n == age_classes.length)
+ n--;
+ if (e.className != age_classes[n])
+ e.className = age_classes[n];
+ }
+}
+
+function aging()
+{
+ var n, next = 24 * 3600,
+ now_ut = Math.round((new Date().getTime() / 1000));
+
+ for (n = 0; n < age_classes.length; n++) {
+ var m, elems = document.getElementsByClassName(age_classes[n]);
+
+ if (elems.length && update_next[n] < next)
+ next = update_next[n];
+
+ for (m = 0; m < elems.length; m++) {
+ var age = now_ut - elems[m].getAttribute("ut");
+
+ render_age(elems[m], age);
+ }
+ }
+
+ /*
+ * We only need to come back when the age might have changed.
+ * Eg, if everything is counted in hours already, once per
+ * 5 minutes is accurate enough.
+ */
+
+ window.setTimeout(aging, next * 1000);
+}
+
+document.addEventListener("DOMContentLoaded", function() {
+ /* we can do the aging on DOM content load since no layout dependency */
+ aging();
+}, false);
+
+})();
diff --git a/ui-shared.c b/ui-shared.c
index 3513e10..2a3c8ad 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -672,7 +672,7 @@ const struct date_mode *cgit_date_mode(enum date_mode_type type)
static void print_rel_date(time_t t, int tz, double value,
const char *class, const char *suffix)
{
- htmlf("<span class='%s' title='", class);
+ htmlf("<span class='%s' ut='%" PRIu64 "' title='", class, (uint64_t)t);
html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
htmlf("'>%.0f %s</span>", value, suffix);
}