TocItem.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <section
  3. :uuid="item.uuid"
  4. :level="level"
  5. :type="item.type"
  6. class="tocitem"
  7. >
  8. <component
  9. :is="titlelevel"
  10. v-if="title"
  11. class="toc-title"
  12. :uuid="item.uuid"
  13. :class="{active: isActive, loaded: isLoaded, notitle: noTitle}"
  14. >
  15. <a
  16. :href="'/edition/'+editionid+'/'+item.uuid"
  17. :uuid="item.uuid"
  18. @click.prevent="onclick"
  19. @keyup.enter="onclick"
  20. >
  21. {{ title }}
  22. </a>
  23. </component>
  24. <ul
  25. v-if="children.length"
  26. class="toc-list"
  27. :class="{opened: isOpened}"
  28. >
  29. <li v-for="child in children" :key="child.uuid">
  30. <TocItem
  31. :item="child"
  32. :level="nextLevel"
  33. :editionid="editionid"
  34. :loadedtextsuuids="loadedtextsuuids"
  35. @onClickTocItem="onClickTocItem"
  36. @onScrollToRef="onScrollToRef"
  37. />
  38. </li>
  39. </ul>
  40. </section>
  41. </template>
  42. <script>
  43. import TocItem from './TocItem'
  44. import { mapState, mapActions } from 'vuex'
  45. export default {
  46. name: 'TocItem',
  47. components: {
  48. TocItem
  49. },
  50. props: {
  51. item: Object,
  52. level: Number,
  53. editionid: String,
  54. loadedtextsuuids: Array
  55. },
  56. data: () => ({
  57. isOpened: false,
  58. noTitle: false
  59. }),
  60. computed: {
  61. ...mapState({
  62. editionsbyuuid: state => state.Corpus.editionsbyuuid
  63. }),
  64. editionTitle () {
  65. return this.editionsbyuuid[this.editionid].title
  66. },
  67. children () {
  68. // check if children exists and if it is an array
  69. // this shoudn't be necessary
  70. return this.item.children ? Array.isArray(this.item.children) ? this.item.children : [this.item.children] : []
  71. },
  72. title () {
  73. // this shoudn't be necessary
  74. if (this.item.title && Array.isArray(this.item.title)) {
  75. return this.item.title.join(' ')
  76. } else if (this.item.title) {
  77. return this.item.title
  78. } else {
  79. console.log('TOC no title', this.item)
  80. return this.item.type
  81. }
  82. },
  83. titlelevel () {
  84. return this.level < 7 ? `h${this.level}` : `span`
  85. },
  86. nextLevel () {
  87. return this.level + 1
  88. },
  89. isActive () {
  90. // console.log('Active', this.$route.params.textid, this.item.uuid)
  91. if (typeof this.$route.params.textid !== 'undefined') {
  92. return this.$route.params.textid === this.item.uuid
  93. } else {
  94. return false
  95. }
  96. },
  97. isLoaded () {
  98. // this is updated when loadedtextsuuids is changing
  99. // but not for isOpened (had to use a watcher + beforeMount)
  100. // don't now why ?
  101. return this.loadedtextsuuids.indexOf(this.item.uuid) !== -1
  102. }
  103. },
  104. watch: {
  105. loadedtextsuuids (n, o) {
  106. // console.log('EdTocItem watch loadedtxtsuuids', o, n)
  107. this.isOpened = this.loadedtextsuuids.some(e => e.indexOf(this.item.uuid) >= 0)
  108. },
  109. isOpened (n, o) {
  110. // console.log('TocItem watch isOpened', o, n)
  111. // if was closed and is opened
  112. if (!o && n) {
  113. this.onOpened()
  114. }
  115. }
  116. },
  117. beforeMount () {
  118. if (typeof this.$route.params.textid !== 'undefined') {
  119. this.isOpened = this.$route.params.textid.indexOf(this.item.uuid) >= 0
  120. }
  121. if (['book', 'volume'].indexOf(this.item.type) >= 0) {
  122. this.isOpened = true
  123. }
  124. if (!this.item.title) {
  125. this.noTitle = true
  126. }
  127. },
  128. methods: {
  129. ...mapActions({
  130. addHistoryItem: 'History/addItem'
  131. }),
  132. onclick (e) {
  133. console.log('clicked on toc text', this.item, e)
  134. this.addHistoryItem({
  135. id: this.editionid,
  136. textid: this.item.uuid,
  137. title: this.item.title,
  138. editionTitle: this.editionTitle
  139. // pages: this.item.pages,
  140. // size: this.item.size
  141. })
  142. this.$emit('onClickTocItem', e.target.getAttribute('uuid'))
  143. },
  144. onClickTocItem (uuid) {
  145. // pass the event to parent
  146. this.$emit('onClickTocItem', uuid)
  147. },
  148. onOpened () {
  149. this.$emit('onScrollToRef', this.item.uuid)
  150. },
  151. onScrollToRef (uuid) {
  152. // pass the event to parent
  153. this.$emit('onScrollToRef', uuid)
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. </style>