RecentBooksPage.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. <template>
  2. <Window ref="window" width="600px" @close="close">
  3. <template #header>
  4. <span v-show="!loading">{{ header }}</span>
  5. <span v-if="loading"><q-spinner class="q-mr-sm" color="lime-12" size="20px" :thickness="7" />
  6. Список загружается
  7. </span>
  8. </template>
  9. <a ref="download" style="display: none;" target="_blank"></a>
  10. <div id="vs-container" ref="vsContainer" class="recent-books-scroll col">
  11. <div ref="header" class="scroll-header row bg-blue-2">
  12. <q-btn class="tool-button" round @click="showSameBookClick">
  13. <q-icon name="la la-caret-right" class="icon" :class="{'expanded-icon': showSameBook}" color="green-8" size="24px" />
  14. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  15. Показать/скрыть версии книг
  16. </q-tooltip>
  17. </q-btn>
  18. <q-btn class="tool-button" round @click="scrollToBegin">
  19. <q-icon name="la la-arrow-up" color="green-8" size="24px" />
  20. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  21. В начало списка
  22. </q-tooltip>
  23. </q-btn>
  24. <q-btn class="tool-button" round @click="scrollToEnd">
  25. <q-icon name="la la-arrow-down" color="green-8" size="24px" />
  26. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  27. В конец списка
  28. </q-tooltip>
  29. </q-btn>
  30. <q-btn class="tool-button" round @click="scrollToActiveBook">
  31. <q-icon name="la la-location-arrow" color="green-8" size="24px" />
  32. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  33. На текущую книгу
  34. </q-tooltip>
  35. </q-btn>
  36. <q-input
  37. ref="input"
  38. v-model="search"
  39. class="q-ml-sm q-mt-xs"
  40. outlined dense
  41. style="width: 185px"
  42. bg-color="white"
  43. placeholder="Найти"
  44. @click.stop
  45. >
  46. <template #append>
  47. <q-icon v-if="search !== ''" name="la la-times" class="cursor-pointer" @click.stop="resetSearch" />
  48. </template>
  49. </q-input>
  50. <q-select
  51. ref="sortMethod"
  52. v-model="sortMethod"
  53. class="q-ml-sm q-mt-xs"
  54. :options="sortMethodOptions"
  55. style="width: 180px"
  56. bg-color="white"
  57. dropdown-icon="la la-angle-down la-sm"
  58. outlined dense emit-value map-options display-value-sanitize options-sanitize
  59. options-html display-value-html
  60. @update:model-value="sortMethodSelected"
  61. >
  62. <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%">
  63. Метод сортировки
  64. </q-tooltip>
  65. <template #selected-item="scope">
  66. <div style="height: 28px; padding-top: 2px; overflow: hidden" v-html="scope.opt.label" />
  67. </template>
  68. </q-select>
  69. </div>
  70. <q-virtual-scroll
  71. ref="virtualScroll"
  72. v-slot="{ item, index }"
  73. :items="tableData"
  74. scroll-target="#vs-container"
  75. virtual-scroll-item-size="80"
  76. @virtual-scroll="onScroll"
  77. >
  78. <div class="table-row row" :class="{even: index % 2 > 0, 'active-book': item.active, 'active-parent-book': item.activeParent}">
  79. <div v-show="item.inGroup" class="row-part column justify-center items-center" style="width: 40px">
  80. <q-icon name="la la-code-branch" size="24px" style="color: green" />
  81. </div>
  82. <div class="row-part column justify-center items-stretch" style="width: 80px">
  83. <div class="col row justify-center items-center clickable" @click="loadBook(item)">
  84. <q-icon name="la la-book" size="40px" style="color: #dddddd" />
  85. </div>
  86. <div v-show="!showSameBook && item.group && item.group.length > 0" class="row justify-center" style="font-size: 70%">
  87. {{ (item.group ? item.group.length + 1 : 0) }} верси{{ wordEnding((item.group ? item.group.length + 1 : 0), 1) }}
  88. </div>
  89. </div>
  90. <div class="row-part column items-stretch clickable break-word" :style="{ 'width': (350 - 40*(+item.inGroup)) + 'px' }" style="font-size: 75%" @click="loadBook(item)">
  91. <div class="col" style="border: 1px solid #cccccc; border-bottom: 0; padding: 4px" :style="{ 'width': (340 - 40*(+item.inGroup)) + 'px' }">
  92. <div class="text-green-10" style="font-size: 105%">
  93. {{ item.desc.author }}
  94. </div>
  95. <div>{{ item.desc.title }}</div>
  96. </div>
  97. <div class="row" style="font-size: 10px">
  98. <div class="row justify-center items-center row-info-top" style="width: 60px">
  99. {{ item.desc.textLen }}
  100. </div>
  101. <div class="row items-center row-info-top" :style="`width: ${(220 - 40*(+item.inGroup))}px; padding: 1px`">
  102. <div class="read-bar" :style="`width: ${100*item.readPart}%`"></div>
  103. </div>
  104. <div class="row justify-center items-center row-info-top" style="width: 59px">
  105. {{ item.desc.perc }}
  106. </div>
  107. <div class="row-info-top" style="width: 1px">
  108. </div>
  109. </div>
  110. <div class="row" style="font-size: 10px" :style="{ 'width': (340 - 40*(+item.inGroup)) + 'px' }">
  111. <div class="row justify-center items-center row-info-bottom" style="width: 30px">
  112. {{ item.num }}
  113. </div>
  114. <div class="col row">
  115. <div class="row justify-center items-center row-info-bottom time-info" style="width: 50%">
  116. Загружен: {{ item.loadTime }}
  117. </div>
  118. <div class="row justify-center items-center row-info-bottom time-info" style="width: 50%">
  119. Читался: {{ item.touchTime }}
  120. </div>
  121. </div>
  122. <div class="row-info-bottom" style="width: 1px">
  123. </div>
  124. </div>
  125. </div>
  126. <div class="row-part column justify-center" style="width: 80px; font-size: 75%">
  127. <div>
  128. <a v-show="isUrl(item.url)" :href="item.url" target="_blank">Оригинал</a><br><br>
  129. <a :href="item.path" @click.prevent="downloadBook(item.path, item.fullTitle)">Скачать FB2</a>
  130. </div>
  131. </div>
  132. <div class="row-part column justify-center">
  133. <q-btn
  134. dense
  135. style="width: 30px; height: 30px; padding: 7px 0 7px 0; margin-left: 4px"
  136. @click="handleDel(item.key)"
  137. >
  138. <q-icon class="la la-times" size="14px" />
  139. </q-btn>
  140. </div>
  141. </div>
  142. </q-virtual-scroll>
  143. </div>
  144. </Window>
  145. </template>
  146. <script>
  147. //-----------------------------------------------------------------------------
  148. import vueComponent from '../../vueComponent.js';
  149. import path from 'path-browserify';
  150. import _ from 'lodash';
  151. import * as utils from '../../../share/utils';
  152. import LockQueue from '../../../share/LockQueue';
  153. import Window from '../../share/Window.vue';
  154. import bookManager from '../share/bookManager';
  155. import readerApi from '../../../api/reader';
  156. const componentOptions = {
  157. components: {
  158. Window,
  159. },
  160. watch: {
  161. search() {
  162. this.updateTableData();
  163. },
  164. sortMethod() {
  165. this.updateTableData();
  166. },
  167. settings() {
  168. this.loadSettings();
  169. },
  170. },
  171. };
  172. class RecentBooksPage {
  173. _options = componentOptions;
  174. loading = false;
  175. search = '';
  176. tableData = [];
  177. sortMethod = '';
  178. showSameBook = false;
  179. created() {
  180. this.commit = this.$store.commit;
  181. this.lastScrollTop1 = 0;
  182. this.lastScrollTop2 = 0;
  183. this.lock = new LockQueue(100);
  184. this.loadSettings();
  185. }
  186. init() {
  187. this.$refs.window.init();
  188. this.$nextTick(() => {
  189. //this.$refs.input.focus();//плохо на планшетах
  190. });
  191. this.inited = true;
  192. (async() => {
  193. this.showBar();
  194. await this.updateTableData();
  195. await this.scrollToActiveBook();
  196. })();
  197. }
  198. loadSettings() {
  199. const settings = this.settings;
  200. this.showSameBook = settings.recentShowSameBook;
  201. this.sortMethod = settings.recentSortMethod || 'loadTimeDesc';
  202. }
  203. get settings() {
  204. return this.$store.state.reader.settings;
  205. }
  206. async updateTableData() {
  207. if (!this.inited)
  208. return;
  209. await this.lock.get();
  210. try {
  211. let result = [];
  212. const sorted = bookManager.getSortedRecent();
  213. const activeBook = bookManager.mostRecentBook();
  214. //подготовка полей
  215. for (const book of sorted) {
  216. if (book.deleted)
  217. continue;
  218. let d = new Date();
  219. d.setTime(book.touchTime);
  220. const touchTime = utils.formatDate(d);
  221. const loadTimeRaw = (book.loadTime ? book.loadTime : 0);//book.addTime);
  222. d.setTime(loadTimeRaw);
  223. const loadTime = utils.formatDate(d);
  224. let readPart = 0;
  225. let perc = '';
  226. let textLen = '';
  227. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  228. if (book.textLength) {
  229. readPart = p/book.textLength;
  230. perc = `${(readPart*100).toFixed(2)}%`;
  231. textLen = `${Math.floor(readPart*book.textLength/1000)}/${Math.floor(book.textLength/1000)}`;
  232. }
  233. const bt = utils.getBookTitle(book.fb2);
  234. let title = bt.bookTitle;
  235. title = (title ? `"${title}"`: '');
  236. const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : (book.uploadFileName ? book.uploadFileName : book.url)));
  237. result.push({
  238. touchTime,
  239. loadTime,
  240. desc: {
  241. author,
  242. title,
  243. perc,
  244. textLen,
  245. },
  246. readPart,
  247. url: book.url,
  248. path: book.path,
  249. fullTitle: bt.fullTitle,
  250. key: book.key,
  251. sameBookKey: book.sameBookKey,
  252. active: (activeBook.key == book.key),
  253. activeParent: false,
  254. inGroup: false,
  255. //для сортировки
  256. loadTimeRaw,
  257. touchTimeRaw: book.touchTime,
  258. });
  259. }
  260. //нумерация
  261. result.sort((a, b) => b.loadTimeRaw - a.loadTimeRaw);
  262. let num = 0;
  263. for (let i = result.length - 1; i >= 0; i--) {
  264. num++;
  265. result[i].num = num;
  266. }
  267. //фильтрация
  268. const search = this.search;
  269. if (search) {
  270. result = result.filter(item => {
  271. return !search ||
  272. item.touchTime.includes(search) ||
  273. item.loadTime.includes(search) ||
  274. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  275. item.desc.author.toLowerCase().includes(search.toLowerCase())
  276. });
  277. }
  278. //сортировка
  279. switch (this.sortMethod) {
  280. case 'loadTimeDesc':
  281. result.sort((a, b) => b.loadTimeRaw - a.loadTimeRaw);
  282. break;
  283. case 'loadTimeAsc':
  284. result.sort((a, b) => a.loadTimeRaw - b.loadTimeRaw);
  285. break;
  286. case 'touchTimeDesc':
  287. result.sort((a, b) => b.touchTimeRaw - a.touchTimeRaw);
  288. break;
  289. case 'touchTimeAsc':
  290. result.sort((a, b) => a.touchTimeRaw - b.touchTimeRaw);
  291. break;
  292. case 'authorDesc':
  293. result.sort((a, b) => b.desc.author.localeCompare(a.desc.author));
  294. break;
  295. case 'authorAsc':
  296. result.sort((a, b) => a.desc.author.localeCompare(b.desc.author));
  297. break;
  298. case 'titleDesc':
  299. result.sort((a, b) => b.desc.title.localeCompare(a.desc.title));
  300. break;
  301. case 'titleAsc':
  302. result.sort((a, b) => a.desc.title.localeCompare(b.desc.title));
  303. break;
  304. }
  305. //группировка
  306. const groups = {};
  307. const parents = {};
  308. let newResult = [];
  309. for (const book of result) {
  310. if (book.sameBookKey !== undefined) {
  311. if (!groups[book.sameBookKey]) {
  312. groups[book.sameBookKey] = [];
  313. parents[book.sameBookKey] = book;
  314. book.group = groups[book.sameBookKey];
  315. newResult.push(book);
  316. } else {
  317. book.inGroup = true;
  318. if (book.active)
  319. parents[book.sameBookKey].activeParent = true;
  320. groups[book.sameBookKey].push(book);
  321. }
  322. } else {
  323. newResult.push(book);
  324. }
  325. }
  326. result = newResult;
  327. //showSameBook
  328. if (this.showSameBook) {
  329. newResult = [];
  330. for (const book of result) {
  331. newResult.push(book);
  332. if (book.group) {
  333. for (const sameBook of book.group) {
  334. newResult.push(sameBook);
  335. }
  336. }
  337. }
  338. result = newResult;
  339. }
  340. //другие стадии
  341. //.....
  342. this.tableData = result;
  343. } finally {
  344. this.lock.ret();
  345. }
  346. }
  347. resetSearch() {
  348. this.search = '';
  349. this.$refs.input.focus();
  350. }
  351. wordEnding(num, type = 0) {
  352. const endings = [
  353. ['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],
  354. ['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й']
  355. ];
  356. const deci = num % 100;
  357. if (deci > 10 && deci < 20) {
  358. return endings[type][0];
  359. } else {
  360. return endings[type][num % 10];
  361. }
  362. }
  363. get header() {
  364. const len = (this.tableData ? this.tableData.length : 0);
  365. return `${(this.search ? 'Найдено' : 'Всего')} ${len} файл${this.wordEnding(len)}`;
  366. }
  367. async downloadBook(fb2path, fullTitle) {
  368. try {
  369. await readerApi.checkCachedBook(fb2path);
  370. const d = this.$refs.download;
  371. d.href = fb2path;
  372. try {
  373. const fn = utils.makeValidFilename(fullTitle);
  374. d.download = fn.substring(0, 100) + '.fb2';
  375. } catch(e) {
  376. d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
  377. }
  378. d.click();
  379. } catch (e) {
  380. let errMes = e.message;
  381. if (errMes.indexOf('404') >= 0)
  382. errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
  383. this.$root.stdDialog.alert(errMes, 'Ошибка', {color: 'negative'});
  384. }
  385. }
  386. async handleDel(key) {
  387. await bookManager.delRecentBook({key});
  388. //this.updateTableData();//обновление уже происходит Reader.bookManagerEvent
  389. if (!bookManager.mostRecentBook())
  390. this.close();
  391. }
  392. loadBook(row) {
  393. this.$emit('load-book', {url: row.url, path: row.path});
  394. this.close();
  395. }
  396. isUrl(url) {
  397. if (url)
  398. return (url.indexOf('disk://') != 0);
  399. else
  400. return false;
  401. }
  402. showBar() {
  403. this.lastScrollTop1 = this.$refs.vsContainer.scrollTop;
  404. this.$refs.header.style.position = 'sticky';
  405. this.$refs.header.style.top = 0;
  406. }
  407. onScroll() {
  408. const curScrollTop = this.$refs.vsContainer.scrollTop;
  409. if (this.lockScroll) {
  410. this.lastScrollTop1 = curScrollTop;
  411. return;
  412. }
  413. if (curScrollTop - this.lastScrollTop1 > 100) {
  414. this.$refs.header.style.top = `-${this.$refs.header.offsetHeight}px`;
  415. this.$refs.header.style.transition = 'top 0.2s ease 0s';
  416. this.lastScrollTop1 = curScrollTop;
  417. } else if (curScrollTop - this.lastScrollTop2 < 0) {
  418. this.$refs.header.style.position = 'sticky';
  419. this.$refs.header.style.top = 0;
  420. this.lastScrollTop1 = curScrollTop;
  421. }
  422. this.lastScrollTop2 = curScrollTop;
  423. }
  424. showSameBookClick() {
  425. this.showSameBook = !this.showSameBook;
  426. const newSettings = _.cloneDeep(this.settings);
  427. newSettings.recentShowSameBook = this.showSameBook;
  428. this.commit('reader/setSettings', newSettings);
  429. this.updateTableData();
  430. }
  431. sortMethodSelected() {
  432. const newSettings = _.cloneDeep(this.settings);
  433. newSettings.recentSortMethod = this.sortMethod;
  434. this.commit('reader/setSettings', newSettings);
  435. }
  436. async scrollToActiveBook() {
  437. this.lockScroll = true;
  438. try {
  439. let activeIndex = -1;
  440. let activeParentIndex = -1;
  441. for (let i = 0; i < this.tableData.length; i++) {
  442. const book = this.tableData[i];
  443. if (book.active)
  444. activeIndex = i;
  445. if (book.activeParent)
  446. activeParentIndex = i;
  447. if (activeIndex >= 0 && activeParentIndex >= 0)
  448. break;
  449. }
  450. const index = (activeIndex >= 0 ? activeIndex : activeParentIndex);
  451. if (index >= 0) {
  452. this.$refs.virtualScroll.scrollTo(index, 'center');
  453. }
  454. } finally {
  455. await utils.sleep(100);
  456. this.lockScroll = false;
  457. }
  458. }
  459. async scrollToBegin() {
  460. this.lockScroll = true;
  461. try {
  462. this.$refs.virtualScroll.scrollTo(0, 'center');
  463. } finally {
  464. await utils.sleep(100);
  465. this.lockScroll = false;
  466. }
  467. }
  468. async scrollToEnd() {
  469. this.lockScroll = true;
  470. try {
  471. this.$refs.virtualScroll.scrollTo(this.tableData.length, 'center');
  472. } finally {
  473. await utils.sleep(100);
  474. this.lockScroll = false;
  475. }
  476. }
  477. get sortMethodOptions() {
  478. return [
  479. {label: '<span style="font-size: 150%">&uarr;</span> Время загрузки', value: 'loadTimeDesc'},
  480. {label: '<span style="font-size: 150%">&darr;</span> Время загрузки', value: 'loadTimeAsc'},
  481. {label: '<span style="font-size: 150%">&uarr;</span> Время чтения', value: 'touchTimeDesc'},
  482. {label: '<span style="font-size: 150%">&darr;</span> Время чтения', value: 'touchTimeAsc'},
  483. {label: '<span style="font-size: 150%">&uarr;</span> Автор', value: 'authorDesc'},
  484. {label: '<span style="font-size: 150%">&darr;</span> Автор', value: 'authorAsc'},
  485. {label: '<span style="font-size: 150%">&uarr;</span> Название', value: 'titleDesc'},
  486. {label: '<span style="font-size: 150%">&darr;</span> Название', value: 'titleAsc'},
  487. ];
  488. }
  489. close() {
  490. this.$emit('recent-books-close');
  491. }
  492. keyHook(event) {
  493. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  494. this.close();
  495. }
  496. return true;
  497. }
  498. }
  499. export default vueComponent(RecentBooksPage);
  500. //-----------------------------------------------------------------------------
  501. </script>
  502. <style scoped>
  503. .recent-books-scroll {
  504. width: 573px;
  505. overflow-y: auto;
  506. overflow-x: hidden;
  507. }
  508. .scroll-header {
  509. height: 50px;
  510. position: sticky;
  511. z-index: 1;
  512. top: 0;
  513. border-bottom: 2px solid #aaaaaa;
  514. padding-left: 5px;
  515. }
  516. .table-row {
  517. min-height: 80px;
  518. }
  519. .row-part {
  520. padding: 4px 4px 4px 4px;
  521. }
  522. .clickable {
  523. cursor: pointer;
  524. }
  525. .break-word {
  526. line-height: 180%;
  527. overflow-wrap: break-word;
  528. word-wrap: break-word;
  529. white-space: normal;
  530. }
  531. .even {
  532. background-color: #f2f2f2;
  533. }
  534. .active-book {
  535. background-color: #b0f0b0 !important;
  536. }
  537. .active-parent-book {
  538. background-color: #ffbbbb !important;
  539. }
  540. .icon {
  541. transition: transform 0.2s;
  542. }
  543. .expanded-icon {
  544. transform: rotate(90deg);
  545. }
  546. .tool-button {
  547. min-width: 30px;
  548. width: 30px;
  549. min-height: 30px;
  550. height: 30px;
  551. margin: 10px 6px 0px 3px;
  552. background-color: white;
  553. }
  554. .row-info-bottom {
  555. line-height: 110%;
  556. border-left: 1px solid #cccccc;
  557. border-bottom: 1px solid #cccccc;
  558. height: 12px;
  559. }
  560. .row-info-top {
  561. line-height: 110%;
  562. border: 1px solid #cccccc;
  563. border-right: 0;
  564. height: 12px;
  565. }
  566. .time-info, .row-info-top {
  567. color: #888888;
  568. }
  569. .read-bar {
  570. height: 6px;
  571. background-color: #bbbbbb;
  572. }
  573. </style>