AuthorList.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <div>
  3. <a ref="download" style="display: none;"></a>
  4. <LoadingMessage :message="loadingMessage" z-index="2" />
  5. <LoadingMessage :message="loadingMessage2" z-index="1" />
  6. <!-- Формирование списка ------------------------------------------------------------------------>
  7. <div v-for="item in tableData" :key="item.key" class="column" :class="{'odd-item': item.num % 2}" style="font-size: 120%">
  8. <div class="row items-center q-ml-md q-mr-xs no-wrap">
  9. <div class="row items-center clickable2 q-py-xs no-wrap" @click="expandAuthor(item)">
  10. <div style="min-width: 30px">
  11. <div v-if="!isExpandedAuthor(item)">
  12. <q-icon name="la la-plus-square" size="28px" />
  13. </div>
  14. <div v-else>
  15. <q-icon name="la la-minus-square" size="28px" />
  16. </div>
  17. </div>
  18. </div>
  19. <div class="clickable2 q-ml-xs q-py-sm text-green-10 text-bold" @click="selectAuthor(item.author)">
  20. {{ item.name }}
  21. </div>
  22. <div class="q-ml-sm text-bold" style="color: #555">
  23. {{ getBookCount(item) }}
  24. </div>
  25. </div>
  26. <div v-if="item.bookLoading" class="book-row row items-center">
  27. <q-icon class="la la-spinner icon-rotate text-blue-8" size="28px" />
  28. <div class="q-ml-xs">
  29. Обработка...
  30. </div>
  31. </div>
  32. <div v-if="isExpandedAuthor(item) && item.books">
  33. <div v-for="book in item.books" :key="book.key" class="book-row column">
  34. <!-- серия книг -->
  35. <div v-if="book.type == 'series'" class="column">
  36. <div class="row items-center q-mr-xs no-wrap text-grey-9">
  37. <div class="row items-center clickable2 q-py-xs no-wrap" @click="expandSeries(book)">
  38. <div style="min-width: 30px">
  39. <div v-if="!isExpandedSeries(book)">
  40. <q-icon name="la la-plus-square" size="28px" />
  41. </div>
  42. <div v-else>
  43. <q-icon name="la la-minus-square" size="28px" />
  44. </div>
  45. </div>
  46. </div>
  47. <div class="clickable2 q-ml-xs q-py-sm text-bold" @click="selectSeries(book.series)">
  48. Серия: {{ book.series }}
  49. </div>
  50. <div class="q-ml-sm text-bold" style="color: #555">
  51. {{ getSeriesBookCount(item, book) }}
  52. </div>
  53. </div>
  54. <div v-if="isExpandedSeries(book) && book.seriesBooks">
  55. <div v-if="book.showAllBooks" class="book-row column">
  56. <BookView
  57. v-for="seriesBook in book.allBooks" :key="seriesBook.id"
  58. :book="seriesBook"
  59. mode="series"
  60. :genre-map="genreMap" :show-read-link="showReadLink"
  61. :title-color="isFoundSeriesBook(book, seriesBook) ? 'text-blue-10' : 'text-red'"
  62. @book-event="bookEvent"
  63. />
  64. </div>
  65. <div v-else class="book-row column">
  66. <BookView
  67. v-for="seriesBook in book.seriesBooks" :key="seriesBook.key"
  68. :book="seriesBook" mode="author" :genre-map="genreMap" :show-read-link="showReadLink" @book-event="bookEvent"
  69. />
  70. </div>
  71. <div
  72. v-if="book.allBooksLoaded && book.allBooksLoaded.length != book.seriesBooks.length"
  73. class="row items-center q-my-sm"
  74. style="margin-left: 100px"
  75. >
  76. <div v-if="book.showAllBooks && book.showMoreAll" class="row items-center q-mr-md">
  77. <i class="las la-ellipsis-h text-red" style="font-size: 40px"></i>
  78. <q-btn class="q-ml-md" color="red" style="width: 200px" dense rounded no-caps @click="showMoreAll(book)">
  79. Показать еще (~{{ showMoreCount }})
  80. </q-btn>
  81. <q-btn class="q-ml-sm" color="red" style="width: 200px" dense rounded no-caps @click="showMoreAll(book, true)">
  82. Показать все ({{ (book.allBooksLoaded && book.allBooksLoaded.length) || '?' }})
  83. </q-btn>
  84. </div>
  85. <div v-if="book.showAllBooks" class="row items-center clickable2 text-blue-10" @click="book.showAllBooks = false">
  86. <q-icon class="la la-long-arrow-alt-up" size="28px" />
  87. Только найденные книги
  88. </div>
  89. <div v-else class="row items-center clickable2 text-red" @click="book.showAllBooks = true">
  90. <q-icon class="la la-long-arrow-alt-down" size="28px" />
  91. Все книги серии
  92. </div>
  93. </div>
  94. </div>
  95. </div>
  96. <!-- книга без серии -->
  97. <BookView v-else :book="book" mode="author" :genre-map="genreMap" :show-read-link="showReadLink" @book-event="bookEvent" />
  98. </div>
  99. <!--div v-if="isExpandedAuthor(item) && item.books && !item.books.length" class="book-row row items-center">
  100. <q-icon class="la la-meh q-mr-xs" size="24px" />
  101. По каждому из заданных критериев у этого автора были найдены разные книги, но нет полного совпадения
  102. </div-->
  103. </div>
  104. <div v-if="isExpandedAuthor(item) && item.showMore" class="row items-center book-row q-mb-sm">
  105. <i class="las la-ellipsis-h text-blue-10" style="font-size: 40px"></i>
  106. <q-btn class="q-ml-md" color="primary" style="width: 200px" dense rounded no-caps @click="showMore(item)">
  107. Показать еще (~{{ showMoreCount }})
  108. </q-btn>
  109. <q-btn class="q-ml-sm" color="primary" style="width: 200px" dense rounded no-caps @click="showMore(item, true)">
  110. Показать все ({{ (item.booksLoaded && item.booksLoaded.length) || '?' }})
  111. </q-btn>
  112. </div>
  113. </div>
  114. <!-- Формирование списка конец ------------------------------------------------------------------>
  115. <div v-if="!refreshing && !tableData.length" class="row items-center q-ml-md" style="font-size: 120%">
  116. <q-icon class="la la-meh q-mr-xs" size="28px" />
  117. Поиск не дал результатов
  118. </div>
  119. </div>
  120. </template>
  121. <script>
  122. //-----------------------------------------------------------------------------
  123. import vueComponent from '../../vueComponent.js';
  124. import { reactive } from 'vue';
  125. import BaseList from '../BaseList';
  126. import authorBooksStorage from '../authorBooksStorage';
  127. import * as utils from '../../../share/utils';
  128. import _ from 'lodash';
  129. class AuthorList extends BaseList {
  130. cachedAuthors = {};
  131. showHiddenHelp() {
  132. this.$root.stdDialog.alert(`
  133. Книги скрытых авторов помечены как удаленные. Для того, чтобы их увидеть, необходимо установить опцию "Показывать удаленные" в настройках.
  134. `, 'Пояснение', {iconName: 'la la-info-circle'});
  135. }
  136. get foundCountMessage() {
  137. return `${this.list.totalFound} автор${utils.wordEnding(this.list.totalFound)}`;
  138. }
  139. isFoundSeriesBook(seriesItem, seriesBook) {
  140. if (!seriesItem.booksSet) {
  141. seriesItem.booksSet = new Set(seriesItem.seriesBooks.map(b => b.id));
  142. }
  143. return seriesItem.booksSet.has(seriesBook.id);
  144. }
  145. getBookCount(item) {
  146. let result = '';
  147. if (!this.showCounts || item.count === undefined)
  148. return result;
  149. if (item.booksLoaded) {
  150. let count = 0;
  151. for (const book of item.booksLoaded) {
  152. if (book.type == 'series')
  153. count += book.seriesBooks.length;
  154. else
  155. count++;
  156. }
  157. result = `${count}/${item.count}`;
  158. } else
  159. result = `#/${item.count}`;
  160. return `(${result})`;
  161. }
  162. getSeriesBookCount(item, book) {
  163. let result = '';
  164. if (!this.showCounts || book.type != 'series')
  165. return result;
  166. let count = book.seriesBooks.length;
  167. result = `${count}`;
  168. if (item.seriesLoaded) {
  169. const rec = item.seriesLoaded[book.series];
  170. const totalCount = (this.showDeleted ? rec.bookCount + rec.bookDelCount : rec.bookCount);
  171. result += `/${totalCount}`;
  172. }
  173. return `(${result})`;
  174. }
  175. async expandAuthor(item) {
  176. this.$emit('listEvent', {action: 'ignoreScroll'});
  177. const expanded = _.cloneDeep(this.expandedAuthor);
  178. const key = item.author;
  179. if (!this.isExpandedAuthor(item)) {
  180. expanded.push(key);
  181. await this.getAuthorBooks(item);
  182. if (expanded.length > 10) {
  183. expanded.shift();
  184. }
  185. this.setSetting('expandedAuthor', expanded);
  186. } else {
  187. const i = expanded.indexOf(key);
  188. if (i >= 0) {
  189. expanded.splice(i, 1);
  190. this.setSetting('expandedAuthor', expanded);
  191. }
  192. }
  193. }
  194. async getAuthorSeries(item) {
  195. if (item.seriesLoaded)
  196. return;
  197. const series = await this.loadAuthorSeries(item.key);
  198. const loaded = {};
  199. for (const s of series) {
  200. loaded[s.series] = {bookCount: s.bookCount, bookDelCount: s.bookDelCount};
  201. }
  202. item.seriesLoaded = loaded;
  203. }
  204. async getAuthorBooks(item) {
  205. if (item.books) {
  206. if (item.count > this.maxItemCount) {
  207. item.bookLoading = true;
  208. await utils.sleep(1);//для перерисовки списка
  209. item.bookLoading = false;
  210. }
  211. return;
  212. }
  213. if (!this.getBooksFlag)
  214. this.getBooksFlag = 0;
  215. this.getBooksFlag++;
  216. if (item.count > this.maxItemCount)
  217. item.bookLoading = true;
  218. try {
  219. if (this.getBooksFlag == 1) {
  220. (async() => {
  221. await utils.sleep(500);
  222. if (this.getBooksFlag > 0)
  223. this.loadingMessage2 = 'Загрузка списка книг...';
  224. })();
  225. }
  226. const booksToFilter = await this.loadAuthorBooks(item.key);
  227. const filtered = this.filterBooks(booksToFilter);
  228. if (!filtered.length && this.list.totalFound == 1) {
  229. this.list.queryFound = 0;
  230. this.list.totalFound = 0;
  231. this.searchResult.found = [];
  232. return false;
  233. }
  234. const prepareBook = (book) => {
  235. return Object.assign(
  236. {
  237. key: book.id,
  238. type: 'book',
  239. },
  240. book
  241. );
  242. };
  243. //объединение по сериям
  244. const books = [];
  245. const seriesIndex = {};
  246. for (const book of filtered) {
  247. if (book.series) {
  248. let index = seriesIndex[book.series];
  249. if (index === undefined) {
  250. index = books.length;
  251. books.push(reactive({
  252. key: book.series,
  253. type: 'series',
  254. series: book.series,
  255. allBooksLoaded: false,
  256. allBooks: false,
  257. showAllBooks: false,
  258. showMoreAll: false,
  259. seriesBooks: [],
  260. }));
  261. seriesIndex[book.series] = index;
  262. }
  263. books[index].seriesBooks.push(prepareBook(book));
  264. } else {
  265. books.push(prepareBook(book));
  266. }
  267. }
  268. //сортировка
  269. books.sort((a, b) => {
  270. if (a.type == 'series') {
  271. return (b.type == 'series' ? a.key.localeCompare(b.key) : -1);
  272. } else {
  273. return (b.type == 'book' ? a.title.localeCompare(b.title) : 1);
  274. }
  275. });
  276. //сортировка внутри серий
  277. for (const book of books) {
  278. if (book.type == 'series') {
  279. this.sortSeriesBooks(book.seriesBooks);
  280. //асинхронно подгрузим все книги серии, если она раскрыта
  281. if (this.isExpandedSeries(book)) {
  282. this.getSeriesBooks(book);//no await
  283. }
  284. }
  285. }
  286. if (books.length == 1 && books[0].type == 'series' && !this.isExpandedSeries(books[0])) {
  287. this.expandSeries(books[0]);
  288. }
  289. item.booksLoaded = books;
  290. this.getAuthorSeries(item);//no await
  291. this.showMore(item);
  292. await this.$nextTick();
  293. } finally {
  294. item.bookLoading = false;
  295. this.getBooksFlag--;
  296. if (this.getBooksFlag == 0)
  297. this.loadingMessage2 = '';
  298. }
  299. }
  300. async updateTableData() {
  301. let result = [];
  302. const expandedSet = new Set(this.expandedAuthor);
  303. const authors = this.searchResult.found;
  304. if (!authors)
  305. return;
  306. let num = 0;
  307. for (const rec of authors) {
  308. this.cachedAuthors[rec.author] = rec;
  309. const count = (this.showDeleted ? rec.bookCount + rec.bookDelCount : rec.bookCount);
  310. const item = reactive({
  311. key: rec.id,
  312. num,
  313. author: rec.author,
  314. name: rec.author.replace(/,/g, ', '),
  315. count,
  316. booksLoaded: false,
  317. seriesLoaded: false,
  318. books: false,
  319. bookLoading: false,
  320. showMore: false,
  321. });
  322. num++;
  323. if (expandedSet.has(item.author)) {
  324. if (authors.length > 1 || item.count > this.maxItemCount)
  325. this.getAuthorBooks(item);//no await
  326. else
  327. if (await this.getAuthorBooks(item) === false) {
  328. this.tableData = [];
  329. return;
  330. }
  331. }
  332. result.push(item);
  333. }
  334. if (result.length == 1 && !this.isExpandedAuthor(result[0])) {
  335. this.expandAuthor(result[0]);
  336. }
  337. this.tableData = result;
  338. }
  339. async refresh() {
  340. //параметры запроса
  341. const newQuery = this.getQuery();
  342. if (_.isEqual(newQuery, this.prevQuery))
  343. return;
  344. this.prevQuery = newQuery;
  345. //оптимизация, вместо запроса к серверу, берем из кеша
  346. if (this.abCacheEnabled && this.search.author && this.search.author[0] == '=') {
  347. const authorSearch = this.search.author.substring(1);
  348. const author = this.cachedAuthors[authorSearch];
  349. if (author) {
  350. const key = `author-${author.id}-${this.list.inpxHash}`;
  351. let data = await authorBooksStorage.getData(key);
  352. if (data) {
  353. this.list.queryFound = 1;
  354. this.list.totalFound = 1;
  355. this.searchResult = {found: [author]};
  356. await this.updateTableData();
  357. return;
  358. }
  359. }
  360. }
  361. this.queryExecute = newQuery;
  362. if (this.refreshing)
  363. return;
  364. this.refreshing = true;
  365. (async() => {
  366. await utils.sleep(500);
  367. if (this.refreshing)
  368. this.loadingMessage = 'Поиск авторов...';
  369. })();
  370. try {
  371. while (this.queryExecute) {
  372. const query = this.queryExecute;
  373. this.queryExecute = null;
  374. try {
  375. const response = await this.api.search('author', query);
  376. this.list.queryFound = response.found.length;
  377. this.list.totalFound = response.totalFound;
  378. this.list.inpxHash = response.inpxHash;
  379. this.searchResult = response;
  380. await utils.sleep(1);
  381. if (!this.queryExecute) {
  382. await this.updateTableData();
  383. this.scrollToTop();
  384. this.highlightPageScroller(query);
  385. }
  386. } catch (e) {
  387. this.$root.stdDialog.alert(e.message, 'Ошибка');
  388. }
  389. }
  390. } finally {
  391. this.refreshing = false;
  392. this.loadingMessage = '';
  393. }
  394. }
  395. }
  396. export default vueComponent(AuthorList);
  397. //-----------------------------------------------------------------------------
  398. </script>
  399. <style scoped>
  400. .clickable2 {
  401. cursor: pointer;
  402. }
  403. .odd-item {
  404. background-color: #e8e8e8;
  405. }
  406. .book-row {
  407. margin-left: 50px;
  408. }
  409. </style>