-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlate.js
executable file
·169 lines (129 loc) · 4.78 KB
/
Slate.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
'user strict';
var notes = [];
var noteCount = 0;
var priorityClasses = {
3 : 'high',
2 : 'medium',
1 : 'low',
};
window.addEventListener("load",function () {
var storedNotes = localStorage.getItem("notes");
if (storedNotes) {
notes = JSON.parse(storedNotes);
noteCount = Number(localStorage.getItem('noteCount'));
for(var i = 0; i < notes.length; ++i) {
noteAdd(notes[i]);
}
}
});
function noteCreate() {
var newNote = {};
newNote.id = noteCount;
newNote.title = $("#title-input").val();
newNote.content = $("#content-input").val();
newNote.priority = $(".priority").val();
noteCount++;
notes.push(newNote);
noteAdd(newNote);
$("#title-input").val('');
$("#content-input").val('');
$(".priority").val('2');
noteStore();
}
function noteAdd(note) {
var newNoteContainer = document.createElement('div');
var newNoteBody = document.createElement('div');
var newNoteTitle = document.createElement('h3');
newNoteContainer.innerHTML += '<button title="Close" class="note-button note-close" onclick="noteDelete(event)">☓</button>';
newNoteContainer.innerHTML += '<button title="Edit" class="note-button note-edit" onclick="noteEditStart(event)">✎</button>';
newNoteTitle.innerHTML = note.title;
newNoteBody.innerHTML = note.content;
newNoteContainer.className = "note";
newNoteContainer.className += " " + priorityClasses[note.priority];
newNoteTitle.className = "note-title";
newNoteBody.className = "note-body";
newNoteContainer.id = "note-" + note.id;
newNoteContainer.appendChild(newNoteTitle);
newNoteContainer.appendChild(newNoteBody);
document.querySelector('#notes-container').appendChild(newNoteContainer);
}
function noteDelete(event) {
var elementToDelete = event.target.parentNode;
elementToDelete.remove();
var elementCountId = Number(elementToDelete.id.split('-')[1]);
for (var i = notes.length - 1; i >= 0; --i) {
if (notes[i].id == elementCountId) {
notes.splice(i, 1);
break;
}
}
noteStore();
}
function noteEditStart(event) {
var elementToEdit = event.target.parentNode;
var elementCountId = Number(elementToEdit.id.split('-')[1]);
var currentNote = {};
for (var i = notes.length - 1; i >= 0; --i) {
if (notes[i].id == elementCountId) {
currentNote = notes[i];
break;
}
}
elementToEdit.innerHTML = "";
elementToEdit.innerHTML += '<input class="title" value="' + currentNote.title + '"></input>';
elementToEdit.innerHTML += '<textarea class="content">' + currentNote.content + '</textarea>';
elementToEdit.innerHTML += '<select name="priority" class="priority-edit">' +
'<option value="3">High</option>' +
'<option value="2">Medium</option>' +
'<option value="1">Low</option>' +
'</select>';
elementToEdit.innerHTML += '<button onclick="noteEditFinish(event)">Finish Edit</button>';
elementToEdit.querySelectorAll('select')[0].value = currentNote.priority;
}
function noteEditFinish(event) {
var elementToEdit = event.target.parentNode;
var elementCountId = Number(elementToEdit.id.split('-')[1]);
var newTitle = elementToEdit.querySelectorAll('input')[0].value;
var newContent = elementToEdit.querySelectorAll('textarea')[0].value;
var newPriority = elementToEdit.querySelectorAll('select')[0].value;
var newNoteBody = document.createElement('div');
var newNoteTitle = document.createElement('h3');
newNoteTitle.innerHTML = newTitle;
newNoteBody.innerHTML = newContent;
newNoteTitle.className = "note-title";
newNoteBody.className = "note-body";
for (var i = notes.length - 1; i >= 0; --i) {
if (notes[i].id === elementCountId) {
notes[i].title = newTitle;
notes[i].content = newContent;
notes[i].priority = newPriority;
break;
}
}
elementToEdit.innerHTML = "";
elementToEdit.innerHTML += '<button title="Close" class="note-button note-close" onclick="noteDelete(event)">✘</button>';
elementToEdit.innerHTML += '<button title="Edit" class="note-button note-edit" onclick="noteEditStart(event)">📝</button>';
elementToEdit.appendChild(newNoteTitle);
elementToEdit.appendChild(newNoteBody);
elementToEdit.className = "note " + priorityClasses[newPriority];
noteStore();
}
function noteSort() {
var compare = function (a, b) {
if (a.priority < b.priority)
return 1;
if (a.priority > b.priority)
return -1;
return 0;
}
notes = notes.sort(compare);
document.querySelector('#notes-container').innerHTML = "";
for (var i = 0; i < notes.length; ++i) {
noteAdd(notes[i]);
}
noteStore();
}
function noteStore() {
localStorage.setItem('notes',JSON.stringify(notes));
localStorage.setItem('noteCount',String(noteCount));
}