RecentBooksPage.vue 12 KB

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