Search.vue 19 KB

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