TocItem.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. export default {
  43. name: 'TocItem',
  44. components: {
  45. TocItem
  46. },
  47. props: {
  48. item: Object,
  49. level: Number,
  50. editionid: String,
  51. loadedtextsuuids: Array
  52. },
  53. data: () => ({
  54. isOpened: false
  55. }),
  56. computed: {
  57. children () {
  58. // check if children exists and if it is an array
  59. // this shoudn't be necessary
  60. return this.item.children ? Array.isArray(this.item.children) ? this.item.children : [this.item.children] : []
  61. },
  62. title () {
  63. // this shoudn't be necessary
  64. if (this.item.title && Array.isArray(this.item.title)) {
  65. return this.item.title.join(' ')
  66. } else {
  67. return this.item.title
  68. }
  69. },
  70. titlelevel () {
  71. return this.level < 7 ? `h${this.level}` : `span`
  72. },
  73. nextLevel () {
  74. return this.level + 1
  75. },
  76. isActive () {
  77. // console.log('Active', this.$route.params.textid, this.item.uuid)
  78. if (typeof this.$route.params.textid !== 'undefined') {
  79. return this.$route.params.textid === this.item.uuid
  80. } else {
  81. return false
  82. }
  83. },
  84. isLoaded () {
  85. // this is updated when loadedtextsuuids is changing
  86. // but not for isOpened (had to use a watcher + beforeMount)
  87. // don't now why ?
  88. return this.loadedtextsuuids.indexOf(this.item.uuid) !== -1
  89. }
  90. },
  91. watch: {
  92. loadedtextsuuids (n, o) {
  93. // console.log('EdTocItem watch loadedtxtsuuids', o, n)
  94. this.isOpened = this.loadedtextsuuids.some(e => e.indexOf(this.item.uuid) >= 0)
  95. },
  96. isOpened (n, o) {
  97. // console.log('TocItem watch isOpened', o, n)
  98. // if was closed and is opened
  99. if (!o && n) {
  100. this.onOpened()
  101. }
  102. }
  103. },
  104. beforeMount () {
  105. if (typeof this.$route.params.textid !== 'undefined') {
  106. this.isOpened = this.$route.params.textid.indexOf(this.item.uuid) >= 0
  107. }
  108. },
  109. methods: {
  110. onclick (e) {
  111. // console.log('clicked on toc text', this.editionid, e)
  112. this.$emit('onClickTocItem', e.target.getAttribute('uuid'))
  113. },
  114. onClickTocItem (uuid) {
  115. // pass the event to parent
  116. this.$emit('onClickTocItem', uuid)
  117. },
  118. onOpened () {
  119. this.$emit('onScrollToRef', this.item.uuid)
  120. },
  121. onScrollToRef (uuid) {
  122. // pass the event to parent
  123. this.$emit('onScrollToRef', uuid)
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. </style>