RecentBooksPage.vue 12 KB

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