-
Notifications
You must be signed in to change notification settings - Fork 3
/
quotes.html
156 lines (140 loc) · 5.19 KB
/
quotes.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Description" content="The Stoic homepage complete quotes list.">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Quotes list</title>
<link rel="shortcut icon" href="favicon.ico">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<link href="https://fonts.googleapis.com/css?family=Montserrat:300,400|Playfair+Display:400" rel="stylesheet">
</head>
<body>
<nav></nav>
<section class="main">
<h1>Quotes list</h1>
<div class="quotes-filter">
<div class="author-filter-container">
<label for="author-filter">Filter by author:</label>
<select name="author" id="author-filter">
<option value="All">All</option>
</select>
</div>
<div>
<label for="book-filter">Filter by book:</label>
<select name="book" id="book-filter"></select>
</div>
</div>
<div class="quotes-container"></div>
</section>
<footer class="container">
<div class="footer-row">
© Romain Cascino – 2018
</div>
</footer>
<script src="quotes.js"></script>
<script src="nav.js"></script>
<script src="customize.js"></script>
<script>
const body = document.querySelector('body');
const nav = document.querySelector('nav');
const footer = document.querySelector('footer');
const links = document.querySelectorAll('a');
const quotesContainer = document.querySelector('.quotes-container');
const authorFilter = document.querySelector('#author-filter');
const bookFilter = document.querySelector('#book-filter');
nav.innerHTML = navContent;
const resourcesLink = document.querySelector('.resources');
resourcesLink.classList.add('active');
if (localStorage.getItem('theme') && localStorage.getItem('theme') === 'dark') {
setDarkClass(true, [body, footer, nav].concat(Array.from(links)));
}
quotes.sort(sortByReferenceAndAuthor);
quotes.forEach(quote => {
createQuoteDivElement(quote);
});
const authors = [...new Set(quotes.map((quote) => quote.author))];
authors.forEach((author) => {
const authorDivElement = document.createElement('option');
authorDivElement.innerHTML = author;
authorDivElement.value = author;
authorFilter.appendChild(authorDivElement);
});
// Insert all books in the book filter
populateBookFilter([...new Set(quotes.map((quote) => quote.reference.split(',')[0]))]);
authorFilter.addEventListener('change', (e) => {
const author = e.target.value;
populateBookFilter([...new Set(returnQuotesByAuthor(author)
.map((quote) => quote.reference.split(',')[0])
)]);
quotesContainer.innerHTML = '';
returnQuotesByAuthor(author).forEach(quote => {
createQuoteDivElement(quote);
});
});
bookFilter.addEventListener('change', (e) => {
const book = e.target.value;
const author = authorFilter.value;
quotesContainer.innerHTML = '';
returnQuotesByBook(book, author).forEach(quote => {
createQuoteDivElement(quote);
});
});
function createQuoteDivElement(quote) {
const quoteContainer = document.createElement('div');
quoteContainer.classList.add('all-quote-container');
const quoteContent = document.createElement('div');
quoteContent.classList.add('all-quote-content');
const quoteReference = document.createElement('div');
quoteReference.classList.add('all-quote-reference');
quoteContent.innerHTML = quote.content;
quoteReference.innerHTML = quote.reference + ' — ' + quote.author;
quoteContainer.appendChild(quoteContent);
quoteContainer.appendChild(quoteReference);
quotesContainer.appendChild(quoteContainer);
}
function sortByReferenceAndAuthor(a, b) {
const referenceA = a.author.toUpperCase() + a.reference.toUpperCase();
const referenceB = b.author.toUpperCase() + b.reference.toUpperCase();
if (referenceA < referenceB) {
return -1;
}
if (referenceA > referenceB) {
return 1;
}
return 0;
}
function populateBookFilter(books) {
bookFilter.innerHTML = '';
if (books.length > 1) {
const allOption = document.createElement('option');
allOption.innerHTML = 'All';
allOption.value = 'All';
bookFilter.appendChild(allOption);
}
books.forEach((book) => {
const bookDivElement = document.createElement('option');
bookDivElement.innerHTML = book;
bookDivElement.value = book;
bookFilter.appendChild(bookDivElement);
});
}
function returnQuotesByAuthor(author) {
if (author === 'All') {
return quotes;
} else {
return quotes.filter((quote) => quote.author === author);
}
}
function returnQuotesByBook(book, author) {
if (book === 'All') {
return returnQuotesByAuthor(author);
} else {
return returnQuotesByAuthor(author).filter((quote) => quote.reference.split(',')[0] === book);
}
}
</script>
<script src="fathom.js"></script>
</body>
</html>