-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
123 lines (103 loc) · 3.6 KB
/
script.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
const addBtn = document.querySelector('.add');
const saveBtn = document.querySelector('.save');
const cancelBtn = document.querySelector('.cancel');
const deleteAllBtn = document.querySelector('.delete-all');
const noteArea = document.querySelector('.note-area');
const notePanel = document.querySelector('.note-panel');
const category = document.querySelector('#category');
const textArea = document.querySelector('#text');
const error = document.querySelector('.error');
let selectedValue;
let cardID = 0;
const openPanel = () => {
notePanel.style.display = 'flex';
};
const closePanel = () => {
notePanel.style.display = 'none';
error.style.visibility = 'hidden';
textArea.value = '';
category.selectedIndex = 0;
};
const addNote = () => {
if (textArea.value !== '' && category.options[category.selectedIndex].value !== '0') {
createNote();
error.style.visibility = 'hidden';
} else {
error.style.visibility = 'visible';
}
};
const createNote = () => {
const newNote = document.createElement('div');
newNote.classList.add('note');
newNote.setAttribute('id', cardID);
newNote.innerHTML = `
<div class="note-header">
<h3 class="note-title">${selectedValue}</h3>
<div class="edit">
<button class="delete-note" onclick="deleteNote(${cardID})">
<i class="fas fa-times icon"></i>
</button>
<button class="edit-note" onclick="editNote(${cardID})">
<i class="fas fa-edit icon"></i>
</button>
</div>
</div>
<div class="note-body">
${textArea.value}
</div>
<button class="accept" onclick="editedNote(${cardID})">
<i class="fas fa-check icon"></i>
</button>`;
noteArea.appendChild(newNote);
cardID++;
textArea.value = '';
category.selectedIndex = 0;
notePanel.style.display = 'none';
checkColor(newNote);
};
const selectValue = () => {
selectedValue = category.options[category.selectedIndex].text;
};
const checkColor = (note) => {
switch(selectedValue) {
case 'Zakupy':
note.style.backgroundColor = 'rgb(10,200,100)';
break;
case 'Praca':
note.style.backgroundColor = 'rgb(255,243,0)';
break;
case 'Dom':
note.style.backgroundColor = 'rgb(0,170,255)';
break;
case 'Inne':
note.style.backgroundColor = 'rgb(255,90,100)';
break;
}
};
const deleteNote = (id) => {
const noteTodelete = document.getElementById(id);
noteArea.removeChild(noteTodelete);
};
const editNote = (id) => {
const noteEdit = document.getElementById(id);
const noteBody = noteEdit.querySelector('.note-body');
const acceptBtn = noteEdit.querySelector('.accept');
noteBody.setAttribute("contenteditable", "true");
noteBody.style.fontStyle = 'italic';
acceptBtn.style.display = 'block';
};
const editedNote = (id) => {
const noteEdit = document.getElementById(id);
const noteBody = noteEdit.querySelector('.note-body');
const acceptBtn = noteEdit.querySelector('.accept');
noteBody.setAttribute("contenteditable", "false");
noteBody.style.fontStyle = '';
acceptBtn.style.display = 'none';
};
const deleteAllNotes = () => {
noteArea.textContent = '';
};
addBtn.addEventListener('click', openPanel);
cancelBtn.addEventListener('click', closePanel);
saveBtn.addEventListener('click', addNote);
deleteAllBtn.addEventListener('click', deleteAllNotes);