RecentBooksPage.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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="bg-white" rounded style="width: 35px; height: 35px; margin: 6px 6px 0px 6px" @click="showSameBookClick">
  13. <q-icon name="la la-caret-right" class="icon" :class="{'expanded-icon': showSameBook}" color="green-8" size="24px" />
  14. </q-btn>
  15. <q-input
  16. ref="input" v-model="search"
  17. outlined dense
  18. style="position: absolute; top: 4px; left: 200px; width: 350px" bg-color="white"
  19. placeholder="Найти"
  20. @click.stop
  21. >
  22. <template #append>
  23. <q-icon v-if="search !== ''" name="la la-times" class="cursor-pointer" @click.stop="resetSearch" />
  24. </template>
  25. </q-input>
  26. </div>
  27. <q-virtual-scroll
  28. v-slot="{ item, index }"
  29. :items="tableData"
  30. scroll-target="#vs-container"
  31. virtual-scroll-item-size="80"
  32. @virtual-scroll="onScroll"
  33. >
  34. <div class="table-row row" :class="{even: index % 2 > 0, 'active-book': item.active, 'active-parent-book': item.activeParent}">
  35. <div v-show="item.inGroup" class="row-part column justify-center items-center" style="width: 40px; border-right: 1px solid #cccccc">
  36. <q-icon name="la la-code-branch" size="24px" style="color: green" />
  37. </div>
  38. <div class="row-part column justify-center items-stretch" style="width: 80px">
  39. <div class="col row justify-center items-center clickable" @click="loadBook(item)">
  40. <q-icon name="la la-book" size="40px" style="color: #dddddd" />
  41. </div>
  42. <div v-show="!showSameBook && item.group && item.group.length > 0" class="row justify-center" style="font-size: 70%">
  43. {{ (item.group ? item.group.length + 1 : 0) }} верси{{ wordEnding((item.group ? item.group.length + 1 : 0), 1) }}
  44. </div>
  45. </div>
  46. <div class="row-part column items-stretch clickable break-word" :style="{ 'width': (350 - 40*(+item.inGroup)) + 'px' }" style="font-size: 75%" @click="loadBook(item)">
  47. <div class="row" style="font-size: 80%">
  48. <div class="row justify-center row-info-top" style="width: 30px">
  49. {{ item.num }}
  50. </div>
  51. <div class="row justify-center row-info-top" style="width: 130px">
  52. Читался: {{ item.touchTime }}
  53. </div>
  54. <div class="row justify-center row-info-top" style="width: 138px">
  55. Загружен: {{ item.loadTime }}
  56. </div>
  57. <div class="row justify-center row-info-top" style="width: 1px">
  58. </div>
  59. </div>
  60. <div class="col q-mt-xs" :style="{ 'width': (340 - 40*(+item.inGroup)) + 'px' }">
  61. <div class="text-green-10" style="font-size: 105%">
  62. {{ item.desc.author }}
  63. </div>
  64. <div>{{ item.desc.title }}</div>
  65. <!--div>{{ item.path }}</div-->
  66. </div>
  67. <div class="row q-mt-xs" style="font-size: 80%">
  68. <div class="row justify-center row-info-bottom" style="width: 60px">
  69. {{ item.desc.textLen }}
  70. </div>
  71. <div class="row justify-center row-info-bottom" style="width: 60px">
  72. {{ item.desc.perc }}
  73. </div>
  74. <div class="row justify-center row-info-bottom" style="width: 1px">
  75. </div>
  76. </div>
  77. <div class="read-bar" :style="`width: ${(340 - 40*(+item.inGroup))*item.readPart}px`"></div>
  78. </div>
  79. <div class="row-part column justify-center" style="width: 80px; font-size: 75%">
  80. <div>
  81. <a v-show="isUrl(item.url)" :href="item.url" target="_blank">Оригинал</a><br><br>
  82. <a :href="item.path" @click.prevent="downloadBook(item.path, item.fullTitle)">Скачать FB2</a>
  83. </div>
  84. </div>
  85. <div class="row-part column justify-center">
  86. <q-btn
  87. dense
  88. style="width: 30px; height: 30px; padding: 7px 0 7px 0; margin-left: 4px"
  89. @click="handleDel(item.key)"
  90. >
  91. <q-icon class="la la-times" size="14px" />
  92. </q-btn>
  93. </div>
  94. </div>
  95. </q-virtual-scroll>
  96. </div>
  97. <!--q-table
  98. class="recent-books-table col"
  99. :rows="tableData"
  100. row-key="key"
  101. :columns="columns"
  102. :pagination="pagination"
  103. separator="cell"
  104. hide-bottom
  105. virtual-scroll
  106. dense
  107. >
  108. <template #header="props">
  109. <q-tr :props="props">
  110. <q-th key="num" class="td-mp" style="width: 25px" :props="props">
  111. <span v-html="props.cols[0].label"></span>
  112. </q-th>
  113. <q-th key="date" class="td-mp break-word" style="width: 77px" :props="props">
  114. <span v-html="props.cols[1].label"></span>
  115. </q-th>
  116. <q-th key="desc" class="td-mp" style="width: 300px" :props="props" colspan="4">
  117. <q-input
  118. ref="input" v-model="search"
  119. outlined dense style="position: absolute; top: 6px; left: 90px; width: 350px" bg-color="white"
  120. placeholder="Найти"
  121. @click.stop
  122. >
  123. <template #append>
  124. <q-icon v-if="search !== ''" name="la la-times" class="cursor-pointer" @click.stop="resetSearch" />
  125. </template>
  126. </q-input>
  127. <span v-html="props.cols[2].label"></span>
  128. </q-th>
  129. </q-tr>
  130. </template>
  131. <template #body="props">
  132. <q-tr :props="props">
  133. <q-td key="num" :props="props" class="td-mp" auto-width>
  134. <div class="break-word" style="width: 25px">
  135. {{ props.row.num }}
  136. </div>
  137. </q-td>
  138. <q-td key="date" auto-width :props="props" class="td-mp clickable" @click="loadBook(props.row)">
  139. <div class="break-word" style="width: 68px">
  140. {{ props.row.touchDate }}<br>
  141. {{ props.row.touchTime }}
  142. </div>
  143. </q-td>
  144. <q-td key="desc" auto-width :props="props" class="td-mp clickable" @click="loadBook(props.row)">
  145. <div class="break-word" style="width: 300px; font-size: 90%">
  146. <div style="color: green">
  147. {{ props.row.desc.author }}
  148. </div>
  149. <div>{{ props.row.desc.title }}</div>
  150. <div class="read-bar" :style="`width: ${300*props.row.readPart}px`"></div>
  151. </div>
  152. </q-td>
  153. <q-td key="links" :props="props" class="td-mp" auto-width>
  154. <div class="break-word" style="width: 75px; font-size: 90%">
  155. <a v-show="isUrl(props.row.url)" :href="props.row.url" target="_blank">Оригинал</a><br>
  156. <a :href="props.row.path" @click.prevent="downloadBook(props.row.path, props.row.fullTitle)">Скачать FB2</a>
  157. </div>
  158. </q-td>
  159. <q-td key="close" :props="props" class="td-mp" auto-width>
  160. <div style="width: 38px">
  161. <q-btn
  162. dense
  163. style="width: 30px; height: 30px; padding: 7px 0 7px 0; margin-left: 4px"
  164. @click="handleDel(props.row.key)"
  165. >
  166. <q-icon class="la la-times" size="14px" />
  167. </q-btn>
  168. </div>
  169. </q-td>
  170. <q-td key="last" :props="props" class="no-mp">
  171. </q-td>
  172. </q-tr>
  173. </template>
  174. </q-table-->
  175. </Window>
  176. </template>
  177. <script>
  178. //-----------------------------------------------------------------------------
  179. import vueComponent from '../../vueComponent.js';
  180. import path from 'path-browserify';
  181. import _ from 'lodash';
  182. import * as utils from '../../../share/utils';
  183. import LockQueue from '../../../share/LockQueue';
  184. import Window from '../../share/Window.vue';
  185. import bookManager from '../share/bookManager';
  186. import readerApi from '../../../api/reader';
  187. const componentOptions = {
  188. components: {
  189. Window,
  190. },
  191. watch: {
  192. search() {
  193. this.updateTableData();
  194. },
  195. settings() {
  196. this.loadSettings();
  197. },
  198. },
  199. };
  200. class RecentBooksPage {
  201. _options = componentOptions;
  202. loading = false;
  203. search = '';
  204. tableData = [];
  205. sortMethod = '';
  206. showSameBook = false;
  207. created() {
  208. this.commit = this.$store.commit;
  209. this.lastScrollTop1 = 0;
  210. this.lastScrollTop2 = 0;
  211. this.lock = new LockQueue(100);
  212. this.loadSettings();
  213. }
  214. init() {
  215. this.$refs.window.init();
  216. this.$nextTick(() => {
  217. //this.$refs.input.focus();//плохо на планшетах
  218. });
  219. this.updateTableData();//no await
  220. }
  221. loadSettings() {
  222. const settings = this.settings;
  223. this.showSameBook = settings.recentShowSameBook;
  224. this.sortMethod = settings.recentSortMethod || 'loadTimeDesc';
  225. }
  226. get settings() {
  227. return this.$store.state.reader.settings;
  228. }
  229. async updateTableData() {
  230. await this.lock.get();
  231. try {
  232. let result = [];
  233. const sorted = bookManager.getSortedRecent();
  234. const activeBook = bookManager.mostRecentBook();
  235. //подготовка полей
  236. for (const book of sorted) {
  237. if (book.deleted)
  238. continue;
  239. let d = new Date();
  240. d.setTime(book.touchTime);
  241. const touchTime = utils.formatDate(d);
  242. const loadTimeRaw = (book.loadTime ? book.loadTime : 0);//book.addTime);
  243. d.setTime(loadTimeRaw);
  244. const loadTime = utils.formatDate(d);
  245. let readPart = 0;
  246. let perc = '';
  247. let textLen = '';
  248. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  249. if (book.textLength) {
  250. readPart = p/book.textLength;
  251. perc = `${(readPart*100).toFixed(2)}%`;
  252. textLen = `${Math.floor(readPart*book.textLength/1000)}/${Math.floor(book.textLength/1000)}`;
  253. }
  254. const bt = utils.getBookTitle(book.fb2);
  255. let title = bt.bookTitle;
  256. title = (title ? `"${title}"`: '');
  257. const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : (book.uploadFileName ? book.uploadFileName : book.url)));
  258. result.push({
  259. touchTime,
  260. loadTime,
  261. desc: {
  262. author,
  263. title,
  264. perc,
  265. textLen,
  266. },
  267. readPart,
  268. url: book.url,
  269. path: book.path,
  270. fullTitle: bt.fullTitle,
  271. key: book.key,
  272. sameBookKey: book.sameBookKey,
  273. active: (activeBook.key == book.key),
  274. activeParent: false,
  275. inGroup: false,
  276. //для сортировки
  277. loadTimeRaw,
  278. touchTimeRaw: book.touchTime,
  279. descString: `${author}${title}${perc}${textLen}`,
  280. });
  281. }
  282. //нумерация
  283. let num = 0;
  284. result.sort((a, b) => b.loadTimeRaw - a.loadTimeRaw);
  285. for (const book of result) {
  286. num++;
  287. book.num = num;
  288. }
  289. //фильтрация
  290. const search = this.search;
  291. if (search) {
  292. result = result.filter(item => {
  293. return !search ||
  294. item.touchTime.includes(search) ||
  295. item.loadTime.includes(search) ||
  296. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  297. item.desc.author.toLowerCase().includes(search.toLowerCase())
  298. });
  299. }
  300. //сортировка
  301. switch (this.sortMethod) {
  302. case 'loadTimeDesc':
  303. result.sort((a, b) => b.loadTimeRaw - a.loadTimeRaw);
  304. break;
  305. case 'loadTimeAsc':
  306. result.sort((a, b) => a.loadTimeRaw - b.loadTimeRaw);
  307. break;
  308. case 'touchTimeDesc':
  309. result.sort((a, b) => b.touchTimeRaw - a.touchTimeRaw);
  310. break;
  311. case 'touchTimeAsc':
  312. result.sort((a, b) => a.touchTimeRaw - b.touchTimeRaw);
  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. /*async updateTableData(limit) {
  358. while (this.updating) await utils.sleep(100);
  359. this.updating = true;
  360. let result = [];
  361. const sorted = bookManager.getSortedRecent();
  362. let num = 0;
  363. for (let i = 0; i < sorted.length; i++) {
  364. const book = sorted[i];
  365. if (book.deleted)
  366. continue;
  367. num++;
  368. if (limit && result.length >= limit)
  369. break;
  370. let d = new Date();
  371. d.setTime(book.touchTime);
  372. const t = utils.formatDate(d).split(' ');
  373. let readPart = 0;
  374. let perc = '';
  375. let textLen = '';
  376. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  377. if (book.textLength) {
  378. readPart = p/book.textLength;
  379. perc = ` [${(readPart*100).toFixed(2)}%]`;
  380. textLen = ` ${Math.round(book.textLength/1000)}k`;
  381. }
  382. const bt = utils.getBookTitle(book.fb2);
  383. let title = bt.bookTitle;
  384. title = (title ? `"${title}"`: '');
  385. const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : (book.uploadFileName ? book.uploadFileName : book.url)));
  386. result.push({
  387. num,
  388. touchDateTime: book.touchTime,
  389. touchDate: t[0],
  390. touchTime: t[1],
  391. desc: {
  392. author,
  393. title: `${title}${perc}${textLen}`,
  394. },
  395. readPart,
  396. descString: `${author}${title}${perc}${textLen}`,//для сортировки
  397. url: book.url,
  398. path: book.path,
  399. fullTitle: bt.fullTitle,
  400. key: book.key,
  401. });
  402. }
  403. const search = this.search;
  404. result = result.filter(item => {
  405. return !search ||
  406. item.touchTime.includes(search) ||
  407. item.touchDate.includes(search) ||
  408. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  409. item.desc.author.toLowerCase().includes(search.toLowerCase())
  410. });
  411. this.tableData = result;
  412. this.updating = false;
  413. }*/
  414. resetSearch() {
  415. this.search = '';
  416. this.$refs.input.focus();
  417. }
  418. wordEnding(num, type = 0) {
  419. const endings = [
  420. ['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],
  421. ['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й']
  422. ];
  423. const deci = num % 100;
  424. if (deci > 10 && deci < 20) {
  425. return endings[type][0];
  426. } else {
  427. return endings[type][num % 10];
  428. }
  429. }
  430. get header() {
  431. const len = (this.tableData ? this.tableData.length : 0);
  432. return `${(this.search ? 'Найдено' : 'Всего')} ${len} файл${this.wordEnding(len)}`;
  433. }
  434. async downloadBook(fb2path, fullTitle) {
  435. try {
  436. await readerApi.checkCachedBook(fb2path);
  437. const d = this.$refs.download;
  438. d.href = fb2path;
  439. try {
  440. const fn = utils.makeValidFilename(fullTitle);
  441. d.download = fn.substring(0, 100) + '.fb2';
  442. } catch(e) {
  443. d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
  444. }
  445. d.click();
  446. } catch (e) {
  447. let errMes = e.message;
  448. if (errMes.indexOf('404') >= 0)
  449. errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
  450. this.$root.stdDialog.alert(errMes, 'Ошибка', {color: 'negative'});
  451. }
  452. }
  453. async handleDel(key) {
  454. await bookManager.delRecentBook({key});
  455. //this.updateTableData();//обновление уже происходит Reader.bookManagerEvent
  456. if (!bookManager.mostRecentBook())
  457. this.close();
  458. }
  459. loadBook(row) {
  460. this.$emit('load-book', {url: row.url, path: row.path});
  461. this.close();
  462. }
  463. isUrl(url) {
  464. if (url)
  465. return (url.indexOf('disk://') != 0);
  466. else
  467. return false;
  468. }
  469. onScroll() {
  470. const curScrollTop = this.$refs.vsContainer.scrollTop;
  471. if (curScrollTop - this.lastScrollTop1 > 150) {
  472. this.$refs.header.style.top = `-${this.$refs.header.offsetHeight}px`;
  473. this.$refs.header.style.transition = 'top 0.2s ease 0s';
  474. this.lastScrollTop1 = curScrollTop;
  475. } else if (curScrollTop - this.lastScrollTop2 < 0) {
  476. this.$refs.header.style.position = 'sticky';
  477. this.$refs.header.style.top = 0;
  478. this.lastScrollTop1 = curScrollTop;
  479. }
  480. this.lastScrollTop2 = curScrollTop;
  481. }
  482. showSameBookClick() {
  483. this.showSameBook = !this.showSameBook;
  484. const newSettings = _.cloneDeep(this.settings);
  485. newSettings.recentShowSameBook = this.showSameBook;
  486. this.commit('reader/setSettings', newSettings);
  487. this.updateTableData();
  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. }
  515. .table-row {
  516. min-height: 80px;
  517. border-bottom: 1px solid #cccccc;
  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. .read-bar {
  532. height: 3px;
  533. background-color: #aaaaaa;
  534. margin-bottom: 2px;
  535. }
  536. .even {
  537. background-color: #f2f2f2;
  538. }
  539. .active-book {
  540. background-color: #b0f0b0 !important;
  541. }
  542. .active-parent-book {
  543. background-color: #ffbbbb !important;
  544. }
  545. .icon {
  546. transition: transform 0.2s;
  547. }
  548. .expanded-icon {
  549. transform: rotate(90deg);
  550. }
  551. .row-info-top {
  552. line-height: 110%;
  553. border-left: 1px solid #cccccc;
  554. border-bottom: 1px solid #cccccc;
  555. }
  556. .row-info-bottom {
  557. line-height: 110%;
  558. border: 1px solid #cccccc;
  559. border-right: 0;
  560. }
  561. </style>