TitlePage.js 3.2 KB

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