HistoryPage.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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"></i> Список загружается</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 = null;
  115. created() {
  116. }
  117. init() {
  118. bookManager.addEventListener(this.bookManagerEvent);
  119. this.updateTableData(5);
  120. this.$nextTick(() => {
  121. //this.$refs.input.focus();
  122. });
  123. (async() => {//отбражение подгрузки списка
  124. await utils.sleep(1000);
  125. if (this.bookManagerLoaded)
  126. this.updateTableData();
  127. else {
  128. let i = 0;
  129. let j = 5;
  130. while (i < 500 && !this.bookManagerLoaded) {
  131. if (i % j == 0) {
  132. bookManager.sortedRecentCached = null;
  133. this.updateTableData(100);
  134. j *= 2;
  135. }
  136. await utils.sleep(100);
  137. i++;
  138. }
  139. }
  140. })();
  141. }
  142. bookManagerEvent(eventName) {
  143. if (eventName == 'load-stored-finish') {
  144. this.updateTableData();
  145. this.bookManagerLoaded = true;
  146. }
  147. }
  148. rowKey(row) {
  149. return row.key;
  150. }
  151. updateTableData(limit) {
  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. this.tableData = result;
  217. }
  218. headerCellStyle(cell) {
  219. let result = {margin: 0, padding: 0};
  220. if (cell.columnIndex > 0) {
  221. result['border-bottom'] = 0;
  222. }
  223. if (cell.rowIndex > 0) {
  224. result.height = '0px';
  225. result['border-right'] = 0;
  226. }
  227. return result;
  228. }
  229. getFileNameFromPath(fb2Path) {
  230. return path.basename(fb2Path).substr(0, 10) + '.fb2';
  231. }
  232. openOriginal(url) {
  233. window.open(url, '_blank');
  234. }
  235. openFb2(path) {
  236. window.open(path, '_blank');
  237. }
  238. async handleDel(key) {
  239. await bookManager.delRecentBook({key});
  240. this.updateTableData();
  241. if (!bookManager.mostRecentBook())
  242. this.close();
  243. }
  244. loadBook(url) {
  245. this.$emit('load-book', {url});
  246. this.close();
  247. }
  248. isUrl(url) {
  249. if (url)
  250. return (url.indexOf('file://') != 0);
  251. else
  252. return false;
  253. }
  254. close() {
  255. this.$emit('history-toggle');
  256. }
  257. keyHook(event) {
  258. if (event.type == 'keydown' && event.code == 'Escape') {
  259. this.close();
  260. }
  261. return true;
  262. }
  263. }
  264. //-----------------------------------------------------------------------------
  265. </script>
  266. <style scoped>
  267. .main {
  268. position: absolute;
  269. width: 100%;
  270. height: 100%;
  271. z-index: 50;
  272. display: flex;
  273. flex-direction: column;
  274. align-items: center;
  275. }
  276. .mainWindow {
  277. height: 100%;
  278. display: flex;
  279. }
  280. .desc {
  281. cursor: pointer;
  282. }
  283. </style>