Search.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <template>
  2. <div class="root column fit" style="position: relative">
  3. <div v-show="loadingMessage" class="fit row justify-center items-center" style="position: absolute; background-color: rgba(0, 0, 0, 0.2); z-index: 2">
  4. <div class="bg-white row justify-center items-center q-px-lg" style="min-width: 180px; height: 50px; border-radius: 10px; box-shadow: 2px 2px 10px #333333">
  5. <q-spinner color="primary" size="2em" />
  6. <div class="q-ml-sm">
  7. {{ loadingMessage }}
  8. </div>
  9. </div>
  10. </div>
  11. <div v-show="loadingMessage2" class="fit row justify-center items-center" style="position: absolute; background-color: rgba(0, 0, 0, 0.2); z-index: 1">
  12. <div class="bg-white row justify-center items-center q-px-lg" style="min-width: 180px; height: 50px; border-radius: 10px; box-shadow: 2px 2px 10px #333333">
  13. <q-spinner color="primary" size="2em" />
  14. <div class="q-ml-sm">
  15. {{ loadingMessage2 }}
  16. </div>
  17. </div>
  18. </div>
  19. <div ref="scroller" class="col fit column no-wrap" style="overflow: auto; position: relative" @scroll="onScroll">
  20. <div ref="toolPanel" class="tool-panel column bg-green-11" style="position: sticky; top: 0; z-index: 10;">
  21. <div class="header q-mx-md q-mb-xs q-mt-sm row items-center">
  22. <div class="row items-center" style="font-size: 150%;">
  23. <div class="q-px-sm q-py-xs bg-green-12" style="border: 1px solid #aaaaaa; border-radius: 6px">
  24. {{ projectName }}
  25. </div>
  26. <div class="q-ml-md q-mr-xs">
  27. Коллекция
  28. </div>
  29. <div class="clickable" @click="showCollectionInfo">
  30. {{ collection }}
  31. </div>
  32. </div>
  33. <DivBtn class="q-ml-md text-white bg-secondary" :size="30" :icon-size="24" :imt="1" icon="la la-cog" round @click="settingsDialogVisible = true" />
  34. </div>
  35. <div class="row q-mx-md q-mb-sm items-center">
  36. <q-input
  37. ref="authorInput" v-model="author" :maxlength="5000" :debounce="inputDebounce"
  38. class="bg-white q-mt-xs" style="width: 300px;" label="Автор" stack-label outlined dense clearable
  39. />
  40. <div class="q-mx-xs" />
  41. <q-input
  42. v-model="series" :maxlength="inputMaxLength" :debounce="inputDebounce"
  43. class="bg-white q-mt-xs" style="width: 200px;" label="Серия" stack-label outlined dense clearable
  44. />
  45. <div class="q-mx-xs" />
  46. <q-input
  47. v-model="title" :maxlength="inputMaxLength" :debounce="inputDebounce"
  48. class="bg-white q-mt-xs" style="width: 200px;" label="Название" stack-label outlined dense clearable
  49. />
  50. <div class="q-mx-xs" />
  51. <q-input
  52. v-model="genreNames" :maxlength="inputMaxLength" :debounce="inputDebounce"
  53. class="bg-white q-mt-xs" input-style="cursor: pointer" style="width: 200px;" label="Жанр" stack-label outlined dense clearable readonly
  54. @click="selectGenre"
  55. >
  56. <q-tooltip v-if="genreNames" :delay="500" anchor="bottom right" content-style="font-size: 80%" max-width="400px">
  57. {{ genreNames }}
  58. </q-tooltip>
  59. </q-input>
  60. <div class="q-mx-xs" />
  61. <q-input
  62. v-model="lang" :maxlength="inputMaxLength" :debounce="inputDebounce"
  63. class="bg-white q-mt-xs" input-style="cursor: pointer" style="width: 80px;" label="Язык" stack-label outlined dense clearable readonly
  64. @click="selectLang"
  65. />
  66. <div class="q-mx-xs" />
  67. <DivBtn class="text-white bg-grey-13" :size="30" :icon-size="24" icon="la la-question" round @click="showSearchHelp" />
  68. <div class="q-mx-xs" />
  69. <div class="row items-center q-mt-xs">
  70. <div v-show="queryFound > 0">
  71. {{ foundAuthorsMessage }}
  72. </div>
  73. <div v-show="queryFound == 0">
  74. Ничего не найдено
  75. </div>
  76. </div>
  77. </div>
  78. </div>
  79. <div v-if="pageCount > 1" class="row justify-center">
  80. <PageScroller v-model="page" :page-count="pageCount" />
  81. </div>
  82. <div v-else class="q-my-sm" />
  83. <!-- Формирование списка ------------------------------------------------------------------------>
  84. <div v-for="item in tableData" :key="item.key" class="column" :class="{'odd-author': item.num % 2}" style="font-size: 120%">
  85. <div class="row items-center q-ml-md q-mr-xs no-wrap">
  86. <!--div style="min-width: 35px">
  87. <DivBtn v-if="tableData.length > 1" :icon-size="24" icon="la la-check-circle" @click="selectAuthor(item.author)">
  88. <q-tooltip :delay="1500" anchor="bottom right" content-style="font-size: 80%">
  89. Только этот автор
  90. </q-tooltip>
  91. </DivBtn>
  92. </div-->
  93. <div class="row items-center clickable2 q-py-xs no-wrap" @click="expandAuthor(item)">
  94. <div style="min-width: 30px">
  95. <div v-if="!isExpanded(item)">
  96. <q-icon name="la la-plus-square" size="28px" />
  97. </div>
  98. <div v-else>
  99. <q-icon name="la la-minus-square" size="28px" />
  100. </div>
  101. </div>
  102. </div>
  103. <div class="clickable q-ml-xs q-py-sm" style="font-weight: bold" @click="selectAuthor(item.author)">
  104. {{ item.name }}
  105. </div>
  106. <div class="q-ml-sm" style="font-weight: bold; color: #555">
  107. {{ getBookCount(item) }}
  108. </div>
  109. </div>
  110. <div v-if="isExpanded(item) && item.books">
  111. <div v-for="book in item.books" :key="book.key" class="book-row column">
  112. <div class="q-my-sm" @click="selectTitle(book.title)">
  113. {{ book.title }} {{ book.src.del }}
  114. </div>
  115. <!--div>
  116. {{ item.key }} {{ book.src }}
  117. </div-->
  118. </div>
  119. </div>
  120. </div>
  121. <!-- Формирование списка конец ------------------------------------------------------------------>
  122. <div v-if="pageCount > 1" class="row justify-center">
  123. <PageScroller v-model="page" :page-count="pageCount" />
  124. </div>
  125. <div v-else class="q-my-sm" />
  126. </div>
  127. <Dialog v-model="settingsDialogVisible">
  128. <template #header>
  129. <div class="row items-center">
  130. <q-icon class="q-mr-sm" name="la la-cog" size="28px"></q-icon>
  131. Настройки
  132. </div>
  133. </template>
  134. <div class="q-mx-md column" style="min-width: 300px; font-size: 120%;">
  135. <div class="row items-center q-ml-sm">
  136. <div class="q-mr-sm">
  137. Результатов на странице
  138. </div>
  139. <q-select
  140. v-model="limit" :options="limitOptions" class="bg-white"
  141. dropdown-icon="la la-angle-down la-sm"
  142. outlined dense emit-value map-options
  143. />
  144. </div>
  145. <q-checkbox v-model="showCounts" size="36px" label="Показывать количество" />
  146. <q-checkbox v-model="showDeleted" size="36px" label="Показывать удаленные" />
  147. <q-checkbox v-model="abCacheEnabled" size="36px" label="Кешировать запросы" />
  148. </div>
  149. <template #footer>
  150. <q-btn class="q-px-md q-ml-sm" color="primary" dense no-caps @click="settingsDialogVisible = false">
  151. OK
  152. </q-btn>
  153. </template>
  154. </Dialog>
  155. <SelectGenreDialog v-model="selectGenreDialogVisible" v-model:genre="genre" :genre-tree="genreTree" />
  156. </div>
  157. </template>
  158. <script>
  159. //-----------------------------------------------------------------------------
  160. import vueComponent from '../vueComponent.js';
  161. import { reactive } from 'vue';
  162. import PageScroller from './PageScroller/PageScroller.vue';
  163. import SelectGenreDialog from './SelectGenreDialog/SelectGenreDialog.vue';
  164. import authorBooksStorage from './authorBooksStorage';
  165. import DivBtn from '../share/DivBtn.vue';
  166. import Dialog from '../share/Dialog.vue';
  167. import * as utils from '../../share/utils';
  168. import _ from 'lodash';
  169. const componentOptions = {
  170. components: {
  171. PageScroller,
  172. SelectGenreDialog,
  173. Dialog,
  174. DivBtn
  175. },
  176. watch: {
  177. config() {
  178. this.makeTitle();
  179. },
  180. settings() {
  181. this.loadSettings();
  182. },
  183. author() {
  184. this.refresh();
  185. },
  186. series() {
  187. this.refresh();
  188. },
  189. title() {
  190. this.refresh();
  191. },
  192. genre() {
  193. this.refresh();
  194. },
  195. lang() {
  196. this.refresh();
  197. },
  198. page() {
  199. this.refresh();
  200. },
  201. limit(newValue) {
  202. this.setSetting('limit', newValue);
  203. this.updatePageCount();
  204. this.refresh();
  205. },
  206. showCounts(newValue) {
  207. this.setSetting('showCounts', newValue);
  208. },
  209. showDeleted(newValue) {
  210. this.setSetting('showDeleted', newValue);
  211. },
  212. abCacheEnabled(newValue) {
  213. this.setSetting('abCacheEnabled', newValue);
  214. },
  215. totalFound() {
  216. this.updatePageCount();
  217. },
  218. },
  219. };
  220. class Search {
  221. _options = componentOptions;
  222. collection = '';
  223. projectName = '';
  224. loadingMessage = '';
  225. loadingMessage2 = '';
  226. settingsDialogVisible = false;
  227. selectGenreDialogVisible = false;
  228. page = 1;
  229. pageCount = 1;
  230. //input field consts
  231. inputMaxLength = 1000;
  232. inputDebounce = 200;
  233. //search fields
  234. author = '';
  235. series = '';
  236. title = '';
  237. genre = '';
  238. lang = '';
  239. limit = 50;//settings
  240. //settings
  241. expanded = [];
  242. showCounts = true;
  243. showDeleted = false;
  244. abCacheEnabled = true;
  245. //stuff
  246. queryFound = -1;
  247. totalFound = 0;
  248. bookRowsOnPage = 100;
  249. inpxHash = '';
  250. genreTree = [];
  251. genreTreeInpxHash = '';
  252. limitOptions = [
  253. {label: '10', value: 10},
  254. {label: '20', value: 20},
  255. {label: '50', value: 50},
  256. {label: '100', value: 100},
  257. {label: '200', value: 200},
  258. {label: '500', value: 500},
  259. {label: '1000', value: 1000},
  260. ];
  261. searchResult = {};
  262. tableData = [];
  263. created() {
  264. this.commit = this.$store.commit;
  265. this.loadSettings();
  266. }
  267. mounted() {
  268. (async() => {
  269. await authorBooksStorage.init();
  270. this.api = this.$root.api;
  271. if (!this.$root.isMobileDevice)
  272. this.$refs.authorInput.focus();
  273. this.ready = true;
  274. this.refresh();//no await
  275. })();
  276. }
  277. loadSettings() {
  278. const settings = this.settings;
  279. this.limit = settings.limit;
  280. this.expanded = _.cloneDeep(settings.expanded);
  281. this.showCounts = settings.showCounts;
  282. this.showDeleted = settings.showDeleted;
  283. this.abCacheEnabled = settings.abCacheEnabled;
  284. }
  285. get config() {
  286. return this.$store.state.config;
  287. }
  288. get settings() {
  289. return this.$store.state.settings;
  290. }
  291. get genreNames() {
  292. let result = [];
  293. const genre = new Set(this.genre.split(','));
  294. for (const section of this.genreTree) {
  295. for (const g of section.value)
  296. if (genre.has(g.value))
  297. result.push(g.name);
  298. }
  299. return result.join(', ');
  300. }
  301. makeTitle() {
  302. const collection = this.config.dbConfig.inpxInfo.collection.split('\n');
  303. this.collection = collection[0].trim();
  304. this.projectName = `${this.config.name} v${this.config.version}`;
  305. }
  306. showSearchHelp() {
  307. this.$root.stdDialog.alert(`
  308. <p>
  309. Здесь должна быть подсказка<br>
  310. </p>
  311. `, 'Подсказка', {iconName: 'la la-info-circle'});
  312. }
  313. showCollectionInfo() {
  314. this.$root.stdDialog.alert(`
  315. <p>
  316. Здесь должна быть информация о коллекции<br>
  317. </p>
  318. `, 'Статистика по коллекции', {iconName: 'la la-info-circle'});
  319. }
  320. selectGenre() {
  321. this.selectGenreDialogVisible = true;
  322. }
  323. selectLang() {
  324. this.$root.stdDialog.alert('Выбор языка');
  325. }
  326. onScroll() {
  327. if (this.ignoreScrolling)
  328. return;
  329. const curScrollTop = this.$refs.scroller.scrollTop;
  330. if (!this.lastScrollTop)
  331. this.lastScrollTop = 0;
  332. if (!this.lastScrollTop2)
  333. this.lastScrollTop2 = 0;
  334. if (curScrollTop - this.lastScrollTop > 0) {
  335. this.$refs.toolPanel.style.position = 'relative';
  336. this.$refs.toolPanel.style.top = `${this.lastScrollTop2}px`;
  337. } else if (curScrollTop - this.lastScrollTop <= 0) {
  338. this.$refs.toolPanel.style.position = 'sticky';
  339. this.$refs.toolPanel.style.top = 0;
  340. this.lastScrollTop2 = curScrollTop;
  341. }
  342. this.lastScrollTop = curScrollTop;
  343. }
  344. async ignoreScroll(ms = 50) {
  345. this.ignoreScrolling = true;
  346. await utils.sleep(ms);
  347. this.ignoreScrolling = false;
  348. }
  349. scrollToTop() {
  350. this.$refs.scroller.scrollTop = 0;
  351. this.lastScrollTop = 0;
  352. }
  353. get foundAuthorsMessage() {
  354. return `Найден${utils.wordEnding(this.totalFound, 2)} ${this.totalFound} автор${utils.wordEnding(this.totalFound)}`;
  355. }
  356. updatePageCount() {
  357. this.pageCount = Math.ceil(this.totalFound/this.limit);
  358. this.pageCount = (this.pageCount < 1 ? 1 : this.pageCount);
  359. if (this.page > this.pageCount)
  360. this.page = 1;
  361. }
  362. selectAuthor(author) {
  363. this.author = `=${author}`;
  364. this.scrollToTop();
  365. }
  366. selectTitle(title) {
  367. this.title = `=${title}`;
  368. }
  369. isExpanded(item) {
  370. return this.expanded.indexOf(item.author) >= 0;
  371. }
  372. setSetting(name, newValue) {
  373. const newSettings = _.cloneDeep(this.settings);
  374. newSettings[name] = _.cloneDeep(newValue);
  375. this.commit('setSettings', newSettings);
  376. }
  377. expandAuthor(item) {
  378. const expanded = _.cloneDeep(this.expanded);
  379. const author = item.author;
  380. if (!this.isExpanded(item)) {
  381. expanded.push(author);
  382. this.getBooks(item);
  383. if (expanded.length > 10) {
  384. expanded.shift();
  385. }
  386. this.setSetting('expanded', expanded);
  387. this.ignoreScroll();
  388. } else {
  389. const i = expanded.indexOf(author);
  390. if (i >= 0) {
  391. expanded.splice(i, 1);
  392. this.setSetting('expanded', expanded);
  393. }
  394. }
  395. }
  396. getBookCount(item) {
  397. let result = '';
  398. if (!this.showCounts || item.bookCount === undefined)
  399. return result;
  400. if (this.showDeleted) {
  401. result = item.bookCount + item.bookDelCount;
  402. } else {
  403. result = item.bookCount;
  404. }
  405. if (item.books)
  406. result = `${item.books.length}/${result}`;
  407. else
  408. result = `#/${result}`;
  409. return `(${result})`;
  410. }
  411. async loadBooks(authorId) {
  412. try {
  413. let result;
  414. if (this.abCacheEnabled) {
  415. const key = `${authorId}-${this.inpxHash}`;
  416. const data = await authorBooksStorage.getData(key);
  417. if (data) {
  418. result = JSON.parse(data);
  419. } else {
  420. result = await this.api.getBookList(authorId);
  421. await authorBooksStorage.setData(key, JSON.stringify(result));
  422. }
  423. } else {
  424. result = await this.api.getBookList(authorId);
  425. }
  426. return JSON.parse(result.books);
  427. } catch (e) {
  428. this.$root.stdDialog.alert(e.message, 'Ошибка');
  429. }
  430. }
  431. filterBooks(loadedBooks) {
  432. return loadedBooks;
  433. }
  434. async getBooks(item) {
  435. if (item.books)
  436. return;
  437. if (!this.getBooksFlag)
  438. this.getBooksFlag = 0;
  439. this.getBooksFlag++;
  440. try {
  441. if (this.getBooksFlag == 1) {
  442. (async() => {
  443. await utils.sleep(500);
  444. if (this.getBooksFlag > 0)
  445. this.loadingMessage2 = 'Загрузка списка книг...';
  446. })();
  447. }
  448. const loadedBooks = await this.loadBooks(item.key);
  449. const filtered = this.filterBooks(loadedBooks);
  450. filtered.sort((a, b) => a.title.localeCompare(b.title));
  451. const books = [];
  452. for (const book of filtered) {
  453. books.push({key: book.id, title: book.title, src: book});
  454. }
  455. item.books = books;
  456. } finally {
  457. this.getBooksFlag--;
  458. if (this.getBooksFlag == 0)
  459. this.loadingMessage2 = '';
  460. }
  461. }
  462. async updateGenreTreeIfNeeded() {
  463. try {
  464. if (this.genreTreeInpxHash !== this.inpxHash) {
  465. let result;
  466. if (this.abCacheEnabled) {
  467. const key = `genre-tree-${this.inpxHash}`;
  468. const data = await authorBooksStorage.getData(key);
  469. if (data) {
  470. result = JSON.parse(data);
  471. } else {
  472. result = await this.api.getGenreTree();
  473. await authorBooksStorage.setData(key, JSON.stringify(result));
  474. }
  475. } else {
  476. result = await this.api.getGenreTree();
  477. }
  478. this.genreTree = result.genreTree;
  479. this.genreTreeInpxHash = result.inpxHash;
  480. }
  481. } catch (e) {
  482. this.$root.stdDialog.alert(e.message, 'Ошибка');
  483. }
  484. }
  485. async updateTableData() {
  486. let result = [];
  487. const expandedSet = new Set(this.expanded);
  488. const authors = this.searchResult.author;
  489. let num = 0;
  490. for (const rec of authors) {
  491. const item = reactive({
  492. key: rec.id,
  493. num,
  494. author: rec.author,
  495. name: rec.author.replace(/,/g, ', '),
  496. bookCount: rec.bookCount,
  497. bookDelCount: rec.bookDelCount,
  498. book: false,
  499. });
  500. num++;
  501. if (expandedSet.has(item.author)) {
  502. this.getBooks(item);//no await
  503. }
  504. result.push(item);
  505. }
  506. if (result.length == 1 && !this.isExpanded(result[0])) {
  507. this.expandAuthor(result[0]);
  508. }
  509. this.tableData = result;
  510. }
  511. async refresh() {
  512. if (!this.ready)
  513. return;
  514. const offset = (this.page - 1)*this.limit;
  515. const newQuery = {
  516. author: this.author,
  517. series: this.series,
  518. title: this.title,
  519. genre: this.genre,
  520. lang: this.lang,
  521. limit: this.limit,
  522. offset,
  523. };
  524. this.queryExecute = newQuery;
  525. if (this.refreshing)
  526. return;
  527. this.refreshing = true;
  528. try {
  529. while (this.queryExecute) {
  530. const query = this.queryExecute;
  531. this.queryExecute = null;
  532. let inSearch = true;
  533. (async() => {
  534. await utils.sleep(500);
  535. if (inSearch)
  536. this.loadingMessage = 'Поиск авторов...';
  537. })();
  538. try {
  539. const result = await this.api.search(query);
  540. this.queryFound = result.author.length;
  541. this.totalFound = result.totalFound;
  542. this.inpxHash = result.inpxHash;
  543. this.searchResult = result;
  544. await this.updateGenreTreeIfNeeded();
  545. await this.updateTableData();
  546. this.scrollToTop();
  547. } catch (e) {
  548. this.$root.stdDialog.alert(e.message, 'Ошибка');
  549. } finally {
  550. inSearch = false;
  551. this.loadingMessage = '';
  552. }
  553. }
  554. } finally {
  555. this.refreshing = false;
  556. }
  557. }
  558. }
  559. export default vueComponent(Search);
  560. //-----------------------------------------------------------------------------
  561. </script>
  562. <style scoped>
  563. .root {
  564. }
  565. .tool-panel {
  566. border-bottom: 1px solid black;
  567. }
  568. .header {
  569. min-height: 30px;
  570. }
  571. .clickable {
  572. color: blue;
  573. cursor: pointer;
  574. }
  575. .clickable2 {
  576. cursor: pointer;
  577. }
  578. .odd-author {
  579. background-color: #e8e8e8;
  580. }
  581. .book-row {
  582. margin-left: 100px;
  583. }
  584. </style>