TitlePage.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. const BasePage = require('./BasePage');
  2. class TitlePage extends BasePage {
  3. constructor(config) {
  4. super(config);
  5. this.id = 'title';
  6. this.title = 'Книги';
  7. }
  8. async body(req) {
  9. const result = {};
  10. const query = {
  11. title: req.query.title || '',
  12. genre: req.query.genre || '',
  13. del: 0,
  14. depth: 0,
  15. };
  16. query.depth = query.title.length + 1;
  17. if (query.title == '___others') {
  18. query.title = '';
  19. query.depth = 1;
  20. query.others = true;
  21. }
  22. const entry = [];
  23. if (query.title && query.title[0] == '=') {
  24. //книги по названию
  25. const res = await this.webWorker.search('title', query);
  26. if (res.found.length) {
  27. const books = res.found[0].books || [];
  28. const filtered = this.filterBooks(books, query);
  29. for (const book of filtered) {
  30. const title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'} (${book.ext})`;
  31. entry.push(
  32. this.makeEntry({
  33. id: book._uid,
  34. title,
  35. link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
  36. content: {
  37. '*ATTRS': {type: 'text'},
  38. '*TEXT': this.bookAuthor(book.author),
  39. },
  40. })
  41. );
  42. }
  43. }
  44. } else {
  45. if (query.depth == 1 && !query.genre && !query.others) {
  46. entry.push(
  47. this.makeEntry({
  48. id: 'select_genre',
  49. title: '[Выбрать жанр]',
  50. link: this.navLink({href: `/genre?from=${this.id}`}),
  51. })
  52. );
  53. }
  54. //навигация по каталогу
  55. const queryRes = await this.opdsQuery('title', query, '[Остальные названия]');
  56. for (const rec of queryRes) {
  57. entry.push(
  58. this.makeEntry({
  59. id: rec.id,
  60. title: rec.title,
  61. link: this.navLink({href: `/${this.id}?title=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
  62. })
  63. );
  64. }
  65. }
  66. result.entry = entry;
  67. return this.makeBody(result, req);
  68. }
  69. }
  70. module.exports = TitlePage;