-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (126 loc) · 5.04 KB
/
Copy pathscript.js
File metadata and controls
143 lines (126 loc) · 5.04 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
137
138
139
140
141
142
143
const city = document.getElementById("city");
const currTemp = document.getElementById("curr-temp");
const icon = document.getElementById("icon");
const searchbox = document.querySelector(".search-box");
const searchboxMobile = document.querySelector("#search-box-mobile");
const description = document.getElementById("description")
const feelsLike = document.getElementById("feels-like");
const sunrise = document.getElementById("sunrise");
const sunset = document.getElementById("sunset");
const maxTemp = document.getElementById("max-temp");
const minTemp = document.getElementById("min-temp");
const humidity = document.getElementById("humidity-data");
const uvi = document.getElementById("uvi-data");
const alerts = document.getElementById("alert-data");
const currLocationbtn = document.getElementById("curr-location");
const mainDesc = document.getElementById("main-desc");
//autocomplete
autocomplete('search-auto-mobile','search-box-mobile',(listItemTextContent)=>{
document.querySelector("#"+searchboxMobile.id).value = listItemTextContent;
searchEvent(searchboxMobile.id);
});
autocomplete('search-auto','search-box-pc',(listItemTextContent)=>{
document.querySelector("#"+searchbox.id).value = listItemTextContent;
searchEvent(searchbox.id);
});
//render function
function getTime(unixTime){
date = new Date(unixTime);
return date.toLocaleTimeString('en-IN').toUpperCase();
}
function render(data){
city.innerHTML = data["name"];
currTemp.innerHTML = data["main"]["temp"].toPrecision(3)+ "°c";
iconCode = data["weather"][0]["icon"];
icon.src = `http://openweathermap.org/img/wn/${iconCode}@2x.png`;
description.innerHTML = data["weather"][0]["description"][0].toUpperCase()+data["weather"][0]["description"].substr(1);
maxTemp.innerHTML = data["main"]["temp_max"].toPrecision(3)+ "°c";
minTemp.innerHTML = data["main"]["temp_min"].toPrecision(3)+ "°c";
sunrise.innerHTML = getTime(data["sys"]["sunrise"]*1000);
sunset.innerHTML = getTime(data["sys"]["sunset"]*1000);
feelsLike.innerHTML = data["main"]["feels_like"].toPrecision(3) + "°c";
humidity.innerHTML = data["main"]["humidity"] + "%";
mainDesc.innerHTML = data["weather"][0]["main"];
setHumidityStat(data["main"]["humidity"]);
setVaiOneCallApi(data["coord"]["lat"],data["coord"]["lon"]);
}
//circular stats
function setHumidityStat(humidity){
let humidityStat = document.getElementById("humidity-stat");
humidityStat.style.strokeDashoffset = Math.abs((1-(humidity/100))*565);
}
function setVaiOneCallApi(lat,lon){
let URL = `https://api.openweathermap.org/data/2.5/onecall?lat=${lat}&lon=${lon}&exclude=hourly,daily&appid=${API_KEY}&units=metric`;
fetch(URL)
.then(res => {
res.json()
.then(data => {
let uviData = data["current"]["uvi"];
uvi.innerHTML = Math.round(uviData);
let uviStat = document.getElementById("uvi-stat");
uviStat.style.strokeDashoffset = Math.abs(((10-uviData)/10)*565);
if(data.hasOwnProperty("alerts"))
alerts.innerHTML = data["alerts"][0]["description"];
else
alerts.innerHTML = "No alert";
})
})
.catch(error => console.log(error()))
}
//current location button
currLocationbtn.addEventListener('click',renderByCurrentlaoction);
//on load asked for geolocation
function renderByCurrentlaoction(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition((position)=>{
const lat = position.coords.latitude;
const lon = position.coords.longitude;
let url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_KEY}&units=metric`;
fetch(url)
.then((res)=>{
if(res.ok){
res.json()
.then((data)=>{
render(data);
})
}
}).catch((error)=> console.log(error))
})
}
}
window.addEventListener('load',renderByCurrentlaoction);
//on search
searchbox.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
searchEvent(searchbox.id)
}
});
searchboxMobile.addEventListener("keyup", function(event) {
if (event.keyCode === 13) {
searchEvent(searchboxMobile.id)
}
});
function searchEvent(id){
let cityName = document.querySelector("#"+id).value;
const URL = `https://api.openweathermap.org/data/2.5/weather?q=${cityName}&appid=${API_KEY}&units=metric`;
if(cityName==undefined||cityName==null||cityName==="")
return
fetch(URL)
.then((res)=>{
if(res.ok){
res.json()
.then((data)=>{
render(data);
})
}
else{
alert("Wrong city name")
}
})
.catch((error)=>console.log(error));
}
//circular stats
function setHumidityStat(humidity){
let humidityStat = document.getElementById("humidity-stat");
humidityStat.style.strokeDashoffset = Math.abs((1-(humidity/100))*565);
}