-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
203 lines (189 loc) · 5.83 KB
/
script.js
File metadata and controls
203 lines (189 loc) · 5.83 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
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');
const alertContainer = document.getElementById('alert-container');
const alertIcon = document.getElementById('alert-icon');
let message = document.getElementById('alert-message');
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');
clearAlertMessage();
});
window.addEventListener('click', (e) => {
if (e.target === modal) {
modal.classList.remove('show-modal');
clearAlertMessage();
}
})
// Validate URL
function validateUrl(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 (!urlValue.match(regex)) {
showDangerAlert('Please provide a valid web address.');
bookmarkForm[1].style.borderColor = "coral";
return false;
}
return true;
}
// Validate Form
function validate(nameValue, urlValue) {
clearAlertMessage();
if (!nameValue || !urlValue) {
showDangerAlert('Please submit values for both fields.');
bookmarkForm[0].style.borderColor = "coral";
validateUrl(urlValue)
return false;
}
if (!validateUrl(urlValue)) {
return false;
}
// Valid
return true;
}
// Build Bookmarks DOM
function buildBookmarks() {
// Remove all bookmark elements
bookmarksContainer.textContent = '';
// Build items
Object.keys(bookmarks).forEach((id) => {
const {name, url} = bookmarks[id];
// Item
const item = document.createElement('div');
item.classList.add('item', 'link');
// Close icon
const closeIcon = document.createElement('i');
closeIcon.classList.add('fas', 'fa-times', 'delete-icon');
closeIcon.setAttribute('title', 'Delete Bookmark');
closeIcon.setAttribute('onclick', `deleteBookmark('${id}')`);
// Favicon / Link Container
const linkInfo = document.createElement('div');
linkInfo.classList.add('name');
// Favicon
const favicon = document.createElement('img');
favicon.setAttribute('src', `https://www.google.com/s2/u/0/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(linkInfo, closeIcon);
bookmarksContainer.appendChild(item);
});
}
// Fetch Bookmarks
function fetchBookmarks() {
// Get bookmarks from localStorage if available
if (localStorage.getItem('bookmarks')) {
bookmarks = JSON.parse(localStorage.getItem('bookmarks'));
} else {
// Create bookmarks object in localStorage
bookmarks = {
'https://yahoo.com/': {
name: 'yahoo',
url: 'https://yahoo.com/'
},
'https://google.com/': {
name: 'google',
url: 'https://google.com/'
},
'https://youtube.com/': {
name: 'youtube',
url: 'https://youtube.com/'
}
};
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
}
buildBookmarks();
}
// Delete Bookmark
function deleteBookmark(id) {
if (bookmarks[id]) {
delete bookmarks[id];
}
// Update bookmarks object in localStorage, re-populate DOM
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
}
// Show Danger Alert
function showDangerAlert(messageText) {
message.textContent = messageText;
alertContainer.classList.add('alert-container', 'alert-danger');
alertContainer.hidden = false;
alertIcon.classList.add('fas', 'fa-exclamation');
}
// Show Success Alert
function showSuccessAlert(messageText) {
message.textContent = messageText;
alertContainer.classList.add('alert-container', 'alert-success');
alertContainer.hidden = false;
alertIcon.classList.add('fas', 'fa-bookmark');
}
// Clear Alert Message
function clearAlertMessage() {
message.textContent = '';
alertContainer.className = '';
alertContainer.hidden = true;
alertIcon.className = '';
bookmarkForm[0].style.borderColor = "";
bookmarkForm[1].style.borderColor = "";
}
// Alert Event Listener
window.addEventListener('click', (e) => {
if (e.target === alertContainer) {
clearAlertMessage();
}
})
// Handle Data from Form
function storeBookmark(e) {
e.preventDefault();
clearAlertMessage();
const nameValue = websiteNameEl.value;
let urlValue = websiteUrlEl.value;
// Add 'https://' if not there
if (!urlValue.includes('https://', 'http://')) {
urlValue = `https://${urlValue}`;
}
let isLastSlash = urlValue.charAt(urlValue.length-1) === '/' ? true : false;
if(!isLastSlash) {
urlValue += '/';
}
// Validate
if (!validate(nameValue, urlValue)) {
return false;
}
// Set bookmark object
const bookmark = {
name: nameValue,
url: urlValue,
};
if (bookmarks[bookmark.url]) {
showSuccessAlert('Bookmark updated!');
} else {
showSuccessAlert('Bookmark added!');
}
bookmarks[urlValue] = bookmark;
// Set bookmarks in localStorage, fetch, reset input fields
localStorage.setItem('bookmarks', JSON.stringify(bookmarks));
fetchBookmarks();
bookmarkForm.reset();
websiteNameEl.focus();
}
// Event Listener
bookmarkForm.addEventListener('submit', storeBookmark);
// On Load, Fetch Bookmarks
fetchBookmarks();