-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
136 lines (123 loc) · 4.61 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
const addBtn = document.getElementById('add-bookmark');
const addForm = document.getElementById('add-bookmark-form');
const formBtn = document.getElementById('add-bookmark-btn');
const popUpWrapper = document.getElementById('pop-up-wrapper');
const overlay = document.getElementById('overlay');
const bookmarksSection = document.getElementById('bookmarks-section');
const permBookmark = document.getElementById('permBookmark');
const closeFormBtn = document.getElementById('closeFormBtn');
// //Hide Add Form
function hideAddForm() {
popUpWrapper.hidden = "true";
}
// Show Add Form
function showAddForm() {
popUpWrapper.removeAttribute('hidden');
}
//CreatefaviconImgSrc
function createfaviconImgsrc(l) {
return `${l.slice(0, (l.indexOf('.com') + 4))}/favicon.ico`;
}
//Create New Bookmark
function createBookmark(id, bookmarkURL, bookmarkNickname) {
//Create bookmark div
let newBookmark = document.createElement("div");
//Create favicon img element
let faviconImg = document.createElement('img');
//Set src for faviconImg
faviconImg.src = createfaviconImgsrc(bookmarkURL);
//Set class for styling of faviconImg
faviconImg.classList.add('favicon');
//Append faviconIMg to newBookmark
newBookmark.appendChild(faviconImg);
//Create anchor tag
let link = document.createElement(`a`);
// Assign bookmarkURL to anchor href
link.href = `${bookmarkURL}`;
//Set bookmarkNickname to display
if ((bookmarkNickname === undefined) || !bookmarkNickname) {
link.textContent = `${bookmarkURL.slice(0, 15)}...`
link.title = `${bookmarkURL}`;
} else {
link.textContent = `${bookmarkNickname}`;
link.title = `${bookmarkURL}`;
}
//Set anchor target to new window
link.target = '_blank';
// Add anchor to new bookmark div
newBookmark.appendChild(link);
// Create remove-icon/button wrapper
let removeWrapper = document.createElement('remove-wrapper');
// Add classList for Styling
removeWrapper.classList.add('remove-wrapper');
//Create button element
let button = document.createElement('button');
//Append button to remove-wrapper
removeWrapper.appendChild(button);
//Create icon element
let icon = document.createElement('i');
//Add Font Awesome icon classes to icon
icon.classList.add('far');
icon.classList.add('fa-times-circle');
icon.classList.add('remove');
//Set id of icon to later provide for deletion
icon.id = `${id}`;
//Append icon as a child of button
button.appendChild(icon);
//Append remove-wrapper to newBookmark
newBookmark.appendChild(removeWrapper);
//Add bookmark class for styling to newBookmark
newBookmark.classList.add('bookmark');
//Append newBOokmark to bookmarks section BEFORE the permBookmark
bookmarksSection.insertBefore(newBookmark, permBookmark);
addEventListenersToRemoveBtns();
addForm.reset();
}
function formSubmitToCreateBookmark(e) {
//Prevent default execution
e.preventDefault();
//Declare Variables
let bookmarkURL = e.srcElement[1].value;
let bookmarkNickname = e.srcElement[2].value;
let object = {
url: `${bookmarkURL}`,
nickname: `${bookmarkNickname}`
}
bookmarksArray.push(object);
object.id = bookmarksArray.indexOf(object);
//Add Values to Local Storage
localStorage.setItem("bookmarks", JSON.stringify(bookmarksArray));
//
hideAddForm();
createBookmark(object.id, object.url, object.nickname);
}
//Remove Bookmark
function removeBookmark(e) {
//Remove bookmark from DOM
e.srcElement.parentElement.parentElement.parentElement.remove();
let i = Number(e.srcElement.id);
//Remove obj from local storage
bookmarksArray = bookmarksArray.filter(obj => {return obj.id !== i});
localStorage.setItem("bookmarks", JSON.stringify(bookmarksArray));
console.log(bookmarksArray);
}
// Event Listeners
addBtn.addEventListener('click', showAddForm);
addForm.addEventListener('submit', formSubmitToCreateBookmark);
closeFormBtn.addEventListener('click', hideAddForm)
function addEventListenersToRemoveBtns() {
let removeBtns = document.querySelectorAll('i.remove');
removeBtns.forEach((btn) => btn.addEventListener('click', removeBookmark));
}
//ON LOAD
addEventListenersToRemoveBtns();
let bookmarksArray = [];
//Check local storage to retrieve if values
if (localStorage.getItem("bookmarks")) {
bookmarksArray = JSON.parse(localStorage.getItem("bookmarks"));
bookmarksArray.forEach((obj) => {
obj.id = bookmarksArray.indexOf(obj);
let {url , nickname, id} = obj;
createBookmark(id, url, nickname);
});
}