RecentBooksPage.vue 12 KB

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