ContentsPage.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <template>
  2. <Window width="600px" ref="window" @close="close">
  3. <template slot="header">
  4. Оглавление/закладки
  5. </template>
  6. <div class="bg-grey-3 row">
  7. <q-tabs
  8. v-model="selectedTab"
  9. active-color="black"
  10. active-bg-color="white"
  11. indicator-color="white"
  12. dense
  13. no-caps
  14. inline-label
  15. class="no-mp bg-grey-4 text-grey-7"
  16. >
  17. <q-tab name="contents" icon="la la-list" label="Оглавление" />
  18. <q-tab name="images" icon="la la-image" label="Изображения" />
  19. <q-tab name="bookmarks" icon="la la-bookmark" label="Закладки" />
  20. </q-tabs>
  21. </div>
  22. <div class="q-mb-sm"/>
  23. <div class="tab-panel" v-show="selectedTab == 'contents'">
  24. <div>
  25. <div v-for="item in contents" :key="item.key" class="column" style="width: 540px">
  26. <div class="row q-px-sm no-wrap" :class="{'item': !item.isBookPos, 'item-book-pos': item.isBookPos}">
  27. <div v-if="item.list.length" class="row justify-center items-center expand-button clickable" @click="expandClick(item.key)">
  28. <q-icon name="la la-caret-right" class="icon" :class="{'expanded-icon': item.expanded}" color="green-8" size="20px"/>
  29. </div>
  30. <div v-else class="no-expand-button clickable" @click="setBookPos(item.offset)">
  31. <q-icon name="la la-stop" class="icon" style="visibility: hidden" size="20px"/>
  32. </div>
  33. <div class="col row clickable" @click="setBookPos(item.offset)">
  34. <div :style="item.indentStyle"></div>
  35. <div class="q-mr-sm col overflow-hidden column justify-center" :style="item.labelStyle" v-html="item.label"></div>
  36. <div class="column justify-center">{{ item.perc }}%</div>
  37. </div>
  38. </div>
  39. <div v-if="item.expanded" :ref="`subitem${item.key}`" class="subitems-transition">
  40. <div v-for="subitem in item.list" :key="subitem.key" class="row q-px-sm no-wrap" :class="{'subitem': !subitem.isBookPos, 'subitem-book-pos': subitem.isBookPos}">
  41. <div class="col row clickable" @click="setBookPos(subitem.offset)">
  42. <div class="no-expand-button"></div>
  43. <div :style="subitem.indentStyle"></div>
  44. <div class="q-mr-sm col overflow-hidden column justify-center" :style="item.labelStyle" v-html="subitem.label"></div>
  45. <div class="column justify-center">{{ subitem.perc }}%</div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <div v-if="!contents.length" class="column justify-center items-center" style="height: 100px">
  51. Оглавление отсутствует
  52. </div>
  53. </div>
  54. </div>
  55. <div class="tab-panel" v-show="selectedTab == 'images'">
  56. <div>
  57. <div v-for="item in images" :key="item.key" class="column" style="width: 540px">
  58. <div class="row q-px-sm no-wrap" :class="{'item': !item.isBookPos, 'item-book-pos': item.isBookPos}">
  59. <div class="col row clickable" @click="setBookPos(item.offset)">
  60. <div class="image-thumb-box row justify-center items-center">
  61. <div v-show="!imageLoaded[item.id]" class="image-thumb column justify-center"><i class="loading-img-icon la la-images"></i></div>
  62. <img v-show="imageLoaded[item.id]" class="image-thumb" :src="imageSrc[item.id]"/>
  63. </div>
  64. <div class="no-expand-button column justify-center items-center">
  65. <div v-show="item.type == 'image/jpeg'" class="image-type it-jpg-color row justify-center">JPG</div>
  66. <div v-show="item.type == 'image/png'" class="image-type it-png-color row justify-center">PNG</div>
  67. <div v-show="!item.local" class="image-type it-net-color row justify-center">INET</div>
  68. </div>
  69. <div :style="item.indentStyle"></div>
  70. <div class="q-mr-sm col overflow-hidden column justify-center" :style="item.labelStyle" v-html="item.label"></div>
  71. <div class="column justify-center">{{ item.perc }}%</div>
  72. </div>
  73. </div>
  74. </div>
  75. <div v-if="!images.length" class="column justify-center items-center" style="height: 100px">
  76. Изображения отсутствуют
  77. </div>
  78. </div>
  79. </div>
  80. <div class="tab-panel" v-show="selectedTab == 'bookmarks'">
  81. <div class="column justify-center items-center" style="height: 100px">
  82. Раздел находится в разработке
  83. </div>
  84. </div>
  85. </Window>
  86. </template>
  87. <script>
  88. //-----------------------------------------------------------------------------
  89. import Vue from 'vue';
  90. import Component from 'vue-class-component';
  91. //import _ from 'lodash';
  92. import Window from '../../share/Window.vue';
  93. import * as utils from '../../../share/utils';
  94. const ContentsPageProps = Vue.extend({
  95. props: {
  96. bookPos: Number,
  97. isVisible: Boolean,
  98. }
  99. });
  100. export default @Component({
  101. components: {
  102. Window,
  103. },
  104. watch: {
  105. bookPos: function() {
  106. this.updateBookPosSelection();
  107. }
  108. },
  109. })
  110. class ContentsPage extends ContentsPageProps {
  111. selectedTab = 'contents';
  112. contents = [];
  113. images = [];
  114. imageSrc = [];
  115. imageLoaded = [];
  116. created() {
  117. }
  118. async init(currentBook, parsed) {
  119. this.$refs.window.init();
  120. //закладки
  121. //проверим, надо ли обновлять списки
  122. if (this.parsed == parsed) {
  123. this.updateBookPosSelection();
  124. return;
  125. }
  126. //далее формирование оглавления
  127. this.parsed = parsed;
  128. this.contents = [];
  129. await this.$nextTick();
  130. const pc = parsed.contents;
  131. const newpc = [];
  132. //преобразуем все, кроме первого, разделы body в title-subtitle
  133. let curSubtitles = [];
  134. let prevBodyIndex = -1;
  135. for (let i = 0; i < pc.length; i++) {
  136. const cont = pc[i];
  137. if (prevBodyIndex != cont.bodyIndex)
  138. curSubtitles = [];
  139. prevBodyIndex = cont.bodyIndex;
  140. if (cont.bodyIndex > 1) {
  141. if (cont.inset < 1) {
  142. newpc.push(Object.assign({}, cont, {subtitles: curSubtitles}));
  143. } else {
  144. curSubtitles.push(Object.assign({}, cont, {inset: cont.inset - 1}));
  145. }
  146. } else {
  147. newpc.push(cont);
  148. }
  149. }
  150. const prepareLabel = (title, bolder = false) => {
  151. let titleParts = title.split('<p>');
  152. const textParts = titleParts.filter(v => v).map(v => `<div>${utils.removeHtmlTags(v)}</div>`);
  153. if (bolder && textParts.length > 1)
  154. textParts[0] = `<b>${textParts[0]}</b>`;
  155. return textParts.join('');
  156. }
  157. const getIndentStyle = inset => `width: ${inset*20}px`;
  158. const getLabelStyle = (inset) => {
  159. const fontSizes = ['110%', '100%', '90%', '85%'];
  160. inset = (inset > 3 ? 3 : inset);
  161. return `font-size: ${fontSizes[inset]}`;
  162. };
  163. //формируем newContents
  164. let i = 0;
  165. const newContents = [];
  166. newpc.forEach((cont) => {
  167. const label = prepareLabel(cont.title, true);
  168. const indentStyle = getIndentStyle(cont.inset);
  169. const labelStyle = getLabelStyle(cont.inset);
  170. let j = 0;
  171. const list = [];
  172. cont.subtitles.forEach((sub) => {
  173. const l = prepareLabel(sub.title);
  174. const s = getIndentStyle(sub.inset + 1);
  175. const ls = getLabelStyle(cont.inset + 1);
  176. const p = parsed.para[sub.paraIndex];
  177. list[j] = {perc: (p.offset/parsed.textLength*100).toFixed(2), label: l, key: j, offset: p.offset, indentStyle: s, labelStyle: ls};
  178. j++;
  179. });
  180. const p = parsed.para[cont.paraIndex];
  181. newContents[i] = {perc: (p.offset/parsed.textLength*100).toFixed(0), label, key: i, offset: p.offset, indentStyle, labelStyle, expanded: false, list};
  182. i++;
  183. });
  184. this.contents = newContents;
  185. //формируем newImages
  186. const newImages = [];
  187. const ims = parsed.images;
  188. for (i = 0; i < ims.length; i++) {
  189. const image = ims[i];
  190. const bin = parsed.binary[image.id];
  191. const type = (bin ? bin.type : '');
  192. const label = `Изображение ${image.num}`;
  193. const indentStyle = getIndentStyle(1);
  194. const labelStyle = getLabelStyle(0);
  195. const p = parsed.para[image.paraIndex];
  196. newImages.push({perc: (p.offset/parsed.textLength*100).toFixed(0), label, key: i, offset: p.offset,
  197. indentStyle, labelStyle, type, id: image.id, local: image.local});
  198. }
  199. this.images = newImages;
  200. if (this.selectedTab == 'contents' && !this.contents.length && this.images.length)
  201. this.selectedTab = 'images';
  202. //выделим на bookPos
  203. this.updateBookPosSelection();
  204. //асинхронная загрузка изображений
  205. this.imageSrc = [];
  206. this.imageLoaded = [];
  207. await utils.sleep(50);
  208. (async() => {
  209. for (i = 0; i < ims.length; i++) {
  210. const {id, local} = ims[i];
  211. const bin = this.parsed.binary[id];
  212. if (local)
  213. this.$set(this.imageSrc, id, (bin ? `data:${bin.type};base64,${bin.data}` : ''));
  214. else
  215. this.$set(this.imageSrc, id, id);
  216. this.imageLoaded[id] = true;
  217. await utils.sleep(5);
  218. }
  219. })();
  220. }
  221. async updateBookPosSelection() {
  222. if (!this.isVisible)
  223. return;
  224. await utils.sleep(50);
  225. const bp = this.bookPos;
  226. for (let i = 0; i < this.contents.length; i++) {
  227. const item = this.contents[i];
  228. const nextOffset = (i < this.contents.length - 1 ? this.contents[i + 1].offset : this.parsed.textLength);
  229. for (let j = 0; j < item.list.length; j++) {
  230. const subitem = item.list[j];
  231. const nextSubOffset = (j < item.list.length - 1 ? item.list[j + 1].offset : nextOffset);
  232. if (bp >= subitem.offset && bp < nextSubOffset) {
  233. subitem.isBookPos = true;
  234. this.$set(this.contents, i, Object.assign(item, {list: item.list}));
  235. } else if (subitem.isBookPos) {
  236. subitem.isBookPos = false;
  237. this.$set(this.contents, i, Object.assign(item, {list: item.list}));
  238. }
  239. }
  240. if (bp >= item.offset && bp < nextOffset) {
  241. this.$set(this.contents, i, Object.assign(item, {isBookPos: true}));
  242. } else if (item.isBookPos) {
  243. this.$set(this.contents, i, Object.assign(item, {isBookPos: false}));
  244. }
  245. }
  246. for (let i = 0; i < this.images.length; i++) {
  247. const img = this.images[i];
  248. const nextOffset = (i < this.images.length - 1 ? this.images[i + 1].offset : this.parsed.textLength);
  249. if (bp >= img.offset && bp < nextOffset) {
  250. this.$set(this.images, i, Object.assign(img, {isBookPos: true}));
  251. } else if (img.isBookPos) {
  252. this.$set(this.images, i, Object.assign(img, {isBookPos: false}));
  253. }
  254. }
  255. }
  256. async expandClick(key) {
  257. const item = this.contents[key];
  258. const expanded = !item.expanded;
  259. if (!expanded) {
  260. const subitems = this.$refs[`subitem${key}`][0];
  261. subitems.style.height = '0';
  262. await utils.sleep(200);
  263. }
  264. this.$set(this.contents, key, Object.assign({}, item, {expanded}));
  265. if (expanded) {
  266. await this.$nextTick();
  267. const subitems = this.$refs[`subitem${key}`][0];
  268. subitems.style.height = subitems.scrollHeight + 'px';
  269. }
  270. }
  271. async setBookPos(newValue) {
  272. this.$emit('book-pos-changed', {bookPos: newValue});
  273. this.close();
  274. }
  275. close() {
  276. this.$emit('do-action', {action: 'contents'});
  277. }
  278. keyHook(event) {
  279. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  280. this.close();
  281. }
  282. return true;
  283. }
  284. }
  285. //-----------------------------------------------------------------------------
  286. </script>
  287. <style scoped>
  288. .tab-panel {
  289. overflow-x: hidden;
  290. overflow-y: auto;
  291. font-size: 90%;
  292. padding: 0 10px 0px 10px;
  293. }
  294. .clickable {
  295. cursor: pointer;
  296. padding: 10px 0 10px 0;
  297. }
  298. .item, .subitem, .item-book-pos, .subitem-book-pos {
  299. border-bottom: 1px solid #e0e0e0;
  300. }
  301. .item:hover, .subitem:hover {
  302. background-color: #f0f0f0;
  303. }
  304. .item-book-pos {
  305. background-color: #b0f0b0;
  306. }
  307. .subitem-book-pos {
  308. background-color: #d0f5d0;
  309. }
  310. .item-book-pos:hover {
  311. background-color: #b0e0b0;
  312. }
  313. .subitem-book-pos:hover {
  314. background-color: #d0f0d0;
  315. }
  316. .expand-button, .no-expand-button {
  317. width: 40px;
  318. }
  319. .subitems-transition {
  320. height: 0;
  321. transition: height 0.2s linear;
  322. overflow: hidden;
  323. }
  324. .icon {
  325. transition: transform 0.2s;
  326. }
  327. .expanded-icon {
  328. transform: rotate(90deg);
  329. }
  330. .image-type {
  331. border: 1px solid black;
  332. border-radius: 6px;
  333. font-size: 80%;
  334. padding: 2px 0 2px 0;
  335. width: 34px;
  336. }
  337. .it-jpg-color {
  338. background: linear-gradient(to right, #fabc3d, #ffec6d);
  339. }
  340. .it-png-color {
  341. background: linear-gradient(to right, #4bc4e5, #6bf4ff);
  342. }
  343. .it-net-color {
  344. background: linear-gradient(to right, #00c400, #00f400);
  345. }
  346. .image-thumb-box {
  347. width: 120px;
  348. overflow: hidden;
  349. }
  350. .image-thumb {
  351. height: 50px;
  352. }
  353. .loading-img-icon {
  354. font-size: 250%;
  355. }
  356. </style>