-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
437 lines (401 loc) · 15.1 KB
/
Copy pathindex.html
File metadata and controls
437 lines (401 loc) · 15.1 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<!DOCTYPE HTML>
<title>live timezone map</title>
<meta charset="UTF-8">
<!-- FIXME: requires builds from separate repositories -->
<script src="tz.js/tz.js.gz"></script>
<script src="tzmap.js/tzmap.js"></script>
<script>
var gMapLoaded = false;
function tzmap_loaded() {
document.getElementById("loadingtext").firstChild.data = "";
gMapLoaded = true;
hashchange_listener();
}
function tzmap_failed() {
document.getElementById("loadingtext").firstChild.data = "Loading timezone data failed.";
}
document.addEventListener("DOMContentLoaded", function(event) {
tzmap.loadData("tzmap.js/", tzmap_loaded, tzmap_failed);
}, false);
</script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.6.0/dist/leaflet.css" integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ==" crossorigin=" anonymous">
<script src="https://unpkg.com/leaflet@1.6.0/dist/leaflet.js" integrity="sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==" crossorigin="anonymous"></script>
<script>
// Use Stamen toner-lite tiles over HTTPS. Stamen doesn't provide a
// script for this, since
// https://stamen-maps.a.ssl.fastly.net/js/tile.stamen.js?v1.3.0
// loads the tiles over http. But it's pretty simple, based on that
// and http://maps.stamen.com/#usage-ssl
L.StamenSSLTileLayer = L.TileLayer.extend({
initialize: function(name, options) {
L.TileLayer.prototype.initialize.call(this,
[ "https://stamen-tiles-{s}.a.ssl.fastly.net/", name, "/{z}/{x}/{y}.png" ].join(""),
L.Util.extend({}, options, {
// NOTE: These options are hard-coded to be the toner values!
minZoom: 0,
maxZoom: 20,
subdomains: ["a", "b", "c", "d" ],
scheme: "xyz",
attribution: [
'Map tiles by <a href="http://stamen.com/">Stamen Design</a>, ',
'under <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a>. ',
'Data by <a href="http://openstreetmap.org/">OpenStreetMap</a>, ',
'under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.'
].join(""),
sa_id: name
}));
}
});
L.stamenSSLTileLayer = function(name, options) {
return new L.StamenSSLTileLayer(name, options);
}
</script>
<script>
var gDefaults = {
zoom: 2,
lat: 15,
lng: 0,
time: "now" // "now" or a number in seconds since the epoch
};
function gethash() {
var hash = {};
if (window.location.hash != "") {
try {
hash = JSON.parse(unescape(window.location.hash.substring(1)));
} catch(ex) {}
}
for (var prop in gDefaults) {
if (!(prop in hash)) {
hash[prop] = gDefaults[prop];
}
}
return hash;
}
function sethash(json) {
window.location.hash = escape(JSON.stringify(json));
}
var gMap, gZonesLayer, gZoneTime, gTimeDesc, gNowTimer, gState;
function dom_load_handler() {
gZoneTime = document.getElementById("zonetime");
gTimeDesc = document.getElementById("timedesc").firstChild;
gTimeChooser = document.getElementById("timechooser");
gState = gethash();
sethash(gState);
// FIXME: Use fitWorld by default.
gMap = L.map("map").setView([gState.lat, gState.lng], gState.zoom);
gZonesLayer = L.layerGroup();
gZonesLayer.addTo(gMap);
// See http://maps.stamen.com/
gMap.addLayer(L.stamenSSLTileLayer("toner-lite"));
gMap.addEventListener("moveend", center_changed_listener);
gMap.addEventListener("zoomend", zoom_changed_listener);
hashchange_listener();
window.addEventListener("hashchange", hashchange_listener, false);
document.getElementById("dropdown").addEventListener("click",
function(event) {
if (event.button != 0 || event.detail != 1)
return;
event.stopPropagation();
toggle_chooser_dropdown();
}, false);
gTimeChooser.addEventListener("click",
function(event) {
if (event.detail != 1)
return;
event.stopPropagation();
}, false);
document.body.addEventListener("click",
function(event) {
if (event.detail != 1)
return;
if (gDropdownOpen) {
toggle_chooser_dropdown();
}
}, false);
}
function center_changed_listener() {
var ctr = gMap.getCenter();
gState.lat = ctr.lat;
gState.lng = ctr.lng;
sethash(gState);
}
function zoom_changed_listener() {
gState.zoom = gMap.getZoom();
sethash(gState);
}
function hashchange_listener() {
gState = gethash();
gMap.setZoom(gState.zoom);
gMap.panTo(L.latLng(gState.lat, gState.lng));
if (gState.time == "now") {
if (gNowTimer === undefined && gMapLoaded) {
now_timer();
}
gTimeDesc.data = "Present time.";
} else {
if (gNowTimer !== undefined) {
clearTimeout(gNowTimer);
gNowTimer = undefined;
}
if (typeof(gState.time) == "number") {
if (gMapLoaded) {
show_polygons_for(new Date(gState.time * 1000));
}
// FIXME: better description
// FIXME: add support for a time zone this was in reference to
gTimeDesc.data = gState.time + " seconds since the epoch.";
}
}
}
function now_timer() {
var d = new Date();
gNowTimer = setTimeout(now_timer, (60 - d.getUTCSeconds()) * 1000);
show_polygons_for(d);
}
/**
* For a given Date object |d|, return an object mapping UTC offsets to
* an object with a zones property listing all the time zones with that
* UTC offset at the given date.
*/
function zone_groups_for(d) {
var groups = {};
var zones = tzmap.allZones();
for (var zoneIdx in zones) {
var tzmapZone = zones[zoneIdx];
var tzZone = tz.canonicalName(tzmapZone);
if (!tzZone) {
if (tzmapZone != "uninhabited") {
console.log("Bad zone name " + tzmapZone + " in tzmap database.");
}
continue;
}
var info = tz.zoneAt(tzZone, d);
if (info.offset in groups) {
groups[info.offset].zones.push(tzmapZone);
} else {
groups[info.offset] = { zones: [ tzmapZone ] };
}
}
for (var offset in groups) {
// Keep the lists sorted so we can compare them stably.
groups[offset].zones.sort();
}
return groups;
}
function tzmap_polygons_to_leaflet_polygon(polygons, options) {
var topArray = [];
for (var polygonIdx in polygons) {
var innerArray = [];
var polygon = polygons[polygonIdx];
for (var pointIdx in polygon) {
var pt = polygon[pointIdx];
innerArray.push(L.latLng(pt[1], pt[0]));
}
topArray.push(innerArray);
}
return L.polygon(topArray, options);
}
var gHourColors = [
"hsl(0, 100%, 25%)",
"hsl(30, 100%, 25%)",
"hsl(60, 100%, 25%)",
"hsl(120, 100%, 25%)",
"hsl(240, 100%, 30%)",
"hsl(300, 100%, 40%)",
"hsl(0, 100%, 50%)",
"hsl(30, 100%, 60%)",
"hsl(60, 100%, 65%)",
"hsl(120, 100%, 65%)",
"hsl(240, 100%, 65%)",
"hsl(300, 100%, 65%)",
"hsl(0, 100%, 65%)",
"hsl(30, 100%, 65%)",
"hsl(60, 100%, 65%)",
"hsl(120, 100%, 65%)",
"hsl(240, 100%, 60%)",
"hsl(300, 100%, 50%)",
"hsl(0, 100%, 40%)",
"hsl(30, 100%, 30%)",
"hsl(60, 100%, 25%)",
"hsl(120, 100%, 25%)",
"hsl(240, 100%, 25%)",
"hsl(300, 100%, 25%)"
];
var gMonthNames = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var gDayNames = [
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
];
function padmins(min) {
return ((min < 10) ? "0" : "") + min;
}
function display_text_for(offset, d) {
var du = new Date(d.valueOf() + offset * 1000);
// The UTC offset
var minutePart = Math.abs(offset) / 60;
var hourPart = Math.floor(minutePart / 60);
minutePart -= hourPart * 60;
return gDayNames[du.getUTCDay()] + " (" +
gMonthNames[du.getUTCMonth()] + " " + du.getUTCDate() +
") at " + du.getUTCHours() + ":" +
padmins(du.getUTCMinutes()) + " (UTC" +
((offset < 0) ? "-" : "+") + hourPart + ":" +
padmins(minutePart) + ")";
}
function color_for_offset(d, offset) {
var color;
var hour = (d.getUTCHours() + 24 + Math.floor(offset / 3600)) % 24;
if (offset % 3600 == 0) {
color = gHourColors[hour];
} else {
var c1 = gHourColors[hour];
var c2 = gHourColors[(hour + 1) % 24];
var portion = ((offset / 3600) + 24) % 1;
// SVG doesn't support CSS gradients in Gecko or Chromium yet. See
// https://bugzilla.mozilla.org/show_bug.cgi?id=1013889 .
// color = "repeating-linear-gradient(135deg, " + c2 + " 0px, " + c2 + " " + (portion * 16) + "px, " + c1 + " " + (portion * 16) + "px, " + c1 + " 16px)";
var id="stripe-" + offset;
if (!document.getElementById(id)) {
var elem = document.getElementById("stripe-template").cloneNode(true);
elem.setAttribute("id", id);
elem.childNodes[0].setAttribute("stop-color", c2);
elem.childNodes[1].setAttribute("stop-color", c2);
elem.childNodes[2].setAttribute("stop-color", c1);
elem.childNodes[3].setAttribute("stop-color", c1);
elem.childNodes[1].setAttribute("offset", portion);
elem.childNodes[2].setAttribute("offset", portion);
document.getElementById("stripes").appendChild(elem);
}
color = "url(#" + id + ")";
}
return color;
}
function show_polygon(d, offset, groupobj) {
groupobj.color = color_for_offset(d, offset);
groupobj.polygon = tzmap_polygons_to_leaflet_polygon(
tzmap.polygonsFor(groupobj.zones), {
fillOpacity: 0.3,
fillColor: groupobj.color,
opacity: 0.5,
color: "black",
weight: 1,
clickable: true, // needed for mouseout/over
});
groupobj.polygon.addTo(gZonesLayer);
groupobj.textNode = document.createTextNode(display_text_for(offset, d));
groupobj.hovered = false;
function over() {
groupobj.polygon.setStyle({ color: "#ff6600",
opacity: 1.0,
fillColor: "yellow"});
gZoneTime.appendChild(groupobj.textNode);
groupobj.hovered = true;
}
function out() {
groupobj.polygon.setStyle({ color: "black",
opacity: 0.5,
fillColor: groupobj.color});
gZoneTime.removeChild(groupobj.textNode);
groupobj.hovered = false;
}
groupobj.polygon.addEventListener('mouseover', over);
groupobj.polygon.addEventListener('mouseout', out);
}
var gCurrentGroups;
function groups_match(a, b) {
// Since both sets of groups have the same zones put into them, we
// only need to enumerate the offsets of one. If the sets of
// offsets are different, we will catch a zone missing.
for (var offset in a) {
if (!(offset in b)) {
return false;
}
if (JSON.stringify(a[offset].zones) !=
JSON.stringify(b[offset].zones)) {
return false;
}
}
return true;
}
function show_polygons_for(d) {
var stripes_holder = document.getElementById("stripes");
while (stripes_holder.lastChild) {
stripes_holder.removeChild(stripes_holder.lastChild);
}
var groups = zone_groups_for(d);
if (gCurrentGroups && groups_match(groups, gCurrentGroups)) {
for (var offset in gCurrentGroups) {
var groupobj = gCurrentGroups[offset];
groupobj.textNode.data = display_text_for(offset, d);
groupobj.color = color_for_offset(d, offset);
if (!groupobj.hovered) {
groupobj.polygon.setStyle({ fillColor: groupobj.color });
}
}
return;
}
gZonesLayer.clearLayers();
gCurrentGroups = groups;
for (var offset in groups) {
show_polygon(d, offset, groups[offset]);
}
}
window.addEventListener("DOMContentLoaded", dom_load_handler);
var gTimeChooser;
var gDropdownOpen = false;
function toggle_chooser_dropdown() {
gTimeChooser.setAttribute("class", gDropdownOpen ? "closed" : "open");
gDropdownOpen = !gDropdownOpen;
}
</script>
<style>
html, body { width: 100%; height: 100%; margin: 0; padding: 0; }
body { display: table; border-spacing: 0 }
#ui, #mapcontainer { display: table-row; }
#mapcontainer, #map { height: 100% }
#ui { position: relative }
#map { position: relative; z-index: 1 }
#chooser-container { position: relative; z-index: 2 }
#timechooser { position: relative; display: inline-block; padding: 2px; background: blue; color: white }
#chooser-container-outer { position: absolute; height: 0; bottom: 0; left: 0; }
#timechooser.closed > #chooser-container-outer { display: none }
#chooser-container { width: 25em; background: white; color: blue }
#dropdown { border: none; padding: 0; margin: 0; background: transparent; color: inherit; }
#chooser-container ul, #chooser-container li { list-style-type: none; margin: 0; padding: 0 }
#chooser-container li > ul { padding-left: 2em }
#github { position: absolute; top: 0; right: 0 }
</style>
<svg style="display:block; height: 0">
<defs>
<!-- lack of whitespace here is important, since we index with element.childNodes -->
<linearGradient id="stripe-template" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="10" y2="10" spreadMethod="repeat"><stop offset="0"></stop><stop offset="0.5"></stop><stop offset="0.5"></stop><stop offset="1"></stop></linearGradient>
</defs>
<defs id="stripes">
</defs>
</svg>
<div id="ui">
<div id="timechooser" class="closed">
<span id="timedesc">Present time.</span>
<button id="dropdown">[v]</button>
<div id="chooser-container-outer">
<div id="chooser-container">
<ul>
<li><label for="present"><input type="radio" name="time" value="present" id="present">Present time</label></li>
<li><label for="specific"><input type="radio" name="time" value="specific" id="specific">Specific time</label>:
<input type="text" size="4" name="year" placeholder="YYYY">-<input type="text" size="2" name="month" placeholder="MM">-<input type="text" size="2" name="date" placeholder="DD">
<ul>
<li><label for="utc"><input type="radio" name="zone" value="utc" id="utc">UTC</label></li>
<li><label for="byplace"><input type="radio" name="zone" value="byplace" id="byplace">At Location (FIXME: add location selector)</label></li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<span id="loadingtext">Loading timezone data...</span><span id="zonetime"></span>
<div id="github"><a href="https://github.com/dbaron/timezone-map">source on GitHub</a></div>
</div>
<div id="mapcontainer"><div id="map"></div></div>