-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
147 lines (126 loc) · 3.63 KB
/
script.js
File metadata and controls
147 lines (126 loc) · 3.63 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
144
145
146
147
const savedWeatherData = JSON.parse(localStorage.getItem("saved-weather"));
const todoInput = document.querySelector("#todo-input");
const todoList = document.querySelector("#todo-list");
const savedTodoList = JSON.parse(localStorage.getItem("saved-items"));
const createTodo = function (storageData) {
let todoContents = todoInput.value;
if (storageData) {
todoContents = storageData.content;
}
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() !== "") {
createTodo();
}
};
const deleteAll = function () {
const liList = document.querySelectorAll("li");
for (let i = 0; i < liList.length; i++) {
liList[i].remove();
}
saveItemsFn();
};
const saveItemsFn = function () {
const saveItems = [];
// console.log(todoList.children[0].querySelector("span").textContent);
for (let i = 0; i < todoList.children.length; i++) {
const todoObj = {
content: 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 (saveItems.length === 0) {
// localStorage.removeItem("saved-items");
// } else {
// localStorage.setItem("saved-items", JSON.stringify(saveItems));
// }
};
const weatherDataActive = function ({ location, weather }) {
const weatherMainList = [
"clear",
"Clouds",
"Drizzle",
"Rain",
"Snow",
"Thunderstorm",
];
weather = weatherMainList.includes(weather) ? weather : "Fog";
const locationNameTag = document.querySelector("#location-name-tag");
locationNameTag.textContent = location;
document.body.style.background = `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 }) {
const openWeatherRes = fetch(
`https://api.openweathermap.org/data/2.5/weather?lat=${latitude}&lon=${longitude}&appid=37abffb4d65d043679cbd8ae0281607a`
)
.then((res) => {
return res.json();
})
.then((json) => {
const weatherData = {
location: json.name,
weather: json.weather[0].main,
};
weatherDataActive(weatherData);
})
.catch((err) => {
console.log(err);
});
};
if (savedTodoList) {
for (let i = 0; i < savedTodoList.length; i++) {
createTodo(savedTodoList[i]);
}
}
const accessToGeo = function ({ coords }) {
const { latitude, longitude } = coords;
//shorthand property
const positionObj = {
latitude,
longitude,
};
weatherSearch(positionObj);
};
const askForLocation = function () {
navigator.geolocation.getCurrentPosition(accessToGeo, (err) => {
console.log(err);
});
};
askForLocation();
if (savedWeatherData) {
weatherDataActive(savedWeatherData);
}