SeriesPage.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const BasePage = require('./BasePage');
  2. class SeriesPage extends BasePage {
  3. constructor(config) {
  4. super(config);
  5. this.id = 'series';
  6. this.title = 'Серии';
  7. }
  8. sortSeriesBooks(seriesBooks) {
  9. seriesBooks.sort((a, b) => {
  10. const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
  11. const dtitle = a.title.localeCompare(b.title);
  12. const dext = a.ext.localeCompare(b.ext);
  13. return (dserno ? dserno : (dtitle ? dtitle : dext));
  14. });
  15. return seriesBooks;
  16. }
  17. async body(req) {
  18. const result = {};
  19. const query = {
  20. series: req.query.series || '',
  21. genre: req.query.genre || '',
  22. del: 0,
  23. all: req.query.all || '',
  24. depth: 0,
  25. };
  26. query.depth = query.series.length + 1;
  27. if (query.series == '___others') {
  28. query.series = '';
  29. query.depth = 1;
  30. query.others = true;
  31. }
  32. const entry = [];
  33. if (query.series && query.series[0] == '=') {
  34. //книги по серии
  35. const bookList = await this.webWorker.getSeriesBookList(query.series.substring(1));
  36. if (bookList.books) {
  37. let books = JSON.parse(bookList.books);
  38. const booksAll = this.filterBooks(books, {del: 0});
  39. const filtered = (query.all ? booksAll : this.filterBooks(books, query));
  40. const sorted = this.sortSeriesBooks(filtered);
  41. if (booksAll.length > filtered.length) {
  42. entry.push(
  43. this.makeEntry({
  44. id: 'all_series_books',
  45. title: '[Все книги серии]',
  46. link: this.navLink({
  47. href: `/${this.id}?series=${encodeURIComponent(query.series)}&all=1`}),
  48. })
  49. );
  50. }
  51. for (const book of sorted) {
  52. const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'} (${book.ext})`;
  53. const e = {
  54. id: book._uid,
  55. title,
  56. link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
  57. };
  58. if (query.all) {
  59. e.content = {
  60. '*ATTRS': {type: 'text'},
  61. '*TEXT': this.bookAuthor(book.author),
  62. }
  63. }
  64. entry.push(
  65. this.makeEntry(e)
  66. );
  67. }
  68. }
  69. } else {
  70. if (query.depth == 1 && !query.genre && !query.others) {
  71. entry.push(
  72. this.makeEntry({
  73. id: 'select_genre',
  74. title: '[Выбрать жанр]',
  75. link: this.navLink({href: `/genre?from=${this.id}`}),
  76. })
  77. );
  78. }
  79. //навигация по каталогу
  80. const queryRes = await this.opdsQuery('series', query, '[Остальные серии]');
  81. for (const rec of queryRes) {
  82. entry.push(
  83. this.makeEntry({
  84. id: rec.id,
  85. title: rec.title,
  86. link: this.navLink({href: `/${this.id}?series=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
  87. })
  88. );
  89. }
  90. }
  91. result.entry = entry;
  92. return this.makeBody(result, req);
  93. }
  94. }
  95. module.exports = SeriesPage;