-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
142 lines (126 loc) · 4.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
const modal = document.getElementById('modal');
const modalShow = document.getElementById('show-modal');
const modalClose = document.getElementById('close-modal');
const bookmarkForm = document.getElementById('bookmark-form');
const websiteNameEl = document.getElementById('website-name');
const websiteUrlEl = document.getElementById('website-url');
const bookmarksContainer = document.getElementById('bookmarks-container');
let bookmarks = [];
// show modal, focus on input
function showModal() {
modal.classList.add('show-modal');
websiteUrlEl.focus();
}
// modal event listners
modalShow.addEventListener('click', showModal);
modalClose.addEventListener('click', () => modal.classList.remove('show-modal'));
window.addEventListener('click', (e) => (e.target === modal ? modal.classList.remove('show-modal') : false));
// validate form
function validate(nameValue, urlValue) {
const expression = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g;
const regex = new RegExp(expression);
if (!nameValue || !urlValue) {
alert('please submit values for both fields');
return false;
}
if (!urlValue.match(regex)) {
alert('Please provide valid web address');
return false;
}
// valid info
return true;
}
// build bookmarks DOM
function buildBookmarks() {
// remove bookmarks DOM
bookmarksContainer.textContent = '';
// build items
bookmarks.forEach((bookmark) => {
// method below is called destructuring
const { name, url } = bookmark;
// console.log(name, url);
// item
const item = document.createElement('div');
item.classList.add('item');
// close icon
const closeIcon = document.createElement('i');
closeIcon.classList.add('fas', 'fa-times');
closeIcon.setAttribute('title', 'Delete Bookmark');
closeIcon.setAttribute('onclick', `deleteBookmark('${url}')`);
// favicon / link container
const linkInfo = document.createElement('div');
linkInfo.classList.add('name');
// favicon
const favicon = document.createElement('img');
favicon.setAttribute('src', `https://s2.googleusercontent.com/s2/favicons?domain=${url}`);
favicon.setAttribute('alt', 'Favicon');
// link
const link = document.createElement('a');
link.setAttribute('href', `${url}`);
link.setAttribute('target', '_blank');
link.textContent = name;
// append to bookmarks container
linkInfo.append(favicon, link);
item.append(closeIcon, linkInfo);
bookmarksContainer.appendChild(item);
});
}
// fetch bookmarks
function fetchBookmarks() {
// get bookmarks if available
if (localStorage.getItem('bookmarks')){
bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
} else {
// create bookmarks array in localstorage
bookmarks = [
{
name: 'Dwight Mckenzie',
url: 'https://dwightmckenzie.com'
}
];
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
// console.log(bookmarks);
buildBookmarks();
}
// delete a bookmark
function deleteBookmark(url) {
console.log('delete url', url);
bookmarks.forEach((bookmark, i) => {
if (bookmark.url === url) {
bookmarks.splice(i, 1);
}
});
// update bookmarks array in local storage, re-populate DOM
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
}
// handle data from form
function storeBookmark(e) {
e.preventDefault(); // to prevent form from sending form or calling request
const nameValue = websiteNameEl.value;
let urlValue = websiteUrlEl.value;
if (!urlValue.includes('http://') && !urlValue.includes('https://')) {
urlValue = `https://${urlValue}`;
console.log(nameValue, urlValue);
}
// console.log(nameValue, urlValue);
if (!validate(nameValue, urlValue)) {
// alert('please submit values for both fields');
return false;
}
const bookmark = {
name: nameValue,
url: urlValue
};
bookmarks.push(bookmark);
// console.log(JSON.stringify(bookmarks));
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
bookmarkForm.reset();
websiteNameEl.focus();
}
// event listenrs
bookmarkForm.addEventListener('submit', storeBookmark);
// on load
fetchBookmarks();