Search.vue 11 KB

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