AuthorList.vue 17 KB

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