library/frontend/files/index.html

244 lines
7.2 KiB
HTML
Raw Normal View History

2021-06-26 18:08:35 +00:00
<!DOCTYPE html>
<html>
<head>
<title>Library</title>
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css"
/>
2021-08-05 02:59:23 +00:00
<link rel="stylesheet" href="style.css" />
2021-06-26 18:08:35 +00:00
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link
2021-08-05 02:59:23 +00:00
href="https://fonts.googleapis.com/css?family=Libre+Baskerville:400,700&display=swap"
2021-08-09 01:23:43 +00:00
as="style"
rel="stylesheet preload prefetch"
2021-06-26 18:08:35 +00:00
/>
<script type="text/javascript">
var sortState = {
sortBy: "sortAuthor",
2021-06-26 18:08:35 +00:00
sortOrder: "asc",
};
function init() {
fetch("/api")
.then((response) => response.json())
2021-08-04 01:08:33 +00:00
.then((books) => {
// prepare response
books.forEach((book) => {
book.sortTitle = titleCleaner(book.title);
if (!book["isbn-10"] && book["isbn-13"]) {
book["isbn-10"] = ISBNfromEAN(book["isbn-13"]);
}
if (!book.coverurl && book["isbn-10"]) {
book.coverurl =
`https://images-na.ssl-images-amazon.com/images/P/` +
book["isbn-10"] +
`.01.LZZ.jpg`;
}
});
return books;
})
.then((books) => {
document.getElementById("search").addEventListener("input", (e) => {
search(books, e.target.value);
});
return books;
})
.then(renderTable);
2021-08-03 01:31:54 +00:00
}
2021-06-26 18:08:35 +00:00
2021-08-03 01:31:54 +00:00
function search(books, searchBy) {
searchBy = searchCleaner(searchBy);
if (searchBy !== "") {
books = books.filter(
({ title, authors, genre, publisher, series, year }) => {
return Object.values({
title,
authors: authors.join(" "),
genre,
publisher,
series,
year,
}).find((field) => searchCleaner(field).indexOf(searchBy) !== -1);
}
);
}
renderTable(books);
2021-06-26 18:08:35 +00:00
}
2021-08-03 01:31:54 +00:00
function renderTable(books, sortField) {
2021-06-26 18:08:35 +00:00
if (sortField) {
2021-08-03 01:31:54 +00:00
if (sortState.sortBy === sortField && sortState.sortOrder === "asc") {
sortState.sortOrder = "desc";
2021-06-26 18:08:35 +00:00
} else {
2021-08-03 01:31:54 +00:00
sortState.sortOrder = "asc";
2021-06-26 18:08:35 +00:00
}
sortState.sortBy = sortField;
}
2021-08-03 01:31:54 +00:00
books.sort((one, two) =>
(one[sortState.sortBy] + one["sortTitle"]).localeCompare(
two[sortState.sortBy] + two["sortTitle"]
)
2021-06-26 18:08:35 +00:00
);
2021-08-03 01:31:54 +00:00
if (sortState.sortOrder === "desc") {
books.reverse();
}
2021-08-04 01:08:33 +00:00
books.forEach((e, i) => (e.rowNumber = i)); // re-key
2021-06-26 18:08:35 +00:00
2021-08-04 01:08:33 +00:00
// rendering
2021-08-05 02:31:48 +00:00
var bookElement = document.getElementById("books");
bookElement.innerHTML = TableTemplate(books);
// add listeners for selecting book to view
Array.from(bookElement.querySelectorAll("tbody tr"))
.slice(1) // remove header from Array
.forEach((row) => {
row.addEventListener("click", (e) => {
// add listener to swap current book
document.getElementById("current").innerHTML = BookTemplate(
books[e.currentTarget.id]
);
});
});
// add sorting callbacks
Array.from(
bookElement.querySelectorAll("tbody tr th[data-sort-by]")
).forEach((row) => {
row.addEventListener("click", function (e) {
renderTable(books, e.target.dataset.sortBy); // only add callback when there's a sortBy attribute
2021-06-26 18:08:35 +00:00
});
});
2021-08-05 02:31:48 +00:00
// mark currently active column
bookElement
.querySelector("tbody tr th[data-sort-by=" + sortState.sortBy + "]")
.classList.add(sortState.sortOrder);
2021-06-26 18:08:35 +00:00
}
function titleCleaner(title) {
return title
.replace('"', "")
.replace(":", "")
.replace(/^(An?|The)\s/i, "");
}
2021-08-03 01:31:54 +00:00
function searchCleaner(str) {
return str
.toLowerCase()
2021-10-31 23:25:44 +00:00
.replaceAll('"', "")
.replaceAll(":", "")
.replaceAll("'", "")
.replaceAll(" ", "");
2021-08-03 01:31:54 +00:00
}
function ISBNfromEAN(EAN) {
2021-06-26 18:08:35 +00:00
ISBN = EAN.slice(3, 12);
var checkdigit =
(11 -
2021-08-03 01:31:54 +00:00
(ISBN.split("").reduce((s, n, k) => s + n * (10 - k), 0) % 11)) %
2021-06-26 18:08:35 +00:00
11;
2021-08-03 01:31:54 +00:00
return ISBN + (checkdigit === 10 ? "X" : checkdigit);
2021-06-26 18:08:35 +00:00
}
2021-08-04 00:10:24 +00:00
function BookTemplate({
"isbn-13": isbn13,
authors,
coverurl,
description,
format,
notes,
onLoan,
publisher,
series,
signed,
title,
volume,
year,
}) {
return `${coverurl ? `<img src="${coverurl}"/>` : ""}
<h1 ${onLoan ? "class='onLoan' " : ""}>${title}</h1>
<h2>${authors}</h2>
<span>${isbn13}</span><br/>
<span>${publisher}, ${year}</span><br/>
${
series
? `<span>${series}${volume ? `, Volume ${volume}` : ""}</span><br/>`
: ""
}
${signed ? "<span>Signed by the author ✒</span><br/>" : ""}
<span>${format}</span>
${onLoan ? `<h2 class="onLoan">On loan to ${onLoan}</h2>` : ""}
<div class="description">
<p>${description}</p>
${notes ? `<span>Notes:</span><p>${notes}</p>` : ""}
</div>`;
}
function TableRowTemplate({
"isbn-13": isbn13,
authors,
onLoan,
publisher,
rowNumber,
signed,
title,
year,
}) {
return `<tr class="tRow ${onLoan ? "onLoan" : ""}" id="${rowNumber}">
<td class="title">
${title} ${
signed
? '<span class="signed" title="Signed by the author" ></span>'
: ""
}
</td>
<td class="author">${authors}</td>
<td class="publisher">${publisher}</td>
<td class="year">${year}</td>
<td class="isbn">${isbn13}</td>
</tr>`;
}
function TableTemplate(books) {
return `<table class="bookTable">
<tr>
<th data-sort-by="sortTitle" class="tHeader title">Title</th>
<th data-sort-by="sortAuthor" class="tHeader author">Author</th>
<th data-sort-by="publisher" class="tHeader publisher">Publisher</th>
<th data-sort-by="year" class="tHeader year">Year</th>
<th class="tHeader isbn">ISBN</th>
</tr>${books.reduce((acc, book) => {
return acc.concat(TableRowTemplate(book));
}, "")} </table>`;
}
2021-08-04 01:08:33 +00:00
window.addEventListener("DOMContentLoaded", () => {
init();
});
2021-06-26 18:08:35 +00:00
</script>
</head>
<body>
<div class="wrapper">
<div id="header">
<h1>Library</h1>
2021-08-05 02:59:23 +00:00
<a
target="_blank"
rel="noreferrer"
href="https://git.yetaga.in/alazyreader/library"
2021-06-26 18:08:35 +00:00
>git</a
>
<div id="searchBox">
<input
id="search"
type="text"
name="search"
placeholder="Search..."
/>
</div>
</div>
<div id="current">No Book Selected</div>
<div id="books"></div>
<!-- Table goes here -->
</div>
</body>
</html>