Search.vue 13 KB

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