-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
467 lines (404 loc) · 14.9 KB
/
app.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//Storage control
const StorageCtrl = (function(){
//public methods
return {
storeNote: function(note){
let notes;
//check if any item in ls
if(localStorage.getItem('notes') === null){
notes = [];
//push new item
notes.push(note);
//set ls
localStorage.setItem('notes', JSON.stringify(notes));
} else{
//get items already in the ls
notes = JSON.parse(localStorage.getItem('notes'));
//push new item
notes.push(note);
//Re set ls
localStorage.setItem('notes', JSON.stringify(notes));
}
},
getNotesFromStorage: function(){
let notes;
if(localStorage.getItem('notes') === null){
notes = [];
}else{
notes = JSON.parse(localStorage.getItem('notes'));
}
return notes;
},
updateNoteStorage: function(updatedItem){
let notes = JSON.parse(localStorage.getItem('notes'));
notes.forEach(function(note, index){
if(updatedItem.id === note.id){
notes.splice(index, 1, updatedItem);
}
});
localStorage.setItem('notes', JSON.stringify(notes));
},
deleteNoteFromStorage: function(id){
let notes = JSON.parse(localStorage.getItem('notes'));
notes.forEach(function(note,index){
if(id === note.id){
notes.splice(index, 1);
}
});
localStorage.setItem('notes', JSON.stringify(notes));
},
clearNotesFromStorage: function(){
localStorage.removeItem('notes');
}
}
})();
//Item control
const ItemCtrl = (function(){
//Note Constructor
const Note = function(id, title, content, color){
this.id = id;
this.title = title;
this.content = content;
this.color = color;
}
//Data Structure
const data = {
notes: StorageCtrl.getNotesFromStorage(),
currentNote: null,
currentColor: '#a8d5baff'
}
//Return
return {
//Getting the Notes
getNotes: function(){
return data.notes;
},
//Adding a new note
addNote: function(title, content, color){
let ID;
if(data.notes.length > 0){
ID = data.notes[data.notes.length - 1].id + 1;
} else {
ID = 0;
}
title = title.toUpperCase();
newNote = new Note(ID, title, content, color);
data.notes.push(newNote);
return newNote;
},
//Getting a note by it's ID
getNoteById: function(id){
let found = null;
data.notes.forEach(function(note){
if(note.id == id){
found = note;
}
});
return found;
},
//Updating a note
updateNote: function(title, content){
let found = null;
data.notes.forEach(function(note){
if(note.id === data.currentNote.id){
note.title = title.toUpperCase();
note.content = content;
found = note;
}
});
return found;
},
//Deleting a note
deleteNote: function(id){
const ids = data.notes.map(function(note){
return note.id;
});
const index = ids.indexOf(id);
data.notes.splice(index, 1);
},
//Clear off all the notes
clearAllNotes: function(){
data.notes = [];
},
//Setting the current note
setCurrentNote: function(note){
data.currentNote = note;
},
//Getting the current note
getCurrentNote: function(){
return data.currentNote;
},
//set note color
setCurrentColor: function(colorValue){
switch(colorValue){
case 0 :
data.currentColor = '#a8d5baff';
break;
case 1 :
data.currentColor = '#d7a9e3ff';
break;
case 2 :
data.currentColor = '#8bbee8ff';
break;
default :
data.currentColor = '#a8d5baff';
break;
}
},
getCurrentColor: function(){
return data.currentColor;
},
//Getting the data
logData: function(){
return data;
}
}
})();
//UI control
const UICtrl = (function(){
//UI Selectors
const UISelectors = {
noteList: '#bottom-body',
listNotes: '#bottom-body li',
newBtn: '#new-btn',
saveBtn: '#save-btn',
updateBtn: '#update-btn',
deleteBtn: '#delete-btn',
backBtn: '#back-btn',
clearBtn: '#clear-btn',
noteTitle: '#title',
noteContent: '#note',
createdNoteTitle: '.note-title',
createdNoteContent: '.note-content',
secondaryEdit: '.secondary-btn',
greenBtn: '.green-btn',
pinkBtn: '.pink-btn',
cyanBtn: '.cyan-btn',
noteColor: '.color-btn'
}
//Return
return {
//Populate the notes
populateNoteList: function(notes){
document.querySelector(UISelectors.noteList).style.display = 'grid';
let html = '';
notes.forEach(function(note){
html += `
<li class="note-item card list-group-item" id="note-${note.id}" style="background-color:${note.color}">
<div class="note-title card-header">${note.title}</div>
<div class="note-content card-body">${note.content}</div>
</li>` ;
});
document.querySelector(UISelectors.noteList).innerHTML = html;
},
//Getting the note input
getNoteInput: function(){
return {
title: document.querySelector(UISelectors.noteTitle).value,
content: document.querySelector(UISelectors.noteContent).value
}
},
//Adding a note into UI
addNote: function(note){
document.querySelector(UISelectors.noteList).style.display = 'grid';
const div = document.createElement('li');
div.className = 'note-item card list-group-item';
div.id = `note-${note.id}`;
div.innerHTML = `
<div class="note-title card-header">${note.title}</div>
<div class="note-content card-body">${note.content}</div>`;
document.querySelector(UISelectors.noteList).insertAdjacentElement('beforeend', div);
let color = ItemCtrl.getCurrentColor();
document.querySelector(`#note-${note.id}`).style.backgroundColor = `${color}`;
ItemCtrl.setCurrentColor(0);
},
//Updating a note into UI
updateNoteList: function(note){
let listNotes = document.querySelectorAll(UISelectors.listNotes);
listNotes = Array.from(listNotes);
listNotes.forEach(function(listNote){
const noteID = listNote.getAttribute('id');
if(noteID === `note-${note.id}`){
document.querySelector(`#${noteID}`).innerHTML = `
<div class="note-title card-header">${note.title}</div>
<div class="note-content card-body">${note.content}</div>`;
}
});
},
//Deleting a note from UI
deleteNoteList: function(id){
const noteID = `#note-${id}`;
const note = document.querySelector(noteID);
note.remove();
},
//Remove all notes from UI
removeNotes: function(){
let listNotes = document.querySelectorAll(UISelectors.listNotes);
listNotes = Array.from(listNotes);
listNotes.forEach(function(note){
note.remove();
});
this.defaultState();
this.clearEditState();
},
//Initial state of the UI
defaultState: function(){
document.querySelector(UISelectors.newBtn).style.display = 'block';
document.querySelector(UISelectors.saveBtn).style.display = 'none';
document.querySelector(UISelectors.noteTitle).style.display = 'none';
document.querySelector(UISelectors.noteContent).style.display = 'none';
document.querySelector(UISelectors.noteColor).style.display = 'none';
},
//Note creating state in UI
onCreateNew: function(){
document.querySelector(UISelectors.newBtn).style.display = 'none';
document.querySelector(UISelectors.saveBtn).style.display = 'block';
document.querySelector(UISelectors.noteTitle).style.display = 'block';
document.querySelector(UISelectors.noteContent).style.display = 'block';
document.querySelector(UISelectors.noteColor).style.display = 'block';
},
//Display the inputs of Selected note in UI
addNoteToForm: function(){
document.querySelector(UISelectors.noteTitle).value = ItemCtrl.getCurrentNote().title;
document.querySelector(UISelectors.noteContent).value = ItemCtrl.getCurrentNote().content;
UICtrl.showEditState();
},
//Hide the Notes container from UI
hideNotes: function(){
document.querySelector(UISelectors.noteList).style.display = 'none';
},
//Clear off the Edit state in UI
clearEditState: function(){
UICtrl.clearInput();
document.querySelector(UISelectors.secondaryEdit).style.display = 'none';
},
//Activate the Edit State in UI
showEditState: function(){
document.querySelector(UISelectors.newBtn).style.display = 'none';
document.querySelector(UISelectors.noteTitle).style.display = 'block';
document.querySelector(UISelectors.noteContent).style.display = 'block';
document.querySelector(UISelectors.secondaryEdit).style.display = 'inline';
},
//Clear the input in UI
clearInput: function(){
document.querySelector(UISelectors.noteTitle).value = '';
document.querySelector(UISelectors.noteContent).value = '';
},
//Getting UISelectors for access
getSelectors: function(){
return UISelectors;
}
}
})();
//App control
const AppCtrl = (function(ItemCtrl, StorageCtrl, UICtrl){
//Loading the Event Listeners
const loadEventListeners = function(){
const UISelectors = UICtrl.getSelectors();
document.querySelector(UISelectors.newBtn).addEventListener('click', newNoteCreate);
document.querySelector(UISelectors.saveBtn).addEventListener('click', newNoteSave);
document.addEventListener('keypress', function(e){
if(e.keyCode === 13 || e.which === 13){
e.preventDefault();
return false;
}
});
document.querySelector(UISelectors.noteList).addEventListener('click', noteEditClick);
document.querySelector(UISelectors.updateBtn).addEventListener('click', noteUpdateSubmit);
document.querySelector(UISelectors.deleteBtn).addEventListener('click', noteDeleteSubmit);
document.querySelector(UISelectors.backBtn).addEventListener('click', noteBackSubmit);
document.querySelector(UISelectors.clearBtn).addEventListener('click', clearAllNotes);
document.querySelector(UISelectors.greenBtn).addEventListener('click', greenBtnSelect);
document.querySelector(UISelectors.pinkBtn).addEventListener('click', pinkBtnSelect);
document.querySelector(UISelectors.cyanBtn).addEventListener('click', cyanBtnSelect);
}
//Creating a new note
const newNoteCreate = function(e){
UICtrl.onCreateNew();
}
//Saving a new note
const newNoteSave = function(e){
const input = UICtrl.getNoteInput();
const color = ItemCtrl.getCurrentColor();
if(input.title !== '' && input.content !== ''){
const newNote = ItemCtrl.addNote(input.title, input.content, color);
UICtrl.addNote(newNote);
StorageCtrl.storeNote(newNote);
UICtrl.defaultState();
UICtrl.clearInput();
}
}
//Click the note to Edit
const noteEditClick = function(e){
if(e.target.classList.contains('note-title') || e.target.classList.contains('note-content')){
const noteId = e.target.parentNode.id;
const noteIdArr = noteId.split('-');
const id = parseInt(noteIdArr[1]);
const noteToEdit = ItemCtrl.getNoteById(id);
ItemCtrl.setCurrentNote(noteToEdit);
UICtrl.addNoteToForm();
}
e.preventDefault();
}
//Updating the selected note
const noteUpdateSubmit = function(e){
const input = UICtrl.getNoteInput();
const updatedItem = ItemCtrl.updateNote(input.title, input.content);
UICtrl.updateNoteList(updatedItem);
StorageCtrl.updateNoteStorage(updatedItem);
UICtrl.clearEditState();
UICtrl.defaultState();
e.preventDefault();
}
//Deleting the selected note
const noteDeleteSubmit = function(e){
const currentNote = ItemCtrl.getCurrentNote();
ItemCtrl.deleteNote(currentNote.id);
UICtrl.deleteNoteList(currentNote.id);
StorageCtrl.deleteNoteFromStorage(currentNote.id);
UICtrl.clearEditState();
UICtrl.defaultState();
e.preventDefault();
}
//Back to normal
const noteBackSubmit = function(){
UICtrl.clearEditState();
UICtrl.defaultState();
}
//Clear off all the notes
const clearAllNotes = function(){
ItemCtrl.clearAllNotes();
UICtrl.removeNotes();
StorageCtrl.clearNotesFromStorage();
UICtrl.hideNotes();
}
//green color
const greenBtnSelect = function(){
ItemCtrl.setCurrentColor(0);
}
//Pink color
const pinkBtnSelect = function(){
ItemCtrl.setCurrentColor(1);
}
//Cyan color
const cyanBtnSelect = function(){
ItemCtrl.setCurrentColor(2);
}
//Return
return {
init: function(){
UICtrl.defaultState();
UICtrl.clearInput();
UICtrl.clearEditState();
const notes = ItemCtrl.getNotes();
if(notes.length === 0){
UICtrl.hideNotes();
} else{
UICtrl.populateNoteList(notes);
}
loadEventListeners();
}
}
})(ItemCtrl, StorageCtrl, UICtrl);
//App initialization
AppCtrl.init();