-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (100 loc) · 3.64 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
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
let notes = [
{
id: new Date(),
title: 'Sample Note',
body: 'Description of smaple note',
bgColor: 'pink'
}
];
const createElement = (tag, classes = []) => {
const element = document.createElement(tag);
classes.forEach(cl => {
element.classList.add(cl);
})
return element;
}
const createNoteView = (note) => {
const noteDiv = createElement('div', ['note']);
noteDiv.id = note.id;
const textDiv = createElement('div', ['text']);
textDiv.style.background = note.bgColor;
const titleP = createElement('b',['title']);
titleP.innerHTML = note.title;
const bodyP = createElement('p', ['body']);
bodyP.innerHTML = note.body;
const editButton = createElement('button', ['edit']);
editButton.innerHTML = 'Edit Note';
const deleteButton = createElement('button', ['delete']);
deleteButton.innerHTML = 'Delete Note';
textDiv.append(titleP)
textDiv.append(bodyP)
noteDiv.append(textDiv)
noteDiv.append(editButton)
noteDiv.append(deleteButton)
editButton.onclick = () => editNote(noteDiv);
deleteButton.onclick = () => deleteNote(noteDiv);
return noteDiv;
}
const cancelEdit = (noteDiv) => {
const titleP = noteDiv.querySelector('b.title');
titleP.contentEditable = false;
const bodyP = noteDiv.querySelector('p.body');
bodyP.contentEditable = false;
const editButton = noteDiv.querySelector('button.edit');
editButton.innerHTML = 'edit note';
const deleteButton = noteDiv.querySelector('button.delete');
deleteButton.innerHTML = 'delete note';
const note = notes.find(note => note.id == noteDiv.id);
titleP.innerHTML = note.title;
bodyP.innerHTML = note.body;
editButton.onclick = () => editNote(noteDiv);
deleteButton.onclick = () => deleteNote(noteDiv);
}
const editNote = (noteDiv, editSave = false) => {
const titleP = noteDiv.querySelector('b.title')
titleP.contentEditable = true ;
titleP.focus();
const bodyP = noteDiv.querySelector('p.body');
bodyP.contentEditable = true;
const editButton = noteDiv.querySelector('button.edit');
editButton.innerHTML = 'Save Note';
const deleteButton = noteDiv.querySelector('button.delete');
deleteButton.innerHTML = 'Cancel Edit';
deleteButton.onclick =() => cancelEdit(noteDiv);
editButton.onclick = () => editNote(noteDiv, true);
if (editSave) {
const note = notes.find(note => note.id == noteDiv.id);
note.title = titleP.innerText.trim();
note.body = bodyP.innerText.trim();
deleteButton.innerHTML = 'delete note';
editButton.innerHTML = 'edit note';
titleP.contentEditable = false;
bodyP.contentEditable = false;
editButton.onclick = () => editNote(noteDiv);
deleteButton.onclick = () => deletetNote(noteDiv);
}
}
const saveNote =() => {
const titleInput = document.querySelector('input#title');
const bodyInput = document.querySelector('input#body');
const bgColorInput = document.querySelector('select');
const id = new Date().getTime();
const note ={
id, title: titleInput.value, body: bodyInput.value, bgColor: bgColorInput.value
}
const noteDiv = createNoteView(note)
notesDiv.prepend(noteDiv);
titleInput.value='';
bodyInput.value='';
bgColorInput.value='';
}
const deleteNote = (noteDiv) => {
noteDiv.remove();
notes = notes.filter(note => note.id != notesDiv.id)
}
document.querySelector('button.add').onclick = () => saveNote();
const notesDiv = document.querySelector ('#notes .notesDiv');
notes.forEach(note =>{
const noteDiv = createNoteView(note);
notesDiv.append(noteDiv);
})