-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
171 lines (143 loc) · 7.1 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WEATHER NOW! WEATHER 4 U!</title>
<link rel="stylesheet" href="./static/globalStyles.css" />
<script type="module" src="./components/layout/pageContent.js"></script>
<script type="module" src="./components/widgets/locationWidget.js"></script>
<script type="module" src="./components/widgets/weeklyForecast.js"></script>
<script type="module" src="./components/widgets/hourlyChart.js"></script>
<script type="module" src="./components/widgets/forecastHeader.js"></script>
<script type="module" src="./components/widgets/alertBar.js"></script>
<script type="module" src="./components/widgets/currentRadar.js"></script>
<script type="module" src="./components/widgets/weatherDetails.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ol.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/ol.css">
<script type="module">
import { get, set } from 'https://cdn.jsdelivr.net/npm/idb-keyval@6/+esm';
//Prepare USA state and county data
fetch("./static/data/USAMeta.json")
.then(res => res.json())
.then(res => {
set("states", res.metaTuples.filter(([x, y]) => x == "state"))
})
fetch("https://api.weather.gov/zones/county")
.then(res => res.json())
.then(res => {
set("counties", res.features.map(r => {
return {
state: r.properties.state,
name: r.properties.name,
url: r.properties["@id"],
offices: r.properties.forecastOffices
}
}))
})
document.addEventListener("locationChange", (e) => {
fetch(`https://api.weather.gov/points/${e.detail[1].toFixed(4)},${e.detail[0].toFixed(4)}`)
.then(res => res.json())
.then(pointInfo => {
if (!pointInfo.status) {
const header = document.querySelector("#forecast-header")
header.setAttribute("data-city", pointInfo.properties.relativeLocation.properties.city)
header.setAttribute("data-state", pointInfo.properties.relativeLocation.properties.state)
const weeklyForecast = document.querySelector("#weekly-forecast")
weeklyForecast.setAttribute("data-forecast-url", pointInfo.properties.forecast)
const hourlyChart = document.querySelector("#hourly-chart")
hourlyChart.setAttribute("data-forecast-url", pointInfo.properties.forecastHourly)
const currentRadar = document.querySelector("current-radar")
currentRadar.setAttribute("data-radar", pointInfo.properties.radarStation)
const alertBar = document.querySelector("alert-bar")
alertBar.setAttribute("data-lon-lat", JSON.stringify({ lon: e.detail[0].toFixed(4), lat: e.detail[1].toFixed(4) }))
const weatherDetails = document.querySelector("weather-details")
weatherDetails.setAttribute("data-grid-url", pointInfo.properties.forecastGridData)
} else {
throw new Error("Forecast unavailable for this location")
}
}).catch((error) => {
console.error(error)
document.dispatchEvent(new CustomEvent("InvalidLocation"))
})
})
document.addEventListener("InvalidLocation", () => {
alert("Something went wrong, helpfully providing weather for Tucson, Arizona!")
const header = document.querySelector("#forecast-header")
header.setAttribute("data-city", "South Tucson")
header.setAttribute("data-state", "AZ")
const weeklyForecast = document.querySelector("#weekly-forecast")
weeklyForecast.setAttribute("data-forecast-url", "https://api.weather.gov/gridpoints/TWC/91,49/forecast")
const hourlyChart = document.querySelector("#hourly-chart")
hourlyChart.setAttribute("data-forecast-url", "https://api.weather.gov/gridpoints/TWC/91,49/forecast/hourly")
const pageContent = document.querySelector("#page-content")
const currentRadar = document.querySelector("current-radar")
currentRadar.setAttribute("data-radar", "KEMX")
const alertBar = document.querySelector("alert-bar")
alertBar.setAttribute("data-lon-lat", JSON.stringify({ lon: -110.9759, lat: 32.2229 }))
pageContent.childNodes.forEach(n => {
if (n.nodeName !== "#text") {
n.setAttribute("data-gridX", 91)
n.setAttribute("data-gridY", 49)
if (n.id === "current-container") {
n.childNodes.forEach(c => {
n.setAttribute("data-gridX", 91)
n.setAttribute("data-gridY", 49)
})
}
}
})
})
</script>
</head>
<body>
<h1>Cool Weather, Bro!</h1>
<page-content id="page-content">
<div class="header-controls-div" slot="page-slot">
<forecast-header id="forecast-header"></forecast-header>
<location-widget id="location-widget"></location-widget>
</div>
<weekly-forecast id="weekly-forecast" slot="page-slot"></weekly-forecast>
<hourly-chart id="hourly-chart" slot="page-slot"></hourly-chart>
<div class="alert-radar-combo" slot="page-slot">
<alert-bar id="alert-bar"></alert-bar>
<current-radar id="current-radar"></current-radar>
<weather-details id="weather-details"></weather-details>
</div>
</page-content>
</body>
<template id="forecast-box">
<div class="forecast-box">
<label class="forecast-label"></label>
<img class="forecast-rep" />
<p class="forecast-temperature"></p>
<p class="forecast-description"></p>
<p class="forecast-detailed-description"></p>
</div>
</template>
<template id="alert-box">
<div class="alert-box">
<details>
<summary>
<span class="alert-event-label"></span>
<p class="alert-area-expiration"></p>
</summary>
<div class="alert-radar-chart-container">
<canvas class="alert-radar-chart"></canvas>
</div>
<label class="alert-headline"></label>
<p class="alert-description"></p>
<p class="alert-area-description"></p>
</details>
</div>
</template>
<template id="weather-detail">
<div class="weather-detail-container">
<label></label>
<p></p>
<div style="height: 60px;">
<canvas id="details-canvas"></canvas>
</div>
</div>
</template>
</html>