Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
130 lines (100 loc) · 4.3 KB
/
Copy pathscript.js
File metadata and controls
130 lines (100 loc) · 4.3 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
const hourElement = document.querySelector('.hour')
const minuteElement = document.querySelector('.minute')
const secondElement = document.querySelector('.second')
const timeElement = document.querySelector('.time')
const dateElement = document.querySelector('.date')
const toggle = document.querySelector('.toggle')
const icon = document.querySelector('.fas')
const form = document.querySelector('.form')
const search = document.querySelector('.search')
const clear = document.querySelector('.clear')
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
toggle.addEventListener('click', (event)=>{
const html = document.querySelector('html')
if(html.classList.contains('dark')){
html.classList.remove('dark')
changeIcon()
}
else{
html.classList.add('dark')
changeIcon()
}
})
function setTime(){
const time = new Date()
const month = time.getMonth()
const day = time.getDay()
const date = time.getDate()
const hours = time.getHours()
const hoursForClock = hours % 12
const minutes = time.getMinutes()
const seconds = time.getSeconds()
const ampm = hours>=12 ? 'PM' : 'AM'
hourElement.style.transform = `translate(-50%, -100%) rotate(${scale(hoursForClock, 0, 11, 0, 360)}deg)`
minuteElement.style.transform = `translate(-50%, -100%) rotate(${scale(minutes, 0, 60, 0, 360)}deg)`
secondElement.style.transform = `translate(-50%, -100%) rotate(${scale(seconds, 0, 60, 0, 360)}deg)`
timeElement.innerHTML = `${hoursForClock==0? 12 : hoursForClock}:${minutes<10 ?`0${minutes}` :minutes}:${seconds<10 ? `0${seconds}`:seconds }
<small class="ampm"> ${ampm} </small>`
dateElement.innerHTML = `${days[day]}, ${months[month]} <span class="circle" >${date}</span>`
// console.log('The hour hand value is ' + hours , hoursForClock)
// console.log(scale(hoursForClock, 0, 11, 0, 360), scale(minutes, 0, 60, 0, 360), scale(seconds, 0, 60, 0, 360) )
}
function changeIcon(){
if(icon.classList.contains('fa-sun')){
icon.classList.remove('fa-sun')
icon.classList.add('fa-moon')
}
else if(icon.classList.contains('fa-moon')){
icon.classList.remove('fa-moon')
icon.classList.add('fa-sun')
}
}
// https://stackoverflow.com/questions/10756313/javascript-jquery-map-a-range-of-numbers-to-another-range-of-numbers
const scale = (num, in_min, in_max, out_min, out_max) => {
return (num - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
setTime()
setInterval(setTime, 1000);
const weatherEl = document.querySelector('.weather-data')
async function getWeather(cityname){
try{
//Weather data from openWeatherMap
const api_url = "https://api.openweathermap.org/data/2.5/weather?q="+cityname+"&appid=fc1eee466732cb993168dc5094445158"
// Please dont use my API Key. It has limited number of requests.
// You can get your own API key from OpenWeatherMap. Its completely free!
const res = await axios.get(api_url)
const data = await res.data
console.log(data)
const temp = Math.floor(data.main.feels_like - 273.13) + "°C"
const desc = data.weather[0].description
const wind = data.wind.speed + " km/h"
const city = data.name
const country = data.sys.country.toUpperCase()
//Weather data fetched from OpenWeathMap injected dynamically
weatherEl.innerHTML =
`<div class="location-data">
<div class="address">
<span class="city"> ${city +","}</span>
<span class="country"> ${" " + country }</span>
</div>
<img class="flag" src="https://flagsapi.com/${country}/flat/64.png">
</div>
<div><span class="desc">${desc}, ${temp} </span></div>
<div class="wind-parent"> Wind:<span class="wind"> ${wind}</span></div>`
}
catch(err){
alert('Invalid city name! Please enter correct city name and try again.')
}
}
//Clear the search input value when clicked
clear.addEventListener('click', ()=>{
search.value =''
})
//Get input value from form when it is submitted
form.addEventListener('submit', (e) =>{
e.preventDefault()
const cityname = search.value
search.value=''
getWeather(cityname)
})