-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
158 lines (139 loc) · 5.31 KB
/
main.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
// Query Selectors
var titleInput = document.querySelector('#titleInput');
var bodyInput = document.querySelector('#bodyInput');
var saveButton = document.querySelector('.save-button');
var outputContainer = document.querySelector('.output-container');
var deleteIcon = document.querySelector('#deleteIcon');
var toggleStarredIdeasButton = document.querySelector('.show-starred-button');
var searchInput = document.querySelector('#searchInput');
// Data Model Variables
var ideas = [];
// Event Listeners
saveButton.addEventListener('click', function(event){
event.preventDefault();
if (titleInput.value !== "" && bodyInput.value !== "") {
saveIdea();
updateCardOutput();
clearInputs();
} else {
inputWarning()
setTimeout(clearInputs, 2000)
}
}
);
outputContainer.addEventListener('click', function(){
if (event.target.className.includes("delete")) {
deleteIdea();
updateCardOutput();
} else if (event.target.className.includes("star")) {
toggleStar();
updateCardOutput();
}
}
);
toggleStarredIdeasButton.addEventListener('click', function(){
if (toggleStarredIdeasButton.innerText === "Show Starred Ideas") {
showStarred();
} else if (toggleStarredIdeasButton.innerText === "Show All Ideas") {
toggleStarredIdeasButton.innerText = "Show Starred Ideas";
updateCardOutput();
}
});
searchInput.addEventListener('keyup', function(){
outputContainer.innerHTML = "";
filterIdeas();
});
// Functions
function saveIdea() {
var idea = new Idea(titleInput.value, bodyInput.value);
ideas.push(idea);
};
function clearInputs() {
titleInput.value = "";
bodyInput.value = "";
};
function deleteIdea() {
for (i = 0; i < ideas.length; i++) {
if (ideas[i].id == event.target.parentNode.id) {
ideas.splice(i, 1);
}
}
};
function toggleStar() {
for (i = 0; i < ideas.length; i++) {
if (ideas[i].id == event.target.parentNode.id) {
ideas[i].updateIdea()
}
}
};
function updateCardOutput(){
outputContainer.innerHTML = ""
for (i = 0; i < ideas.length; i++) {
var starred = ideas[i].star ? "assets/star-active.svg" : "assets/star.svg";
outputContainer.innerHTML +=
`<article class="card-container">
<div class="container-top-nav" id="${ideas[i].id}">
<img class="star-icon icons" src="${starred}" alt="favorite idea"></img>
<img class="delete-icon icons" src="assets/delete.svg" alt="delete idea"></img>
</div>
<div class="idea-container">
<p class="title-output">${ideas[i].title}</p>
<p class="body-output">${ideas[i].body}</p>
</div>
<div class="container-bottom-nav">
<img class="plus-icon icons" src="assets/comment.svg" alt="add comment"></img>
<p class="comment">Comment</p>
</div>
</article>`
}
};
function showStarred() {
toggleStarredIdeasButton.innerText = "Show All Ideas";
outputContainer.innerHTML = "";
for (i = 0; i < ideas.length; i++) {
if (ideas[i].star === true) {
var starred = ideas[i].star ? "assets/star-active.svg" : "assets/star.svg";
outputContainer.innerHTML +=
`<article class="card-container">
<div class="container-top-nav" id="${ideas[i].id}">
<img class="star-icon icons" src="${starred}" alt="favorite idea"></img>
<img class="delete-icon icons" src="assets/delete.svg" alt="delete idea"></img>
</div>
<div class="idea-container">
<p class="title-output">${ideas[i].title}</p>
<p class="body-output">${ideas[i].body}</p>
</div>
<div class="container-bottom-nav">
<img class="plus-icon icons" src="assets/comment.svg" alt="add comment"></img>
<p class="comment">Comment</p>
</div>
</article>`
}
}
};
function inputWarning(){
titleInput.value = "Please enter a title for your idea!"
bodyInput.value = "Please enter your idea text!"
};
function filterIdeas() {
for (i = 0; i < ideas.length; i++) {
if (ideas[i].title.includes(searchInput.value) || ideas[i].body.includes(searchInput.value)) {
var starred = ideas[i].star ? "assets/star-active.svg" : "assets/star.svg";
outputContainer.innerHTML +=
`<article class="card-container">
<div class="container-top-nav" id="${ideas[i].id}">
<img class="star-icon icons" src="${starred}" alt="favorite idea"></img>
<img class="delete-icon icons" src="assets/delete.svg" alt="delete idea"></img>
</div>
<div class="idea-container">
<p class="title-output">${ideas[i].title}</p>
<p class="body-output">${ideas[i].body}</p>
</div>
<div class="container-bottom-nav">
<img class="plus-icon icons" src="assets/comment.svg" alt="add comment"></img>
<p class="comment">Comment</p>
</div>
</article>`
}
}
};