-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
175 lines (154 loc) · 5.63 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
//global notes array
var notes = [];
//accessing the elements
let noteText = document.querySelector('#note_text');
let noteTitle = document.querySelector('#note_title');
let formButtons = document.querySelector('.form_button');
let addBtn = document.querySelector('#addBtn');
let cancelBtn = document.querySelector('#cancelBtn');
let form = document.querySelector('#form');
let notesList = document.querySelector('#notes');
let singleNote = document.querySelector('.note');
let modalForm = document.querySelector('#form-modal');
let deleteNoteModal = document.querySelector('.modal-deleteBtn');
let modal = document.querySelector('.backdrop');
//note Input handler
noteText.addEventListener('click',event=>{
console.log("noteText",event);
noteTitle.style.display="block";
formButtons.style.display="block";
})
form.addEventListener('submit',event=>{
event.preventDefault();
let obj = {
id:notes.length+1,
noteTitle:event.target.note_title.value,
noteText:event.target.note_text.value,
color:""
}
notes.push(obj);
console.log(notes);
event.target.note_title.value = "";
event.target.note_text.value = "";
noteTitle.style.display="none";
formButtons.style.display="none";
displayNotes();
});
//note card handlers
cancelBtn.addEventListener("click",event=>{
noteTitle.style.display="none";
formButtons.style.display="none";
noteTitle.value="";
formButtons.value="";
})
document.addEventListener("click",event=>{
if(event.target && event.target.className==='deleteBtn'){
let parentNote = event.target.parentElement.parentElement.parentElement;
let id = parentNote.getAttribute('data-id');
let noteIndex = notes.findIndex(note=>note.id == id);
deleteNote(noteIndex);
}
})
document.addEventListener("click",event=>{
if(event.target && event.target.className==='editBtn'){
let parentNote = event.target.parentElement.parentElement.parentElement;
let id = parentNote.getAttribute('data-id');
let noteObject = notes.find(note=>note.id == id);
console.log("selectedNote", noteObject)
let modalContent = document.querySelector('.modal-content');
modalContent.innerHTML = fillModal(noteObject);
modal.style.display="block";
modal.childNodes[1].style.backgroundColor = noteObject.color;
}
})
function fillModal(note){
return `
<div>
<div class="${note.noteTitle} modal-note-title"><input data-id="${note.id}" type="text" placeholder="Title" value="${note.noteTitle}" /></div>
<div class="modal-note-text"><textarea onInput="this.parentNode.dataset.replicatedValue = this.value"
class="modal-textarea">${note.noteText}</textarea></div>
</div>
`
}
//modal form handlers
deleteNoteModal.addEventListener('click',event=>{
let form = event.target.parentElement.parentElement;
let noteID = form.elements[0].getAttribute('data-id');
let noteIndex = notes.findIndex(note=>note.id == noteID);
deleteNote(noteIndex);
})
document.addEventListener('submit',event=>{
if(event.target && event.target.id==='modal-form'){
event.preventDefault();
let modalForm = event.target;
let noteTitle = event.target.elements[0].value;
let noteText = event.target.elements[1].value;
let id = event.target.elements[0].getAttribute('data-id');
let formParent = modalForm.parentElement;
let color = formParent.style.backgroundColor;
let newNote = {
id,
noteTitle,
noteText,
color
}
updateNote(newNote);
modal.style.display = 'none';
displayNotes();
}
})
//note color modifier
document.addEventListener('click',event=>{
if(event.target && event.target.classList.contains('solid-color')){
console.log("inside modal colo changer", event.target);
let ele = event.target.classList;
let color = event.target.style.backgroundColor;
console.log("color:: ",color);
let modal = document.querySelector('.modal');
let element = event.target.parentElement;
while("modal-form" !== element.id){
element = element.parentElement;
}
let id = element.elements[0].getAttribute('data-id');
let noteIndex = notes.findIndex(note=>note.id == id);
let noteObj = notes[noteIndex];
noteObj.color = color;
modal.style.backgroundColor = color;
updateNote(noteObj);
displayNotes();
}
})
function deleteNote(noteIndex){
notes.splice(noteIndex,1);
modal.style.display ='none';
displayNotes();
}
function updateNote(newNote){
let { id } = newNote;
let noteIndex = notes.findIndex(note=>note.id == id);
notes[noteIndex] = newNote;
}
function displayNotes(){
if(notes.length >= 0){
notesList.innerHTML = [...notes].map(note=>{
return `
<div style="background-color: ${note.color};" class="note" data-id="${
note.id
}">
<div class="${note.noteTitle} note-title">${note.noteTitle}</div>
<div class="note_text">${note.noteText}</div>
<div class="toolbar">
<div class="delContainer">
<img src="./media/delete-button.svg" class="deleteBtn">
</div>
<div>
<img src="./media/edit.png" class="editBtn">
</div>
</div>
</div>
</div>
</div>
`
}).join('');
}
};