TitlePage.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. const e = {
  58. id: rec.id,
  59. title: rec.title,
  60. link: this.navLink({href: `/${this.id}?title=${rec.q}&genre=${encodeURIComponent(query.genre)}`}),
  61. };
  62. if (rec.count) {
  63. e.content = {
  64. '*ATTRS': {type: 'text'},
  65. '*TEXT': `${rec.count} названий`,
  66. };
  67. }
  68. entry.push(this.makeEntry(e));
  69. }
  70. }
  71. result.entry = entry;
  72. return this.makeBody(result, req);
  73. }
  74. }
  75. module.exports = TitlePage;