-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
91 lines (75 loc) · 2.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
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.contents;
}
const newLi = document.createElement("li");
const newSpan = document.createElement("span");
const newBtn = document.createElement("button");
newBtn.addEventListener("click", () => {
newLi.classList.toggle("complete");
saveItmesFn();
});
newLi.addEventListener("dblclick", () => {
newLi.remove();
saveItmesFn();
});
if (storageData && storageData.complete) {
newLi.classList.add("complete");
}
newSpan.textContent = todoContents;
newLi.appendChild(newBtn);
newLi.appendChild(newSpan);
todoList.appendChild(newLi);
todoInput.value = "";
seveItemsFn();
};
function keyCodeCheck(event) {
if (event.key === "Enter" && todoInput.value.trim() != "") {
createTodo();
}
}
document.querySelector("#todo-input").addEventListener("keydown", keyCodeCheck);
const deleteAll = function () {
const liList = document.querySelectorAll("li");
for (let i = 0; i < liList.length; i++) {
liList[i].remove();
}
};
const saveItmesFn = function () {
const saveItmes = [];
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"),
};
saveItmes.push(todoObj);
}
saveItmes.length === 0
? localStorage.removeItem("saved-items")
: localStorage.setItem("saved-itmes", JSON.stringify(saveItmes));
};
if (savedTodolist) {
for (let i = 0; i < savedTodolist.length; i++) {
createTodo(savedTodolist[i]);
}
}
const weatherSearch = function (position) {
const openweatherRes = fetch(
"https://api.openweathermap.org/data/2.5/onecall?lat=${position.latitude}&lon=${position.longtitude}&appid={APIkey}"
);
};
const accessToGeo = function (position) {
const positionObj = {
latitude: position.coords.latitude,
longtitude: position.coords.longtitude,
};
weatherSearch(positionObj);
};
const askForLocation = function () {
navigator.geolocation.getCurrentPosition(accessToGeo, (err) => {});
};
// askForLocation();