RecentBooksPage.vue 21 KB

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