-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
131 lines (112 loc) · 3.97 KB
/
app.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
const container = document.querySelector('.container');
const list = document.getElementById("links-table-body");
const linksForm = document.getElementById('links-form');
const topic = document.getElementById('topic');
const slink = document.getElementById('slink');
const contentType = document.getElementById('content-type');
// StudyLink Class: Represents a study link
class StudyLink {
constructor(topic, study_link, study_type) {
this.topic = topic;
this.study_link = study_link;
this.study_type = study_type;
}
}
// UI Class: Handle UI Tasks
class UI {
static displayLinks() {
const links = LocalStore.getLinks();
links.forEach(link => UI.addLinkToList(link));
}
static addLinkToList(link) {
// Add row to table for each link
const row = document.createElement('tr');
row.innerHTML = `
<td>${link.topic}</td>
<td><a href="${link.study_link}" target="_blank">${link.study_link}</a></td>
<td>${link.study_type}</td>
<td><button class="btn btn-danger btn-sm delete">X</button></td>
`;
list.appendChild(row);
}
static clearFormFields() {
topic.value = "";
slink.value = "";
}
static showAlert(message, className) {
const alertDiv = document.createElement('div');
alertDiv.className = `alert alert-${className}`;
alertDiv.appendChild(document.createTextNode(message));
container.insertBefore(alertDiv, linksForm);
// Remove alert after 3 seconds
setTimeout(() => {
document.querySelector('.alert').remove();
}, 3000);
}
static removeLink(e) {
// We want to delete "tr". Button's parent: "td". td's parent: "tr"
if (e.target.classList.contains('delete')) {
e.target.parentElement.parentElement.remove();
// Show removed alert
UI.showAlert('Link removed successfully!', 'warning');
// Remove from LocalStorage
const targetHref = e.target.parentElement.parentElement.children[1].firstChild.href;
console.log('calling LS', targetHref);
LocalStore.removeLink(targetHref);
}
}
}
// Store Class: Handles LocalStorage
class LocalStore {
static getLinks() {
let links;
if (localStorage.getItem('links') === null) {
links = [];
} else {
links = JSON.parse(localStorage.getItem('links'));
}
return links;
}
static addLink(link) {
const links = LocalStore.getLinks();
links.push(link);
localStorage.setItem('links', JSON.stringify(links));
}
static removeLink(targetHref) {
const links = LocalStore.getLinks();
links.forEach((link, index) => {
if (link.study_link === targetHref) {
links.splice(index, 1);
} else {
if (link.study_link.endsWith('/')) {
if (link.study_link === targetHref)
links.splice(index, 1);
} else {
link.study_link += "/";
if (link.study_link === targetHref)
links.splice(index, 1);
}
}
});
// links.filter not working.
localStorage.setItem('links', JSON.stringify(links));
}
}
// Event: Display Links
document.addEventListener('DOMContentLoaded', UI.displayLinks);
// Event: Add a Link
linksForm.onsubmit = (e) => {
// Prevent submit to server
e.preventDefault();
// Get form values to instantiate new StudyLink object
const newLink = new StudyLink(topic.value, slink.value, contentType.value);
UI.addLinkToList(newLink);
// Add link to LocalStorage
LocalStore.addLink(newLink);
// Show success alert
UI.showAlert('New link added successfully!', 'primary');
// Clear text fields
UI.clearFormFields();
}
// Event: Remove a Link
list.addEventListener('click', UI.removeLink);