-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
82 lines (72 loc) · 2.51 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
var notes = [];
// Registering all the event handlers when the page loads
document.addEventListener("DOMContentLoaded", event => {
if (localStorage.getItem("notes")) {
notes = JSON.parse(localStorage.getItem("notes"));
}
renderNotes();
document.querySelector("form").addEventListener("submit", event => {
event.preventDefault();
const note = document.querySelector("textarea").value;
if (note.length==0) {
alert("You didn't input any content");
} else {
notes.push(note);
renderNotes();
save();
document.querySelector("textarea").value = "";
}
});
document.querySelector("#btnLearn").addEventListener("click", event => {
location.href = "https://frontendmasters.com";
})
let bipEvent = null;
window.addEventListener("beforeinstallprompt", event => {
event.preventDefault();
bipEvent = event;
})
document.querySelector("#btnInstall").addEventListener("click", event => {
if (bipEvent) {
bipEvent.prompt();
} else {
// incompatible browser, your PWA is not passing the criteria, the user has already installed the PWA
//TODO: show the user information on how to install the app
alert("To install the app look for Add to Homescreen or Install in your browser's menu");
}
})
document.querySelector("#btnShare").addEventListener("click", event => {
let notesString = "";
for (let note of notes) {
notesString += note + " | "
}
navigator.share({
title: "Codepad",
text: notesString
})
})
})
// Render the notes on the DOM
function renderNotes() {
const ul = document.querySelector("#notes");
ul.innerHTML = "";
notes.forEach( (note, index) => {
// Create the note LI
const li = document.createElement("li");
li.innerHTML = note;
// Delete element for each note
const deleteButton = document.createElement("a");
deleteButton.innerHTML = '<span class="icon">delete</span>';
deleteButton.addEventListener("click", event => {
if (confirm("Do you want to delete this note?")) {
notes.splice(index, 1);
renderNotes();
save();
}
});
li.appendChild(deleteButton);
ul.appendChild(li);
})
}
function save() {
localStorage.setItem("notes", JSON.stringify(notes));
}