RecentBooksPage.vue 12 KB

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