RecentBooksPage.vue 23 KB

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