RecentBooksPage.vue 12 KB

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