-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
158 lines (115 loc) · 3.34 KB
/
script.js
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
let todoInput = document.querySelector("#todo-input");
const todoList = document.querySelector("#todo-list");
const savedWeatherData = JSON.parse(localStorage.getItem('saved-weather'));
const savedTodoList = JSON.parse(localStorage.getItem("saved-items"));
const creatTodo = function (storageData) {
let todoContents = todoInput.value;
if (storageData) {
todoContents = storageData.contents;
}
const newLi = document.createElement("li");
const newSpan = document.createElement("span");
const newBtn = document.createElement("button");
newBtn.addEventListener("click", () => {
newLi.classList.toggle("complete");
saveItemsFn();
});
newLi.addEventListener("dblclick", () => {
newLi.remove();
saveItemsFn();
});
if (storageData?.complete) {
newLi.classList.add("complete");
}
newSpan.textContent = todoContents;
newLi.appendChild(newBtn);
newLi.appendChild(newSpan);
todoList.appendChild(newLi);
todoInput.value = "";
saveItemsFn();
};
const keyCodeCheck = function () {
if (window.event.keyCode === 13 && todoInput.value.trim() !== "") {
creatTodo();
}
};
const deletAll = function () {
const liList = document.querySelectorAll("li");
for (let i = 0; i < liList.length; i++) {
liList[i].remove();
}
saveItemsFn();
};
const saveItemsFn = function () {
const saveItems = [];
for (let i = 0; i < todoList.children.length; i++) {
const todoObj = {
contents: todoList.children[i].querySelector("span").textContent,
complete: todoList.children[i].classList.contains("complete"),
};
saveItems.push(todoObj);
}
saveItems.length === 0
? localStorage.removeItem("saved-items")
: localStorage.setItem("saved-items", JSON.stringify(saveItems));
};
if (savedTodoList) {
for (let i = 0; i < savedTodoList.length; i++) {
creatTodo(savedTodoList[i]);
}
}
const weatherDataActive = function ({ location, weather }) {
const weatherMainList = [
"Clear",
"Clouds",
"Drizzle",
"Snow",
"Thunderstorm",
];
weather = weatherMainList.includes(weather) ? weather : "Fog";
const locationNameTag = document.querySelector("#location-name-tag");
locationNameTag.textContent = location;
document.body.style.backgroundImage = `url('./images/${weather}.jpg')`;
if (
!savedWeatherData ||
savedWeatherData?.location !== location ||
savedWeatherData?.weather !== weather
) {
localStorage.setItem(
'saved-weather',
JSON.stringify({ location, weather })
);
}
};
const weatherSearch = function ({ latitude, longitude }) {
fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=9ae7795f987d44a9e387cf24d276b794`
)
.then((res) => {
return res.json();
})
.then((json) => {
const weatherData = {
location: json.name,
weather: json.weather[0].main,
};
weatherDataActive(weatherData);
})
.catch((err) => {
console.log(err);
});
};
const accessToGeo = function ({ coords }) {
const { latitude, longitude } = coords;
const positionObj = {
latitude,
longitude,
};
weatherSearch(positionObj);
};
const askForLocation = function () {
navigator.geolocation.getCurrentPosition(accessToGeo, (err) => {
console.log(err);
});
};
askForLocation();