-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.js
More file actions
148 lines (118 loc) · 4.14 KB
/
file.js
File metadata and controls
148 lines (118 loc) · 4.14 KB
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
const addBook=document.querySelector(".newBook");
const formContainer=document.querySelector(".formContainer");
const body=document.querySelector("body");
const submitButton=document.querySelector(".submit");
const resetButton=document.querySelector(".reset");
const closeDialog=document.querySelector(".closeDialog");
addBook.addEventListener('click', () => {
formContainer.showModal();
});
closeDialog.addEventListener('click', () => {
formContainer.close();
});
const myLibrary=[];
function Book(title,author,pages,read) {
this.title=title;
this.author=author;
this.pages=pages;
this.read=read;
}
Book.prototype.info = function(){
return `${this.title} `;
}
Book.prototype.writer = function(){
return `${this.author}`;
}
Book.prototype.number = function(){
return `${this.pages}`;
}
Book.prototype.right = function(){
return `${this.read}`;
}
Book.prototype.toggleReadStatus = function(){
this.read = !this.read; // On toggling the button,read status is changed.
}
function addBookToLibrary(title,author,pages,read) {
const newBook= new Book(title,author,pages,read);
myLibrary.push(newBook);
displayLibrary();
}
function displayLibrary() {
const library = document.querySelector(".library");
library.innerHTML=""; // We created a section in which details of books will be added.
myLibrary.forEach((book,index) => {
const bookDiv = document.createElement("div");
bookDiv.classList.add("book");
const info=document.createElement("p");
info.textContent="Title: ";
info.textContent += book.info();
bookDiv.appendChild(info);
const writer=document.createElement("p");
writer.textContent="Author: ";
writer.textContent += book.writer();
bookDiv.appendChild(writer);
const number=document.createElement("p");
number.textContent="Pages: ";
number.textContent += book.number();
bookDiv.appendChild(number);
const right=document.createElement("p");
right.classList.add("option");
right.textContent="Status: ";
if(book.right() == "true") right.textContent += "✔ (Read)";
else right.textContent += "✕ (Not Read)";
bookDiv.appendChild(right);
const removeButton = document.createElement("button");
removeButton.textContent = "Remove";
removeButton.classList.add("remove");
removeButton.addEventListener('click', () => {
myLibrary.splice(index,1);
displayLibrary();
});
bookDiv.appendChild(removeButton);
const toggleButton = document.createElement("button");
if(book.right() == "true")
{
toggleButton.textContent = "Not Read";
bookDiv.classList.add("readed");
}
else
{
toggleButton.textContent = "Read";
bookDiv.classList.add("notReaded");
}
toggleButton.classList.add("toggleRead");
toggleButton.addEventListener('click' , () => {
book.toggleReadStatus();
displayLibrary();
})
bookDiv.appendChild(toggleButton);
library.appendChild(bookDiv);
});
}
submitButton.addEventListener('click' , (event) => {
event.preventDefault();
const title=document.querySelector("#title").value;
const author=document.querySelector("#author").value;
const pages=document.querySelector("#pages").value;
const read=document.querySelector("#read").checked;
addBookToLibrary(title,author,pages,read);
document.querySelector("#title").value='';
document.querySelector("#author").value='';
document.querySelector("#pages").value='';
document.querySelector("#read").checked= false;
formContainer.close();
});
/* For Changing of Dark Mode */
let mode = document.querySelector(".myMode");
let cnt=0;
mode.addEventListener( 'click', () => {
if(cnt % 2 == 0){
body.style.backgroundColor = "white";
console.log("white -> Black done");
}
else{
body.style.backgroundColor = "black";
console.log("black -> white done");
}
cnt++;
});