RecentBooksPage.vue 22 KB

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