-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
205 lines (167 loc) · 5.56 KB
/
index.js
File metadata and controls
205 lines (167 loc) · 5.56 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
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
// --------------------------
//#region Initialization
// --------------------------
const bookshelfElement = document.querySelector(".books");
const bookshelf = new Bookshelf(bookshelfElement);
bookshelf.seed(bookData);
//#endregion Initialization
// --------------------------
//#region Favorite Feature
// --------------------------
const favCount = document.querySelector(".favCount");
const updateBtn = document.querySelector(".favUpdateBtn");
updateBtn.addEventListener("click", () => {
favCount.textContent = bookshelf.countFavoriteBooks();
});
//#endregion Favorite Feature
// --------------------------
//#region Searching
// --------------------------
const searchInput = document.querySelector(".search input");
const searchBtn = document.querySelector(".searchBtn");
// NOTE: This only searches through the titles of the books!
searchBtn.addEventListener("click", () => {
const query = searchInput.value
.toLowerCase()
.split()
.filter((word) => word !== "the");
const searchFn = (b) => b.title.toLowerCase().includes(query);
bookshelf.filterVisibleBooks(searchFn);
});
//#endregion Searching
// --------------------------
//#region Sorting
// --------------------------
const sortBy = document.querySelector(".sortBy");
// NOTE: This only sorts by the titles of the books!
sortBy.addEventListener("change", () => {
const query = sortBy.value;
let sortFn;
if (query === "titleaz") {
sortFn = (a, z) => a.title.localeCompare(z.title);
} else if (query === "titleza") {
sortFn = (a, z) => z.title.localeCompare(a.title);
}
bookshelf.sortVisibleBooks(sortFn);
});
//#endregion Sorting
// --------------------------
//#region Add Book Form
// --------------------------
const addBook = document.querySelector(".book-form");
addBook.addEventListener("submit", (event) => {
event.preventDefault();
const authorInputs = document.querySelector("#author").value;
const author = authorInputs.split();
const language = document.querySelector("#language").value;
const subjectInputs = document.querySelector("#subject").value;
const subject = subjectInputs.split(",");
const title = document.querySelector("#title").value;
if (title === "" || author === [] || language === "" || subject === []) {
alert("Please fill in required fields");
} else {
const book = new Book(
author,
language,
subject,
title,
(comments = []),
bookshelf
);
bookshelf.addBook(book);
bookshelf.render();
bookshelf.clearFields();
}
});
//#endregion Add Book Form
// --------------------------
//#region Book Count Dropdown Feature
// --------------------------
const dropdown = document.querySelector(".dropdown");
dropdown.addEventListener("click", (event) => {
if (dropdown.classList.contains("closed")) {
dropdown.classList.remove("closed");
} else {
dropdown.classList.add("closed");
}
});
//#endregion Book Count Dropdown Feature
// --------------------------
//#region Book Count Feature
// --------------------------
const bookCount = document.querySelector(".bookCount");
const countBtn = document.querySelector(".countBtn");
countBtn.addEventListener("click", () => {
bookCount.innerText = bookshelf.bookCount();
});
const forLangBooks = document.querySelector(".nonEngCount");
const nonEngBtn = document.querySelector(".nonEngBtn");
nonEngBtn.addEventListener("click", () => {
forLangBooks.innerText = bookshelf.booksByLanguage();
});
const avgSubNum = document.querySelector(".avgSubCount");
const avgSubBtn = document.querySelector(".avgSubBtn");
avgSubBtn.addEventListener("click", () => {
avgSubNum.innerText = bookshelf.averageSubjects();
});
//#endregion Book Count Feature
// --------------------------
//#region Registration Feature
// --------------------------
const errorMsg = document.querySelector(".container-modal-content--error");
const successMsg = document.querySelector(".container-modal-content--success");
const userName = document.querySelector("#userName");
const userPassword = document.querySelector("#userPassword");
const registrationForm = document.getElementById("regForm");
const modal = document.querySelector(".container-msg-modal");
const modalContent = document.querySelectorAll(".container-modal-content");
const menu = document.getElementById("menuList");
menu.style.display = "none";
//-----------------------
//USE THIS DEFAULT LOGIN
//-----------------------
const login = {
userName: "bookshelf",
password: "bookshelf",
};
registrationForm.addEventListener("submit", (event) => {
event.preventDefault();
userLogin();
});
function userLogin() {
const nameVal = userName.value,
passwordVal = userPassword.value;
let isLogin = true;
if (nameVal === login.userName && passwordVal === login.password) {
verifyLogin(isLogin);
seeHomepage();
} else {
verifyLogin(!isLogin);
}
}
function verifyLogin(isLogin) {
modal.classList.add("enabled");
if (isLogin) {
successMsg.classList.add("enabled");
} else {
errorMsg.classList.add("enabled");
}
setTimeout(function () {
modal.classList.remove("enabled");
registrationForm.reset();
modalContent.forEach((content) => {
content.classList.remove("enabled");
});
}, 2000);
}
function seeHomepage() {
const registration = document.getElementById("registration");
const home = document.getElementById("home");
const about = document.getElementById("about");
const contact = document.getElementById("contact");
registration.style.display = "none";
home.style.display = "flex";
about.style.display = "none";
contact.style.display = "none";
menu.style.display = "flex";
}