RecentBooksPage.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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: 100%"
  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="90px"
  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="300px"
  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="100px"
  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. await this.updateTableData(10);
  126. if (bookManager.getSortedRecent().length > 10)
  127. await utils.sleep(1800);
  128. } else {
  129. let i = 0;
  130. let j = 5;
  131. while (i < 500 && !bookManager.loaded) {
  132. if (i % j == 0) {
  133. bookManager.sortedRecentCached = null;
  134. await this.updateTableData(100);
  135. j *= 2;
  136. }
  137. await utils.sleep(100);
  138. i++;
  139. }
  140. }
  141. await this.updateTableData();
  142. this.initing = false;
  143. })();
  144. }
  145. rowKey(row) {
  146. return row.key;
  147. }
  148. async updateTableData(limit) {
  149. while (this.updating) await utils.sleep(100);
  150. this.updating = true;
  151. let result = [];
  152. this.loading = !!limit;
  153. const sorted = bookManager.getSortedRecent();
  154. for (let i = 0; i < sorted.length; i++) {
  155. const book = sorted[i];
  156. if (book.deleted)
  157. continue;
  158. if (limit && result.length >= limit)
  159. break;
  160. let d = new Date();
  161. d.setTime(book.touchTime);
  162. const t = utils.formatDate(d).split(' ');
  163. let perc = '';
  164. let textLen = '';
  165. const p = (book.bookPosSeen ? book.bookPosSeen : (book.bookPos ? book.bookPos : 0));
  166. if (book.textLength) {
  167. perc = ` [${((p/book.textLength)*100).toFixed(2)}%]`;
  168. textLen = ` ${Math.round(book.textLength/1000)}k`;
  169. }
  170. const fb2 = (book.fb2 ? book.fb2 : {});
  171. let title = fb2.bookTitle;
  172. if (title)
  173. title = `"${title}"`;
  174. else
  175. title = '';
  176. let author = '';
  177. if (fb2.author) {
  178. const authorNames = fb2.author.map(a => _.compact([
  179. a.lastName,
  180. a.firstName,
  181. a.middleName
  182. ]).join(' '));
  183. author = authorNames.join(', ');
  184. } else {
  185. author = _.compact([
  186. fb2.lastName,
  187. fb2.firstName,
  188. fb2.middleName
  189. ]).join(' ');
  190. }
  191. author = (author ? author : (fb2.bookTitle ? fb2.bookTitle : book.url));
  192. result.push({
  193. touchDateTime: book.touchTime,
  194. touchDate: t[0],
  195. touchTime: t[1],
  196. desc: {
  197. title: `${title}${perc}${textLen}`,
  198. author,
  199. },
  200. url: book.url,
  201. path: book.path,
  202. key: book.key,
  203. });
  204. if (result.length >= 100)
  205. break;
  206. }
  207. const search = this.search;
  208. result = result.filter(item => {
  209. return !search ||
  210. item.touchTime.includes(search) ||
  211. item.touchDate.includes(search) ||
  212. item.desc.title.toLowerCase().includes(search.toLowerCase()) ||
  213. item.desc.author.toLowerCase().includes(search.toLowerCase())
  214. });
  215. /*for (let i = 0; i < result.length; i++) {
  216. if (!_.isEqual(this.tableData[i], result[i])) {
  217. this.$set(this.tableData, i, result[i]);
  218. await utils.sleep(10);
  219. }
  220. }
  221. if (this.tableData.length > result.length)
  222. this.tableData.splice(result.length);*/
  223. this.tableData = result;
  224. this.updating = false;
  225. }
  226. headerCellStyle(cell) {
  227. let result = {margin: 0, padding: 0};
  228. if (cell.columnIndex > 0) {
  229. result['border-bottom'] = 0;
  230. }
  231. if (cell.rowIndex > 0) {
  232. result.height = '0px';
  233. result['border-right'] = 0;
  234. }
  235. return result;
  236. }
  237. getFileNameFromPath(fb2Path) {
  238. return path.basename(fb2Path).substr(0, 10) + '.fb2';
  239. }
  240. openOriginal(url) {
  241. window.open(url, '_blank');
  242. }
  243. openFb2(path) {
  244. window.open(path, '_blank');
  245. }
  246. async handleDel(key) {
  247. await bookManager.delRecentBook({key});
  248. this.updateTableData();
  249. if (!bookManager.mostRecentBook())
  250. this.close();
  251. }
  252. loadBook(url) {
  253. this.$emit('load-book', {url});
  254. this.close();
  255. }
  256. isUrl(url) {
  257. if (url)
  258. return (url.indexOf('file://') != 0);
  259. else
  260. return false;
  261. }
  262. close() {
  263. this.$emit('recent-books-toggle');
  264. }
  265. keyHook(event) {
  266. if (event.type == 'keydown' && event.code == 'Escape') {
  267. this.close();
  268. }
  269. return true;
  270. }
  271. }
  272. //-----------------------------------------------------------------------------
  273. </script>
  274. <style scoped>
  275. .desc {
  276. cursor: pointer;
  277. }
  278. </style>