-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (55 loc) · 1.71 KB
/
index.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
const notesContainer = document.querySelector('#app')
const addNoteButton = document.querySelector('.add-note')
getNotes().forEach((note) => {
const noteElement = createNoteElement(note.id, note.content)
notesContainer.insertBefore(noteElement, addNoteButton)
});
addNoteButton.addEventListener("click", ()=>{addNote()})
function getNotes(){
return JSON.parse(localStorage.getItem("stickynotes-notes") || "[]")
}
function saveNotes(notes){
localStorage.setItem("stickynotes-notes", JSON.stringify(notes))
}
function createNoteElement(id, content){
const element = document.createElement("textarea")
element.classList.add("note")
element.value = content
element.placeholder = "Empty Sticky Note"
element.addEventListener("change", ()=>{
updateNote(id, element.value);
})
element.addEventListener("dblclick", ()=>{
const doDelete = confirm ("Are you sure you want to delete this Note")
if(doDelete){
deleteNote(id, element)
}
})
return element
}
function addNote(){
const notes = getNotes()
const noteObject = {
id: Math.floor(Math.random() *10000),
content:""
};
const noteElement = createNoteElement(noteObject.id, noteObject.content);
notesContainer.insertBefore(noteElement, addNoteButton)
notes.push(noteObject);
saveNotes(notes);
}
function updateNote(id, newContent){
const notes = getNotes()
const targetNote = notes.filter(note => note.id == id)[0];
targetNote.content = newContent;
saveNotes(notes)
console.log("updating Note")
console.log(id, newContent)
}
function deleteNote(id, element){
const notes = getNotes().filter((note) => note.id !=id);
saveNotes(notes)
notesContainer.removeChild(element)
console.log("Deleting Note")
console.log(id + "Deleted.")
}