TocItem.vue 3.2 KB

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