RecentBooksPage.vue 17 KB

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