RecentBooksPage.vue 11 KB

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