-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
110 lines (99 loc) · 3.23 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
const modal = document.getElementById('modal');
const modalShow = document.getElementById('add-bookmark');
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');
websiteNameEl.focus();
}
// Modal Event Listeners
modalShow.addEventListener('click', showModal);
modalClose.addEventListener('click', () => modal.classList.remove('show-modal'));
bookmarkForm.addEventListener('submit', () => 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 = /[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-z]{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 a valid URL');
return false;
}
// Valid
return true;
}
// Build Bookmarks DOM
function buildBookmarks() {
// Remove all bookmark elements
bookmarksContainer.textContent = '';
// Build items
bookmarks.forEach((bookmark) => {
const { name, url } = bookmark;
// Create Item
const item = `
<div class="item">
<i class="fas fa-times" id="delete-bookmark" title="Delete Bookmark" onclick="deleteBookmark('${url}')"></i>
<div class="name">
<img src="https://s2.googleusercontent.com/s2/favicons?domain=${url}" alt="Favicon">
<a href="${url}" target="_blank">${name}</a>
</div>
</div>
`;
// Insert to bookmarks container
bookmarksContainer.insertAdjacentHTML("beforeend", item);
});
}
// Fetch Bookmarks
function fetchBookmarks() {
// Get bookmarks from localStorage if available
if(localStorage.getItem('bookmarks')) {
bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
buildBookmarks();
}
}
// Delete Bookmark
function deleteBookmark(url) {
console.log('delete');
bookmarks.forEach((bookmark, i) => {
if (bookmark.url === url) {
bookmarks.splice(i, 1);
}
});
// Update bookmarks array in localStroage, re-populate DOM
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
}
// Handle Data from Form
function storeBookmark(e) {
e.preventDefault();
const nameValue = websiteNameEl.value ;
let urlValue = websiteUrlEl.value ;
if (!urlValue.includes('http://', 'https://')) {
urlValue = `https://${urlValue}`;
}
if(!validate(nameValue, urlValue)) {
return false;
}
const bookmark = {
name: nameValue,
url: urlValue,
};
bookmarks.push(bookmark);
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
bookmarkForm.reset();
websiteNameEl.focus();
}
// Event Listener
bookmarkForm.addEventListener('submit', storeBookmark);
// On Load, Fetch Bookmarks
fetchBookmarks();