TocItem.vue 3.7 KB

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