AuthorList.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. // заплатка для исправления https://github.com/bookpauk/inpx-web/issues/10
  171. // по невыясненным причинам rec иногда равен undefined
  172. if (rec) {
  173. const totalCount = (this.showDeleted ? rec.bookCount + rec.bookDelCount : rec.bookCount);
  174. result += `/${totalCount}`;
  175. }
  176. }
  177. return `(${result})`;
  178. }
  179. async expandAuthor(item) {
  180. this.$emit('listEvent', {action: 'ignoreScroll'});
  181. const expanded = _.cloneDeep(this.expandedAuthor);
  182. const key = item.author;
  183. if (!this.isExpandedAuthor(item)) {
  184. expanded.push(key);
  185. await this.getAuthorBooks(item);
  186. if (expanded.length > 10) {
  187. expanded.shift();
  188. }
  189. this.setSetting('expandedAuthor', expanded);
  190. } else {
  191. const i = expanded.indexOf(key);
  192. if (i >= 0) {
  193. expanded.splice(i, 1);
  194. this.setSetting('expandedAuthor', expanded);
  195. }
  196. }
  197. }
  198. async getAuthorSeries(item) {
  199. if (item.seriesLoaded)
  200. return;
  201. const series = await this.loadAuthorSeries(item.key);
  202. const loaded = {};
  203. for (const s of series) {
  204. loaded[s.series] = {bookCount: s.bookCount, bookDelCount: s.bookDelCount};
  205. }
  206. item.seriesLoaded = loaded;
  207. }
  208. async getAuthorBooks(item) {
  209. if (item.books) {
  210. if (item.count > this.maxItemCount) {
  211. item.bookLoading = true;
  212. await utils.sleep(1);//для перерисовки списка
  213. item.bookLoading = false;
  214. }
  215. return;
  216. }
  217. if (!this.getBooksFlag)
  218. this.getBooksFlag = 0;
  219. this.getBooksFlag++;
  220. if (item.count > this.maxItemCount)
  221. item.bookLoading = true;
  222. try {
  223. if (this.getBooksFlag == 1) {
  224. (async() => {
  225. await utils.sleep(500);
  226. if (this.getBooksFlag > 0)
  227. this.loadingMessage2 = 'Загрузка списка книг...';
  228. })();
  229. }
  230. const booksToFilter = await this.loadAuthorBooks(item.key);
  231. const filtered = this.filterBooks(booksToFilter);
  232. if (!filtered.length && this.list.totalFound == 1) {
  233. this.list.queryFound = 0;
  234. this.list.totalFound = 0;
  235. this.searchResult.found = [];
  236. return false;
  237. }
  238. const prepareBook = (book) => {
  239. return Object.assign(
  240. {
  241. key: book.id,
  242. type: 'book',
  243. },
  244. book
  245. );
  246. };
  247. //объединение по сериям
  248. const books = [];
  249. const seriesIndex = {};
  250. for (const book of filtered) {
  251. if (book.series) {
  252. let index = seriesIndex[book.series];
  253. if (index === undefined) {
  254. index = books.length;
  255. books.push(reactive({
  256. key: book.series,
  257. type: 'series',
  258. series: book.series,
  259. allBooksLoaded: false,
  260. allBooks: false,
  261. showAllBooks: false,
  262. showMoreAll: false,
  263. seriesBooks: [],
  264. }));
  265. seriesIndex[book.series] = index;
  266. }
  267. books[index].seriesBooks.push(prepareBook(book));
  268. } else {
  269. books.push(prepareBook(book));
  270. }
  271. }
  272. //сортировка
  273. books.sort((a, b) => {
  274. if (a.type == 'series') {
  275. return (b.type == 'series' ? a.key.localeCompare(b.key) : -1);
  276. } else {
  277. return (b.type == 'book' ? a.title.localeCompare(b.title) : 1);
  278. }
  279. });
  280. //сортировка внутри серий
  281. for (const book of books) {
  282. if (book.type == 'series') {
  283. this.sortSeriesBooks(book.seriesBooks);
  284. //асинхронно подгрузим все книги серии, если она раскрыта
  285. if (this.isExpandedSeries(book)) {
  286. this.getSeriesBooks(book);//no await
  287. }
  288. }
  289. }
  290. if (books.length == 1 && books[0].type == 'series' && !this.isExpandedSeries(books[0])) {
  291. this.expandSeries(books[0]);
  292. }
  293. item.booksLoaded = books;
  294. this.getAuthorSeries(item);//no await
  295. this.showMore(item);
  296. await this.$nextTick();
  297. } finally {
  298. item.bookLoading = false;
  299. this.getBooksFlag--;
  300. if (this.getBooksFlag == 0)
  301. this.loadingMessage2 = '';
  302. }
  303. }
  304. async updateTableData() {
  305. let result = [];
  306. const expandedSet = new Set(this.expandedAuthor);
  307. const authors = this.searchResult.found;
  308. if (!authors)
  309. return;
  310. let num = 0;
  311. for (const rec of authors) {
  312. this.cachedAuthors[rec.author] = rec;
  313. const count = (this.showDeleted ? rec.bookCount + rec.bookDelCount : rec.bookCount);
  314. const item = reactive({
  315. key: rec.id,
  316. num,
  317. author: rec.author,
  318. name: rec.author.replace(/,/g, ', '),
  319. count,
  320. booksLoaded: false,
  321. seriesLoaded: false,
  322. books: false,
  323. bookLoading: false,
  324. showMore: false,
  325. });
  326. num++;
  327. if (expandedSet.has(item.author)) {
  328. if (authors.length > 1 || item.count > this.maxItemCount)
  329. this.getAuthorBooks(item);//no await
  330. else
  331. if (await this.getAuthorBooks(item) === false) {
  332. this.tableData = [];
  333. return;
  334. }
  335. }
  336. result.push(item);
  337. }
  338. if (result.length == 1 && !this.isExpandedAuthor(result[0])) {
  339. this.expandAuthor(result[0]);
  340. }
  341. this.tableData = result;
  342. }
  343. async refresh() {
  344. //параметры запроса
  345. const newQuery = this.getQuery();
  346. if (_.isEqual(newQuery, this.prevQuery))
  347. return;
  348. this.prevQuery = newQuery;
  349. //оптимизация, вместо запроса к серверу, берем из кеша
  350. if (this.abCacheEnabled && this.search.author && this.search.author[0] == '=') {
  351. const authorSearch = this.search.author.substring(1);
  352. const author = this.cachedAuthors[authorSearch];
  353. if (author) {
  354. const key = `author-${author.id}-${this.list.inpxHash}`;
  355. let data = await authorBooksStorage.getData(key);
  356. if (data) {
  357. this.list.queryFound = 1;
  358. this.list.totalFound = 1;
  359. this.searchResult = {found: [author]};
  360. await this.updateTableData();
  361. return;
  362. }
  363. }
  364. }
  365. this.queryExecute = newQuery;
  366. if (this.refreshing)
  367. return;
  368. this.refreshing = true;
  369. (async() => {
  370. await utils.sleep(500);
  371. if (this.refreshing)
  372. this.loadingMessage = 'Поиск авторов...';
  373. })();
  374. try {
  375. while (this.queryExecute) {
  376. const query = this.queryExecute;
  377. this.queryExecute = null;
  378. try {
  379. const response = await this.api.search('author', query);
  380. this.list.queryFound = response.found.length;
  381. this.list.totalFound = response.totalFound;
  382. this.list.inpxHash = response.inpxHash;
  383. this.searchResult = response;
  384. await utils.sleep(1);
  385. if (!this.queryExecute) {
  386. await this.updateTableData();
  387. this.scrollToTop();
  388. this.highlightPageScroller(query);
  389. }
  390. } catch (e) {
  391. this.$root.stdDialog.alert(e.message, 'Ошибка');
  392. }
  393. }
  394. } finally {
  395. this.refreshing = false;
  396. this.loadingMessage = '';
  397. }
  398. }
  399. }
  400. export default vueComponent(AuthorList);
  401. //-----------------------------------------------------------------------------
  402. </script>
  403. <style scoped>
  404. .clickable2 {
  405. cursor: pointer;
  406. }
  407. .odd-item {
  408. background-color: #e8e8e8;
  409. }
  410. .book-row {
  411. margin-left: 50px;
  412. }
  413. </style>