SeriesPage.js 4.3 KB

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