RecentBooksPage.vue 17 KB

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