HistoryPage.vue 11 KB

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