Search.vue 22 KB

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