-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (146 loc) · 5.1 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
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
const $ = document
const containerElem = $.querySelector('.container'),
addNewElemHandle = $.querySelector('.add-new-note'),
modalElem = $.querySelector('.modal'),
overlayElem = $.querySelector('.overlay'),
btnAddElem = $.querySelector('.btn-add'),
inputTitleElem = $.querySelector('.input-title'),
inputDescElem = $.querySelector('.input-desc'),
closeModalElem = $.querySelector('.close-modal'),
btnEditElem = $.querySelector('.btn-edit'),
btnDeleteElem = $.querySelector('.btn-delete'),
noteElem = $.querySelector('.note'),
noteTitle = $.querySelector('.note-title'),
noteButton = $.querySelector('.note-button');
let isUpdate = false
let updateId = null
let notesArray = []
const openModal = (noteId, editnoteTitle, editnoteDesc) => {
modalElem.style.display = "flex"
overlayElem.style.display = "flex"
if (isUpdate) {
noteTitle.textContent = "Update Note"
noteButton.textContent = "Update Note"
inputTitleElem.value = editnoteTitle
inputDescElem.value = editnoteDesc
}else{
noteTitle.textContent = "Add a New Note"
noteButton.textContent = "Add Note"
inputTitleElem.placeholder = "enter title"
inputDescElem.placeholder = "enter description"
}
inputTitleElem.focus()
}
const generateNotes = (notes) => {
modalElem.style.display = "none"
overlayElem.style.display = "none"
$.querySelectorAll('.note').forEach(note => note.remove())
notes.forEach((note, index) => { containerElem.insertAdjacentHTML("beforeend" ,`<div class="box note">
<div class="head-box">
<h2>${note.title}</h2>
<p>${note.desc}</p>
</div>
<div class="foot-box">
<p class="date">${note.date}</p>
<img class="menu-svg" onclick="showMenu(this)" src="assets/ellipsis-solid.svg" width="40px" alt="menu-icon">
<div class="menu-extend">
<span onclick="editNoteFromDom(${index}, '${note.title}', '${note.desc}')" class="flex btn-edit">
<img src="./assets/pen-solid.svg" width="15px" alt="">
<h4>Edit</h4>
</span>
<span onclick="deleteNoteFromDom(${index})" class="flex btn-delete">
<img src="./assets/trash-solid.svg" width="15px" alt="">
<h4>Delete</h4>
</span>
</div>
</div>
</div>`
)
})
}
const showMenu = (el) => {
el.nextElementSibling.classList.add("show")
$.addEventListener('click', ev => {
if (ev.target !== el) {
el.nextElementSibling.classList.remove("show")
}
})
}
const getLocalStorage = () => {
let localStorageNotes = localStorage.getItem('notes')
if (localStorageNotes) {
notesArray = JSON.parse(localStorageNotes)
} else {
notesArray = []
}
return notesArray
}
const setLocalStorage = (notesList) => {
localStorage.setItem('notes', JSON.stringify(notesList))
}
window.addEventListener('load', () => {
let notes = getLocalStorage()
console.log(notes);
generateNotes(notes)
})
const closeModal = () => {
modalElem.style.display = "none"
overlayElem.style.display = "none"
}
const clearInput = () => {
inputTitleElem.value = ""
inputDescElem.value = ""
}
const editNoteFromDom = (noteId, noteTitle, noteDesc) => {
isUpdate = true
openModal(noteId, noteTitle, noteDesc)
updateId = noteId
}
const deleteNoteFromDom = (noteIndex) => {
let deleted = confirm("Are you sure to delete note?")
if (deleted) {
let localNotes = getLocalStorage()
localNotes.splice(noteIndex, 1)
setLocalStorage(localNotes)
generateNotes(localNotes)
}
}
window.addEventListener('keyup', event => {
if (event.key === "Escape") {
closeModal()
}
})
btnAddElem.addEventListener('click', () => {
const days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
const currentData = new Date()
if (isUpdate) {
let allNotes = getLocalStorage()
allNotes.some((note, index) => {
if (index === updateId) {
note.title = inputTitleElem.value
note.desc = inputDescElem.value
}
})
setLocalStorage(allNotes)
generateNotes(allNotes)
closeModal()
clearInput()
isUpdate = false
} else {
let newNoteObj = {
id: notesArray.length + 1,
title: inputTitleElem.value.trim(),
desc: inputDescElem.value.trim(),
date: currentData.toLocaleString('default', {month: 'long'})
+ ' ' + currentData.getDate() + ', '
+ currentData.getFullYear() + ' '
+ '('+ days[currentData.getDay()] +')'
}
notesArray.push(newNoteObj)
setLocalStorage(notesArray)
generateNotes(notesArray)
clearInput()
}
})
addNewElemHandle.addEventListener('click', openModal)
closeModalElem.addEventListener('click', closeModal)