-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
92 lines (77 loc) · 2.71 KB
/
javascript.js
File metadata and controls
92 lines (77 loc) · 2.71 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
function get_weather_data(predictions, reference) {
if (predictions) {
return "<div><label class='date'>" + predictions[reference] + "</label> "
+ predictions["weather_desc"]
+ " <i class=\"" + predictions["weather_icon"] + "\"></i> "
+ Math.round(predictions["temp_min"]) + "℃ – "
+ Math.round(predictions["temp_max"]) + "℃</div>";
}
}
function print_backcasts(date, city) {
$("#backcasts").empty();
backcasts = weather_data[date][city].backcasts;
var i = backcasts.length - 2;
for (i; i > -1; i--) {
$("#backcasts").append(
get_weather_data(backcasts[i], "date_taken") + "<br/>");
}
}
function print_forecasts(date, city) {
$("#forecasts").empty();
for (var i = 1; i < 6; i++) {
next_day = weather_data[get_future_date(i)][city].backcasts;
forecast = get_weather_data(next_day[next_day.length - 1], "date_for") + "<br/>";
$("#forecasts").append(forecast);
}
}
function print_weather_data_date_for_city(date, city) {
if (!(city in weather_data[date])) {
city = "zurich";
}
$("#city_title").html(weather_data[date][city].display_name + " today");
todays = weather_data[date][city].backcasts;
$("#city_weather").html(get_weather_data(todays[todays.length-1], "date_taken"));
print_backcasts(date, city);
print_forecasts(date, city);
}
function date_to_string(date) {
var dd = String(date.getDate()).padStart(2, '0');
var mm = String(date.getMonth() + 1).padStart(2, '0');
var yyyy = date.getFullYear();
return yyyy + '-' + mm + '-' + dd;
}
function get_today() {
return get_future_date(0);
}
function get_future_date(offset) {
const fdate = new Date();
fdate.setDate(fdate.getDate() + offset);
return date_to_string(fdate);
}
$(document).ready(function() {
// Get the code after # in the url and load that city
var startHash = window.location.hash.substring(1);
// If there's none, load our main city
if (!startHash) {
startHash = "zurich";
}
// Print today's weather backcast/forecast for the selected city
var startCity = startHash.charAt(0) + startHash.slice(1);
print_weather_data_date_for_city(get_today(), startCity);
// Add even listeners for each city button
$("#zurich").click(function() {
print_weather_data_date_for_city(get_today(), "zurich");
});
$("#budapest").click(function() {
print_weather_data_date_for_city(get_today(), "budapest");
});
$("#krakow").click(function() {
print_weather_data_date_for_city(get_today(), "krakow");
});
$("#balatonederics").click(function() {
print_weather_data_date_for_city(get_today(), "balatonederics");
});
$("#mounteverest").click(function() {
print_weather_data_date_for_city(get_today(), "mounteverest");
});
} );