SeriesPage.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. const e = {
  55. id: book._uid,
  56. title,
  57. link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
  58. };
  59. if (query.all) {
  60. e.content = {
  61. '*ATTRS': {type: 'text'},
  62. '*TEXT': this.bookAuthor(book.author),
  63. }
  64. }
  65. entry.push(
  66. this.makeEntry(e)
  67. );
  68. }
  69. }
  70. } else {
  71. if (query.depth == 1 && !query.genre && !query.others) {
  72. entry.push(
  73. this.makeEntry({
  74. id: 'select_genre',
  75. title: '[Выбрать жанр]',
  76. link: this.navLink({href: `/genre?from=${this.id}`}),
  77. })
  78. );
  79. }
  80. //навигация по каталогу
  81. const queryRes = await this.opdsQuery('series', query, '[Остальные серии]');
  82. for (const rec of queryRes) {
  83. const e = {
  84. id: rec.id,
  85. title: (rec.count ? rec.title : `Серия: ${rec.title}`),
  86. link: this.navLink({href: `/${this.id}?series=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
  87. };
  88. if (rec.count) {
  89. e.content = {
  90. '*ATTRS': {type: 'text'},
  91. '*TEXT': `${rec.count} сери${utils.wordEnding(rec.count, 1)}`,
  92. };
  93. }
  94. entry.push(this.makeEntry(e));
  95. }
  96. }
  97. result.entry = entry;
  98. return this.makeBody(result, req);
  99. }
  100. }
  101. module.exports = SeriesPage;