RecentBooksPage.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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: 4px; 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. {{ item.num }}
  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. sortMethod = '';
  166. created() {
  167. this.lastScrollTop1 = 0;
  168. this.lastScrollTop2 = 0;
  169. this.lock = new LockQueue(100);
  170. }
  171. init() {
  172. this.$refs.window.init();
  173. this.$nextTick(() => {
  174. //this.$refs.input.focus();//плохо на планшетах
  175. });
  176. this.updateTableData();//no await
  177. }
  178. async updateTableData() {
  179. await this.lock.get();
  180. try {
  181. let result = [];
  182. const sorted = bookManager.getSortedRecent();
  183. const activeBook = bookManager.mostRecentBook();
  184. //подготовка полей
  185. for (const book of sorted) {
  186. if (book.deleted)
  187. continue;
  188. let d = new Date();
  189. d.setTime(book.touchTime);
  190. const touchTime = utils.formatDate(d);
  191. let readPart = 0;
  192. let perc = '';
  193. let textLen = '';
  194. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  195. if (book.textLength) {
  196. readPart = p/book.textLength;
  197. perc = ` [${(readPart*100).toFixed(2)}%]`;
  198. textLen = ` ${Math.round(book.textLength/1000)}k`;
  199. }
  200. const bt = utils.getBookTitle(book.fb2);
  201. let title = bt.bookTitle;
  202. title = (title ? `"${title}"`: '');
  203. const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : (book.uploadFileName ? book.uploadFileName : book.url)));
  204. result.push({
  205. touchTime,
  206. desc: {
  207. author,
  208. title: `${title}${perc}${textLen}`,
  209. },
  210. readPart,
  211. url: book.url,
  212. path: book.path,
  213. fullTitle: bt.fullTitle,
  214. key: book.key,
  215. sameBookKey: book.sameBookKey,
  216. active: (activeBook.key == book.key),
  217. //для сортировки
  218. addTimeRaw: book.addTime,
  219. touchTimeRaw: book.touchTime,
  220. descString: `${author}${title}${perc}${textLen}`,
  221. });
  222. }
  223. //нумерация
  224. let num = 0;
  225. result.sort((a, b) => b.addTimeRaw - a.addTimeRaw);
  226. for (const book of result) {
  227. num++;
  228. book.num = num;
  229. }
  230. //фильтрация
  231. const search = this.search;
  232. if (search) {
  233. result = result.filter(item => {
  234. return !search ||
  235. item.touchTime.includes(search) ||
  236. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  237. item.desc.author.toLowerCase().includes(search.toLowerCase())
  238. });
  239. }
  240. //сортировка
  241. switch (this.sortMethod) {
  242. case 'addTimeDesc':
  243. result.sort((a, b) => b.addTimeRaw - a.addTimeRaw);
  244. break;
  245. case 'addTimeAsc':
  246. result.sort((a, b) => a.addTimeRaw - b.addTimeRaw);
  247. break;
  248. case 'touchTimeDesc':
  249. result.sort((a, b) => b.touchTimeRaw - a.touchTimeRaw);
  250. break;
  251. case 'touchTimeAsc':
  252. result.sort((a, b) => a.touchTimeRaw - b.touchTimeRaw);
  253. break;
  254. }
  255. //группировка
  256. const groups = {};
  257. const newResult = [];
  258. for (const book of result) {
  259. if (book.sameBookKey !== undefined) {
  260. if (!groups[book.sameBookKey]) {
  261. groups[book.sameBookKey] = [];
  262. book.group = groups[book.sameBookKey];
  263. newResult.push(book);
  264. } else {
  265. groups[book.sameBookKey].push(book);
  266. }
  267. } else {
  268. newResult.push(book);
  269. }
  270. }
  271. result = newResult;
  272. //другие стадии
  273. //.....
  274. this.tableData = result;
  275. } finally {
  276. this.lock.ret();
  277. }
  278. }
  279. /*async updateTableData(limit) {
  280. while (this.updating) await utils.sleep(100);
  281. this.updating = true;
  282. let result = [];
  283. const sorted = bookManager.getSortedRecent();
  284. let num = 0;
  285. for (let i = 0; i < sorted.length; i++) {
  286. const book = sorted[i];
  287. if (book.deleted)
  288. continue;
  289. num++;
  290. if (limit && result.length >= limit)
  291. break;
  292. let d = new Date();
  293. d.setTime(book.touchTime);
  294. const t = utils.formatDate(d).split(' ');
  295. let readPart = 0;
  296. let perc = '';
  297. let textLen = '';
  298. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  299. if (book.textLength) {
  300. readPart = p/book.textLength;
  301. perc = ` [${(readPart*100).toFixed(2)}%]`;
  302. textLen = ` ${Math.round(book.textLength/1000)}k`;
  303. }
  304. const bt = utils.getBookTitle(book.fb2);
  305. let title = bt.bookTitle;
  306. title = (title ? `"${title}"`: '');
  307. const author = (bt.author ? bt.author : (bt.bookTitle ? bt.bookTitle : (book.uploadFileName ? book.uploadFileName : book.url)));
  308. result.push({
  309. num,
  310. touchDateTime: book.touchTime,
  311. touchDate: t[0],
  312. touchTime: t[1],
  313. desc: {
  314. author,
  315. title: `${title}${perc}${textLen}`,
  316. },
  317. readPart,
  318. descString: `${author}${title}${perc}${textLen}`,//для сортировки
  319. url: book.url,
  320. path: book.path,
  321. fullTitle: bt.fullTitle,
  322. key: book.key,
  323. });
  324. }
  325. const search = this.search;
  326. result = result.filter(item => {
  327. return !search ||
  328. item.touchTime.includes(search) ||
  329. item.touchDate.includes(search) ||
  330. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  331. item.desc.author.toLowerCase().includes(search.toLowerCase())
  332. });
  333. this.tableData = result;
  334. this.updating = false;
  335. }*/
  336. resetSearch() {
  337. this.search = '';
  338. this.$refs.input.focus();
  339. }
  340. wordEnding(num) {
  341. const endings = ['ов', '', 'а', 'а', 'а', 'ов', 'ов', 'ов', 'ов', 'ов'];
  342. const deci = num % 100;
  343. if (deci > 10 && deci < 20) {
  344. return 'ов';
  345. } else {
  346. return endings[num % 10];
  347. }
  348. }
  349. get header() {
  350. const len = (this.tableData ? this.tableData.length : 0);
  351. return `${(this.search ? 'Найдено' : 'Всего')} ${len} файл${this.wordEnding(len)}`;
  352. }
  353. async downloadBook(fb2path, fullTitle) {
  354. try {
  355. await readerApi.checkCachedBook(fb2path);
  356. const d = this.$refs.download;
  357. d.href = fb2path;
  358. try {
  359. const fn = utils.makeValidFilename(fullTitle);
  360. d.download = fn.substring(0, 100) + '.fb2';
  361. } catch(e) {
  362. d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
  363. }
  364. d.click();
  365. } catch (e) {
  366. let errMes = e.message;
  367. if (errMes.indexOf('404') >= 0)
  368. errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
  369. this.$root.stdDialog.alert(errMes, 'Ошибка', {color: 'negative'});
  370. }
  371. }
  372. async handleDel(key) {
  373. await bookManager.delRecentBook({key});
  374. //this.updateTableData();//обновление уже происходит Reader.bookManagerEvent
  375. if (!bookManager.mostRecentBook())
  376. this.close();
  377. }
  378. loadBook(row) {
  379. this.$emit('load-book', {url: row.url, path: row.path});
  380. this.close();
  381. }
  382. isUrl(url) {
  383. if (url)
  384. return (url.indexOf('disk://') != 0);
  385. else
  386. return false;
  387. }
  388. onScroll() {
  389. const curScrollTop = this.$refs.vsContainer.scrollTop;
  390. if (curScrollTop - this.lastScrollTop1 > 150) {
  391. this.$refs.header.style.top = `-${this.$refs.header.offsetHeight}px`;
  392. this.$refs.header.style.transition = 'top 0.2s ease 0s';
  393. this.lastScrollTop1 = curScrollTop;
  394. } else if (curScrollTop - this.lastScrollTop2 < 0) {
  395. this.$refs.header.style.position = 'sticky';
  396. this.$refs.header.style.top = 0;
  397. this.lastScrollTop1 = curScrollTop;
  398. }
  399. this.lastScrollTop2 = curScrollTop;
  400. }
  401. close() {
  402. this.$emit('recent-books-close');
  403. }
  404. keyHook(event) {
  405. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  406. this.close();
  407. }
  408. return true;
  409. }
  410. }
  411. export default vueComponent(RecentBooksPage);
  412. //-----------------------------------------------------------------------------
  413. </script>
  414. <style scoped>
  415. .recent-books-scroll {
  416. width: 573px;
  417. overflow-y: auto;
  418. overflow-x: hidden;
  419. }
  420. .scroll-header {
  421. height: 50px;
  422. position: sticky;
  423. z-index: 1;
  424. top: 0;
  425. border-bottom: 2px solid #aaaaaa;
  426. }
  427. .table-row {
  428. min-height: 80px;
  429. border-bottom: 1px solid #cccccc;
  430. }
  431. .row-part {
  432. padding: 4px 4px 4px 4px;
  433. }
  434. .clickable {
  435. cursor: pointer;
  436. }
  437. .break-word {
  438. line-height: 150%;
  439. overflow-wrap: break-word;
  440. word-wrap: break-word;
  441. white-space: normal;
  442. }
  443. .read-bar {
  444. height: 3px;
  445. background-color: #aaaaaa;
  446. }
  447. .even {
  448. background-color: #f0f0f0;
  449. }
  450. .active-book {
  451. background-color: #b0f0b0 !important;
  452. }
  453. </style>