-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (126 loc) · 4.46 KB
/
script.js
File metadata and controls
136 lines (126 loc) · 4.46 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
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
$(document).ready(function() {
var term;
var getlocation = $("#getlocation"); // MAIN LOCATION
var locationStatus = $("#mainType"); // WHETHER ITS HAZE, SMOKY, SUNNY .... ETC
var showTemp = $("#showTemp"); // WILL SHOW EXACT TEMP
var shortNews = $("#shortNews"); // WILL SHOW THE SHORT REPORT SUMMARY
var dataTable = $("#dataTable"); // DATA TABLE WITH DETAILED INFO
var content = $("#contentContainer");
var loader = $("#loader");
var icons = [
{ type: "Haze", icon: '<i class="fas fa-cloud-sun"></i>' },
{ type: "Rain", icon: '<i class="fas fa-cloud-showers-heavy"></i>' },
{ type: "Smoke", icon: '<i class="fas fa-smog"></i>' },
{ type: "Clouds", icon: '<i class="fas fa-cloud"></i>' },
{ type: "Clear", icon: '<i class="fas fa-sun"></i>' },
{ type: "Snow", icon: '<i class="far fa-snowflake"></i>' },
{ type: "Other", icon: '<i class="fas fa-wind"></i>' }
];
// TEMPORARILY HIDING HTML DATA
function clearScreen() {
getlocation.html("");
locationStatus.html("");
showTemp.html("");
shortNews.html("");
dataTable.hide();
$("#errorMessage").hide();
content.hide();
loader.hide();
}
clearScreen();
$("#location").keypress(function(e) {
var key = e.which;
if (key == 13) {
term = $("#location").val();
document.getElementById("location").value = "";
if (term != undefined && term != null && term != "" && term != " ")
clearScreen();
searchLocation(term);
e.preventDefault();
}
});
$("#searchBtn").click(function(e) {
e.preventDefault();
term = $("#location").val();
document.getElementById("location").value = "";
if (term != undefined && term != null && term != "" && term != " ")
clearScreen();
searchLocation(term);
});
function searchLocation(currentLocation) {
$("#errorMessage").html("");
loader.show();
axios({
method: "get",
url: `http://api.openweathermap.org/data/2.5/weather?q=${currentLocation}&appid=9fbc7ccff6470a8201aaac9d32862582&units=metric`
})
.then(function(response) {
loader.hide();
content.show();
var weatherData = response.data;
getlocation.html(`${weatherData.name}`);
var weatherType = weatherData.weather[0].main;
locationStatus.html(`${weatherType}`);
for (var i = 0; i < icons.length; i++) {
if (icons[i].type === weatherType) {
break;
} else if (icons[i].type !== weatherType && i == icons.length - 1) {
weatherType = "Other";
}
}
console.log(weatherType);
for (var i = 0; i < icons.length; i++) {
if (icons[i].type === weatherType) {
$("#weatherIcon").html(icons[i].icon);
}
}
showTemp.html(`${response.data.main.temp + String.fromCharCode(176)}C`);
shortNews.html(
`Today: ${
weatherData.weather[0].main
} currently. The low will be ${weatherData.main.temp_min +
String.fromCharCode(176)}C and high will be ${weatherData.main
.temp_max + String.fromCharCode(176)}C.`
);
dataTable.show();
dataTable.html(`
<div class="col text-center details">
<ul id="detailTopics">
<li>Latitude</li>
<li>Longitude</li>
<li>Pressure</li>
<li>Humidity</li>
<li>Min Temp</li>
<li>Max Temp</li>
<li>Wind</li>
</ul>
</div>
<div class="col text-center data">
<ul id="detailData">
<li>${weatherData.coord.lat}</li>
<li>${weatherData.coord.lon}</li>
<li>${weatherData.main.pressure} pa</li>
<li>${weatherData.main.humidity}%</li>
<li>${weatherData.main.temp_min + String.fromCharCode(176)}C</li>
<li>${weatherData.main.temp_max + String.fromCharCode(176)}C</li>
<li>${weatherData.wind.speed} m/s</li>
</ul>
</div>
`);
$(this.dataTable).addClass("data-table");
console.log(response);
console.log(response.data.name);
console.log(response.data.main.temp + String.fromCharCode(176) + "C");
})
.catch(function(error) {
console.log(error);
content.show();
loader.hide();
$("#weatherIcon").hide();
$("#errorMessage").show();
$("#errorMessage").html(
"Sorry, location Not Found! <br> <i class='far fa-frown-open'></i>"
);
});
}
});