RecentBooksPage.vue 24 KB

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