-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
65 lines (51 loc) · 1.6 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
console.log("Welcome to notes app. This is app.js");
showImages();
// If user adds a note, add it to the localStorage
var loadFile = function(event){
var url = URL.createObjectURL(event.target.files[0]);
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
notesObj.push(url);
localStorage.setItem("notes", JSON.stringify(notesObj));
notesObj = [];
showImages();
}
function showImages(){
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
let html = "";
notesObj.forEach(function(element, index) {
html += `
<div class="noteCard my-2 mx-2 card" style="width: 18rem;">
<div class="card-body">
<img class="img-thumbnail mystyle" src="${element}" class="card-text">
<button id="${index}"onclick="deleteNote(this.id)" class="btn btn-primary">Delete Image</button>
</div>
</div>`;
});
let notesElm = document.getElementById("notes");
if (notesObj.length != 0) {
notesElm.innerHTML = html;
} else {
notesElm.innerHTML = '<h5 style="margin-left = 20%">Nothing to show! Click on "Upload Images" to make a new collection.</h5>';
}
}
function deleteNote(index) {
let notes = localStorage.getItem("notes");
if (notes == null) {
notesObj = [];
} else {
notesObj = JSON.parse(notes);
}
notesObj.splice(index, 1);
localStorage.setItem("notes", JSON.stringify(notesObj));
showImages();
}