-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
146 lines (126 loc) · 4.41 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
/* eslint-disable max-classes-per-file */
const bookList = document.getElementById('booklist');
const dateDiv = document.getElementById('date');
const bookContainer = document.getElementById('book-con');
const addBookContainer = document.getElementById('add-book');
const contactInformation = document.getElementById('cont-info');
const list = document.getElementById('list');
const addNew = document.getElementById('add-new');
const contact = document.getElementById('contact');
addNew.addEventListener('click', (e) => {
e.target.classList.add('active');
addBookContainer.classList.remove('hide');
list.classList.remove('active');
bookContainer.classList.add('hide');
contact.classList.remove('active');
contactInformation.classList.add('hide');
});
list.addEventListener('click', (e) => {
e.target.classList.add('active');
bookContainer.classList.remove('hide');
addNew.classList.remove('active');
addBookContainer.classList.add('hide');
contact.classList.remove('active');
contactInformation.classList.add('hide');
});
contact.addEventListener('click', (e) => {
e.target.classList.add('active');
contactInformation.classList.remove('hide');
list.classList.remove('active');
bookContainer.classList.add('hide');
addNew.classList.remove('active');
addBookContainer.classList.add('hide');
});
class MyTime {
nth = (d) => {
if (d > 3 && d < 21) return 'th';
switch (d % 10) {
case 1: return 'st';
case 2: return 'nd';
case 3: return 'rd';
default: return 'th';
}
}
formatedTime = () => {
const date = new Date();
const [hr, pm] = date.toLocaleString('en-US', { hour: 'numeric', hour12: true }).toString().split(' ');
const timeString = `${date.toLocaleString('default', { month: 'long' })} ${date.getDate()}${this.nth(date.getDate())} ${date.getFullYear()} ${hr}:${date.getMinutes()}:${date.getSeconds()} ${pm.toLowerCase()}`;
return timeString;
}
}
const appTime = new MyTime();
setInterval(() => { dateDiv.textContent = appTime.formatedTime(); }, 1000);
class Book {
constructor(title, author, id) {
this.title = title;
this.author = author;
this.id = id;
}
}
class UI {
books = [];
addBook(book) {
this.books.push(book);
localStorage.setItem('books', JSON.stringify(this.books));
return book;
}
createRemoveBtn() {
const removeBtn = document.createElement('button');
removeBtn.textContent = 'Remove';
removeBtn.addEventListener('click', (e) => {
// remove element from interaface
const id = +e.target.parentElement.id;
e.target.parentElement.remove();
// remove element from array
const newBooks = this.books.filter((book) => book.id !== id);
this.books = newBooks;
localStorage.setItem('books', JSON.stringify(this.books));
return this.books;
});
return removeBtn;
}
// Add a book to the application interface
addBkToInterface(book) {
// create div element to hold the title & author
const newBook = document.createElement('div');
newBook.setAttribute('id', book.id);
// create title div
const title = document.createElement('p');
title.textContent = `"${book.title}"`;
newBook.appendChild(title);
const by = document.createElement('p');
by.textContent = 'by';
newBook.appendChild(by);
// create author div
const author = document.createElement('p');
author.textContent = book.author;
newBook.appendChild(author);
// Add a remove button to the book div
newBook.appendChild(this.createRemoveBtn());
// create a horizontal rule & append to div
bookList.append(newBook);
return newBook;
}
}
const addBtn = document.getElementById('add-btn');
const ui = new UI();
addBtn.addEventListener('click', () => {
const author = document.getElementById('author').value;
const title = document.getElementById('title').value;
if (author && title) {
// add book to array
const book = new Book(title, author, ui.books.length + 1);
ui.addBook(book);
// add book to the interface
ui.addBkToInterface(book);
// clear the form fields
document.getElementById('author').value = '';
document.getElementById('title').value = '';
}
// if any field is empty, do nothing
});
document.addEventListener('DOMContentLoaded', () => {
if (!localStorage.getItem('books')) return;
const books = JSON.parse(localStorage.getItem('books'));
books.forEach((book) => ui.addBkToInterface(book));
});