-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.html
709 lines (668 loc) · 20.3 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Well-Tempered Traveler</title>
<link rel="icon" href="img/favicon.png" />
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<style>
body {
font-family: Roboto;
margin: 0;
}
* {
box-sizing: border-box;
}
#header {
background: #f1f1f1;
margin-bottom: 10px;
min-width: 800px;
padding: 5px 20px;
white-space: nowrap;
}
h1 {
cursor: pointer;
font-size: 27px;
font-weight: 100;
line-height: 32px;
}
#search {
margin: 0 20px;
}
#search input {
font-size: 19px;
padding: 5px;
}
#feedback {
text-align: center;
}
h1, #search {
vertical-align: middle;
}
h1, #search, #unit_switch {
display: inline-block;
}
#unit_switch {
margin-right: 20px;
}
/* Pop-up */
#popup {
background-color: #fff;
border: 1px solid #222;
box-shadow: 15px 15px 23px rgba(0, 0, 0, 0.4);
display: none;
padding: 10px;
position: fixed;
width: 100px;
}
#popup img {
vertical-align: middle;
width: 13px;
}
/* Table */
#t {
margin: 20px auto;
text-align: center;
width: 1080px; /* 12 * 80 + 120 */
}
.reset {
cursor: pointer;
}
/* Header */
.lh {
overflow: hidden;
padding-right: 5px;
text-overflow: ellipsis;
width: 120px;
}
.lh span {
white-space: nowrap;
}
/* Line */
.l {
border-bottom: 1px solid #555;
display: inline-block;
white-space: nowrap;
}
/* Cell */
.c {
height: 40px;
padding: 7px;
text-align: center;
width: 40px;
}
/* Double cell */
.c.dbl {
width: 80px;
}
.c:hover {
background-image: url('img/dot.png');
}
/* Rainfall icon */
.rf {
pointer-events: none;
}
.p.lh {
text-align: center;
}
.c.e {
border-right: 1px solid #555;
}
.c.f {
border-left: 1px solid #555;
}
.ch, .lh, .c {
display: inline-block;
}
.lh, .c {
vertical-align: middle;
}
.ch {
cursor: pointer;
font-weight: bold;
padding: 7px 0;
text-align: center;
width: 80px;
}
.ch:hover {
background-color: rgba(35, 133, 255, 0.2);
}
.ch.sel {
background-color: rgb(35, 133, 255);
color: #fff;
}
#legend {
display: inline-block;
}
#legend img {
height: 50px;
vertical-align: middle;
}
#scale {
display: inline-block;
}
#scale .c {
height: 25px;
width: 25px;
}
#footer {
font-size: 12px;
margin: 25px 0;
text-align: center;
}
/* Slider styles */
.switch {
position: relative;
display: inline-block;
height: 34px;
vertical-align: middle;
width: 60px;
}
.switch input {display:none;}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #f00;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
/* Spinner styles */
.spinner {
animation: spin 2s linear infinite;
border: 10px solid #f3f3f3;
border-top: 10px solid #3498db;
border-radius: 50%;
display: inline-block;
height: 120px;
width: 120px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<script>
COLORS = [
[-15, [0, 57, 191]], // Inhumanely cold, very dark blue
[-8, [0, 78, 234]], // Freezing, dark blue
[3, [0, 167, 239]], // Cold, blue
[12, [5, 255, 217]], // Chilly, turquoise
[23, [3, 186, 0]], // Comfy, green
[27, [255, 255, 0]], // Warm, yellow
[29, [255, 140, 0]], // A bit too warm, orange
[33, [255, 0, 34]], // Very hot, red
[40, [148, 0, 0]], // Hellish, dark red
]
var cities = [];
var filteredCities = [];
var data;
var ctryCodes = {};
var continents = {};
var localMonthNames = [];
var resetButtonAdded = false;
var selectedMonth = 0;
var date = new Date();
for (var i = 0; i < 12; i++) {
date.setMonth(i);
date.setDate(1);
localMonthNames.push(
date.toLocaleString(
window.navigator.language, { month: "short" }
));
}
function loadData() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == XMLHttpRequest.DONE) {
var lines = xhr.responseText.split("\n")
data = {};
for (var i = 0, l; l = lines[i]; i++) {
var parts = l.split(":");
data[parts[0]] = JSON.parse(parts.slice(1).join(':'));
}
processData(data);
}
}
xhr.open('GET', 'data.txt', true);
xhr.send(null);
var ctryXhr = new XMLHttpRequest();
ctryXhr.onreadystatechange = function() {
if (ctryXhr.readyState == XMLHttpRequest.DONE) {
ctryData = ctryXhr.responseText;
processCountryData(ctryData);
}
}
ctryXhr.open('GET', 'ctry_codes.txt', true);
ctryXhr.send(null);
// Also set the default value of the temp unit depending on locale.
if (!!navigator && !!navigator.language && navigator.language == 'en-US') {
document.getElementById('wacky').checked = false;
}
}
function assignTooltips() {
var cityNameSpans = document.getElementsByClassName('lhs');
for (var i = 0, span; span = cityNameSpans[i]; i++) {
var ctry = span.getAttribute('ctry');
span.setAttribute('title', span.textContent + ', ' + ctryCodes[ctry]);
}
}
function processCountryData(data) {
var lines = data.split('\n');
for (var i = 0, line; line = lines[i]; i++) {
var parts = line.split(':');
ctryCodes[parts[0]] = parts[1];
}
var continentXhr = new XMLHttpRequest();
continentXhr.onreadystatechange = function() {
if (continentXhr.readyState == XMLHttpRequest.DONE) {
continentData = continentXhr.responseText;
processContinentData(continentData);
}
}
continentXhr.open('GET', 'continents.txt', true);
continentXhr.send(null);
window.setTimeout(assignTooltips, 5000);
}
function processContinentData(data) {
var lines = data.split('\n');
for (var i = 0, line; line = lines[i]; i++) {
var parts = line.split(':');
var theContinent = parts[0];
var currentCountryCodes = parts[1].split(',');
for (var j = 0, c; c = currentCountryCodes[j]; j++) {
continents[c] = theContinent;
}
}
}
const mergedValue = (data, city, month, index) => {
const one = data[city][month + '-1'][index];
const two = data[city][month + '-2'][index];
if (one == -9999 || two == -9999) {
return -9999;
}
return Math.round((one + two) / 2);
};
const postProcessDataForWholeMonths = (data) => {
for (const city of cities) {
if (data[city].length == 12) {
// Data is formatted the way we want, nothing to do.
return;
}
for (let m = 1; m <= 12; m++) {
data[city][m] = [
mergedValue(data, city, m, 0),
mergedValue(data, city, m, 1),
mergedValue(data, city, m, 2),
mergedValue(data, city, m, 3),
];
delete data[city][m + '-1'];
delete data[city][m + '-2'];
}
}
};
function processData(data) {
// For each city, we either get 24 data points (half months), or 12 (whole
// months).
for (city in data) {
var cellsWithData = 0;
// More than 6 half-months (or 3 months) with a temp and we assume we
// have data.
const threshold = Object.keys(data[city]).length === 24 ? 6 : 3;
for (let cell in data[city]) {
if (data[city][cell][1] != -9999) {
cellsWithData++;
if (cellsWithData >= threshold) {
cities.push(city);
break;
}
}
}
}
cities.sort();
filteredCities = cities;
showLegend();
drawData();
setupListeners();
}
function drawData() {
document.getElementById('t').innerHTML = '';
resetButtonAdded = false;
for (var i = 0, city; city = filteredCities[i]; i++) {
if (i % 10 == 0) {
showColumnHeaders();
}
showSingleCity(city);
}
if (!!ctryCodes['FR']) {
assignTooltips();
} else {
// This will happen later.
}
}
function f(tmp_c) {
if (tmp_c === -9999) {
return -9999;
}
return Math.round(1.8 * tmp_c + 32);
}
function format(value, unit) {
if (value == -9999) {
return '-';
}
if (unit == 'mm') {
value *= 10;
}
return '<b>' + value + '</b>' + unit;
}
function setupListeners() {
document.getElementById('t').addEventListener('mouseover', function(e) {
var cell = document.elementFromPoint(e.clientX, e.clientY);
var popup = document.getElementById('popup');
if (!cell.hasAttribute('tmp-avg')) {
popup.style.display = 'none';
return;
}
var tmpMin = parseInt(cell.getAttribute('tmp-min'));
var tmpAvg = parseInt(cell.getAttribute('tmp-avg'));
var tmpMax = parseInt(cell.getAttribute('tmp-max'));
var rainfall = parseInt(cell.getAttribute('rain'));
var useWackyUnit = !document.getElementById('wacky').checked;
var unit = '°' + (useWackyUnit ? 'F' : 'C');
if (useWackyUnit) {
tmpMin = f(tmpMin);
tmpMax = f(tmpMax);
tmpAvg = f(tmpAvg);
}
popup.innerHTML =
'<table style="text-align: center;">' +
'<tr><td><img src="img/hot.png"></td><td>' + format(tmpMax, unit) + '</td></tr>' +
'<tr><td><span style="color: #ddd;">|</span></td><td>' +
format(tmpAvg, unit) + '</td></tr>' +
'<tr><td><img src="img/cold.png"></td><td>' + format(tmpMin, unit) + '</td></tr>' +
'<tr><td><img src="img/droplet.png"></td><td>' +
format(rainfall, 'mm') + '</td></tr>' +
'</table>';
popup.style.display = 'inline-block';
popup.style.left = (e.clientX + 20) + 'px';
popup.style.top = (e.clientY + 20) + 'px';
return true;
});
document.getElementById('t').addEventListener('mouseout', function() {
document.getElementById('popup').style.display = 'none';
});
document.getElementById('t').addEventListener('click', function(e) {
if (e.target.className == 'ch') {
var comfyFactors = {};
var monthNumber = parseInt(e.target.getAttribute('n'));
selectedMonth = monthNumber;
for (var i = 0, city; city = filteredCities[i]; i++) {
comfyFactors[city] = getComfyFactor(city, monthNumber);
}
filteredCities.sort(function(a, b) {
var diff = comfyFactors[b] - comfyFactors[a];
return (diff == 0) ? (a > b ? 1 : -1) : diff;
});
drawData();
}
if (e.target.className == 'reset') {
filteredCities.sort(function(a, b) {
return (a > b ? 1 : -1);
});
selectedMonth = 0;
drawData();
}
});
document.getElementById('popup').style.display = 'none';
document.getElementById('sb').addEventListener('input', function(e) {
var input = e.target.value;
if (input.trim() == '') {
filteredCities = cities;
drawData();
return;
}
input = input.replace(/,/g, ' ').trim();
input = input.replace(/\s+/g, ' ');
var inputParts = input.split(' ');
filteredCities = [];
for (var i = 0, city; city = cities[i]; i++) {
var shouldShowCity = false;
var countryCode = city.split('|')[1];
var countryName = ctryCodes[countryCode];
for (var j = 0, inputPart; inputPart = inputParts[j]; j++) {
inputPart = inputPart.trim();
var lcPart = inputPart.toLowerCase();
if (city.toLowerCase().indexOf(lcPart) != -1 ||
countryName.toLowerCase().indexOf(lcPart) != -1 ||
(continents[countryCode] &&
continents[countryCode].toLowerCase().indexOf(lcPart) != -1)) {
shouldShowCity = true;
}
}
if (shouldShowCity) {
filteredCities.push(city);
}
}
drawData();
});
document.getElementById('sb').focus();
}
function showLegend() {
var scale = document.getElementById('scale');
for (var i = 0; i < COLORS.length; i++) {
var cell = document.createElement('div');
cell.classList.add('c');
cell.style.backgroundColor = getCellColor(COLORS[i][0]);
scale.appendChild(cell);
}
}
function showColumnHeaders() {
var table = document.getElementById('t');
var colHeaders = document.createElement('div');
colHeaders.classList.add('l');
var pivot = document.createElement('div');
if (!resetButtonAdded) {
var resetIcon = document.createElement('img');
resetIcon.setAttribute('src', 'img/reset.png');
resetIcon.className = 'reset';
pivot.appendChild(resetIcon);
resetButtonAdded = true;
}
pivot.classList.add('lh');
pivot.classList.add('p');
colHeaders.appendChild(pivot);
for (var i = 0; i < 12; i++) {
var colHeader = document.createElement('div');
colHeader.textContent = localMonthNames[i];
colHeader.classList.add('ch');
if (selectedMonth == i + 1) {
colHeader.classList.add('sel');
}
colHeader.setAttribute('n', i + 1);
colHeaders.appendChild(colHeader);
}
table.appendChild(colHeaders);
}
function showSingleCell(city, cell, oddNumberCell, firstCell, container,
isDoubleCell) {
var tmps = data[city][cell];
var minTmp = tmps[0];
var average_temperature = tmps[1];
var maxTmp = tmps[2];
var rainfall = tmps[3];
var cell = document.createElement('div');
cell.classList.add('c');
if (isDoubleCell) {
cell.classList.add('dbl');
cell.classList.add('s');
cell.classList.add('e');
} else {
cell.classList.add(oddNumberCell ? 's' : 'e');
}
cell.setAttribute('tmp-min', '' + minTmp);
cell.setAttribute('tmp-avg', '' + average_temperature);
cell.setAttribute('tmp-max', '' + maxTmp);
cell.setAttribute('rain', '' + rainfall);
if (firstCell) {
cell.classList.add('f');
}
cell.style.backgroundColor = getCellColor(parseInt(average_temperature));
var rainImage = document.createElement('img');
rainImage.classList.add('rf');
rainImage.src = 'img/droplet.png';
rainImage.style.opacity = Math.min(1.0, parseInt(rainfall) / 14.0);
cell.appendChild(rainImage);
container.appendChild(cell);
}
function showSingleCity(city) {
var line = document.createElement('div');
var cityNameHeader = document.createElement('div');
var cityNameSpan = document.createElement('span');
line.className = 'l';
var parts = city.split('|');
var cityDisplayName = parts[0];
cityNameSpan.textContent = cityDisplayName;
cityNameSpan.setAttribute('ctry', parts[1]);
cityNameSpan.className = 'lhs';
cityNameHeader.className = 'lh';
cityNameHeader.appendChild(cityNameSpan);
line.appendChild(cityNameHeader);
var i = 0;
for (var month = 1; month < 13; month++) {
if (Object.keys(data[city]).length === 24) {
for (var suffix = 1; suffix < 3; suffix++) {
showSingleCell(city, '' + month + '-' + suffix, i % 2 == 0, i == 0,
line, false /* isDoubleCell */);
i++;
}
} else {
showSingleCell(city, '' + month, undefined, i == 0, line,
true /* isDoubleCell */);
i++;
}
}
document.getElementById('t').appendChild(line);
}
function cssColorFromTuple(tuple) {
return 'rgb(' + tuple[0] + ', ' + tuple[1] + ',' + tuple[2] + ')';
}
function getComfyFactor(city, monthNumber) {
var comfyFactor = 100;
var C = COLORS[4][0];
var minTemps = [];
var meanTemps = [];
var maxTemps = [];
var rainfalls = [];
// If we use whole months, we'll just add the same number twice before
// averaging, which is mildly silly.
const useHalfMonths = (Object.keys(data[city]).length === 24);
for (var i = 1; i < 3; i++) {
const cell = '' + monthNumber + (useHalfMonths ? ('-' + i) : '');
var tmps = data[city][cell];
for (var j = 0; j < tmps.length; j++) {
if (tmps[j] == -9999 || tmps[j] == '-9999') {
return -9999;
}
}
minTemps.push(tmps[0]);
meanTemps.push(tmps[1]);
maxTemps.push(tmps[2]);
rainfalls.push(tmps[3]);
}
for (var i = 0; i < 2; i++) {
comfyFactor -= 0.5 * Math.abs(C - minTemps[i]);
comfyFactor -= Math.abs(C - meanTemps[i]);
comfyFactor -= 0.5 * Math.abs(C - maxTemps[i]);
comfyFactor -= 0.5 * Math.abs(rainfalls[i]);
}
return comfyFactor;
}
function getCellColor(tmp_avg) {
if (tmp_avg == -9999) {
return '#eee';
}
if (tmp_avg <= COLORS[0][0]) {
return cssColorFromTuple(COLORS[0][1]);
}
if (tmp_avg >= COLORS[COLORS.length - 1][0]) {
return cssColorFromTuple(COLORS[COLORS.length - 1][1]);
}
var lowIndex = 0;
for (var i = 0, color; color = COLORS[i]; i++) {
if (tmp_avg >= color[0] && tmp_avg < COLORS[i + 1][0]) {
lowIndex = i;
break;
}
}
var fraction = (tmp_avg - COLORS[lowIndex][0]) /
(COLORS[lowIndex + 1][0] - COLORS[lowIndex][0]);
var targetColor = [];
for (var i = 0; i < 3; i++) {
var lowValue = COLORS[lowIndex][1][i];
var highValue = COLORS[lowIndex + 1][1][i];
targetColor.push(Math.floor(lowValue + fraction * (highValue - lowValue)));
}
return cssColorFromTuple(targetColor);
}
</script>
</head>
<body onload="loadData();">
<div id="header">
<h1 onclick="window.location.reload()">The Well-Tempered Traveler</h1>
<div id="search"><input id="sb" placeholder="🔎"></input></div>
<div id="unit_switch">
<span>°F</span>
<label class="switch">
<input id="wacky" type="checkbox" checked>
<span class="slider round"></span>
</label>
<span>°C</span>
</div>
<div id="legend">
<img src="img/cold.png" alt="Cold">
<div id="scale"></div>
<img src="img/hot.png" alt="Hot">
</div>
</div>
<div id="t"><div class="spinner"></div></div>
<div id="footer">
Made from weather data collected over the past 10
years
–
<a href="https://www.google.com/intl/en/policies/">Privacy & Terms</a>
</div>
<div id="popup"></div>
</body>
</html>