Search.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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-my-xs no-wrap">
  83. <div class="clickable q-mr-sm q-pa-xs">
  84. <div v-if="!isExpanded(item)" @click="expandAuthor(item, true)">
  85. <q-icon name="la la-plus-square" size="24px" />
  86. </div>
  87. <div v-else @click="expandAuthor(item, false)">
  88. <q-icon name="la la-minus-square" size="24px" />
  89. </div>
  90. </div>
  91. <div class="clickable" style="font-weight: bold" @click="authorClick(item.author)">
  92. {{ item.name }}
  93. </div>
  94. </div>
  95. <div v-if="isExpanded(item) && item.books">
  96. {{ item.books[0] }}
  97. </div>
  98. </div>
  99. <!-- Формирование списка конец ------------------------------------------------------------------>
  100. <div v-if="totalPages > 1" class="row justify-center">
  101. <PageScroller v-model="page" :total-pages="totalPages" />
  102. </div>
  103. <div v-else class="q-my-sm" />
  104. </div>
  105. </div>
  106. </template>
  107. <script>
  108. //-----------------------------------------------------------------------------
  109. import vueComponent from '../vueComponent.js';
  110. const { reactive } = require('@vue/reactivity');
  111. import PageScroller from './PageScroller/PageScroller.vue';
  112. import * as utils from '../../share/utils';
  113. import _ from 'lodash';
  114. const componentOptions = {
  115. components: {
  116. PageScroller,
  117. },
  118. watch: {
  119. config() {
  120. this.makeTitle();
  121. },
  122. author() {
  123. this.refresh();
  124. },
  125. series() {
  126. this.refresh();
  127. },
  128. title() {
  129. this.refresh();
  130. },
  131. genre() {
  132. this.refresh();
  133. },
  134. lang() {
  135. this.refresh();
  136. },
  137. page() {
  138. this.refresh();
  139. },
  140. limit(newValue) {
  141. const newSettings = _.cloneDeep(this.settings);
  142. newSettings.limit = newValue;
  143. this.commit('setSettings', newSettings);
  144. this.updatePageCount();
  145. this.refresh();
  146. },
  147. totalFound() {
  148. this.updatePageCount();
  149. },
  150. expanded(newValue) {
  151. const newSettings = _.cloneDeep(this.settings);
  152. newSettings.expanded = _.cloneDeep(newValue);
  153. this.commit('setSettings', newSettings);
  154. },
  155. },
  156. };
  157. class Search {
  158. _options = componentOptions;
  159. collection = '';
  160. projectName = '';
  161. loadingMessage = '';
  162. page = 1;
  163. totalPages = 1;
  164. expanded = [];
  165. //input field consts
  166. inputMaxLength = 1000;
  167. inputDebounce = 200;
  168. //search fields
  169. author = '';
  170. series = '';
  171. title = '';
  172. genre = '';
  173. lang = '';
  174. limit = 50;
  175. //stuff
  176. queryFound = -1;
  177. totalFound = 0;
  178. limitOptions = [
  179. {label: '10', value: 10},
  180. {label: '20', value: 20},
  181. {label: '50', value: 50},
  182. {label: '100', value: 100},
  183. {label: '1000', value: 1000},
  184. ];
  185. searchResult = {};
  186. tableData = [];
  187. created() {
  188. this.commit = this.$store.commit;
  189. this.loadSettings();
  190. }
  191. mounted() {
  192. this.api = this.$root.api;
  193. if (!this.$root.isMobileDevice)
  194. this.$refs.authorInput.focus();
  195. this.ready = true;
  196. this.refresh();//no await
  197. }
  198. loadSettings() {
  199. const settings = this.settings;
  200. this.limit = settings.limit;
  201. this.expanded = _.cloneDeep(settings.expanded);
  202. }
  203. get config() {
  204. return this.$store.state.config;
  205. }
  206. get settings() {
  207. return this.$store.state.settings;
  208. }
  209. makeTitle() {
  210. const collection = this.config.dbConfig.inpxInfo.collection.split('\n');
  211. this.collection = collection[0].trim();
  212. this.projectName = `${this.config.name} v${this.config.version}`;
  213. }
  214. showSearchHelp() {
  215. this.$root.stdDialog.alert(`
  216. <p>
  217. Здесь должна быть подсказка<br>
  218. </p>
  219. `, 'Подсказка', {iconName: 'la la-info-circle'});
  220. }
  221. showCollectionInfo() {
  222. this.$root.stdDialog.alert(`
  223. <p>
  224. Здесь должна быть информация о коллекции<br>
  225. </p>
  226. `, 'Статистика по коллекции', {iconName: 'la la-info-circle'});
  227. }
  228. selectGenre() {
  229. this.$root.stdDialog.alert('Выбор жанра');
  230. }
  231. selectLang() {
  232. this.$root.stdDialog.alert('Выбор языка');
  233. }
  234. onScroll() {
  235. const curScrollTop = this.$refs.scroller.scrollTop;
  236. if (!this.lastScrollTop)
  237. this.lastScrollTop = 0;
  238. if (!this.lastScrollTop2)
  239. this.lastScrollTop2 = 0;
  240. if (curScrollTop - this.lastScrollTop > 0) {
  241. this.$refs.toolPanel.style.position = 'relative';
  242. this.$refs.toolPanel.style.top = `${this.lastScrollTop2}px`;
  243. } else if (curScrollTop - this.lastScrollTop <= 0) {
  244. this.$refs.toolPanel.style.position = 'sticky';
  245. this.$refs.toolPanel.style.top = 0;
  246. this.lastScrollTop2 = curScrollTop;
  247. }
  248. this.lastScrollTop = curScrollTop;
  249. }
  250. scrollToTop() {
  251. this.$refs.scroller.scrollTop = 0;
  252. const curScrollTop = this.$refs.scroller.scrollTop;
  253. this.lastScrollTop = curScrollTop;
  254. }
  255. get foundAuthorsMessage() {
  256. return `Найден${utils.wordEnding(this.totalFound, 2)} ${this.totalFound} автор${utils.wordEnding(this.totalFound)}`;
  257. }
  258. updatePageCount() {
  259. this.totalPages = Math.ceil(this.totalFound/this.limit);
  260. this.totalPages = (this.totalPages < 1 ? 1 : this.totalPages);
  261. if (this.page > this.totalPages)
  262. this.page = 1;
  263. }
  264. authorClick(author) {
  265. this.author = `=${author}`;
  266. }
  267. isExpanded(item) {
  268. return this.expanded.indexOf(item.author) >= 0;
  269. }
  270. expandAuthor(item, expand = true) {
  271. const expanded = _.cloneDeep(this.expanded);
  272. const author = item.author;
  273. if (expand) {
  274. if (expanded.indexOf(author) < 0) {
  275. expanded.push(author);
  276. this.getBooks(item);
  277. if (expanded.length > 100) {
  278. expanded.shift();
  279. }
  280. this.expanded = expanded;
  281. }
  282. } else {
  283. const i = expanded.indexOf(author);
  284. if (i >= 0) {
  285. expanded.splice(i, 1);
  286. this.expanded = expanded;
  287. }
  288. }
  289. }
  290. async loadBooks() {
  291. }
  292. async getBooks(item) {
  293. if (item.books)
  294. return;
  295. await utils.sleep(1000);
  296. item.books = [{name: 'book1'}];
  297. }
  298. async updateTableData() {
  299. let result = [];
  300. const authors = this.searchResult.author;
  301. if (authors.length == 1) {
  302. this.expandAuthor(authors[0]);
  303. }
  304. const expandedSet = new Set(this.expanded);
  305. let num = 0;
  306. for (const rec of authors) {
  307. const item = reactive({
  308. key: rec.id,
  309. num,
  310. author: rec.author,
  311. name: rec.author.replace(/,/g, ', '),
  312. book: false,
  313. });
  314. num++;
  315. if (expandedSet.has(item.author)) {
  316. this.getBooks(item);//no await
  317. }
  318. result.push(item);
  319. }
  320. this.tableData = result;
  321. }
  322. async refresh() {
  323. if (!this.ready)
  324. return;
  325. const offset = (this.page - 1)*this.limit;
  326. const newQuery = {
  327. author: this.author,
  328. series: this.series,
  329. title: this.title,
  330. genre: this.genre,
  331. lang: this.lang,
  332. limit: this.limit,
  333. offset,
  334. };
  335. this.queryExecute = newQuery;
  336. if (this.refreshing)
  337. return;
  338. this.refreshing = true;
  339. try {
  340. while (this.queryExecute) {
  341. const query = this.queryExecute;
  342. this.queryExecute = null;
  343. let inSearch = true;
  344. (async() => {
  345. await utils.sleep(500);
  346. if (inSearch)
  347. this.loadingMessage = 'Поиск авторов...';
  348. })();
  349. try {
  350. const result = await this.api.search(query);
  351. this.queryFound = result.author.length;
  352. this.totalFound = result.totalFound;
  353. this.searchResult = result;
  354. await this.updateTableData();
  355. this.scrollToTop();
  356. } catch (e) {
  357. this.$root.stdDialog.alert(e.message, 'Ошибка');
  358. return;
  359. } finally {
  360. inSearch = false;
  361. this.loadingMessage = '';
  362. }
  363. }
  364. } finally {
  365. this.refreshing = false;
  366. }
  367. }
  368. }
  369. export default vueComponent(Search);
  370. //-----------------------------------------------------------------------------
  371. </script>
  372. <style scoped>
  373. .root {
  374. }
  375. .tool-panel {
  376. border-bottom: 1px solid black;
  377. }
  378. .header {
  379. min-height: 30px;
  380. }
  381. .clickable {
  382. cursor: pointer;
  383. }
  384. .odd-author {
  385. background-color: #e7e7e7;
  386. }
  387. </style>