about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@fakecake.org>2026-05-24 21:41:52 +0300
committerAndrew Dolgov <fox@fakecake.org>2026-05-24 21:41:52 +0300
commitb782979806bdd3f3e364ceba5171dc6567231cb0 (patch)
tree7cb2adb69c8657486cb19015f17a09417edd56e5 /src
parent4af3c3398db3c214d2216d56a1154a00ba079923 (diff)
downloadthe-epube-b782979806bdd3f3e364ceba5171dc6567231cb0.tar.gz
the-epube-b782979806bdd3f3e364ceba5171dc6567231cb0.tar.xz
switch index generation to frontend
Diffstat (limited to 'src')
-rw-r--r--src/app.js179
1 files changed, 148 insertions, 31 deletions
diff --git a/src/app.js b/src/app.js
index f83817b..c82a5db 100644
--- a/src/app.js
+++ b/src/app.js
@@ -78,6 +78,10 @@ class AppClass extends AppCommon {
 
 	index_mode = "";
 
+	index_query = "";
+
+	index_offset = 0;
+
 	cached_urls = [];
 
 	version = "UNKNOWN";
@@ -108,6 +112,8 @@ class AppClass extends AppCommon {
 		this.initNightMode();
 		this.initCommonEventHandlers();
 
+		this.populateList();
+
 		if ('serviceWorker' in navigator) {
 			navigator.serviceWorker.addEventListener('message', (event) => {
 
@@ -165,33 +171,6 @@ class AppClass extends AppCommon {
 			this.getAll();
 		});
 
-		$(".summary_link").on('click', (e) => {
-			e.preventDefault();
-			this.showSummary(e.currentTarget);
-		});
-
-		$(".fav_item").on('click', (e) => {
-			e.preventDefault();
-			this.toggleFavorite(e.currentTarget);
-		});
-
-		$(".offline_dropitem").on('click', async (e) => {
-			e.preventDefault();
-			const bookId = e.currentTarget.getAttribute("data-book-id");
-			const cacheId = "epube-book." + bookId;
-			const book = await localforage.getItem(cacheId);
-
-			if (book) {
-				this.remove(bookId, () => {
-					this.updateOfflinePrompt(e.currentTarget);
-				});
-			} else {
-				this.get(bookId, () => {
-					this.updateOfflinePrompt(e.currentTarget);
-				});
-			}
-		});
-
 	}
 
 	logout() {
@@ -367,7 +346,7 @@ class AppClass extends AppCommon {
 		if (query)
 			$(".search_query").val(query);
 
-		this.populateList();
+		this.populateOfflineList();
 	}
 
 	get(bookId, callback) {
@@ -496,12 +475,150 @@ class AppClass extends AppCommon {
 		}
 	}
 
+	populateList() {
+		const params = new URLSearchParams({
+			op: "list",
+			mode: this.index_mode,
+			query: this.index_query,
+			offset: String(this.index_offset),
+			limit: String(60),
+		});
+
+		$.get("backend.php?" + params.toString()).done((data) => {
+			const books = $("#books_container");
+			books.html("");
+
+			if (data.books && data.books.length > 0) {
+				data.books.forEach((book) => {
+					const thumb_class = book.is_read ? "read" : "";
+					const title_class = book.in_progress ? "in_progress" : "";
+					const fav_attr = book.is_fav ? "1" : "0";
+					const fav_prompt = book.is_fav ? "Remove from favorites" : "Add to favorites";
+
+					const series_html = book.series_name ?
+						`<div><a title="${this.escapeHtml(book.series_full)}" href="${this.escapeHtml(book.series_link)}">${this.escapeHtml(book.series_full)}</a></div>` :
+						"";
+
+					const author_link = book.author_sort ?
+						`?${new URLSearchParams({query: book.author_sort}).toString()}` :
+						"";
+
+					const download_html = book.download_formats
+						.map((fmt) => {
+							const label_class = fmt.format === "EPUB" ? "label-success" : "label-primary";
+							return `<li><a target="_blank" href="backend.php?op=download&id=${fmt.id}">Download: <span class="label ${label_class}">${fmt.format}</span></a></li>`;
+						})
+						.join("");
+
+					const cell = $(`<div class="col-xxs-6 col-xs-3 col-sm-3 col-md-2" id="cell-${book.id}">
+						${book.read_link ?
+							`<a class="thumbnail ${thumb_class}" href="${book.read_link}">` :
+							`<span class="thumbnail ${thumb_class}">`}
+							<img style="display : none" data-book-id="${book.id}" data-cover-link="${book.cover_link}">
+						${book.read_link ? "</a>" : "</span>"}
+						<div class="caption">
+							<div title="${this.escapeHtml(book.title)}" class="${title_class}">
+								${book.read_link ?
+									`<a href="${book.read_link}">${this.escapeHtml(book.title)}</a>` :
+									this.escapeHtml(book.title)}
+							</div>
+							${series_html}
+							<div><a title="${this.escapeHtml(book.author_sort)}" href="${this.escapeHtml(author_link)}">${this.escapeHtml(book.author_sort)}</a></div>
+						</div>
+						<div class="dropdown" style="white-space : nowrap">
+							<a href="#" data-toggle="dropdown" role="button">More...<span class="caret"></span></a>
+							<ul class="dropdown-menu" aria-labelledby="dLabel">
+								<li><a href="#" class="summary_link" data-book-id="${book.id}">Summary</a></li>
+								<li><a href="#" class="fav_item" data-is-fav="${fav_attr}" data-book-id="${book.id}">${fav_prompt}</a></li>
+								${book.epub_id ? `<li><a href="#" data-book-id="${book.id}" class="offline_dropitem"></a></li><li class="divider"></li>` : ""}
+								${download_html}
+							</ul>
+						</div>
+					</div>`);
+
+					books.append(cell);
+				});
+
+				this.showCovers();
+				this.updateOfflinePrompts();
+
+				$(".summary_link").on('click', (e) => {
+					e.preventDefault();
+					this.showSummary(e.currentTarget);
+				});
+
+				$(".fav_item").on('click', (e) => {
+					e.preventDefault();
+					this.toggleFavorite(e.currentTarget);
+				});
+
+				$(".offline_dropitem").on('click', async (e) => {
+					e.preventDefault();
+					const bookId = e.currentTarget.getAttribute("data-book-id");
+					const cacheId = "epube-book." + bookId;
+					const book = await localforage.getItem(cacheId);
+
+					if (book) {
+						this.remove(bookId, () => {
+							this.updateOfflinePrompt(e.currentTarget);
+						});
+					} else {
+						this.get(bookId, () => {
+							this.updateOfflinePrompt(e.currentTarget);
+						});
+					}
+				});
+			}
+
+			this.updateIndexPagination(data);
+		});
+	}
+
+	updateIndexPagination(data) {
+		const prevLi = $(".pager .previous");
+		const nextLi = $(".pager .next");
+
+		const prevParams = new URLSearchParams({
+			mode: this.index_mode,
+			query: this.index_query,
+			offset: String(this.index_offset - 60),
+		});
+		const nextParams = new URLSearchParams({
+			mode: this.index_mode,
+			query: this.index_query,
+			offset: String(this.index_offset + 60),
+		});
+
+		if (this.index_offset > 0) {
+			prevLi.removeClass("disabled");
+			prevLi.find("a").attr("href", "?" + prevParams.toString());
+		} else {
+			prevLi.addClass("disabled");
+			prevLi.find("a").attr("href", "#");
+		}
+
+		if (data.books && data.books.length >= 60) {
+			nextLi.removeClass("disabled");
+			nextLi.find("a").attr("href", "?" + nextParams.toString());
+		} else {
+			nextLi.addClass("disabled");
+			nextLi.find("a").attr("href", "#");
+		}
+	}
+
+	escapeHtml(str) {
+		if (!str) return "";
+		const div = document.createElement("div");
+		div.appendChild(document.createTextNode(str));
+		return div.innerHTML;
+	}
+
 	async search() {
 		const query = $(".search_query").val();
 
 		await localforage.setItem("epube.search-query", query);
 
-		this.populateList();
+		this.populateOfflineList();
 
 		return false;
 	}
@@ -519,11 +636,11 @@ class AppClass extends AppCommon {
 
 			await Promise.all(promises);
 
-			this.populateList();
+			this.populateOfflineList();
 		}
 	}
 
-	populateList() {
+	populateOfflineList() {
 		let query = this.urlParam("query");
 
 		if (query) query = String(query).toLowerCase();