ContentsPage.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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="bookmarks" icon="la la-bookmark" label="Закладки" />
  19. </q-tabs>
  20. </div>
  21. <div class="q-mb-sm"/>
  22. <div class="tab-panel" v-show="selectedTab == 'contents'">
  23. <div>
  24. <div v-for="item in contents" :key="item.key" class="column" style="width: 540px">
  25. <div class="row item q-px-sm no-wrap">
  26. <div v-if="item.list.length" class="row justify-center items-center expand-button clickable" @click="expandClick(item.key)">
  27. <q-icon name="la la-arrow-circle-down" class="icon" :class="{'expanded-icon': item.expanded}" color="green-8" size="24px"/>
  28. </div>
  29. <div v-else class="no-expand-button clickable" @click="setBookPos(item.offset)">
  30. </div>
  31. <div class="col row clickable" @click="setBookPos(item.offset)">
  32. <div :style="item.indentStyle"></div>
  33. <div class="q-mr-sm col overflow-hidden column justify-center" v-html="item.label"></div>
  34. <div class="column justify-center">{{ item.perc }}%</div>
  35. </div>
  36. </div>
  37. <div v-if="item.expanded" :ref="`subitem${item.key}`" class="subitems-transition">
  38. <div v-for="subitem in item.list" :key="subitem.key" class="row subitem q-px-sm no-wrap">
  39. <div class="col row clickable" @click="setBookPos(subitem.offset)">
  40. <div class="no-expand-button"></div>
  41. <div :style="subitem.indentStyle"></div>
  42. <div class="q-mr-sm col overflow-hidden column justify-center" v-html="subitem.label"></div>
  43. <div class="column justify-center">{{ subitem.perc }}%</div>
  44. </div>
  45. </div>
  46. </div>
  47. </div>
  48. </div>
  49. </div>
  50. <div class="tab-panel" v-show="selectedTab == 'bookmarks'">
  51. <div class="column justify-center items-center" style="height: 100px">
  52. Раздел находится в разработке
  53. </div>
  54. </div>
  55. </Window>
  56. </template>
  57. <script>
  58. //-----------------------------------------------------------------------------
  59. import Vue from 'vue';
  60. import Component from 'vue-class-component';
  61. //import _ from 'lodash';
  62. import Window from '../../share/Window.vue';
  63. import * as utils from '../../../share/utils';
  64. export default @Component({
  65. components: {
  66. Window,
  67. },
  68. watch: {
  69. },
  70. })
  71. class ContentsPage extends Vue {
  72. selectedTab = 'contents';
  73. contents = [];
  74. created() {
  75. }
  76. async init(currentBook, parsed) {
  77. this.$refs.window.init();
  78. //закладки
  79. //далее формаирование оглавления
  80. if (this.parsed == parsed)
  81. return;
  82. this.parsed = parsed;
  83. this.contents = [];
  84. await this.$nextTick();
  85. const prepareLabel = (title, bolder = false) => {
  86. let titleParts = title.split('<p>');
  87. const textParts = titleParts.filter(v => v).map(v => `<div>${v.replace(/(<([^>]+)>)/ig, '')}</div>`);
  88. if (bolder && textParts.length > 1)
  89. textParts[0] = `<b>${textParts[0]}</b>`;
  90. return textParts.join('');
  91. }
  92. const insetStyle = inset => `width: ${inset*20}px`;
  93. const pc = parsed.contents;
  94. const newpc = [];
  95. //преобразуем не первые разделы body в title-subtitle
  96. let curSubtitles = [];
  97. let prevBodyIndex = -1;
  98. for (let i = 0; i < pc.length; i++) {
  99. const cont = pc[i];
  100. if (prevBodyIndex != cont.bodyIndex)
  101. curSubtitles = [];
  102. prevBodyIndex = cont.bodyIndex;
  103. if (cont.bodyIndex > 1) {
  104. if (cont.inset < 1) {
  105. newpc.push(Object.assign({}, cont, {subtitles: curSubtitles}));
  106. } else {
  107. curSubtitles.push(Object.assign({}, cont, {inset: cont.inset - 1}));
  108. }
  109. } else {
  110. newpc.push(cont);
  111. }
  112. }
  113. //формируем newContents
  114. let i = 0;
  115. const newContents = [];
  116. newpc.forEach((cont) => {
  117. const label = prepareLabel(cont.title, true);
  118. const indentStyle = insetStyle(cont.inset);
  119. let j = 0;
  120. const list = [];
  121. cont.subtitles.forEach((sub) => {
  122. const l = prepareLabel(sub.title);
  123. const s = insetStyle(sub.inset + 1);
  124. const p = parsed.para[sub.paraIndex];
  125. list[j] = {perc: (p.offset/parsed.textLength*100).toFixed(2), label: l, key: j, offset: p.offset, indentStyle: s};
  126. j++;
  127. });
  128. const p = parsed.para[cont.paraIndex];
  129. newContents[i] = {perc: (p.offset/parsed.textLength*100).toFixed(0), label, key: i, offset: p.offset, indentStyle, expanded: false, list};
  130. i++;
  131. });
  132. this.contents = newContents;
  133. }
  134. async expandClick(key) {
  135. const item = this.contents[key];
  136. const expanded = !item.expanded;
  137. if (!expanded) {
  138. const subitems = this.$refs[`subitem${key}`][0];
  139. subitems.style.height = '0';
  140. await utils.sleep(200);
  141. }
  142. this.$set(this.contents, key, Object.assign({}, item, {expanded}));
  143. if (expanded) {
  144. await this.$nextTick();
  145. const subitems = this.$refs[`subitem${key}`][0];
  146. subitems.style.height = subitems.scrollHeight + 'px';
  147. }
  148. }
  149. async setBookPos(newValue) {
  150. this.$emit('book-pos-changed', {bookPos: newValue});
  151. this.close();
  152. }
  153. close() {
  154. this.$emit('do-action', {action: 'contents'});
  155. }
  156. keyHook(event) {
  157. if (!this.$root.stdDialog.active && event.type == 'keydown' && event.key == 'Escape') {
  158. this.close();
  159. }
  160. return true;
  161. }
  162. }
  163. //-----------------------------------------------------------------------------
  164. </script>
  165. <style scoped>
  166. .tab-panel {
  167. overflow-x: hidden;
  168. overflow-y: auto;
  169. font-size: 90%;
  170. padding: 0 10px 0px 10px;
  171. }
  172. .clickable {
  173. cursor: pointer;
  174. }
  175. .item, .subitem {
  176. height: 40px;
  177. border-bottom: 1px solid #e0e0e0;
  178. }
  179. .item:hover, .subitem:hover {
  180. background-color: #f0f0f0;
  181. }
  182. .expand-button, .no-expand-button {
  183. width: 40px;
  184. }
  185. .expand-button:hover {
  186. background-color: white;
  187. border-radius: 15px;
  188. border: 1px solid #d0d0d0;
  189. }
  190. .subitems-transition {
  191. height: 0;
  192. transition: height 0.2s linear;
  193. overflow: hidden;
  194. }
  195. .icon {
  196. transition: transform 0.2s;
  197. }
  198. .expanded-icon {
  199. transform: rotate(180deg);
  200. }
  201. </style>