RecentBooksPage.vue 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  296. item.desc.author.toLowerCase().includes(search.toLowerCase())
  297. });
  298. }
  299. //сортировка
  300. switch (this.sortMethod) {
  301. case 'loadTimeDesc':
  302. result.sort((a, b) => b.loadTimeRaw - a.loadTimeRaw);
  303. break;
  304. case 'loadTimeAsc':
  305. result.sort((a, b) => a.loadTimeRaw - b.loadTimeRaw);
  306. break;
  307. case 'touchTimeDesc':
  308. result.sort((a, b) => b.touchTimeRaw - a.touchTimeRaw);
  309. break;
  310. case 'touchTimeAsc':
  311. result.sort((a, b) => a.touchTimeRaw - b.touchTimeRaw);
  312. break;
  313. }
  314. //группировка
  315. const groups = {};
  316. const parents = {};
  317. let newResult = [];
  318. for (const book of result) {
  319. if (book.sameBookKey !== undefined) {
  320. if (!groups[book.sameBookKey]) {
  321. groups[book.sameBookKey] = [];
  322. parents[book.sameBookKey] = book;
  323. book.group = groups[book.sameBookKey];
  324. newResult.push(book);
  325. } else {
  326. book.inGroup = true;
  327. if (book.active)
  328. parents[book.sameBookKey].activeParent = true;
  329. groups[book.sameBookKey].push(book);
  330. }
  331. } else {
  332. newResult.push(book);
  333. }
  334. }
  335. result = newResult;
  336. //showSameBook
  337. if (this.showSameBook) {
  338. newResult = [];
  339. for (const book of result) {
  340. newResult.push(book);
  341. if (book.group) {
  342. for (const sameBook of book.group) {
  343. newResult.push(sameBook);
  344. }
  345. }
  346. }
  347. result = newResult;
  348. }
  349. //другие стадии
  350. //.....
  351. this.tableData = result;
  352. } finally {
  353. this.lock.ret();
  354. }
  355. }
  356. /*async updateTableData(limit) {
  357. while (this.updating) await utils.sleep(100);
  358. this.updating = true;
  359. let result = [];
  360. const sorted = bookManager.getSortedRecent();
  361. let num = 0;
  362. for (let i = 0; i < sorted.length; i++) {
  363. const book = sorted[i];
  364. if (book.deleted)
  365. continue;
  366. num++;
  367. if (limit && result.length >= limit)
  368. break;
  369. let d = new Date();
  370. d.setTime(book.touchTime);
  371. const t = utils.formatDate(d).split(' ');
  372. let readPart = 0;
  373. let perc = '';
  374. let textLen = '';
  375. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  376. if (book.textLength) {
  377. readPart = p/book.textLength;
  378. perc = ` [${(readPart*100).toFixed(2)}%]`;
  379. textLen = ` ${Math.round(book.textLength/1000)}k`;
  380. }
  381. const bt = utils.getBookTitle(book.fb2);
  382. let title = bt.bookTitle;
  383. title = (title ? `"${title}"`: '');
  384. const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : (book.uploadFileName ? book.uploadFileName : book.url)));
  385. result.push({
  386. num,
  387. touchDateTime: book.touchTime,
  388. touchDate: t[0],
  389. touchTime: t[1],
  390. desc: {
  391. author,
  392. title: `${title}${perc}${textLen}`,
  393. },
  394. readPart,
  395. descString: `${author}${title}${perc}${textLen}`,//для сортировки
  396. url: book.url,
  397. path: book.path,
  398. fullTitle: bt.fullTitle,
  399. key: book.key,
  400. });
  401. }
  402. const search = this.search;
  403. result = result.filter(item => {
  404. return !search ||
  405. item.touchTime.includes(search) ||
  406. item.touchDate.includes(search) ||
  407. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  408. item.desc.author.toLowerCase().includes(search.toLowerCase())
  409. });
  410. this.tableData = result;
  411. this.updating = false;
  412. }*/
  413. resetSearch() {
  414. this.search = '';
  415. this.$refs.input.focus();
  416. }
  417. wordEnding(num, type = 0) {
  418. const endings = [
  419. ['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'],
  420. ['й', 'я', 'и', 'и', 'и', 'й', 'й', 'й', 'й', 'й']
  421. ];
  422. const deci = num % 100;
  423. if (deci > 10 && deci < 20) {
  424. return endings[type][0];
  425. } else {
  426. return endings[type][num % 10];
  427. }
  428. }
  429. get header() {
  430. const len = (this.tableData ? this.tableData.length : 0);
  431. return `${(this.search ? 'Найдено' : 'Всего')} ${len} файл${this.wordEnding(len)}`;
  432. }
  433. async downloadBook(fb2path, fullTitle) {
  434. try {
  435. await readerApi.checkCachedBook(fb2path);
  436. const d = this.$refs.download;
  437. d.href = fb2path;
  438. try {
  439. const fn = utils.makeValidFilename(fullTitle);
  440. d.download = fn.substring(0, 100) + '.fb2';
  441. } catch(e) {
  442. d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
  443. }
  444. d.click();
  445. } catch (e) {
  446. let errMes = e.message;
  447. if (errMes.indexOf('404') >= 0)
  448. errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
  449. this.$root.stdDialog.alert(errMes, 'Ошибка', {color: 'negative'});
  450. }
  451. }
  452. async handleDel(key) {
  453. await bookManager.delRecentBook({key});
  454. //this.updateTableData();//обновление уже происходит Reader.bookManagerEvent
  455. if (!bookManager.mostRecentBook())
  456. this.close();
  457. }
  458. loadBook(row) {
  459. this.$emit('load-book', {url: row.url, path: row.path});
  460. this.close();
  461. }
  462. isUrl(url) {
  463. if (url)
  464. return (url.indexOf('disk://') != 0);
  465. else
  466. return false;
  467. }
  468. onScroll() {
  469. const curScrollTop = this.$refs.vsContainer.scrollTop;
  470. if (curScrollTop - this.lastScrollTop1 > 150) {
  471. this.$refs.header.style.top = `-${this.$refs.header.offsetHeight}px`;
  472. this.$refs.header.style.transition = 'top 0.2s ease 0s';
  473. this.lastScrollTop1 = curScrollTop;
  474. } else if (curScrollTop - this.lastScrollTop2 < 0) {
  475. this.$refs.header.style.position = 'sticky';
  476. this.$refs.header.style.top = 0;
  477. this.lastScrollTop1 = curScrollTop;
  478. }
  479. this.lastScrollTop2 = curScrollTop;
  480. }
  481. showSameBookClick() {
  482. this.showSameBook = !this.showSameBook;
  483. const newSettings = _.cloneDeep(this.settings);
  484. newSettings.recentShowSameBook = this.showSameBook;
  485. this.commit('reader/setSettings', newSettings);
  486. this.updateTableData();
  487. }
  488. close() {
  489. this.$emit('recent-books-close');
  490. }
  491. keyHook(event) {
  492. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  493. this.close();
  494. }
  495. return true;
  496. }
  497. }
  498. export default vueComponent(RecentBooksPage);
  499. //-----------------------------------------------------------------------------
  500. </script>
  501. <style scoped>
  502. .recent-books-scroll {
  503. width: 573px;
  504. overflow-y: auto;
  505. overflow-x: hidden;
  506. }
  507. .scroll-header {
  508. height: 50px;
  509. position: sticky;
  510. z-index: 1;
  511. top: 0;
  512. border-bottom: 2px solid #aaaaaa;
  513. }
  514. .table-row {
  515. min-height: 80px;
  516. border-bottom: 1px solid #cccccc;
  517. }
  518. .row-part {
  519. padding: 4px 4px 4px 4px;
  520. }
  521. .clickable {
  522. cursor: pointer;
  523. }
  524. .break-word {
  525. line-height: 180%;
  526. overflow-wrap: break-word;
  527. word-wrap: break-word;
  528. white-space: normal;
  529. }
  530. .read-bar {
  531. height: 3px;
  532. background-color: #aaaaaa;
  533. margin-bottom: 2px;
  534. }
  535. .even {
  536. background-color: #f2f2f2;
  537. }
  538. .active-book {
  539. background-color: #b0f0b0 !important;
  540. }
  541. .active-parent-book {
  542. background-color: #ffbbbb !important;
  543. }
  544. .icon {
  545. transition: transform 0.2s;
  546. }
  547. .expanded-icon {
  548. transform: rotate(90deg);
  549. }
  550. .row-info-top {
  551. line-height: 110%;
  552. border-left: 1px solid #cccccc;
  553. border-bottom: 1px solid #cccccc;
  554. }
  555. .row-info-bottom {
  556. line-height: 110%;
  557. border: 1px solid #cccccc;
  558. border-right: 0;
  559. }
  560. </style>