diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-06-15 20:26:59 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-06-15 20:26:59 +0300 |
| commit | 2be5ac92f7c7a7a195c90236b03c24c3d138d6c9 (patch) | |
| tree | a67f4201deabdc7bb3c4311447517e7259df9c4d /src | |
| parent | 39389e6a59249b644fdc7a96bfd73c3ee01ce2e7 (diff) | |
| download | the-epube-2be5ac92f7c7a7a195c90236b03c24c3d138d6c9.tar.gz the-epube-2be5ac92f7c7a7a195c90236b03c24c3d138d6c9.tar.xz | |
unify rendering of book cells between offline and online list modes
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.js | 239 | ||||
| -rw-r--r-- | src/index.d.ts | 19 |
2 files changed, 130 insertions, 128 deletions
diff --git a/src/app.js b/src/app.js index 3734e11..df9c890 100644 --- a/src/app.js +++ b/src/app.js @@ -270,6 +270,12 @@ class AppClass extends AppCommon { }); } + updateFavoritePrompt(elem, is_fav) { + $(elem) + .text(is_fav ? "Remove from favorites" : "Add to favorites") + .attr('data-is-fav', is_fav ? 1 : 0); + } + toggleFavorite(elem) { const bookId = elem.getAttribute("data-book-id"); @@ -277,15 +283,7 @@ class AppClass extends AppCommon { $.post("backend.php", {op: "togglefav", id: bookId}, (data) => { if (data) { - let msg = "[Error]"; - - if (data.status == 0) { - msg = "Add to favorites"; - } else if (data.status == 1) { - msg = "Remove from favorites"; - } - - $(elem).html(msg).attr('data-is-fav', data.status); + this.updateFavoritePrompt(elem, data.status == "1"); if (this.index_mode == "favorites" && data.status == 0) { $("#cell-" + bookId).remove(); @@ -523,19 +521,6 @@ class AppClass extends AppCommon { 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"; @@ -543,64 +528,28 @@ class AppClass extends AppCommon { }) .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>`); + const menuItems = [ + `<li><a href="#" class="summary_link" data-book-id="${book.id}">Summary</a></li>`, + `<li><a href="#" class="fav_item" data-is-fav="${book.is_fav ? "1" : "0"}" + data-book-id="${book.id}">${book.is_fav ? "Remove from favorites" : "Add to favorites"}</a></li>`, + ]; - books.append(cell); - }); + if (book.epub_id) { + menuItems.push(`<li><a href="#" data-book-id="${book.id}" class="offline_dropitem"></a></li>`); + menuItems.push('<li class="divider"></li>'); + } - this.showCovers(); - this.updateOfflinePrompts(); + menuItems.push(download_html); - $(".summary_link").on('click', (e) => { - e.preventDefault(); - this.showSummary(e.currentTarget); - }); + const cell = this.renderBookCell(book, { + menuItems: menuItems, + }); - $(".fav_item").on('click', (e) => { - e.preventDefault(); - this.toggleFavorite(e.currentTarget); + books.append(cell); }); - $(".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.showCovers(); + this.updateOfflinePrompts(); } this.updateIndexPagination(data); @@ -658,6 +607,75 @@ class AppClass extends AppCommon { return div.innerHTML; } + /** + * Render a book cell DOM element. + * @param {BookInfo} book - Book data object. + * @param {Object} opts - Rendering options. + * @param {string[]} opts.menuItems - Array of <li> menu item HTML strings. + * @param {String} [opts.seriesUrl] - Search by series override + * @param {String} [opts.authorUrl] - Search by author override + * @returns {jQuery} The created cell element. + */ + renderBookCell(book, opts) { + console.log('renderBookCell', book, opts); + + const cellThumbClass = book.is_read ? "read" : ""; + const cellTitleClass = book.in_progress ? "in_progress" : ""; + + const isOpenable = book.epub_id != 0; + + const authorUrl = opts.authorUrl ?? "?" + new URLSearchParams({"query": book.author_sort, "mode": this.index_mode}).toString(); + const seriesUrl = opts.seriesUrl ?? "?" + new URLSearchParams({"query": book.series_name, "mode": this.index_mode}).toString(); + + const readUrl = `read.html?id=${book.epub_id}&b=${book.id}&rt=${this.index_mode}`; + + const titleHtml = isOpenable ? + `<a href="${readUrl}">${this.escapeHtml(book.title)}</a>` : + this.escapeHtml(book.title); + + const authorHtml = `<a title="${this.escapeHtml(book.author_sort)}" href="${authorUrl}"> + ${this.escapeHtml(book.author_sort)} + </a>`; + + const seriesHtml = book.series_name ? + `<div><a class="series_link" href="${seriesUrl}"> + ${this.escapeHtml(book.series_name) + " [" + book.series_index + "]"} + </a></div>` : ''; + + const cell = $(`<div class="col-xxs-6 col-xs-3 col-sm-3 col-md-2" id="cell-${book.id}"> + ${isOpenable ? + `<a class="thumbnail ${cellThumbClass}" href="${readUrl}">` : + `<span class="thumbnail ${cellThumbClass}">`} + <img style="display : none" data-book-id="${book.id}" data-cover-link="${book.cover_link || ''}"> + ${isOpenable ? "</a>" : "</span>"} + <div class="caption"> + <div title="${this.escapeHtml(book.title)}" class="${cellTitleClass}"> + ${titleHtml} + </div> + ${seriesHtml} + <div>${authorHtml}</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"> + ${opts.menuItems.join('\n')} + </ul> + </div> + </div>`); + + cell.find('.summary_link').on('click', (evt) => { + evt.preventDefault(); + this.showSummary(evt.currentTarget); + }); + + cell.find('.fav_item').on('click', (evt) => { + evt.preventDefault(); + this.toggleFavorite(evt.currentTarget); + }); + + return cell; + } + async search() { const query = $(".search_query").val(); @@ -685,15 +703,14 @@ class AppClass extends AppCommon { } } - populateOfflineList() { + populateOfflineList() { let query = this.urlParam("query"); - if (query) query = String(query).toLowerCase(); const books = $("#books_container"); books.html(""); - localforage.iterate((value, key/*, i*/) => { + localforage.iterate((value, key) => { if (key.match(/epube-book\.\d{1,}$/)) { Promise.all([ @@ -704,6 +721,8 @@ class AppClass extends AppCommon { ]).then((results) => { if (results[0] && results[3]) { + + /** @type BookInfo */ const info = results[0]; if (query) { @@ -716,51 +735,24 @@ class AppClass extends AppCommon { } let cover = ""; - - if (results && results[1]) { + if (results[1]) { cover = URL.createObjectURL(results[1]); } - let in_progress = false; - let is_read = false; - const lastread = results[2]; - if (lastread) { - in_progress = lastread.page > 0; - is_read = lastread.total > 0 && lastread.total - lastread.page < 5; - } - - const thumb_class = is_read ? "read" : ""; - const title_class = in_progress ? "in_progress" : ""; - - const series_link = info.series_name ? `<div><a class="series_link" href="#">${info.series_name + " [" + info.series_index + "]"}</a></div>` : ""; - - const cell = $(`<div class="col-xxs-6 col-xs-4 col-sm-3 col-md-2" id="cell-${info.id}"> - <a class="thumbnail ${thumb_class}" href="read.html?id=${info.epub_id}&b=${info.id}"> - <img style="display : none"> - </a> - <div class="caption"> - <div><a class="${title_class}" href="read.html?id=${info.epub_id}&b=${info.id}">${info.title}</a></div> - <div><a class="author_link" href="#">${info.author_sort}</a></div> - ${series_link} - </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"> - <li><a href="#" class="offline_summary" data-book-id="${info.id}">Summary</a></li> - <li><a href="#" class="remove_offline_data" data-book-id="${info.id}">Remove offline data</a></li> - </ul> - </div> - </div>`); - - $(cell).find(".offline_summary").on('click', (e) => { - e.preventDefault(); - this.showSummary(e.currentTarget); - }); - - $(cell).find(".remove_offline_data").on('click', (e) => { - e.preventDefault(); - this.removeFromList(e.currentTarget); + info.in_progress = lastread ? lastread.page > 0 : false; + info.is_read = lastread ? (lastread.total > 0 && lastread.total - lastread.page < 5) : false; + + const authorUrl = `offline.html?query=${encodeURIComponent(info.author_sort)}`; + const seriesUrl = `offline.html?query=${encodeURIComponent(info.series_name)}`; + + const cell = this.renderBookCell(info, { + menuItems: [ + `<li><a href="#" class="summary_link" data-book-id="${info.id}">Summary</a></li>`, + `<li><a href="#" class="remove_offline_data" data-book-id="${info.id}">Remove offline data</a></li>`, + ], + authorUrl: authorUrl, + seriesUrl: seriesUrl, }); if (cover) { @@ -768,18 +760,9 @@ class AppClass extends AppCommon { .css("background-image", "url(" + cover + ")") .fadeIn(); } else { - cell - .fadeIn(); + cell.fadeIn(); } - cell.find(".series_link") - .attr("title", info.series_name + " [" + info.series_index + "]") - .attr("href", "offline.html?query=" + encodeURIComponent(info.series_name)); - - cell.find(".author_link") - .attr("title", info.author_sort) - .attr("href", "offline.html?query=" + encodeURIComponent(info.author_sort)); - books.append(cell); } }); diff --git a/src/index.d.ts b/src/index.d.ts index 4bf1153..d7a8861 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -10,6 +10,25 @@ declare interface EpubeApp { setOffline(offline: boolean): void; } +declare interface DownloadFormat { + format: string; + id: number; +} + +declare interface BookInfo { + id: number; + title: string; + author_sort: string; + series_name: string; + series_index: number; + cover_link: string; + in_progress: boolean; + is_read: boolean; + is_fav: boolean; + epub_id: number; + download_formats: DownloadFormat[]; +} + declare var EpubeApp: EpubeApp; declare var localforage: LocalForage; |
