-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
96 lines (77 loc) · 2.48 KB
/
Copy pathmain.js
File metadata and controls
96 lines (77 loc) · 2.48 KB
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
$(document).ready(function() {
loadNotes();
$('#addNoteBtn').click(function() {
addNote();
});
$('#noteTitle').keypress(function(e) {
if (e.which === 13) {
addNote();
}
});
$('#noteContent').keypress(function(e) {
if (e.ctrlKey && e.which === 13) {
addNote();
}
});
function addNote() {
const title = $('#noteTitle').val().trim();
const content = $('#noteContent').val().trim();
if (title === '' || content === '') {
alert('Заполните все поля!');
return;
}
const note = {
id: Date.now(),
title: title,
content: content,
date: new Date().toLocaleString('ru-RU')
};
saveNote(note);
displayNote(note);
$('#noteTitle').val('');
$('#noteContent').val('');
$('#emptyMessage').hide();
}
function displayNote(note) {
const noteElement = $(`
<div class="note" data-id="${note.id}">
<div class="note-header">
<div class="note-title">${note.title}</div>
<button class="delete-btn">✕</button>
</div>
<div class="note-content">${note.content}</div>
</div>
`);
noteElement.find('.delete-btn').click(function() {
deleteNote(note.id);
noteElement.remove();
if ($('.note').length === 0) {
$('#emptyMessage').show();
}
});
$('#notesContainer').prepend(noteElement);
}
function saveNote(note) {
let notes = getNotesFromStorage();
notes.unshift(note);
localStorage.setItem('notes', JSON.stringify(notes));
}
function deleteNote(id) {
let notes = getNotesFromStorage();
notes = notes.filter(note => note.id !== id);
localStorage.setItem('notes', JSON.stringify(notes));
}
function getNotesFromStorage() {
const notesJSON = localStorage.getItem('notes');
return notesJSON ? JSON.parse(notesJSON) : [];
}
function loadNotes() {
const notes = getNotesFromStorage();
if (notes.length > 0) {
$('#emptyMessage').hide();
notes.forEach(note => {
displayNote(note);
});
}
}
});