-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
147 lines (119 loc) · 4.46 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//Link Class: Represents the Link
class List{
constructor(description, link, title){
this.description = description;
this.link = link;
this.title = title;
}
}
//Storage Class : To work with Local storage
class Storage{
//extracting the data from the local storage
static getData(){
let items;
if(localStorage.getItem("items") === null)
items = [];
else
items = JSON.parse(localStorage.getItem("items"));
return items;
}
static addItem(item){
let items = Storage.getData();
items.push(item);
localStorage.setItem("items", JSON.stringify(items));
}
static removeItem(title){
let items = Storage.getData();
items.forEach((item, index)=>{
if(item.title === title){
console.log("in Storage remove");
items.splice(index, 1);
}
});
localStorage.setItem("items", JSON.stringify(items));
}
}
//UI Class : To handle the UI components of the website
class UI{
static displayList(){
const lists = Storage.getData();
lists.forEach((list) => UI.addItemToList(list));
}
static addItemToList(item){
console.log(item);
const list = document.querySelector("#linkList");
const row = document.createElement('tr');
row.classList.add('center-align');
row.innerHTML = `
<td class="left-align">
<ul class="collapsible">
<li>
<div class="collapsible-header" id="title">${item.title}</div>
<div class="collapsible-body">
<span id="link"><b>Link : </b><a href="${item.link}" target="_blank">${item.link}</a></span><br><br>
<span id="description"><b>Description : </b> ${item.description}</span>
</div>
</li>
</ul>
</td>
<td class="center-align"><a href="#" class="small material-icons red black-text delete">delete_forever</a></td>
`;
list.appendChild(row);
// document.querySelectorAll('collapsible').collapsible();
}
static removeItemFromList(e){
if(e.classList.contains('delete')){
// console.log(e.parentElement.parentElement);
e.parentElement.parentElement.remove();
UI.showAlert("Item Deleted!!", "red");
}
}
static insertAfter(newNode, existingNode) {
existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
}
static showAlert(message, color){
const div = document.createElement('div');
div.classList.add("row", "alert");
div.innerHTML = `<div class="col s8 offset-s2">
<div class="row card-panel ${color} accent-4 center-align">${message}</div>
</div>
`;
const appendelment = document.querySelector('#heading');
UI.insertAfter(div, appendelment);
setTimeout(()=>document.querySelector(".alert").remove(), 3000);
}
static clearFields(){
document.querySelector("#title").value = "";
document.querySelector("#link").value = "";
document.querySelector("#description").value = "";
}
}
//Event : Display books on load
document.addEventListener('DOMContentLoaded', UI.displayList());
//Event : Add books to the list
document.querySelector(".insertForm").addEventListener('submit', e=>{
e.preventDefault();
const title = document.querySelector("#title").value;
const link = document.querySelector("#link").value;
const description = document.querySelector("#description").value;
if(title==="" || link==="" || description===""){
UI.showAlert("Please fill all the fields!!", "amber");
}
else{
//TODO: add validation for unique titles
const item = new List(description, link, title);
//item is added to the UI
UI.addItemToList(item);
//item is added to the local storage
Storage.addItem(item);
UI.clearFields();
UI.showAlert("Succesfully added!!", "green");
}
});
//Event : Delete books from the list
document.querySelector("#linkList").addEventListener('click', e =>{
let targetValue = e.target.parentElement.previousSibling.previousSibling.children[0].children[0].children[0].textContent;
UI.removeItemFromList(e.target);
Storage.removeItem(targetValue);
console.log(targetValue);
});