RecentBooksPage.vue 23 KB

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