/
script.ts
457 lines (414 loc) · 19.2 KB
/
script.ts
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
import {WeatherData} from "./WeatherData"
import $ = require("jquery");
const months: string[] = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
const daysShort: string[] = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const appid: string = "178e8d4180edebe4e2c02fcad75b72fd";
$(document).ready(function () {
var deg = '°';
var celsius = 'C';
var weatherDiv = $('#weather');
getLocation();
/**
* Callback function for custom location searches
* @returns {Promise<void>}
*/
function customSearch() {
var searchParameter:string = (<HTMLInputElement>document.getElementById("searchBox")).value;
try {
return new Promise(function (resolve: any, reject: any) {
if (searchParameter !== "") {
$.getJSON("https://api.openweathermap.org/data/2.5/forecast?q=" + searchParameter + "&appid=" + appid, function (weatherResponse) {
resolve(weatherResponse);
});
} else {
alert("Please enter a city and it's ISO country code.")
}
}).then(function (weatherResponse: any) {
// Don't have to create a new WeatherData here, just call set general
var weatherData = new WeatherData();
var generalWeatherData = setGeneralInformation(weatherData, weatherResponse);
});
} catch (err) {
showError("We can't find information about your city!");
}
}
$("#searchButton").click(customSearch);
/**
* handler for when the enter key is pressed, do a custom search for location and disallow refresh
*/
$(document).keypress(function (event: any) {
if(event.keyCode === 13) {
event.preventDefault();
customSearch();
}
});
/**
* called when the user clicks the "Search" button
* get the user's current location and calls the appropriate callback function,
* shows an error if the browser does not support Geolocation
*/
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(locationSuccess, locationError);
} else {
showError("Your browser does not support Geolocation!");
}
}
/**
* callback function for when getting the geolocation (ie. lat and lon) succeed
* @param position the fulfilled value from the promise
* @returns {Promise<void>}
*/
function locationSuccess(position: any) {
var lat = position.coords.latitude;
var lon = position.coords.longitude;
try {
return new Promise(function (resolve: any, reject: any) {
$.getJSON("https://api.openweathermap.org/data/2.5/forecast?lat=" + lat + "&lon=" + lon +
"&appid=" + appid, function (weatherResponse) {
resolve(weatherResponse);
});
}).then(function (weatherResponse: any) {
// Don't have to create a new WeatherData here, just call set general
var weatherData = new WeatherData();
var generalWeatherData = setGeneralInformation(weatherData, weatherResponse);
});
} catch (err) {
showError("We can't find information about your city!");
}
};
/**
*
* @param {WeatherData} weatherData
* @param weatherResponse the JSON response from API call
* @param {string[]} uniqueWeek the week of the forecast
* @returns {WeatherData}
*/
function setSingleDateWeatherDataHelper(weatherData: WeatherData, weatherResponse: any, uniqueWeek: string[]): WeatherData {
// TODO: Keep this here since I'm not sure if I can refactor this into using multiple WeatherData data strucures instead.
// weatherData.temperature = weatherResponse.list.main.temp;
// weatherData.minTemperature = weatherResponse.list.main.temp_min;
// weatherData.maxTemperature = weatherResponse.list.main.temp_max;
// weatherData.weather = weatherResponse.list.weather.description;
// weatherData.weatherIcon = weatherResponse.list.weather.icon;
var currentTempWeek: number[] = determineCurrentTemp(weatherResponse, uniqueWeek);
var minTempWeek: number[] = determineMinTemp(weatherResponse, uniqueWeek);
var maxTempWeek: number[] = determineMaxTemp(weatherResponse, uniqueWeek);
var weatherDescriptions: string[] = determineWeatherDescriptions(weatherResponse, uniqueWeek);
var weatherIconIds: string[] = determineWeatherIconIDs(weatherResponse, uniqueWeek);
setCurrentTempWeekIntoHTML(currentTempWeek);
setMinTempWeekIntoHTML(minTempWeek);
setMaxTempWeekIntoHTML(maxTempWeek);
setWeatherDescWeekIntoHTML(weatherDescriptions);
setWeatherIconWeekIntoHTML(weatherIconIds);
return weatherData;
}
/**
* Sets general information into HTML
* @param {WeatherData} weatherData
* @param weatherResponse the JSON response from API call
* @returns {WeatherData}
*/
function setGeneralInformation(weatherData: WeatherData, weatherResponse: any): WeatherData {
var uniqueWeek: string[] = createWeek(weatherResponse);
var daysOfTheWeek: string[] = createDaysOfTheWeek(uniqueWeek);
var datesOfTheWeek: string[] = createDatesOfTheWeek(uniqueWeek);
var city = determineCity(weatherResponse);
var country = determineCountry(weatherResponse);
setDayOfTheWeekIntoHTML(daysOfTheWeek);
setDatesOfTheWeekIntoHTML(datesOfTheWeek);
setCityAndCountryIntoHTML(city, country);
setSingleDateWeatherDataHelper(weatherData, weatherResponse, uniqueWeek);
return weatherData; // TODO: currently there is no point of even returning a WeatherData since it's never used
}
/**
* error handling functions for location errors
* @param error
*/
function locationError(error: any): void {
switch (error.code) {
case error.TIMEOUT:
showError("A timeout occurred! Please try again!");
break;
case error.POSITION_UNAVAILABLE:
showError('We can\'t detect your location. Sorry!');
break;
case error.PERMISSION_DENIED:
showError('Please allow geolocation access for this to work.');
break;
case error.UNKNOWN_ERROR:
showError('An unknown error occured!');
break;
}
};
// TODO: Not sure how this is being used
/**
*
* @param msg
*/
function showError(msg: any) {
weatherDiv.addClass('error').html(msg);
};
/**
* Determines the minimum temperature of each day for a given week
* @param weatherResponse the JSON response from API call
* @param {string[]} uniqueWeek the week of the forecast
* @returns {number[]} the array of minimum temperatures (in Celsius) where 1st element is 1st day, 2nd element is 2nd day, ...
*/
function determineMinTemp(weatherResponse: any, uniqueWeek: string[]): number[] {
var arrayOfMinTempsWeek: number[] = [];
for (let j = 0; j < uniqueWeek.length; j++) {
let arrayOfMinTempsSingleDay: number[] = [];
for (let i = 0; i < weatherResponse.list.length; i++) {
if (weatherResponse.list[i].dt_txt.includes(uniqueWeek[j])) {
arrayOfMinTempsSingleDay.push(weatherResponse.list[i].main.temp_min);
}
};
let trueMinTemp: number = arrayOfMinTempsSingleDay[0];
for (let i = 1; i < arrayOfMinTempsSingleDay.length; i++) {
if (arrayOfMinTempsSingleDay[i] < trueMinTemp) {
trueMinTemp = arrayOfMinTempsSingleDay[i];
}
};
arrayOfMinTempsWeek.push(trueMinTemp);
}
var arrayOfMinTempsCelsiusWeek: number[] = arrayOfMinTempsWeek.map(kelvinToCelsius);
var arrayOfMinTempsCelsiusIntegerWeek: number[] = arrayOfMinTempsCelsiusWeek.map(currentTempKelvin => Math.floor(currentTempKelvin));
return arrayOfMinTempsCelsiusIntegerWeek;
};
/**
* Determines the maximum temperature of each day for a given week
* @param weatherResponse the JSON response from API call
* @param {string[]} uniqueWeek the week of the forecast
* @returns {number[]} the array of maximum temperatures (in Celsius) where 1st element is 1st day, 2nd element is 2nd day, ...
*/
function determineMaxTemp(weatherResponse: any, uniqueWeek: string[]): number[] {
var arrayOfMaxTempsWeek: number[] = [];
for (let j = 0; j < uniqueWeek.length; j++) {
let arrayOfMaxTempsSingleDay: number[] = [];
for (let i = 0; i < weatherResponse.list.length; i++) {
if (weatherResponse.list[i].dt_txt.includes(uniqueWeek[j])) {
arrayOfMaxTempsSingleDay.push(weatherResponse.list[i].main.temp_max);
}
};
let trueMaxTemp: number = arrayOfMaxTempsSingleDay[0];
for (let i = 1; i < arrayOfMaxTempsSingleDay.length; i++) {
if (arrayOfMaxTempsSingleDay[i] > trueMaxTemp) {
trueMaxTemp = arrayOfMaxTempsSingleDay[i];
}
};
arrayOfMaxTempsWeek.push(trueMaxTemp);
}
var arrayOfMaxTempsCelsiusWeek: number[] = arrayOfMaxTempsWeek.map(kelvinToCelsius);
var arrayOfMaxTempsCelsiusIntegerWeek: number[] = arrayOfMaxTempsCelsiusWeek.map(currentTempKelvin => Math.floor(currentTempKelvin));
return arrayOfMaxTempsCelsiusIntegerWeek;
};
/**
* Determines the first temperature for the same time of each day for a given week
* @param weatherResponse the JSON response from API call
* @param {string[]} uniqueWeek the week of the forecast
* @returns {number[]} the array of current temperatures (in Celsius) where 1st element is 1st day, 2nd element is 2nd day, ...
*/
function determineCurrentTemp(weatherResponse: any, uniqueWeek: string[]): number[] {
var arrayOfCurrentTempsWeek: number[] = [];
for (let j = 0; j < uniqueWeek.length; j++) {
for (let i = 0; i < weatherResponse.list.length; i++) {
if (weatherResponse.list[i].dt_txt.includes(uniqueWeek[j])) {
arrayOfCurrentTempsWeek.push(weatherResponse.list[i].main.temp);
break;
}
};
}
var arrayOfCurrentTempsCelsiusWeek: number[] = arrayOfCurrentTempsWeek.map(kelvinToCelsius);
var arrayOfCurrentTempsCelsiusIntegerWeek: number[] = arrayOfCurrentTempsCelsiusWeek.map(currentTempKelvin => Math.floor(currentTempKelvin));
return arrayOfCurrentTempsCelsiusIntegerWeek;
};
/**
* Determines the weather descriptions at a specific time of each day for a given week
* @param weatherResponse the JSON response from API call
* @param {string[]} uniqueWeek the week of the forecast
* @returns {string[]} the array of weather descriptions
*/
function determineWeatherDescriptions(weatherResponse: any, uniqueWeek: string[]): string[] {
var timeOfDay = " 09:00:00";
var arrayOfWeatherDesc: string[] = [];
for (let i = 0; i < uniqueWeek.length; i++) {
for(let j = 0; j < weatherResponse.list.length; j++) {
if (weatherResponse.list[j].dt_txt.includes(uniqueWeek[i] + timeOfDay)) {
let weatherDesc = weatherResponse.list[j].weather[0].description;
arrayOfWeatherDesc.push(weatherDesc);
break;
}
}
}
return arrayOfWeatherDesc;
};
/**
* Determines the weather icon id's at a specific time of each day for a given week
* @param weatherResponse the JSON response from API call
* @param {string[]} uniqueWeek the week of the forecast
* @returns {string[]} the array of weather icon id's
*/
function determineWeatherIconIDs(weatherResponse: any, uniqueWeek: string[]): string[] {
var arrayOfWeatherIconIds: string[] = [];
var timeOfDay = " 09:00:00";
for (let i = 0; i < uniqueWeek.length; i++) {
for(let j = 0; j < weatherResponse.list.length; j++) {
if (weatherResponse.list[j].dt_txt.includes(uniqueWeek[i] + timeOfDay)) {
let weatherIconId = weatherResponse.list[j].weather[0].icon;
arrayOfWeatherIconIds.push(weatherIconId);
break;
}
}
}
return arrayOfWeatherIconIds;
};
/**
* Determines the city of which the API call made it to
* @param weatherResponse the JSON response from API call
* @returns {string} the city corresponding to the weather response
*/
function determineCity(weatherResponse: any): string {
return weatherResponse.city.name;
};
/**
* Determines the country of which the API call made it to
* @param weatherResponse the JSON response from API call
* @returns {string} the country corresponding to the weather response
*/
function determineCountry(weatherResponse: any): string {
return weatherResponse.city.country;
};
/**
* Create a week with 5 consecutive and unique days given an array of date/time calculations (has the date values)
* of UTC in strings
* @param {string[]} setOfDates set of dates for the week
*/
function createWeek(weatherResponse: any): string[] {
var arrayOfDates: string[] = [];
for (let j = 0; j < weatherResponse.list.length; j++) {
let slicedDate = weatherResponse.list[j].dt_txt.slice(0,10);
if (!(arrayOfDates.includes(slicedDate))) {
arrayOfDates.push(slicedDate);
}
}
return arrayOfDates;
};
/**
* Creates the (numbers) days of the week that correspond to the correct date
* eg. Dec.25th 1995 is a 1 which correspoinds to a Monday
* @param {string[]} weekDates the array of 5 consecutive dates in UTC format with only Year-Month-Date
* @returns {string[]} the array of the (word) days of the week in abbreviated form (eg. Monday = Mon)
*/
function createDaysOfTheWeek(weekDates: string[]): string[] {
var daysOfTheWeek: string[] = [];
weekDates.forEach(function (date: string) {
let year = date.slice(0,4);
let month = date.slice(5,7);
let day = date.slice(8,10);
let d = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
let wordDayOfTheWeek = daysShort[d.getDay()];
daysOfTheWeek.push(wordDayOfTheWeek);
});
return daysOfTheWeek;
};
/**
* Sets the HTML for each (word) day of the week to the correct day
* @param {string[]} wordDayOfTheWeek the days of the week in the correct order
*/
function setDayOfTheWeekIntoHTML(wordDayOfTheWeek: string[]) {
for (let i = 0; i < 5; i++) {
document.getElementById("wordDay" + i).innerHTML = wordDayOfTheWeek[i];
};
};
/**
* Creates the (string) month-days of the month that correspond to the correct upcoming next
* 5 days
* (eg of a month-day Jan. 1)
* @param {string[]} weekDates the array of 5 consectuve dates in UTC format with only Year-Month-Date
* @returns {string[]} the array of the (string) month-days of the week
*/
function createDatesOfTheWeek(weekDates: string[]): string[] {
var datesOfTheWeek: string[] = [];
weekDates.forEach(function (date: string) {
let year = date.slice(0,4);
let month = date.slice(5,7);
let day = date.slice(8,10);
let d = new Date(parseInt(year), parseInt(month) - 1, parseInt(day));
let monthDay: string = months[d.getMonth()] + ". " + d.getDate();
datesOfTheWeek.push(monthDay);
});
return datesOfTheWeek;
};
/**
* Sets the HTML for each date of the week to the correct date
* (eg. Dec 30)
* @param {string[]} datesOfTheWeek
*/
function setDatesOfTheWeekIntoHTML(datesOfTheWeek: string[]) {
for (var i = 0; i < 5; i++) {
document.getElementById("monthDay" + i).innerHTML = datesOfTheWeek[i];
};
};
/**
* Sets the HTML for the city and country corresponding to the weather response
* @param {string} city
* @param {string} country
*/
function setCityAndCountryIntoHTML(city: string, country: string) {
document.getElementById("currentCityAndCountry").innerText = "Location: " + city + ", " + country;
};
/**
* Sets the HTML for the current temperature for each day of the week
* @param {number[]} currentTempWeek
*/
function setCurrentTempWeekIntoHTML(currentTempWeek: number[]) {
for (let i = 0; i < 5; i++) {
document.getElementById("currentTemp" + i).innerHTML = currentTempWeek[i] + " " + deg + celsius;
}
};
/**
* Sets the HTML for the minimum temperature for each day of the week
* @param {number[]} minTempWeek
*/
function setMinTempWeekIntoHTML(minTempWeek: number[]) {
for (let i = 0; i < 5; i++) {
document.getElementById("minTemp" + i).innerHTML = "Low: " + minTempWeek[i] + " " + deg + celsius;
}
};
/**
* Sets the HTML for the maximum temperature for each day of the week
* @param {number[]} maxTempWeek
*/
function setMaxTempWeekIntoHTML(maxTempWeek: number[]) {
for (let i = 0; i < 5; i++) {
document.getElementById("maxTemp" + i).innerHTML = "High: " + maxTempWeek[i] + " " + deg + celsius;
}
};
/**
* Sets the HTML for the weather descriptions for each day of the week
* @param {string[]} weatherDescriptions
*/
function setWeatherDescWeekIntoHTML(weatherDescriptions: string[]) {
for (let i = 0; i < 5; i++) {
document.getElementById("forecast" + i).innerHTML = weatherDescriptions[i];
}
}
/**
* Sets the HTML for the weather icons for each day of the week
* @param {string[]} weatherIconIds
*/
function setWeatherIconWeekIntoHTML(weatherIconIds: string[]) {
for (let i = 0; i < weatherIconIds.length; i++) {
document.getElementById("icon" + i).setAttribute("src","http://openweathermap.org/img/w/" + weatherIconIds[i] + ".png");
}
}
/**
* Converts a temperature from Kelvin to Celsius
* @param {number} kelvin
* @returns {number} the same temperature in degrees Celsius
*/
function kelvinToCelsius(kelvin: number): number {
return kelvin - 273.15;
};
});