RecentBooksPage.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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-show="loading"><i class="el-icon-loading" style="font-size: 25px"></i> <span style="position: relative; top: -4px">Список загружается</span></span>
  6. </template>
  7. <a ref="download" style='display: none;'></a>
  8. <el-table
  9. :data="tableData"
  10. style="width: 570px"
  11. size="mini"
  12. height="1px"
  13. stripe
  14. border
  15. :default-sort = "{prop: 'touchDateTime', order: 'descending'}"
  16. :header-cell-style = "headerCellStyle"
  17. :row-key = "rowKey"
  18. >
  19. <el-table-column
  20. type="index"
  21. width="35px"
  22. >
  23. </el-table-column>
  24. <el-table-column
  25. prop="touchDateTime"
  26. min-width="85px"
  27. sortable
  28. >
  29. <template slot="header" slot-scope="scope"><!-- eslint-disable-line vue/no-unused-vars -->
  30. <span style="font-size: 90%">Время<br>просм.</span>
  31. </template>
  32. <template slot-scope="scope"><!-- eslint-disable-line vue/no-unused-vars -->
  33. <div class="desc" @click="loadBook(scope.row.url)">
  34. {{ scope.row.touchDate }}<br>
  35. {{ scope.row.touchTime }}
  36. </div>
  37. </template>
  38. </el-table-column>
  39. <el-table-column
  40. >
  41. <template slot="header" slot-scope="scope"><!-- eslint-disable-line vue/no-unused-vars -->
  42. <!--el-input ref="input"
  43. :value="search" @input="search = $event"
  44. size="mini"
  45. style="margin: 0; padding: 0; vertical-align: bottom; margin-top: 10px"
  46. placeholder="Найти"/-->
  47. <div class="el-input el-input--mini">
  48. <input class="el-input__inner"
  49. ref="input"
  50. placeholder="Найти"
  51. style="margin: 0; vertical-align: bottom; margin-top: 20px; padding: 0 10px 0 10px"
  52. :value="search" @input="search = $event.target.value"
  53. />
  54. </div>
  55. </template>
  56. <el-table-column
  57. min-width="280px"
  58. >
  59. <template slot-scope="scope">
  60. <div class="desc" @click="loadBook(scope.row.url)">
  61. <span style="color: green">{{ scope.row.desc.author }}</span><br>
  62. <span>{{ scope.row.desc.title }}</span>
  63. </div>
  64. </template>
  65. </el-table-column>
  66. <el-table-column
  67. min-width="90px"
  68. >
  69. <template slot-scope="scope">
  70. <a v-show="isUrl(scope.row.url)" :href="scope.row.url" target="_blank">Оригинал</a><br>
  71. <a :href="scope.row.path" @click.prevent="downloadBook(scope.row.path)">Скачать FB2</a>
  72. </template>
  73. </el-table-column>
  74. <el-table-column
  75. width="60px"
  76. >
  77. <template slot-scope="scope">
  78. <el-button
  79. size="mini"
  80. style="width: 30px; padding: 7px 0 7px 0; margin-left: 4px"
  81. @click="handleDel(scope.row.key)"><i class="el-icon-close"></i>
  82. </el-button>
  83. </template>
  84. </el-table-column>
  85. </el-table-column>
  86. </el-table>
  87. </Window>
  88. </template>
  89. <script>
  90. //-----------------------------------------------------------------------------
  91. import Vue from 'vue';
  92. import Component from 'vue-class-component';
  93. import path from 'path';
  94. import _ from 'lodash';
  95. import * as utils from '../../../share/utils';
  96. import Window from '../../share/Window.vue';
  97. import bookManager from '../share/bookManager';
  98. import readerApi from '../../../api/reader';
  99. export default @Component({
  100. components: {
  101. Window,
  102. },
  103. watch: {
  104. search: function() {
  105. this.updateTableData();
  106. }
  107. },
  108. })
  109. class RecentBooksPage extends Vue {
  110. loading = false;
  111. search = null;
  112. tableData = [];
  113. created() {
  114. }
  115. init() {
  116. this.$refs.window.init();
  117. this.$nextTick(() => {
  118. //this.$refs.input.focus();
  119. });
  120. (async() => {//отбражение подгрузки списка, иначе тормозит
  121. if (this.initing)
  122. return;
  123. this.initing = true;
  124. await this.updateTableData(3);
  125. await utils.sleep(200);
  126. if (bookManager.loaded) {
  127. const t = Date.now();
  128. await this.updateTableData(10);
  129. if (bookManager.getSortedRecent().length > 10)
  130. await utils.sleep(10*(Date.now() - t));
  131. } else {
  132. let i = 0;
  133. let j = 5;
  134. while (i < 500 && !bookManager.loaded) {
  135. if (i % j == 0) {
  136. bookManager.sortedRecentCached = null;
  137. await this.updateTableData(100);
  138. j *= 2;
  139. }
  140. await utils.sleep(100);
  141. i++;
  142. }
  143. }
  144. await this.updateTableData();
  145. this.initing = false;
  146. })();
  147. }
  148. rowKey(row) {
  149. return row.key;
  150. }
  151. async updateTableData(limit) {
  152. while (this.updating) await utils.sleep(100);
  153. this.updating = true;
  154. let result = [];
  155. this.loading = !!limit;
  156. const sorted = bookManager.getSortedRecent();
  157. for (let i = 0; i < sorted.length; i++) {
  158. const book = sorted[i];
  159. if (book.deleted)
  160. continue;
  161. if (limit && result.length >= limit)
  162. break;
  163. let d = new Date();
  164. d.setTime(book.touchTime);
  165. const t = utils.formatDate(d).split(' ');
  166. let perc = '';
  167. let textLen = '';
  168. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  169. if (book.textLength) {
  170. perc = ` [${((p/book.textLength)*100).toFixed(2)}%]`;
  171. textLen = ` ${Math.round(book.textLength/1000)}k`;
  172. }
  173. const fb2 = (book.fb2 ? book.fb2 : {});
  174. let title = fb2.bookTitle;
  175. if (title)
  176. title = `"${title}"`;
  177. else
  178. title = '';
  179. let author = '';
  180. if (fb2.author) {
  181. const authorNames = fb2.author.map(a => _.compact([
  182. a.lastName,
  183. a.firstName,
  184. a.middleName
  185. ]).join(' '));
  186. author = authorNames.join(', ');
  187. } else {//TODO: убрать в будущем
  188. author = _.compact([
  189. fb2.lastName,
  190. fb2.firstName,
  191. fb2.middleName
  192. ]).join(' ');
  193. }
  194. author = (author ? author : (fb2.bookTitle ? fb2.bookTitle : book.url));
  195. result.push({
  196. touchDateTime: book.touchTime,
  197. touchDate: t[0],
  198. touchTime: t[1],
  199. desc: {
  200. title: `${title}${perc}${textLen}`,
  201. author,
  202. },
  203. url: book.url,
  204. path: book.path,
  205. key: book.key,
  206. });
  207. if (result.length >= 100)
  208. break;
  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. /*for (let i = 0; i < result.length; i++) {
  219. if (!_.isEqual(this.tableData[i], result[i])) {
  220. this.$set(this.tableData, i, result[i]);
  221. await utils.sleep(10);
  222. }
  223. }
  224. if (this.tableData.length > result.length)
  225. this.tableData.splice(result.length);*/
  226. this.tableData = result;
  227. this.updating = false;
  228. }
  229. headerCellStyle(cell) {
  230. let result = {margin: 0, padding: 0};
  231. if (cell.columnIndex > 0) {
  232. result['border-bottom'] = 0;
  233. }
  234. if (cell.rowIndex > 0) {
  235. result.height = '0px';
  236. result['border-right'] = 0;
  237. }
  238. return result;
  239. }
  240. async downloadBook(fb2path) {
  241. try {
  242. await readerApi.checkCachedBook(fb2path);
  243. const d = this.$refs.download;
  244. d.href = fb2path;
  245. d.download = path.basename(fb2path).substr(0, 10) + '.fb2';
  246. d.click();
  247. } catch (e) {
  248. let errMes = e.message;
  249. if (errMes.indexOf('404') >= 0)
  250. errMes = 'Файл не найден на сервере (возможно был удален как устаревший)';
  251. this.$alert(errMes, 'Ошибка', {type: 'error'});
  252. }
  253. }
  254. openOriginal(url) {
  255. window.open(url, '_blank');
  256. }
  257. openFb2(path) {
  258. window.open(path, '_blank');
  259. }
  260. async handleDel(key) {
  261. await bookManager.delRecentBook({key});
  262. this.updateTableData();
  263. if (!bookManager.mostRecentBook())
  264. this.close();
  265. }
  266. loadBook(url) {
  267. this.$emit('load-book', {url});
  268. this.close();
  269. }
  270. isUrl(url) {
  271. if (url)
  272. return (url.indexOf('file://') != 0);
  273. else
  274. return false;
  275. }
  276. close() {
  277. this.$emit('recent-books-toggle');
  278. }
  279. keyHook(event) {
  280. if (event.type == 'keydown' && event.code == 'Escape') {
  281. this.close();
  282. }
  283. return true;
  284. }
  285. }
  286. //-----------------------------------------------------------------------------
  287. </script>
  288. <style scoped>
  289. .desc {
  290. cursor: pointer;
  291. }
  292. </style>