-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes.html
More file actions
137 lines (121 loc) · 3.67 KB
/
notes.html
File metadata and controls
137 lines (121 loc) · 3.67 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Notes JS</title>
<link rel="stylesheet"
href="https://use.fontawesome.com/releases/v5.5.0/css/all.css"
integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU"
crossorigin="anonymous">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="notes-container"></div>
<aside class="other-container">
<button id="add_note">
<h4>Add note</h4>
<i class="far fa-sticky-note"></i>
</button>
</aside>
<script src="js/event.js"></script>
<script src="js/generateNodes.js"></script>
<script>
function saveNote (note) {
notes[note.id] = {
creation: +note.id.replace('data', ''),
lastEdit: (new Date()).getTime(),
text: note.value
};
saveNotes();
}
function deleteNote (note) {
var noteElement = note.parentNode;
delete notes[note.id];
noteElement.parentNode.removeChild(noteElement);
saveNotes();
}
function saveNotes () {
localStorage.setItem('notes', JSON.stringify(notes));
}
(function () {
var noteContent = `
<div class="note-container">
<i class="managing fas fa-check"></i>
<i class="remove far fa-trash-alt"></i>
<textarea class="note" placeholder="Add your content.."
rows="10" cols="20" wrap="soft" autofocus></textarea>
</div>`;
var htmlString = noteContent.
replace(/^\s+?(?=<)|(?<=>)\s*?(?=<)/g, '').
replace(/\n/, '');
this.generateNote = function (fragment, noteContent, noteId) {
var note;
generateNodes(htmlString, fragment);
note = fragment.querySelector('.note');
if (noteContent) {
note.id = noteId;
note.value = noteContent;
} else {
note.id = `data${(new Date()).getTime()}`;
}
notesContainer.append(fragment);
};
})();
var notesContainer;
var notes = JSON.parse(localStorage.getItem('notes')) || {};
window.onload = function () {
var body = document.body;
notesContainer = document.getElementById('notes-container');
addEvent(body, 'focusout', function (event) {
var elem = event.target;
var bodyData;
if (elem.classList.contains('note')) {
elem.parentNode.querySelector('.managing').
className = 'managing far fa-edit';
elem.disabled = true;
saveNote(elem);
bodyData = getData(body);
bodyData.disabled = true;
setTimeout(function () {
bodyData.disabled = false;
bodyData = null; // clean memory
}, 300);
}
});
addEvent(body, 'click', function (event) {
var elem = event.target;
if (elem.classList.contains('managing')) {
var note = elem.parentNode.querySelector('.note');
if (elem.classList.contains('fa-edit')) {
note.parentNode.querySelector('.managing').
className = 'managing fas fa-check';
note.removeAttribute('disabled');
note.focus();
} else {
note.parentNode.querySelector('.managing').
className = 'managing far fa-edit';
note.disabled = true;
saveNote(note);
}
} else if (elem.classList.contains('remove')) {
deleteNote(elem.parentNode.querySelector('.note'));
}
});
addEvent(document.getElementById('add_note'), 'click', function () {
var fragment = document.createDocumentFragment();
generateNote(fragment);
notesContainer.append(fragment);
});
for (var noteId in notes) {
if (notes.hasOwnProperty(noteId)) {
var fragment = document.createDocumentFragment();
generateNote(fragment, notes[noteId].text, noteId);
notesContainer.append(fragment);
}
}
};
</script>
</body>
</html>