Search.vue 22 KB

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