Search.vue 24 KB

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