-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbookmark-manager-app.js
More file actions
161 lines (126 loc) · 4.65 KB
/
Copy pathbookmark-manager-app.js
File metadata and controls
161 lines (126 loc) · 4.65 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const mainSection = document.getElementById("main-section");
const formSection = document.getElementById("form-section");
const bookmarkListSection = document.getElementById("bookmark-list-section");
const categoryDropdown = document.getElementById("category-dropdown");
const categoryNameElements = document.querySelectorAll(".category-name");
const categoryList = document.getElementById("category-list");
const nameInput = document.getElementById("name");
const urlInput = document.getElementById("url");
const addBookmarkButton = document.getElementById("add-bookmark-button");
const closeFormButton = document.getElementById("close-form-button");
const addBookmarkButtonForm = document.getElementById("add-bookmark-button-form");
const viewCategoryButton = document.getElementById("view-category-button");
const closeListButton = document.getElementById("close-list-button");
const deleteBookmarkButton = document.getElementById("delete-bookmark-button");
function getBookmarks(){
try{
let array = JSON.parse(localStorage.getItem("bookmarks"));
let validArr;
array.forEach((bookmark)=>{
let hasName = Object.hasOwn(bookmark,"name");
let hasCat = Object.hasOwn(bookmark,"category");
let hasURL = Object.hasOwn(bookmark,"url");
if (hasName && hasCat && hasURL){
validArr = true;
}
else{validArr = false}
})
return validArr ? array : []
}catch(error){return []}
}
const displayOrCloseForm = () => {
mainSection.classList.toggle("hidden");
formSection.classList.toggle("hidden");
};
const displayOrHideCategory = () => {
mainSection.classList.toggle("hidden");
bookmarkListSection.classList.toggle("hidden");
};
addBookmarkButton.addEventListener("click", () => {
const selectedCategory = categoryDropdown.value;
categoryNameElements.forEach((el) => {
el.innerText = selectedCategory;
});
displayOrCloseForm();
});
closeFormButton.addEventListener("click", () => {
displayOrCloseForm();
});
addBookmarkButtonForm.addEventListener("click", (e) => {
e.preventDefault();
const name = nameInput.value;
const category = categoryDropdown.value;
const url = urlInput.value;
const bookmarks = getBookmarks();
bookmarks.push({ name, category, url });
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
nameInput.value = "";
urlInput.value = "";
displayOrCloseForm();
});
viewCategoryButton.addEventListener("click", () => {
const selectedCategory = categoryDropdown.value;
categoryNameElements.forEach((el) => {
el.innerText = selectedCategory;
});
const bookmarks = getBookmarks();
const filteredBookmarks = bookmarks.filter((b) => b.category === selectedCategory);
if (filteredBookmarks.length === 0) {
categoryList.innerHTML = `<p>No Bookmarks Found</p>`;
} else {
categoryList.innerHTML = "";
filteredBookmarks.forEach((b) => {
const radio = document.createElement("input");
radio.type = "radio";
radio.id = b.name;
radio.name = "bookmark-radio";
radio.value = b.name;
const label = document.createElement("label");
label.setAttribute("for", b.name);
const a = document.createElement("a");
a.href = b.url;
a.innerText = b.name;
a.target = "_blank";
label.appendChild(a);
categoryList.appendChild(radio);
categoryList.appendChild(label);
});
}
displayOrHideCategory();
});
closeListButton.addEventListener("click", () => {
displayOrHideCategory();
});
deleteBookmarkButton.addEventListener("click", () => {
const selectedRadio = document.querySelector('input[name="bookmark-radio"]:checked');
if (!selectedRadio) return;
const bookmarkName = selectedRadio.value;
const selectedCategory = categoryDropdown.value;
let bookmarks = getBookmarks();
bookmarks = bookmarks.filter(
(b) => !(b.name === bookmarkName && b.category === selectedCategory)
);
localStorage.setItem("bookmarks", JSON.stringify(bookmarks));
const filteredBookmarks = bookmarks.filter((b) => b.category === selectedCategory);
if (filteredBookmarks.length === 0) {
categoryList.innerHTML = `<p>No Bookmarks Found</p>`;
} else {
categoryList.innerHTML = "";
filteredBookmarks.forEach((b) => {
const radio = document.createElement("input");
radio.type = "radio";
radio.id = b.name;
radio.name = "bookmark-radio";
radio.value = b.name;
const label = document.createElement("label");
label.setAttribute("for", b.name);
const a = document.createElement("a");
a.href = b.url;
a.innerText = b.name;
a.target = "_blank";
label.appendChild(a);
categoryList.appendChild(radio);
categoryList.appendChild(label);
});
}
});