HistoryPage.vue 8.6 KB

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