RecentBooksPage.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. <template>
  2. <Window width="600px" ref="window" @close="close">
  3. <template slot="header">
  4. <span v-show="!loading">Последние {{tableData ? tableData.length : 0}} открытых книг</span>
  5. <span v-if="loading"><q-spinner class="q-mr-sm" color="lime-12" size="20px" :thickness="7"/>Список загружается</span>
  6. </template>
  7. <a ref="download" style='display: none;'></a>
  8. <!--q-table
  9. title="Treats"
  10. :data="data1"
  11. :columns="columns1"
  12. row-key="name"
  13. /-->
  14. <q-table
  15. class="recent-books-table col"
  16. :data="tableData"
  17. :columns="columns"
  18. row-key="key"
  19. :pagination.sync="pagination"
  20. separator="cell"
  21. hide-bottom
  22. virtual-scroll
  23. dense
  24. >
  25. <template v-slot:header="props">
  26. <q-tr :props="props">
  27. <q-th
  28. v-for="col in props.cols"
  29. :key="col.name"
  30. :props="props"
  31. >
  32. <span v-html="col.label"></span>
  33. </q-th>
  34. </q-tr>
  35. </template>
  36. <template v-slot:body="props">
  37. <q-tr :props="props">
  38. <q-td key="num" :props="props" class="td-mp" auto-width>
  39. <div class="break-word" style="width: 25px">
  40. {{ props.row.num }}
  41. </div>
  42. </q-td>
  43. <q-td key="date" :props="props" class="td-mp clickable" @click="loadBook(props.row.url)" auto-width>
  44. <div class="break-word" style="width: 68px">
  45. {{ props.row.touchDate }}<br>
  46. {{ props.row.touchTime }}
  47. </div>
  48. </q-td>
  49. <q-td key="desc" :props="props" class="td-mp clickable" @click="loadBook(props.row.url)" auto-width>
  50. <div class="break-word" style="width: 332px; font-size: 90%">
  51. <div style="color: green">{{ props.row.desc.author }}</div>
  52. <div>{{ props.row.desc.title }}</div>
  53. </div>
  54. </q-td>
  55. <q-td key="links" :props="props" class="td-mp" auto-width>
  56. <div class="break-word" style="width: 75px; font-size: 90%">
  57. <a v-show="isUrl(props.row.url)" :href="props.row.url" target="_blank">Оригинал</a><br>
  58. <a :href="props.row.path" @click.prevent="downloadBook(props.row.path)">Скачать FB2</a>
  59. </div>
  60. </q-td>
  61. <q-td key="close" :props="props" class="td-mp" auto-width>
  62. <div style="width: 38px">
  63. <q-btn
  64. dense
  65. style="width: 30px; height: 30px; padding: 7px 0 7px 0; margin-left: 4px"
  66. @click="handleDel(props.row.key)">
  67. <q-icon class="la la-times" size="16px" style="top: -5px"/>
  68. </q-btn>
  69. </div>
  70. </q-td>
  71. <q-td key="last" :props="props" class="no-mp">
  72. </q-td>
  73. </q-tr>
  74. </template>
  75. </q-table>
  76. </Window>
  77. </template>
  78. <script>
  79. /*
  80. <el-table
  81. :data="tableData"
  82. style="width: 570px"
  83. size="mini"
  84. height="1px"
  85. stripe
  86. border
  87. :default-sort = "{prop: 'touchDateTime', order: 'descending'}"
  88. :header-cell-style = "headerCellStyle"
  89. :row-key = "rowKey"
  90. >
  91. <el-table-column
  92. type="index"
  93. width="35px"
  94. >
  95. </el-table-column>
  96. <el-table-column
  97. prop="touchDateTime"
  98. min-width="85px"
  99. sortable
  100. >
  101. <template slot="header" slot-scope="scope"><!-- eslint-disable-line vue/no-unused-vars -->
  102. <span style="font-size: 90%">Время<br>просм.</span>
  103. </template>
  104. <template slot-scope="scope"><!-- eslint-disable-line vue/no-unused-vars -->
  105. <div class="desc" @click="loadBook(scope.row.url)">
  106. {{ scope.row.touchDate }}<br>
  107. {{ scope.row.touchTime }}
  108. </div>
  109. </template>
  110. </el-table-column>
  111. <el-table-column
  112. >
  113. <template slot="header" slot-scope="scope"><!-- eslint-disable-line vue/no-unused-vars -->
  114. <!--el-input ref="input"
  115. :value="search" @input="search = $event"
  116. size="mini"
  117. style="margin: 0; padding: 0; vertical-align: bottom; margin-top: 10px"
  118. placeholder="Найти"/-->
  119. <div class="el-input el-input--mini">
  120. <input class="el-input__inner"
  121. ref="input"
  122. placeholder="Найти"
  123. style="margin: 0; vertical-align: bottom; margin-top: 20px; padding: 0 10px 0 10px"
  124. :value="search" @input="search = $event.target.value"
  125. />
  126. </div>
  127. </template>
  128. <el-table-column
  129. min-width="280px"
  130. >
  131. <template slot-scope="scope">
  132. <div class="desc" @click="loadBook(scope.row.url)">
  133. <span style="color: green">{{ scope.row.desc.author }}</span><br>
  134. <span>{{ scope.row.desc.title }}</span>
  135. </div>
  136. </template>
  137. </el-table-column>
  138. <el-table-column
  139. min-width="90px"
  140. >
  141. <template slot-scope="scope">
  142. <a v-show="isUrl(scope.row.url)" :href="scope.row.url" target="_blank">Оригинал</a><br>
  143. <a :href="scope.row.path" @click.prevent="downloadBook(scope.row.path)">Скачать FB2</a>
  144. </template>
  145. </el-table-column>
  146. <el-table-column
  147. width="60px"
  148. >
  149. <template slot-scope="scope">
  150. <el-button
  151. size="mini"
  152. style="width: 30px; padding: 7px 0 7px 0; margin-left: 4px"
  153. @click="handleDel(scope.row.key)"><i class="el-icon-close"></i>
  154. </el-button>
  155. </template>
  156. </el-table-column>
  157. </el-table-column>
  158. </el-table>
  159. */
  160. //-----------------------------------------------------------------------------
  161. import Vue from 'vue';
  162. import Component from 'vue-class-component';
  163. import path from 'path';
  164. import _ from 'lodash';
  165. import * as utils from '../../../share/utils';
  166. import Window from '../../share/Window.vue';
  167. import bookManager from '../share/bookManager';
  168. import readerApi from '../../../api/reader';
  169. export default @Component({
  170. components: {
  171. Window,
  172. },
  173. watch: {
  174. search: function() {
  175. this.updateTableData();
  176. }
  177. },
  178. })
  179. class RecentBooksPage extends Vue {
  180. loading = false;
  181. search = null;
  182. tableData = [];
  183. columns = [];
  184. pagination = {};
  185. created() {
  186. /* this.columns1 = [
  187. {
  188. name: 'name',
  189. required: true,
  190. label: 'Dessert (100g serving)',
  191. align: 'left',
  192. field: row => row.name,
  193. format: val => `${val}`,
  194. sortable: true
  195. },
  196. { name: 'calories', align: 'center', label: 'Calories', field: 'calories', sortable: true },
  197. { name: 'fat', label: 'Fat (g)', field: 'fat', sortable: true },
  198. { name: 'carbs', label: 'Carbs (g)', field: 'carbs' },
  199. { name: 'protein', label: 'Protein (g)', field: 'protein' },
  200. { name: 'sodium', label: 'Sodium (mg)', field: 'sodium' },
  201. { name: 'calcium', label: 'Calcium (%)', field: 'calcium', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) },
  202. { name: 'iron', label: 'Iron (%)', field: 'iron', sortable: true, sort: (a, b) => parseInt(a, 10) - parseInt(b, 10) }
  203. ];
  204. this.data1 = [
  205. {
  206. name: 'Frozen Yogurt',
  207. calories: 159,
  208. fat: 6.0,
  209. carbs: 24,
  210. protein: 4.0,
  211. sodium: 87,
  212. calcium: '14%',
  213. iron: '1%'
  214. },
  215. {
  216. name: 'Ice cream sandwich',
  217. calories: 237,
  218. fat: 9.0,
  219. carbs: 37,
  220. protein: 4.3,
  221. sodium: 129,
  222. calcium: '8%',
  223. iron: '1%'
  224. },
  225. {
  226. name: 'Eclair',
  227. calories: 262,
  228. fat: 16.0,
  229. carbs: 23,
  230. protein: 6.0,
  231. sodium: 337,
  232. calcium: '6%',
  233. iron: '7%'
  234. }
  235. ];
  236. */
  237. this.pagination = {rowsPerPage: 0};
  238. this.columns = [
  239. {
  240. name: 'num',
  241. label: '#',
  242. align: 'center',
  243. field: 'num',
  244. },
  245. {
  246. name: 'date',
  247. label: 'Время<br>просм.',
  248. align: 'left',
  249. sortable: true,
  250. sort: (a, b, rowA, rowB) => rowA.touchDateTime - rowB.touchDateTime,
  251. },
  252. {
  253. name: 'desc',
  254. label: 'Название',
  255. align: 'left',
  256. sortable: true,
  257. },
  258. {
  259. name: 'links',
  260. label: '',
  261. align: 'left',
  262. },
  263. {
  264. name: 'close',
  265. label: '',
  266. align: 'left',
  267. },
  268. {
  269. name: 'last',
  270. label: '',
  271. align: 'left',
  272. },
  273. ];
  274. }
  275. init() {
  276. this.$refs.window.init();
  277. this.$nextTick(() => {
  278. //this.$refs.input.focus();
  279. });
  280. (async() => {//подгрузка списка
  281. if (this.initing)
  282. return;
  283. this.initing = true;
  284. if (!bookManager.loaded) {
  285. await this.updateTableData(10);
  286. //для отзывчивости
  287. await utils.sleep(100);
  288. let i = 0;
  289. let j = 5;
  290. while (i < 500 && !bookManager.loaded) {
  291. if (i % j == 0) {
  292. bookManager.sortedRecentCached = null;
  293. await this.updateTableData(20);
  294. j *= 2;
  295. }
  296. await utils.sleep(100);
  297. i++;
  298. }
  299. } else {
  300. //для отзывчивости
  301. await utils.sleep(100);
  302. }
  303. await this.updateTableData();
  304. this.initing = false;
  305. })();
  306. }
  307. async updateTableData(limit) {
  308. while (this.updating) await utils.sleep(100);
  309. this.updating = true;
  310. let result = [];
  311. this.loading = !!limit;
  312. const sorted = bookManager.getSortedRecent();
  313. let num = 0;
  314. for (let i = 0; i < sorted.length; i++) {
  315. const book = sorted[i];
  316. if (book.deleted)
  317. continue;
  318. num++;
  319. if (limit && result.length >= limit)
  320. break;
  321. let d = new Date();
  322. d.setTime(book.touchTime);
  323. const t = utils.formatDate(d).split(' ');
  324. let perc = '';
  325. let textLen = '';
  326. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  327. if (book.textLength) {
  328. perc = ` [${((p/book.textLength)*100).toFixed(2)}%]`;
  329. textLen = ` ${Math.round(book.textLength/1000)}k`;
  330. }
  331. const fb2 = (book.fb2 ? book.fb2 : {});
  332. let title = fb2.bookTitle;
  333. if (title)
  334. title = `"${title}"`;
  335. else
  336. title = '';
  337. let author = '';
  338. if (fb2.author) {
  339. const authorNames = fb2.author.map(a => _.compact([
  340. a.lastName,
  341. a.firstName,
  342. a.middleName
  343. ]).join(' '));
  344. author = authorNames.join(', ');
  345. } else {//TODO: убрать в будущем
  346. author = _.compact([
  347. fb2.lastName,
  348. fb2.firstName,
  349. fb2.middleName
  350. ]).join(' ');
  351. }
  352. author = (author ? author : (fb2.bookTitle ? fb2.bookTitle : book.url));
  353. result.push({
  354. num,
  355. touchDateTime: book.touchTime,
  356. touchDate: t[0],
  357. touchTime: t[1],
  358. desc: {
  359. title: `${title}${perc}${textLen}`,
  360. author,
  361. },
  362. url: book.url,
  363. path: book.path,
  364. key: book.key,
  365. });
  366. }
  367. const search = this.search;
  368. result = result.filter(item => {
  369. return !search ||
  370. item.touchTime.includes(search) ||
  371. item.touchDate.includes(search) ||
  372. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  373. item.desc.author.toLowerCase().includes(search.toLowerCase())
  374. });
  375. /*for (let i = 0; i < result.length; i++) {
  376. if (!_.isEqual(this.tableData[i], result[i])) {
  377. this.$set(this.tableData, i, result[i]);
  378. await utils.sleep(10);
  379. }
  380. }
  381. if (this.tableData.length > result.length)
  382. this.tableData.splice(result.length);*/
  383. this.tableData = result;
  384. this.updating = false;
  385. }
  386. async downloadBook(fb2path) {
  387. try {
  388. await readerApi.checkCachedBook(fb2path);
  389. const d = this.$refs.download;
  390. d.href = fb2path;
  391. d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
  392. d.click();
  393. } catch (e) {
  394. let errMes = e.message;
  395. if (errMes.indexOf('404') >= 0)
  396. errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
  397. this.$alert(errMes, 'Ошибка', {type: 'error'});
  398. }
  399. }
  400. openOriginal(url) {
  401. window.open(url, '_blank');
  402. }
  403. openFb2(path) {
  404. window.open(path, '_blank');
  405. }
  406. async handleDel(key) {
  407. await bookManager.delRecentBook({key});
  408. this.updateTableData();
  409. if (!bookManager.mostRecentBook())
  410. this.close();
  411. }
  412. loadBook(url) {
  413. this.$emit('load-book', {url});
  414. this.close();
  415. }
  416. isUrl(url) {
  417. if (url)
  418. return (url.indexOf('file://') != 0);
  419. else
  420. return false;
  421. }
  422. close() {
  423. this.$emit('recent-books-close');
  424. }
  425. keyHook(event) {
  426. if (event.type == 'keydown' && event.code == 'Escape') {
  427. this.close();
  428. }
  429. return true;
  430. }
  431. }
  432. //-----------------------------------------------------------------------------
  433. </script>
  434. <style scoped>
  435. .recent-books-table {
  436. width: 600px;
  437. overflow-y: auto;
  438. overflow-x: hidden;
  439. }
  440. .clickable {
  441. cursor: pointer;
  442. }
  443. .td-mp {
  444. margin: 0 !important;
  445. padding: 4px 4px 4px 4px !important;
  446. }
  447. .no-mp {
  448. margin: 0 !important;
  449. padding: 0 !important;
  450. }
  451. .break-word {
  452. line-height: 180%;
  453. overflow-wrap: break-word;
  454. word-wrap: break-word;
  455. white-space: normal;
  456. }
  457. </style>
  458. <style>
  459. .recent-books-table .q-table__middle {
  460. height: 100%;
  461. overflow-x: hidden;
  462. }
  463. .recent-books-table thead tr:first-child th {
  464. position: sticky;
  465. z-index: 1;
  466. top: 0;
  467. background-color: #c1f4cd;
  468. }
  469. </style>