AuthorPage.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const BasePage = require('./BasePage');
  2. class AuthorPage extends BasePage {
  3. constructor(config) {
  4. super(config);
  5. this.id = 'author';
  6. this.title = 'Авторы';
  7. }
  8. bookAuthor(author) {
  9. if (author) {
  10. let a = author.split(',');
  11. return a.slice(0, 3).join(', ') + (a.length > 3 ? ' и др.' : '');
  12. }
  13. return '';
  14. }
  15. sortBooks(bookList) {
  16. //схлопывание серий
  17. const books = [];
  18. const seriesSet = new Set();
  19. for (const book of bookList) {
  20. if (book.series) {
  21. if (!seriesSet.has(book.series)) {
  22. books.push({
  23. type: 'series',
  24. book
  25. });
  26. seriesSet.add(book.series);
  27. }
  28. } else {
  29. books.push({
  30. type: 'book',
  31. book
  32. });
  33. }
  34. }
  35. //сортировка
  36. books.sort((a, b) => {
  37. if (a.type == 'series') {
  38. return (b.type == 'series' ? a.book.series.localeCompare(b.book.series) : -1);
  39. } else {
  40. return (b.type == 'book' ? a.book.title.localeCompare(b.book.title) : 1);
  41. }
  42. });
  43. return books;
  44. }
  45. sortSeriesBooks(seriesBooks) {
  46. seriesBooks.sort((a, b) => {
  47. const dserno = (a.serno || Number.MAX_VALUE) - (b.serno || Number.MAX_VALUE);
  48. const dtitle = a.title.localeCompare(b.title);
  49. const dext = a.ext.localeCompare(b.ext);
  50. return (dserno ? dserno : (dtitle ? dtitle : dext));
  51. });
  52. return seriesBooks;
  53. }
  54. async body(req) {
  55. const result = {};
  56. const query = {
  57. author: req.query.author || '',
  58. series: req.query.series || '',
  59. all: req.query.all || '',
  60. depth: 0,
  61. del: 0,
  62. limit: 100
  63. };
  64. query.depth = query.author.length + 1;
  65. if (query.author == '___others') {
  66. query.author = '';
  67. query.depth = 1;
  68. query.others = true;
  69. }
  70. const entry = [];
  71. if (query.series) {
  72. //книги по серии
  73. const bookList = await this.webWorker.getSeriesBookList(query.series);
  74. if (bookList.books) {
  75. let books = JSON.parse(bookList.books);
  76. const filtered = (query.all ? books : this.filterBooks(books, query));
  77. const sorted = this.sortSeriesBooks(filtered);
  78. for (const book of sorted) {
  79. let title = `${book.serno ? `${book.serno}. `: ''}${book.title || 'Без названия'}`;
  80. if (query.all) {
  81. title = `${this.bookAuthor(book.author)} "${title}"`;
  82. }
  83. title += ` (${book.ext})`;
  84. entry.push(
  85. this.makeEntry({
  86. id: book._uid,
  87. title,
  88. link: this.acqLink({href: `/book?uid=${encodeURIComponent(book._uid)}`}),
  89. })
  90. );
  91. }
  92. if (books.length > filtered.length) {
  93. entry.push(
  94. this.makeEntry({
  95. id: 'all_series_books',
  96. title: 'Все книги серии',
  97. link: this.navLink({
  98. href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
  99. `&series=${encodeURIComponent(query.series)}&all=1`}),
  100. })
  101. );
  102. }
  103. }
  104. } else if (query.author && query.author[0] == '=') {
  105. //книги по автору
  106. const bookList = await this.webWorker.getAuthorBookList(0, query.author.substring(1));
  107. if (bookList.books) {
  108. let books = JSON.parse(bookList.books);
  109. books = this.sortBooks(this.filterBooks(books, query));
  110. for (const b of books) {
  111. if (b.type == 'series') {
  112. entry.push(
  113. this.makeEntry({
  114. id: b.book._uid,
  115. title: `Серия: ${b.book.series}`,
  116. link: this.navLink({
  117. href: `/${this.id}?author=${encodeURIComponent(query.author)}` +
  118. `&series=${encodeURIComponent(b.book.series)}`}),
  119. })
  120. );
  121. } else {
  122. const title = b.book.title || 'Без названия';
  123. entry.push(
  124. this.makeEntry({
  125. id: b.book._uid,
  126. title,
  127. link: this.acqLink({href: `/book?uid=${encodeURIComponent(b.book._uid)}`}),
  128. })
  129. );
  130. }
  131. }
  132. }
  133. } else {
  134. //поиск по каталогу
  135. const queryRes = await this.opdsQuery('author', query);
  136. for (const rec of queryRes) {
  137. entry.push(
  138. this.makeEntry({
  139. id: rec.id,
  140. title: this.bookAuthor(rec.title),//${(query.depth > 1 && rec.count ? ` (${rec.count})` : '')}
  141. link: this.navLink({href: `/${this.id}?author=${rec.q}`}),
  142. })
  143. );
  144. }
  145. }
  146. result.entry = entry;
  147. return this.makeBody(result, req);
  148. }
  149. }
  150. module.exports = AuthorPage;