diff options
Diffstat (limited to 'cgit.js')
| -rw-r--r-- | cgit.js | 61 |
1 files changed, 61 insertions, 0 deletions
| @@ -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 | |||
| 13 | var age_classes = [ "age-mins", "age-hours", "age-days", "age-weeks", "age-months", "age-years" ]; | ||
| 14 | var age_suffix = [ "min.", "hours", "days", "weeks", "months", "years", "years" ]; | ||
| 15 | var age_next = [ 60, 3600, 24 * 3600, 7 * 24 * 3600, 30 * 24 * 3600, 365 * 24 * 3600, 365 * 24 * 3600 ]; | ||
| 16 | var age_limit = [ 7200, 24 * 7200, 7 * 24 * 7200, 30 * 24 * 7200, 365 * 25 * 7200, 365 * 25 * 7200 ]; | ||
| 17 | var update_next = [ 10, 5 * 60, 1800, 24 * 3600, 24 * 3600, 24 * 3600, 24 * 3600 ]; | ||
| 18 | |||
| 19 | function 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 | |||
| 37 | function 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 | |||
| 63 | document.addEventListener("DOMContentLoaded", function() { | ||
| 64 | /* we can do the aging on DOM content load since no layout dependency */ | ||
| 65 | aging(); | ||
| 66 | }, false); | ||
| 67 | |||
| 68 | })(); | ||
