Search.vue 17 KB

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