diff options
Diffstat (limited to 'www/cgit.js')
| -rw-r--r-- | www/cgit.js | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/www/cgit.js b/www/cgit.js new file mode 100644 index 0000000..52a8c3c --- /dev/null +++ b/www/cgit.js | |||
| @@ -0,0 +1,108 @@ | |||
| 1 | /* cgit.js: javacript functions for cgit | ||
| 2 | * | ||
| 3 | * Copyright (C) 2006-2018 cgit Development Team <cgit@lists.zx2c4.com> | ||
| 4 | * | ||
| 5 | * Licensed under GNU General Public License v2 | ||
| 6 | * (see COPYING for full license text) | ||
| 7 | */ | ||
| 8 | |||
| 9 | (function () { | ||
| 10 | /* This follows the logic and suffixes used in ui-shared.c */ | ||
| 11 | |||
| 12 | var age_classes = ["age-mins", "age-hours", "age-days", "age-weeks", "age-months", "age-years"]; | ||
| 13 | var age_suffix = ["min.", "hours", "days", "weeks", "months", "years", "years"]; | ||
| 14 | var age_next = [ | ||
| 15 | 60, | ||
| 16 | 3600, | ||
| 17 | 24 * 3600, | ||
| 18 | 7 * 24 * 3600, | ||
| 19 | 30 * 24 * 3600, | ||
| 20 | 365 * 24 * 3600, | ||
| 21 | 365 * 24 * 3600, | ||
| 22 | ]; | ||
| 23 | var age_limit = [ | ||
| 24 | 7200, | ||
| 25 | 24 * 7200, | ||
| 26 | 7 * 24 * 7200, | ||
| 27 | 30 * 24 * 7200, | ||
| 28 | 365 * 25 * 7200, | ||
| 29 | 365 * 25 * 7200, | ||
| 30 | ]; | ||
| 31 | var update_next = [10, 5 * 60, 1800, 24 * 3600, 24 * 3600, 24 * 3600, 24 * 3600]; | ||
| 32 | |||
| 33 | function render_age(e, age) { | ||
| 34 | var t, n; | ||
| 35 | |||
| 36 | for (n = 0; n < age_classes.length; n++) if (age < age_limit[n]) break; | ||
| 37 | |||
| 38 | t = Math.round(age / age_next[n]) + " " + age_suffix[n]; | ||
| 39 | |||
| 40 | if (e.textContent != t) { | ||
| 41 | e.textContent = t; | ||
| 42 | if (n == age_classes.length) n--; | ||
| 43 | if (e.className != age_classes[n]) e.className = age_classes[n]; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | function aging() { | ||
| 48 | var n, | ||
| 49 | next = 24 * 3600, | ||
| 50 | now_ut = Math.round(new Date().getTime() / 1000); | ||
| 51 | |||
| 52 | for (n = 0; n < age_classes.length; n++) { | ||
| 53 | var m, | ||
| 54 | elems = document.getElementsByClassName(age_classes[n]); | ||
| 55 | |||
| 56 | if (elems.length && update_next[n] < next) next = update_next[n]; | ||
| 57 | |||
| 58 | for (m = 0; m < elems.length; m++) { | ||
| 59 | var age = now_ut - elems[m].getAttribute("data-ut"); | ||
| 60 | |||
| 61 | render_age(elems[m], age); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | /* | ||
| 66 | * We only need to come back when the age might have changed. | ||
| 67 | * Eg, if everything is counted in hours already, once per | ||
| 68 | * 5 minutes is accurate enough. | ||
| 69 | */ | ||
| 70 | |||
| 71 | window.setTimeout(aging, next * 1000); | ||
| 72 | } | ||
| 73 | |||
| 74 | function wrap_tables() { | ||
| 75 | const selectors = [ | ||
| 76 | "div#cgit table.list", | ||
| 77 | "div#cgit table.blob", | ||
| 78 | "div#cgit table.bin-blob", | ||
| 79 | "div#cgit table.diff", | ||
| 80 | "div#cgit table.ssdiff", | ||
| 81 | "div#cgit table.commit-info", | ||
| 82 | ]; | ||
| 83 | |||
| 84 | const tables = document.querySelectorAll(selectors.join(", ")); | ||
| 85 | |||
| 86 | tables.forEach((table) => { | ||
| 87 | if (table.parentNode.classList.contains("scrollable")) { | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | const wrapper = document.createElement("div"); | ||
| 92 | wrapper.className = "scrollable"; | ||
| 93 | |||
| 94 | table.parentNode.insertBefore(wrapper, table); | ||
| 95 | wrapper.appendChild(table); | ||
| 96 | }); | ||
| 97 | } | ||
| 98 | |||
| 99 | document.addEventListener( | ||
| 100 | "DOMContentLoaded", | ||
| 101 | function () { | ||
| 102 | /* we can do the aging on DOM content load since no layout dependency */ | ||
| 103 | aging(); | ||
| 104 | wrap_tables(); | ||
| 105 | }, | ||
| 106 | false, | ||
| 107 | ); | ||
| 108 | })(); | ||
