RecentBooksPage.vue 10.0 KB

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