-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
176 lines (135 loc) · 3.39 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
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
const linkCategory = document.querySelector("#linkCategory");
const submitButton = document.querySelector("#submitButton");
const addBtn = document.querySelector("#addBtn");
const cancelButton = document.querySelector("#cancelButton");
const addLinkPanel = document.querySelector("#addLinkPanel");
const addedCategories = document.querySelector("#addedCategories");
let editIndex = -1;
let linkCategories = [];
let links = [
{
title: 'New Link 1',
url: 'url1.com',
categories: ['node', 'angular']
},
{
title: 'New Link 2',
url: 'url2.com',
categories: ['js', 'angular']
},
{
title: 'New Link 3',
url: 'url3.com',
categories: ['node', 'bootstrap']
}
];
displayLinks();
addBtn.addEventListener('click', (event) => {
console.log("Add btn clicked");
showFormPanel();
});
cancelButton.addEventListener('click', (event) => {
event.preventDefault();
console.log("Cancel button clicked");
hideFormPanel();
});
function showFormPanel() {
addLinkPanel.classList.remove('hidden');
displayLinkCategories();
}
function hideFormPanel() {
addLinkPanel.classList.add('hidden');
clearLinkForm();
}
linkCategory.addEventListener('keydown', function (event) {
if (event.keyCode === 188) {
event.preventDefault();
linkCategories.push(linkCategory.value);
linkCategory.value = '';
//Display the updated categories
displayLinkCategories();
}
})
function displayLinkCategories() {
console.log("Displaying Link Categories");
addedCategories.innerHTML = '';
for (let category of linkCategories) {
var categoryHTMLString = `<span class="category">${category}</span>`;
addedCategories.innerHTML += categoryHTMLString;
}
}
function clearLinkForm() {
linkTitle.value = '';
linkUrl.value = '';
linkCategory.value = '';
linkCategories = [];
addedCategories.innerHTML = '';
}
submitButton.addEventListener('click', (event) => {
//Stop form from submitting
event.preventDefault();
const title = linkTitle.value;
const url = linkUrl.value;
const categories = linkCategories;
const newLink = {
title,
url,
categories
}
if (editIndex === -1) {
//push new link to array
links.unshift(newLink);
}
else {
links[editIndex] = newLink;
editIndex = -1;
}
clearLinkForm();
displayLinkCategories();
//hide the addLinkPanel form
hideFormPanel();
displayLinks();
});
function displayLinks() {
linksList.innerHTML = '';
let index = 0;
for (let link of links) {
let linkHTMLString = `
<div class="flex-item">
<div class="link panel">
<div class="link-options">
<button class="btn-sm" onclick="deleteLink(${index})">Delete</button>
<button class="btn-sm" onclick="editLink(${index})">Edit</button>
</div>
<a href="${link.url}">
<h1 class="header">${link.title}</h1>
</a>
<p class="link-date">${Date.now()}</p>
<div class="categories">
Categories:`;
for (let category of link.categories) {
linkHTMLString += `<span class="category">${category}</span>`;
}
linkHTMLString += `
</div>
</div>
</div>
`
;
linksList.innerHTML += linkHTMLString;
index++;
}
}
function deleteLink(index) {
console.log("Deleting link at index", index);
links.splice(index, 1);
displayLinks();
}
function editLink(index) {
console.log("Editing link at index", index);
editIndex = index;
linkTitle.value = links[index].title;
linkUrl.value = links[index].url;
linkCategories = links[index].categories;
showFormPanel();
}