RecentBooksPage.vue 15 KB

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