aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Green2018-06-24 15:05:20 +0800
committerYigit Sever2023-07-21 03:04:19 +0300
commit2dcb46a217e141d949dbce66ee79201658cfb391 (patch)
tree01fd15bdb7ec2cdf86e31d08e26d8e7eedb10ef3
parentf762d291b01eeda27fa9239c99efd607d5bf7a14 (diff)
downloadcgit-2dcb46a217e141d949dbce66ee79201658cfb391.tar.gz
cgit-2dcb46a217e141d949dbce66ee79201658cfb391.tar.bz2
cgit-2dcb46a217e141d949dbce66ee79201658cfb391.zip
js: add dynamic age update
This patch updates the emitted "ages" dynamically on the client side. After updating on completion of the document load, it sets a timer to update according to the smallest age it found. If there are any ages listed in minutes, then it will update again in 10s. When the most recent age is in hours, it updates every 5m. If days, then every 30m and so on. This keeps the cost of the dynamic updates at worst once per 10s. The updates are done entirely on the client side without contact with the server. To make this work reliably, since parsing datetimes is unreliable in browser js, the unix time is added as an attribute to all age spans. To make that reliable cross-platform, the unix time is treated as a uint64_t when it is formatted for printing. The rules for display conversion of the age is aligned with the existing server-side rules in ui-shared.h. If the client or server-side time are not synchronized by ntpd etc, ages shown on the client will not relate to the original ages computed at the server. The client updates the ages immediately when the DOM has finished loading, so in the case the times at the server and client are not aligned, this patch changes what the user sees on the page to reflect patch age compared to client time. If the server and client clocks are aligned, this patch makes no difference to what is seen on the page. Signed-off-by: Andy Green <andy@warmcat.com> Signed-off-by: Christian Hesse <mail@eworm.de>
-rw-r--r--cgit.h1
-rw-r--r--cgit.js61
-rw-r--r--ui-shared.c2
3 files changed, 63 insertions, 1 deletions
diff --git a/cgit.h b/cgit.h
index a0cf5e9..ddd2ccb 100644
--- a/cgit.h
+++ b/cgit.h
@@ -25,6 +25,7 @@
25#include <utf8.h> 25#include <utf8.h>
26#include <notes.h> 26#include <notes.h>
27#include <graph.h> 27#include <graph.h>
28#include <inttypes.h>
28 29
29/* Add isgraph(x) to Git's sane ctype support (see git-compat-util.h) */ 30/* Add isgraph(x) to Git's sane ctype support (see git-compat-util.h) */
30#undef isgraph 31#undef isgraph
diff --git a/cgit.js b/cgit.js
index b35e0bc..df3ad4e 100644
--- a/cgit.js
+++ b/cgit.js
@@ -5,3 +5,64 @@
5 * Licensed under GNU General Public License v2 5 * Licensed under GNU General Public License v2
6 * (see COPYING for full license text) 6 * (see COPYING for full license text)
7 */ 7 */
8
9(function () {
10
11/* This follows the logic and suffixes used in ui-shared.c */
12
13var age_classes = [ "age-mins", "age-hours", "age-days", "age-weeks", "age-months", "age-years" ];
14var age_suffix = [ "min.", "hours", "days", "weeks", "months", "years", "years" ];
15var age_next = [ 60, 3600, 24 * 3600, 7 * 24 * 3600, 30 * 24 * 3600, 365 * 24 * 3600, 365 * 24 * 3600 ];
16var age_limit = [ 7200, 24 * 7200, 7 * 24 * 7200, 30 * 24 * 7200, 365 * 25 * 7200, 365 * 25 * 7200 ];
17var update_next = [ 10, 5 * 60, 1800, 24 * 3600, 24 * 3600, 24 * 3600, 24 * 3600 ];
18
19function render_age(e, age) {
20 var t, n;
21
22 for (n = 0; n < age_classes.length; n++)
23 if (age < age_limit[n])
24 break;
25
26 t = Math.round(age / age_next[n]) + " " + age_suffix[n];
27
28 if (e.textContent != t) {
29 e.textContent = t;
30 if (n == age_classes.length)
31 n--;
32 if (e.className != age_classes[n])
33 e.className = age_classes[n];
34 }
35}
36
37function aging() {
38 var n, next = 24 * 3600,
39 now_ut = Math.round((new Date().getTime() / 1000));
40
41 for (n = 0; n < age_classes.length; n++) {
42 var m, elems = document.getElementsByClassName(age_classes[n]);
43
44 if (elems.length && update_next[n] < next)
45 next = update_next[n];
46
47 for (m = 0; m < elems.length; m++) {
48 var age = now_ut - elems[m].getAttribute("data-ut");
49
50 render_age(elems[m], age);
51 }
52 }
53
54 /*
55 * We only need to come back when the age might have changed.
56 * Eg, if everything is counted in hours already, once per
57 * 5 minutes is accurate enough.
58 */
59
60 window.setTimeout(aging, next * 1000);
61}
62
63document.addEventListener("DOMContentLoaded", function() {
64 /* we can do the aging on DOM content load since no layout dependency */
65 aging();
66}, false);
67
68})();
diff --git a/ui-shared.c b/ui-shared.c
index bc76a6e..959ed8b 100644
--- a/ui-shared.c
+++ b/ui-shared.c
@@ -673,7 +673,7 @@ const struct date_mode *cgit_date_mode(enum date_mode_type type)
673static void print_rel_date(time_t t, int tz, double value, 673static void print_rel_date(time_t t, int tz, double value,
674 const char *class, const char *suffix) 674 const char *class, const char *suffix)
675{ 675{
676 htmlf("<span class='%s' title='", class); 676 htmlf("<span class='%s' data-ut='%" PRIu64 "' title='", class, (uint64_t)t);
677 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601))); 677 html_attr(show_date(t, tz, cgit_date_mode(DATE_ISO8601)));
678 htmlf("'>%.0f %s</span>", value, suffix); 678 htmlf("'>%.0f %s</span>", value, suffix);
679} 679}