-
Notifications
You must be signed in to change notification settings - Fork 40
/
index.html
628 lines (527 loc) · 24.2 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
<!DOCTYPE html>
<html lang="cs">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport"
content="width=320, initial-scale=1, maximum-scale=1, user-scalable=0, target-densitydpi=medium-dpi">
<script src="vendor/moment/moment-with-locales.js" type="text/javascript"></script>
<script src="config.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="vendor/weathericons/css/weather-icons.css"/>
<link rel="stylesheet" type="text/css" href="css/custom-icons.css"/>
<link rel="stylesheet" type="text/css" href="css/kindle.css"/>
<script type="text/javascript">
var apiProtocol = window.location.protocol === 'file:' ? 'https:' : window.location.protocol;
var jsonp = function () {
return {
// JSONP Request
request: function (src, callback, id) {
try {
var self = this,
script = null;
self.done = false;
if (typeof id === "undefined") {
id = "jsonp";
}
if (document.getElementById(id) !== null) {
script = document.getElementById(id);
document.body.removeChild(script);
}
script = document.createElement('script');
script.type = 'application/javascript';
script.id = id;
script.src = src;
script.onload = function () {
console.log("onload");
if (typeof callback !== "undefined") {
callback(self.responseObject);
}
script.onload = null;
document.body.removeChild(script);
script = null;
};
document.body.appendChild(script);
} catch (exception) {
console.log(exception);
}
},
// JSONP Response
response: function (response) {
try {
this.responseObject = response;
} catch (exception) {
console.log(exception);
}
}
}
}();
function refreshActualWeather() {
jsonp.request(apiProtocol + "//api.openweathermap.org/data/2.5/weather?" + _apiParams + "&callback=jsonp.response" + "&_timestamp=" + (new Date()).getTime()
, function (data) {
processActualWeather(data);
}, "actual");
}
function refreshForecastWeather() {
jsonp.request(apiProtocol + "//api.openweathermap.org/data/2.5/forecast?" + _apiParams + "&callback=jsonp.response" + "&_timestamp=" + (new Date()).getTime()
, function (data) {
processForecastWeather(data);
}, "forecast");
}
function refreshData() {
setNightMode();
refreshActualWeather();
refreshForecastWeather();
window.setTimeout("refreshData()", refreshTime);
}
function processForecastWeather(data) {
dataTimeZone = data.city.timezone
for (var i = 0; i < forecastCount; i++) {
var forecast = data.list[i];
var feelsLike = tempType == "feelsLike";
forecastsCells[i].temp.innerHTML = (feelsLike ? Math.round(forecast.main.feels_like) : Math.round(forecast.main.temp)) + unitsSymbolHtml;
forecastsCells[i].icon.className = getIconClassName(forecast.weather[0], new Date(forecast.dt * 1000));
forecastsCells[i].desc.innerHTML = forecast.weather[0].description;
forecastsCells[i].time.innerHTML = momentWithCorrectUtcOffset(new Date(forecast.dt * 1000)).format(timeFormat);
forecastsCells[i].temp.className = feelsLike ? "colTemp feelsLike" : "colTemp";
forecastsCells[i].icon.style.transform = null;
forecastsCells[i].icon.style.webkitTransform = null;
}
setMoonIconsRotation(data.city.coord.lat)
}
function getIconClassName(weather, date) {
var isNight = weather.icon.slice(-1) === "n";
if (isNight && weather.id == 800) {
// Night clear -> real moon phase icon.
var phase = getMoonPhaseFromDate(date);
var icon = getMoonIconClasses(phase, isNightMode());
return icon;
}
return "wi wi-owm-" + (isNight ? "night-" : "day-") + weather.id;
}
function setsUnitsSymbolFromApiUrl(url) {
if (url.indexOf("units=metric") >= 0) {
unitsSymbolHtml = "<i class='wi wi-celsius'></i>";
return
}
if (url.indexOf("units=imperial") >= 0) {
unitsSymbolHtml = "<i class='wi wi-fahrenheit'></i>";
return
}
unitsSymbolHtml = "<span>K</span>";
return
}
function getApiParams() {
if (getUrlParameter("api_params")) {
var url = getUrlParameter("api_params");
setsUnitsSymbolFromApiUrl(url)
return url;
}
if (api_params) {
var url = api_params;
setsUnitsSymbolFromApiUrl(url)
return url;
}
var appId = getUrlParameter("appId") ? getUrlParameter("appId") : api_appId;
lang = getUrlParameter("lang") ? getUrlParameter("lang") : api_lang;
if (typeof(lang) === 'string') {
lang = lang.split(";", 1)
}
var units = getUrlParameter("units") ? getUrlParameter("units") : api_units;
var locParams = api_locParams;
if (getUrlParameter("city")) {
locParams = "q=" + encodeURIComponent(getUrlParameter("city"));
} else if (getUrlParameter("lat") && getUrlParameter("lon")) {
locParams = "lat=" + encodeURIComponent(getUrlParameter("lat")) + "&lon=" + encodeURIComponent(getUrlParameter("lon"));
}
if (!locParams || !appId) {
return null;
}
var url = "";
if (locParams) {
url += locParams;
}
if (appId) {
url += "&appid=" + appId;
}
if (lang) {
url += "&lang=" + lang;
}
if (units) {
url += "&units=" + units;
}
setsUnitsSymbolFromApiUrl(url)
return url;
}
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + RegExp.escape(name) + '=([^&#]*)');
var results = regex.exec(location.search);
if (!results) return null;
if (!results[1]) return '';
return decodeURIComponent(results[1].replace(/\+/g, ' '));
}
RegExp.escape = function (s) {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};
function processActualWeather(data) {
dataTimeZone = data.timezone
var feelsLike = tempType == "feelsLike";
icon.className = getIconClassName(data.weather[0], new Date(data.dt * 1000));
icon.style.transform = null;
icon.style.webkitTransform = null;
temp.innerHTML = feelsLike ? Math.round(data.main.feels_like) : Math.round(data.main.temp);
temp.className = feelsLike ? "feelsLike" : "";
city.innerHTML = data.name;
description.innerHTML = data.weather[0].description;
lastUpdate.innerHTML = momentWithCorrectUtcOffset(new Date()).format(timeFormat) + "/" + momentWithCorrectUtcOffset(new Date(data.dt * 1000)).format(timeFormat);
date.innerHTML = momentWithCorrectUtcOffset(new Date()).format("dd, ll");
var sunrise = momentWithCorrectUtcOffset(new Date(data.sys.sunrise * 1000));
var sunset = momentWithCorrectUtcOffset(new Date(data.sys.sunset * 1000));
_sunriseHour = sunrise.hour();
_sunsetHour = sunset.hour();
sun.innerHTML = "<i class='" + getMoonIconClasses(getCurrentMoonPhase(), isNightMode()) + "'></i> <i class='wi wi-sunrise'></i> " + sunrise.format(timeFormat)
+ " <i class='wi wi-sunset'></i> " + sunset.format(timeFormat);
// alert(data.name);
setNightMode();
setMoonIconsRotation(data.coord.lat)
}
/**
* http://www.ben-daglish.net/moon.shtml
* @param year
* @param month
* @param day
* @returns {number} They all return a single value - the phase day (0 to 1, where 0=new moon, 0.5=full etc.) for the selected date.
* @constructor
*/
function getMoonPhase(year, month, day) {
var lp = 2551443;
var now = new Date(year, month - 1, day, 20, 35, 0);
var new_moon = new Date(1970, 0, 7, 20, 35, 0);
var phase = ((now.getTime() - new_moon.getTime()) / 1000) % lp;
return phase / lp;
}
function getMoonPhaseFromDate(date) {
var lp = 2551443;
var new_moon = new Date(1970, 0, 7, 20, 35, 0);
var phase = ((date.getTime() - new_moon.getTime()) / 1000) % lp;
return phase / lp;
}
/**
* @return {number} They all return a single value - the phase day (0 to 1, where 0=new moon, 0.5=full etc.) for the selected date.
*/
function getCurrentMoonPhase() {
var now = new Date();
return getMoonPhase(now.getFullYear(), now.getMonth() + 1, now.getDate());
}
function getMoonIconClasses(phase, isNightMode) {
var iconInterval = 1 / moonIcons.length;
var index = Math.floor(phase / iconInterval)
return "wi " + moonIcons[index][isNightMode ? 1 : 0] + " moonIcon " + (phase < 0.5 ? "moonFirstHalf" : "moonSecondHalf");
}
function setMoonIconsRotation(latitude) {
var rotation = (latitude - 90)
var elements = document.getElementsByClassName("moonIcon")
for (var i = 0; i < elements.length; i++) {
var direction = elements[i].className.indexOf("moonFirstHalf") !== -1 ? -1 : 1
var transform = "rotate(" + (rotation * direction) + "deg)"
elements[i].style.transform = transform;
elements[i].style.webkitTransform = transform;
}
}
var cleaner = null;
var page = null;
var temp = null;
var icon = null;
var city = null;
var description = null;
var lastUpdate = null;
var date = null;
var sun = null;
var lang = null;
var forecastsCells = null;
var forecastCount = 5;
var forcedRotation = false;
var unitsSymbolHtml = "";
// [normal, night]
var moonIcons = [
["wi-moon-alt-new", "wi-moon-alt-full"],
["wi-moon-alt-waxing-crescent-1","wi-moon-alt-waning-gibbous-1"],
["wi-moon-alt-waxing-crescent-2","wi-moon-alt-waning-gibbous-2"],
["wi-moon-alt-waxing-crescent-3","wi-moon-alt-waning-gibbous-3"],
["wi-moon-alt-waxing-crescent-4","wi-moon-alt-waning-gibbous-4"],
["wi-moon-alt-waxing-crescent-5","wi-moon-alt-waning-gibbous-5"],
["wi-moon-alt-waxing-crescent-6","wi-moon-alt-waning-gibbous-6"],
["wi-moon-alt-first-quarter","wi-moon-alt-third-quarter"],
["wi-moon-alt-waxing-gibbous-1","wi-moon-alt-waning-crescent-1"],
["wi-moon-alt-waxing-gibbous-2","wi-moon-alt-waning-crescent-2"],
["wi-moon-alt-waxing-gibbous-3","wi-moon-alt-waning-crescent-3"],
["wi-moon-alt-waxing-gibbous-4","wi-moon-alt-waning-crescent-4"],
["wi-moon-alt-waxing-gibbous-5","wi-moon-alt-waning-crescent-5"],
["wi-moon-alt-waxing-gibbous-6","wi-moon-alt-waning-crescent-6"],
["wi-moon-alt-full","wi-moon-alt-new"],
["wi-moon-alt-waning-gibbous-1","wi-moon-alt-waxing-crescent-1"],
["wi-moon-alt-waning-gibbous-2","wi-moon-alt-waxing-crescent-2"],
["wi-moon-alt-waning-gibbous-3","wi-moon-alt-waxing-crescent-3"],
["wi-moon-alt-waning-gibbous-4","wi-moon-alt-waxing-crescent-4"],
["wi-moon-alt-waning-gibbous-5","wi-moon-alt-waxing-crescent-5"],
["wi-moon-alt-waning-gibbous-6","wi-moon-alt-waxing-crescent-6"],
["wi-moon-alt-third-quarter","wi-moon-alt-first-quarter"],
["wi-moon-alt-waning-crescent-1","wi-moon-alt-waxing-gibbous-1"],
["wi-moon-alt-waning-crescent-2","wi-moon-alt-waxing-gibbous-2"],
["wi-moon-alt-waning-crescent-3","wi-moon-alt-waxing-gibbous-3"],
["wi-moon-alt-waning-crescent-4","wi-moon-alt-waxing-gibbous-4"],
["wi-moon-alt-waning-crescent-5","wi-moon-alt-waxing-gibbous-5"],
["wi-moon-alt-waning-crescent-6","wi-moon-alt-waxing-gibbous-6"],
];
var timeFormat = "HH:mm";
// Config parameters.
var api_params, api_appId, api_lang, api_units, api_locParams, rotation, night_mode, refreshTime, utcOffset, tempType;
utcOffset = getUrlParameter("utcOffset") ? getUrlParameter("utcOffset") : utcOffset;
tempType = getUrlParameter("tempType") ? getUrlParameter("tempType") : tempType;
var dataTimeZone = 0;
// For night mode.
var _sunsetHour = 18;
var _sunriseHour = 6;
var _apiParams = null;
if (!refreshTime) {
refreshTime = 30 * 60 * 1000;
}
function rotate() {
var rot = getUrlParameter("rotation") ? getUrlParameter("rotation") : rotation;
var v = viewport();
var isPortrait = v.height > v.width;
if (isPortrait && rot === "ll") {
// landscape left
page.className = "landscape";
forcedRotation = true;
page.style.transform = "rotate(90deg)";
page.style.transformOrigin = "bottom left";
page.style.webkitTransform = "rotate(90deg)";
page.style.webkitTransformOrigin = "bottom left";
page.style.position = "absolute";
page.style.top = -v.width + "px";
page.style.height = v.width + "px";
page.style.width = v.height + "px";
} else if (isPortrait && rot === "lr") {
// landscape right
page.className = "landscape";
forcedRotation = true;
page.style.transform = "rotate(-90deg)";
page.style.transformOrigin = "bottom right";
page.style.webkitTransform = "rotate(-90deg)";
page.style.webkitTransformOrigin = "top left";
page.style.position = "absolute";
page.style.top = v.height + "px";
page.style.height = v.width + "px";
page.style.width = v.height + "px";
} else if (rot === "up") {
// upside down
page.className = isPortrait ? "portrait" : "landscape";
forcedRotation = false;
page.style.transform = "rotate(180deg)";
page.style.transformOrigin = "50% 50% 0";
page.style.webkitTransform = "rotate(180deg)";
page.style.webkitTransformOrigin = "50% 50% 0";
} else {
// reset css:
page.className = isPortrait ? "portrait" : "landscape";
forcedRotation = false;
page.style.transform = "";
page.style.transformOrigin = "";
page.style.webkitTransform = "";
page.style.webkitTransformOrigin = "";
page.style.position = "";
page.style.top = "";
page.style.background = "";
page.style.height = "";
page.style.width = "";
}
}
window.addEventListener('load', function () {
console.log("load");
_apiParams = getApiParams();
if (_apiParams === null) {
window.location = "config.html";
}
moment.locale(momentLocale());
cleaner = document.getElementById("cleaner");
page = document.getElementById("page");
temp = document.getElementById("temp");
icon = document.getElementById("icon");
city = document.getElementById("city");
city.addEventListener('click', function (event) {
event.preventDefault();
window.location.href = window.location.href.replace("/index.html", "/config.html");
return false
});
temp.addEventListener('click', function (event) {
event.preventDefault();
var newHref = window.location.href.replace(/[&?]tempType=[^&]*/, "");
newHref = newHref + (newHref.indexOf("?") >= 0 ? "&" : "?") + "tempType=" + (tempType === "feelsLike" ? "actual" : "feelsLike")
window.location.href = newHref
return false
});
description = document.getElementById("description");
lastUpdate = document.getElementById("lastUpdate");
date = document.getElementById("date");
sun = document.getElementById("sun");
forecastsCells = [];
for (var i = 0; i < forecastCount; i++) {
forecastsCells.push({
temp: document.getElementById("temp" + (i + 1)),
icon: document.getElementById("icon" + (i + 1)),
desc: document.getElementById("desc" + (i + 1)),
time: document.getElementById("time" + (i + 1)),
});
}
refreshData();
rotate();
setRem("load");
setNightMode();
// Set Cleanup for 4am
var now = new Date();
var cleanTime = (new Date()).setHours(4,0,0);
var diff = cleanTime - now;
if (diff < 0) {
diff += 86400000;
}
window.setTimeout("clearScreen()", diff);
});
window.addEventListener("resize", function() {
rotate();
setRem("resize");
});
// Simulating vh an vw with rem: http://stackoverflow.com/questions/13948713/is-there-any-cross-browser-javascript-for-making-vh-and-vw-units-work
// But kindle 4 doesn't support rem (so all sizes are fixed fallback for kindle 4)
// Kindle 4 viewport: 600x706 (landscape 800x506) (in window.load event, window.resize event is not called)
// Paperwhite 3 viewport: 1072x1268 (in window.load event it is 0x0, in window.resized
function viewport() {
var e = window, a = 'inner';
if (!('innerWidth' in window )) {
a = 'client';
e = document.documentElement || document.body;
}
return { width : e[ a+'Width' ] , height : e[ a+'Height' ] };
}
function setRem(prefix) {
var v = viewport();
var root = document.querySelector(":root");
root.style.fontSize = ((forcedRotation === false ? v.height : v.width) / 100) + "px";
// console.log("setting root font size to: " + v.height)
// alert(prefix + " viewport:" + v.width + "x" + v.height);
}
function setNightMode() {
var root = document.querySelector(":root");
if (isNightMode()) {
root.className = "night";
} else {
root.className = null;
}
}
function isNightMode() {
var mode = getUrlParameter("night") ? getUrlParameter("night") : night_mode;
if (mode === "on") {
return true;
}
if (mode === "auto") {
return isNight(_sunsetHour, _sunriseHour);
}
var found = mode && mode.match(/([0-9][0-9]?)-([0-9][0-9]?)/);
if (found) {
return isNight(found[1], found[2]);
}
return false;
}
function isNight(f, t) {
var from = parseInt(f);
var to = parseInt(t);
var now = momentWithCorrectUtcOffset(new Date()).hour();
if (from > to) {
return from <= now || to > now;
} else {
return from <= now && to > now;
}
}
function momentLocale() {
var lang = getUrlParameter("lang") ? getUrlParameter("lang") : api_lang;
if (typeof(lang) === 'string') {
var langs = lang.split(";", 2)
return langs.length > 1 ? langs[1] : langs[0];
} else {
return null;
}
}
function clearScreen(times) {
if (typeof(times) == 'undefined') {
times = 5;
}
if (times <= 0) {
cleaner.style.display = 'none';
window.setTimeout("clearScreen()", 86400000);
return;
}
cleaner.style.display = 'block';
cleaner.style.background = times % 2 == 0 ? "#ffffff" : "#000000";
window.setTimeout(function () {
clearScreen(times - 1);
}, 500);
}
function momentWithCorrectUtcOffset(dateTime) {
// Default is auto
if (typeof(utcOffset) === 'undefined' || utcOffset === null || utcOffset === "") {
return moment(dateTime).utcOffset(dataTimeZone / 60, false)
} else if (utcOffset === 'local') {
return moment(dateTime);
} else {
return moment(dateTime).utcOffset(utcOffset, false);
}
}
</script>
</head>
<body>
<div id="page">
<a id="city" href="./config.html"></a>
<div id="iconWrapper"><i id="icon"></i></div>
<div class="tempWrapper">
<div id="temp"></div>
<!--<i class="celsius wi wi-celsius"></i>-->
</div>
<div id="description"></div>
<div id="lastUpdate"></div>
<div class="forecast">
<div class="col col1">
<div id="time1" class="colTime"></div>
<div class="forecastIconWrapper"><i id="icon1" class="colIcon"></i></div>
<div id="temp1" class="colTemp"></div>
<div id="desc1" class="colDesc"></div>
</div>
<div class="col col2">
<div id="time2" class="colTime"></div>
<div class="forecastIconWrapper"><i id="icon2" class="colIcon"></i></div>
<div id="temp2" class="colTemp"></div>
<div id="desc2" class="colDesc"></div>
</div>
<div class="col col3">
<div id="time3" class="colTime"></div>
<div class="forecastIconWrapper"><i id="icon3" class="colIcon"></i></div>
<div id="temp3" class="colTemp"></div>
<div id="desc3" class="colDesc"></div>
</div>
<div class="col col4">
<div id="time4" class="colTime"></div>
<div class="forecastIconWrapper"><i id="icon4" class="colIcon"></i></div>
<div id="temp4" class="colTemp"></div>
<div id="desc4" class="colDesc"></div>
</div>
<div class="col col5">
<div id="time5" class="colTime"></div>
<div class="forecastIconWrapper"><i id="icon5" class="colIcon"></i></div>
<div id="temp5" class="colTemp"></div>
<div id="desc5" class="colDesc"></div>
</div>
</div>
<div id="date"></div>
<div id="sun"></div>
</div>
<div id="cleaner"></div>
</body>
</html>