-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
207 lines (192 loc) · 10.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!-- MIT license. https://github.com/cdanis/gnomon -->
<html height="100%">
<head>
<title>SRE team</title>
<meta name="viewport" content="width=device-width, initial-scale=1, height=device-height">
<meta charset="utf-8">
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew=="
crossorigin=""></script>
<style>
/* Hack: disable fadeout animation, so time updates look seamless. */
.leaflet-fade-anim .leaflet-popup {
transition: none;
}
.leaflet-popup-tip {
visibility: hidden;
}
tr:nth-child(even) {background: #CCC}
th[scope="row"] {padding: 0.25em 0.5em}
table { border-spacing: 0; }
</style>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://underscorejs.org/underscore-min.js"></script>
</head>
<body style="display:flex; justify-content: center; flex-direction: column; flex-grow: 1; min-height: 97vh;">
<div id="tabs" style="display:flex; flex-grow: 1; flex-direction: column;">
<ul>
<li><a href="#map">Map</a></li>
<li><a href="#timezone">Timezones</a></li>
<li><a href="#people">People</a></li>
</ul>
<div id="map" style="flex-grow: 1; display: flex;"></div>
<div id="timezone" style="flex-grow: 1;"></div>
<div id="people" style="flex-grow: 1;"></div>
</div>
<!-- See http://joergdietrich.github.io/Leaflet.Terminator/ -->
<script src="https://unpkg.com/@joergdietrich/[email protected]/L.Terminator.js"></script>
<!-- https://github.com/dj0001/Leaflet.timezones -->
<!-- <script src="https://rawcdn.githack.com/dj0001/Leaflet.timezones/c4f81012e5025b0133b3f06110b92427eac0118e/L.timezones.js"></script> -->
<script src="https://momentjs.com/downloads/moment-with-locales.min.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.min.js"></script>
<script>
var map = L.map('map').setView([30,-10], 3);
//L.tileLayer('https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png');
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
//L.timezones.addTo(map);
var t = L.terminator();
t.addTo(map);
setInterval(function(){t.setTime(window.pseudoMoment)}, 10000);
</script>
<script>
var ourPopups = [];
var lastDrawnMinute = null; // a Moment with minute granularity
function hexToBuffer(hex) {
var rv = new Uint8Array(hex.length/2);
for (var i = 0; i < rv.length; i++) {
rv[i] = parseInt(hex.substr(i*2, 2), 16);
}
return rv;
}
async function tzDisplayName(tz, user) {
if (!('crypto' in window && 'subtle' in window.crypto)) {
return tz;
}
const enc = new TextEncoder();
const ccc = window.crypto.subtle;
const magic = hexToBuffer('51a5d2baf72a65f7edc020526e16e501a08b92320338d7bc036e23334560afd6');
var ud = new Uint8Array(await(ccc.digest('SHA-256', enc.encode(user))));
if (! _.isEqual(ud, magic))
return tz;
var td = new Uint8Array(await(ccc.digest('SHA-256', enc.encode(tz))));
const more_magic = hexToBuffer('95cf9c255a8e74002085398794fad7fb292ceaeaf60a74aba7fc48d76d0dce69');
if (! _.isEqual(more_magic, td))
return tz;
const dec = new TextDecoder();
const magical = hexToBuffer('1c43012584a11b08239f0a10a17d0c7c5af33cb5993a43976350068cb0b2');
return dec.decode(await(ccc.decrypt({name: 'AES-GCM', iv: magic}, await(ccc.importKey('raw', magic, 'AES-GCM', true, ['decrypt'])), magical)));
}
async function maybeRedrawState() {
var now = moment();
if ('pseudoMoment' in window) { // hack for debugging
now = window.pseudoMoment;
}
if (now.tz('UTC').clone().startOf('minute').isSame(lastDrawnMinute))
return;
var offsetPeople = {}; // UTC offset hours as key. Must calculate here, as UTC offset depends on current time!
var offsetExampleTz = {};
for (var tz in tzPeople) {
const offsetHrs = now.tz(tz).utcOffset()/60;
if (offsetHrs in offsetPeople) {
offsetPeople[offsetHrs] = Array.prototype.concat(offsetPeople[offsetHrs], tzPeople[tz]);
} else {
offsetPeople[offsetHrs] = tzPeople[tz];
}
if (!(offsetHrs in offsetExampleTz)) {
offsetExampleTz[offsetHrs] = tz;
}
}
for (var off in offsetPeople) {
offsetPeople[off] = offsetPeople[off].sort();
}
// Delete and re-draw map popups. One popup per UTC offset.
ourPopups.forEach(e => e.remove());
ourPopups = [];
for (var off in offsetPeople) {
const p = offsetPeople[off];
// Get arbitrary timezone, shouldn't matter which.
const tz = peopleTz[p[0]];
const local = now.tz(tz).format('HH:mm');
// cycle over all TZs in this offset, grab all abbrs, and union-ize them.
const abbrs = _.union(p.map(e => moment.tz.zone(peopleTz[e]).abbr(now)));
// Correct our pseudo-longitude scheme for summer time differences: always use the western-most offset.
const nominalOff = _.max(_.flatten(p.map(e => moment.tz.zone(peopleTz[e]).offsets)));
const longitude = 180*nominalOff/12/-60;
const latitude = (Math.abs(longitude)%2)*-20+30;
console.log(`${off}: ${latitude},${longitude}: ${abbrs.join(',')}: ${p.join(',')}`)
ourPopups.push(L.popup({autoClose: false, closeOnClick: false, autoPan: false})
.setLatLng([latitude,longitude])
.setContent(`<div style="text-align:center"><b><div style="font-size: 200%">${local}</div><div style="margin-bottom:0.75em;">${abbrs.join(', ')}</div></b>${p.join("<br>")}</div>`));
}
ourPopups.forEach(e => e.addTo(map));
ourPopups.forEach(p => $(p._container).click(p, e=>e.data.bringToFront()));
// Render the per-person view
var pHtml = `<table>`;
for (const p of Object.keys(peopleTz).sort()) {
var tz = peopleTz[p];
const local = now.tz(tz).format('HH:mm');
const abbr = moment.tz.zone(tz).abbr(now);
pHtml += `<tr><th scope="row" align="left">${p}</th><td>${local} <small>(${abbr}: ${await(tzDisplayName(tz, p))})</small></td></tr>`;
}
pHtml += `</table>`;
$("#people").html(pHtml);
// Calculate the per-UTC-offset timezone view. In this view, always show a placeholder for UTC time.
const userTz = moment.tz.guess();
const userOffset = (moment.tz.zone(userTz).utcOffset(now)/-60).toString();
offsetsPlusUtc = _.union(Object.keys(offsetPeople), ['0', userOffset.toString()]);
if (!(0 in offsetExampleTz)) {
offsetExampleTz[0] = 'UTC';
}
if (!(userOffset in offsetExampleTz)) {
offsetExampleTz[userOffset] = userTz;
}
tzPlusUtc = _.union(Object.keys(tzPeople),['UTC', userTz]);
var tzHtml = `<table>`;
// Present timezones reverse-sorted by offset from UTC (matches map, makes sense)
offsetsPlusUtc.sort((a,b)=>a-b).forEach(off=>{
const p = offsetPeople[off] || [];
const tz = offsetExampleTz[off];
const local = now.tz(tz).format('HH:mm');
const abbrs = p==[]? [tz.abbr(now)] : _.union(p.map(e => moment.tz.zone(peopleTz[e]).abbr(now)));
tzHtml += `<tr><td>${off == userOffset ? `<span title="You are here!" style="font-size: 150%;">📍</span>` : ''}</td><th scope="row"><div style="font-size:200%">${local}</div>${abbrs.join(',')}</th><td>${p.join(", ")}</td></tr>`;
});
tzHtml += `</table>`;
$("#timezone").html(tzHtml);
lastDrawnMinute = now.tz('UTC').startOf('minute');
}
var tzPeople = {};
var peopleTz = {};
// TODO: probably make each person into an object, with 'tz' and maybe also 'subteam' attributes (for filtering by subteam w/ checkboxes)
fetch('tzpeople.json')
.then(res => res.json())
.then(res => {
tzPeople = res;
for (var tz in tzPeople) {
tzPeople[tz] = tzPeople[tz].sort();
tzPeople[tz].forEach(p=>peopleTz[p]=tz);
}
maybeRedrawState();
setInterval(maybeRedrawState, 500);
});
</script>
<script>
// Need to invalidate Leaflet's state after tabs have messed with the DOM
$("#tabs").tabs({
activate: function(event,ui) {
map.invalidateSize();
if (event.toElement.hash == '#map') {
lastDrawnMinute = -1; // kludge https://github.com/cdanis/gnomon/issues/6
}
},
});
map.invalidateSize();
</script>
</body>
</html>