-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (59 loc) · 2.2 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
import BookCollection from './modules/bookcollection.js';
import currentDate from './modules/currentDate.js';
// Current date
const currentDateElement = document.getElementById('current-date');
currentDateElement.textContent = currentDate;
// Create a book collection instance
const bookCollection = new BookCollection();
// Navigation links
const navLinks = document.getElementsByClassName('nav-link');
// Content sections
const sections = {
books: document.getElementById('books-section'),
'add-book': document.getElementById('add-book-section'),
'contact-info': document.getElementById('contact-info-section'),
};
// Function to switch active section
function switchSection(event) {
event.preventDefault();
const { section } = event.target.dataset;
// Remove active class from current section
const currentSection = document.querySelector('.content-section.active');
currentSection.classList.remove('active');
// Add active class to selected section
sections[section].classList.add('active');
// If the selected section is the books section, render the books
if (section === 'books') {
bookCollection.loadFromLocalStorage();
bookCollection.renderBooks();
}
// Remove active class from current link
const currentLink = document.querySelector('.nav-link.active');
currentLink.classList.remove('active');
// Add active class to selected link
event.target.classList.add('active');
}
window.addEventListener('load', () => {
bookCollection.loadFromLocalStorage();
bookCollection.renderBooks();
});
// Attach event listeners to navigation links
for (let i = 0; i < navLinks.length; i += 1) {
const link = navLinks[i];
link.addEventListener('click', switchSection);
}
// Get the add form and input elements
const addForm = document.getElementById('add-form');
const titleInput = document.getElementById('title-input');
const authorInput = document.getElementById('author-input');
// Attach event listener to the add form
addForm.addEventListener('submit', (event) => {
event.preventDefault();
const title = titleInput.value;
const author = authorInput.value;
if (title && author) {
bookCollection.addBook(title, author);
titleInput.value = '';
authorInput.value = '';
}
});