-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (131 loc) · 4 KB
/
index.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
console.log("These are my es6 classes")
class Book{
constructor(name,author,type){
this.name = name,
this.author = author,
this.type = type;
}
}
class Display {
add(book) {
let tableBody = document.getElementById("tableBody");
let uiString = `<tr>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.type}</td>
</tr>`;
tableBody.innerHTML += uiString;
}
clear() {
let libraryForm = document.getElementById("libraryForm");
libraryForm.reset();
// resets form elements
}
// checks if the entry is valid
validate(book) {
if (book.name.length < 2 || book.author.length < 2) {
return false;
} else {
return true;
}
}
// Saves the file , it needs to be called so that list does not disappear on refreshing
saveData() {
let name = localStorage.getItem("name");
let author = localStorage.getItem("author");
let radio = localStorage.getItem("radio");
var authorObj;
var radioObj;
var nameObj;
// for radio btn
if (radio == null) {
radioObj = [];
} else {
radioObj = JSON.parse(radio);
}
var buttonValue;
let fiction = document.getElementById("fiction");
let programming = document.getElementById("programming");
let cooking = document.getElementById("cooking");
if (fiction.checked) {
buttonValue = fiction.value;
} else if (programming.checked) {
buttonValue = programming.value;
} else if (cooking.checked) {
buttonValue = cooking.value;
}
radioObj.push(buttonValue);
localStorage.setItem("radio", JSON.stringify(radioObj));
if (author == null) {
authorObj = [];
} else {
authorObj = JSON.parse(author);
}
//for author
let authorValue = document.getElementById("author").value;
authorObj.push(authorValue);
localStorage.setItem("author", JSON.stringify(authorObj));
if (name == null) {
nameObj = []; //no names added
} else {
nameObj = JSON.parse(name);
}
//for book names
let nameValue = document.getElementById("bookName").value;
nameObj.push(nameValue);
localStorage.setItem("name", JSON.stringify(nameObj));
console.log(nameObj); //name objects
console.log(authorObj); //author objects
console.log(radioObj); //radio objects
}
show (type, displayMessage) { //an alert pop up displaying success or failure of entry
let message = document.getElementById("msg");
message.innerHTML = `<div class="alert alert-${type} alert-dismissible fade show" role="alert">
<strong>Message: </strong> ${displayMessage}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>`;
// Timeout function
setTimeout(function () {
message.innerHTML = "";
}, 2000); //disappears after 2 secs
};
}
// Add submit event listener to form
let libraryForm = document.getElementById("libraryForm");
libraryForm.addEventListener('submit', libraryFormSubmit); //submit btn add event listener
function libraryFormSubmit(e)
{
e.preventDefault();
let name = document.getElementById('bookName').value;
let author = document.getElementById("author").value;
var type;
// fiction,programming,cooking
let fiction = document.getElementById("fiction");
let programming = document.getElementById("programming");
let cooking = document.getElementById("cooking");
if(fiction.checked){
type = fiction.value;
}
else if(programming.checked){
type = programming.value;
}
else if(cooking.checked)
{
type = cooking.value;
}
let book = new Book(name,author,type)
console.log(book);
let display = new Display();
if(display.validate(book))
{
display.add(book);
display.show("success", ' Your book has been successfully added');
display.saveData();
}
else{
display.show('danger', ' Sorry this book could not be added');
}
display.clear();
}