Search.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 class="row justify-center">
  77. <PageScroller v-model="page" :total-pages="totalPages" />
  78. </div>
  79. <!-- Формирование списка ------------------------------------------------------------------------>
  80. <div v-for="item in tableData" :key="item.key" style="border-bottom: 1px solid #aaaaaa">
  81. <div v-if="item.type == 'author'" class="q-my-sm q-ml-md clickable" style="font-size: 120%" @click="authorClick(item.value)">
  82. {{ item.key }} {{ item.name }}
  83. </div>
  84. </div>
  85. <!-- Формирование списка конец ------------------------------------------------------------------>
  86. <div class="row justify-center">
  87. <PageScroller v-model="page" :total-pages="totalPages" />
  88. </div>
  89. </div>
  90. </div>
  91. </template>
  92. <script>
  93. //-----------------------------------------------------------------------------
  94. import vueComponent from '../vueComponent.js';
  95. import PageScroller from './PageScroller/PageScroller.vue';
  96. import * as utils from '../../share/utils';
  97. //import _ from 'lodash';
  98. const componentOptions = {
  99. components: {
  100. PageScroller,
  101. },
  102. watch: {
  103. config() {
  104. this.makeTitle();
  105. },
  106. author() {
  107. this.refresh();
  108. },
  109. series() {
  110. this.refresh();
  111. },
  112. title() {
  113. this.refresh();
  114. },
  115. genre() {
  116. this.refresh();
  117. },
  118. lang() {
  119. this.refresh();
  120. },
  121. page() {
  122. this.refresh();
  123. },
  124. limit() {
  125. this.updatePageCount();
  126. this.refresh();
  127. },
  128. totalFound() {
  129. this.updatePageCount();
  130. }
  131. },
  132. };
  133. class Search {
  134. _options = componentOptions;
  135. collection = '';
  136. projectName = '';
  137. loadingMessage = '';
  138. page = 1;
  139. totalPages = 1;
  140. //input field consts
  141. inputMaxLength = 1000;
  142. inputDebounce = 200;
  143. //search fields
  144. author = '';
  145. series = '';
  146. title = '';
  147. genre = '';
  148. lang = '';
  149. limit = 50;
  150. //stuff
  151. queryFound = -1;
  152. totalFound = 0;
  153. limitOptions = [
  154. {label: '10', value: 10},
  155. {label: '20', value: 20},
  156. {label: '50', value: 50},
  157. {label: '100', value: 100},
  158. {label: '1000', value: 1000},
  159. ];
  160. searchResult = {};
  161. tableData = [];
  162. created() {
  163. this.commit = this.$store.commit;
  164. }
  165. mounted() {
  166. this.api = this.$root.api;
  167. this.$refs.authorInput.focus();
  168. this.refresh();//no await
  169. }
  170. get config() {
  171. return this.$store.state.config;
  172. }
  173. makeTitle() {
  174. const collection = this.config.dbConfig.inpxInfo.collection.split('\n');
  175. this.collection = collection[0].trim();
  176. this.projectName = `${this.config.name} v${this.config.version}`;
  177. }
  178. showSearchHelp() {
  179. this.$root.stdDialog.alert(`
  180. <p>
  181. Здесь должна быть подсказка<br>
  182. </p>
  183. `, 'Подсказка', {iconName: 'la la-info-circle'});
  184. }
  185. showCollectionInfo() {
  186. this.$root.stdDialog.alert(`
  187. <p>
  188. Здесь должна быть информация о коллекции<br>
  189. </p>
  190. `, 'Статистика по коллекции', {iconName: 'la la-info-circle'});
  191. }
  192. selectGenre() {
  193. this.$root.stdDialog.alert('Выбор жанра');
  194. }
  195. selectLang() {
  196. this.$root.stdDialog.alert('Выбор языка');
  197. }
  198. onScroll() {
  199. const curScrollTop = this.$refs.scroller.scrollTop;
  200. if (!this.lastScrollTop)
  201. this.lastScrollTop = 0;
  202. if (!this.lastScrollTop2)
  203. this.lastScrollTop2 = 0;
  204. if (curScrollTop - this.lastScrollTop > 0) {
  205. this.$refs.toolPanel.style.position = 'relative';
  206. this.$refs.toolPanel.style.top = `${this.lastScrollTop2}px`;
  207. } else if (curScrollTop - this.lastScrollTop <= 0) {
  208. this.$refs.toolPanel.style.position = 'sticky';
  209. this.$refs.toolPanel.style.top = 0;
  210. this.lastScrollTop2 = curScrollTop;
  211. }
  212. this.lastScrollTop = curScrollTop;
  213. }
  214. scrollToTop() {
  215. this.$refs.scroller.scrollTop = 0;
  216. const curScrollTop = this.$refs.scroller.scrollTop;
  217. this.lastScrollTop = curScrollTop;
  218. }
  219. get foundAuthorsMessage() {
  220. return `Найден${utils.wordEnding(this.totalFound, 2)} ${this.totalFound} автор${utils.wordEnding(this.totalFound)}`;
  221. }
  222. updatePageCount() {
  223. this.totalPages = Math.ceil(this.totalFound/this.limit);
  224. this.totalPages = (this.totalPages < 1 ? 1 : this.totalPages);
  225. if (this.page > this.totalPages)
  226. this.page = 1;
  227. }
  228. authorClick(authorName) {
  229. this.author = `=${authorName}`;
  230. }
  231. async updateTableData() {
  232. let result = [];
  233. for (const rec of this.searchResult.author) {
  234. result.push({key: rec.id, type: 'author', value: rec.author, name: rec.author.replace(/,/g, ', ')});
  235. }
  236. this.tableData = result;
  237. }
  238. async refresh() {
  239. const offset = (this.page - 1)*this.limit;
  240. const newQuery = {
  241. author: this.author,
  242. series: this.series,
  243. title: this.title,
  244. genre: this.genre,
  245. lang: this.lang,
  246. limit: this.limit,
  247. offset,
  248. };
  249. this.queryExecute = newQuery;
  250. if (this.refreshing)
  251. return;
  252. this.refreshing = true;
  253. try {
  254. while (this.queryExecute) {
  255. const query = this.queryExecute;
  256. this.queryExecute = null;
  257. let inSearch = true;
  258. (async() => {
  259. await utils.sleep(500);
  260. if (inSearch)
  261. this.loadingMessage = 'Поиск авторов...';
  262. })();
  263. try {
  264. const result = await this.api.search(query);
  265. this.queryFound = result.author.length;
  266. this.totalFound = result.totalFound;
  267. this.searchResult = result;
  268. await this.updateTableData();
  269. this.scrollToTop();
  270. } catch (e) {
  271. this.$root.stdDialog.alert(e.message, 'Ошибка');
  272. return;
  273. } finally {
  274. inSearch = false;
  275. this.loadingMessage = '';
  276. }
  277. }
  278. } finally {
  279. this.refreshing = false;
  280. }
  281. }
  282. }
  283. export default vueComponent(Search);
  284. //-----------------------------------------------------------------------------
  285. </script>
  286. <style scoped>
  287. .root {
  288. }
  289. .tool-panel {
  290. border-bottom: 1px solid black;
  291. }
  292. .header {
  293. min-height: 30px;
  294. }
  295. .clickable {
  296. color: blue;
  297. cursor: pointer;
  298. }
  299. </style>