Search.vue 15 KB

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