-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
176 lines (129 loc) · 5.21 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
172
173
174
175
176
function createNote(newNote) {
// If noteDatabase not found then create
if (!localStorage.getItem('notesData'))
localStorage.setItem('notesData', JSON.stringify([]));
const notesData = JSON.parse(localStorage.getItem('notesData'));
notesData.unshift(newNote);
localStorage.setItem('notesData', JSON.stringify(notesData));
}
function AddDummyData()
{
fetch('dummyNotes.json')
.then(response => {
return response.json();
})
.then(data => {
data.forEach(note =>{
createNote(note);
});
getAllNotes();
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
function deleteAll()
{
const result = confirm("Are you sure you want to DELETE ALL NOTES ?");
if(result == true)
{
localStorage.setItem('notesData', JSON.stringify([]));
getAllNotes();
}
}
function createNoteBtn() {
const noteName = document.getElementById("note_name");
const noteContent = document.getElementById("note_content");
let randomId = Math.floor(Math.random() * 9999) + 1;
let unixTime = Math.floor(Date.now() / 1000);
const newNote = { id: randomId, title: noteName.value, content: noteContent.value, timestamp: unixTime }
createNote(newNote);
getAllNotes()
}
function getAllNotes() {
const notesContainer = document.getElementById("notesContainer");
notesContainer.innerHTML = "";
const notesData = JSON.parse(localStorage.getItem('notesData'));
notesData.forEach(note => {
randomColor = Math.floor(Math.random() * 4) + 1;
timestamp = getFormattedDateTime(note.timestamp)
const rawHTML = `<div class="col">
<div class="card notes-card color-${randomColor}" onclick="editNote(${note.id})">
<div class="card-body notes-body">
<div class="row">
<div class="col-10">
<h2 class="notes-heading">${note.title}</h2>
</div>
<div class="col-2">
<span id="iconContainer" onclick="deleteNote(${note.id})"><i class="bi bi-x"></i></span>
</div>
</div>
<p class="notes-text">${note.content}</p>
</div>
<div class="date">
<span><i class="bi bi-calendar"></i> ${timestamp.date}</span>
<span><i class="bi bi-clock"></i> ${timestamp.time}</span>
<span onclick="copyTo()"><i class="bi bi-copy"></i></span>
</div>
</div>
</div>`;
notesContainer.insertAdjacentHTML('beforeend', rawHTML);
});
}
function deleteNote(noteId) {
const result = confirm("Are you sure you want to delete this note ?");
if (result === true) {
const notesData = JSON.parse(localStorage.getItem('notesData'));
const filterData = notesData.filter(note => note.id !== noteId);
localStorage.setItem('notesData', JSON.stringify(filterData));
getAllNotes();
}
}
function editNote(noteId)
{
const modalEl = document.getElementById('editModal')
let myModal = new bootstrap.Modal(modalEl);
// Select edit modal
const noteTitle = document.getElementById("edit_note_name")
const noteContent = document.getElementById("edit_note_content")
noteTitle.setAttribute('data-index', noteId);
const notesData = JSON.parse(localStorage.getItem('notesData'));
const filterData = notesData.find(note => note.id === noteId);
// Set data in edit modal
noteTitle.value = filterData.title;
noteContent.value = filterData.content;
myModal.show();
}
function UpdateNote()
{
// Select edit modal
const noteTitle = document.getElementById("edit_note_name")
const noteContent = document.getElementById("edit_note_content")
const noteId = noteTitle.getAttribute('data-index');
const unixTime = Math.floor(Date.now() / 1000);
const notesData = JSON.parse(localStorage.getItem('notesData'));
const filterIndex = notesData.findIndex(note => note.id == noteId);
notesData[filterIndex].title = noteTitle.value;
notesData[filterIndex].content = noteContent.value;
notesData[filterIndex].timestamp = unixTime;
localStorage.setItem('notesData', JSON.stringify(notesData));
getAllNotes()
}
function getFormattedDateTime(unixTimestamp) {
const date = new Date(unixTimestamp * 1000); // Convert Unix timestamp to milliseconds
const day = date.getDate(); // Get the day of the month (1-31)
const month = date.toLocaleString('default', { month: 'short' }); // Get the month abbreviation (e.g., Jan)
const year = date.getFullYear(); // Get the year (four digits)
let hours = date.getHours(); // Get the hours (0-23)
const minutes = ('0' + date.getMinutes()).slice(-2); // Get the minutes with leading zero if needed
// Convert hours to 12-hour format and determine AM/PM
const amPM = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12 || 12; // Convert hours from 24-hour to 12-hour format
const formattedDate = `${day} ${month} ${year}`;
const formattedTime = `${hours}:${minutes} ${amPM}`;
return {
date: formattedDate,
time: formattedTime
};
}
getAllNotes();