RecentBooksPage.vue 17 KB

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