TocItem.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. //
  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 'h' + this.level
  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. isOpened () {
  84. // console.log('opened', this.$route.params.textid.indexOf(this.item.uuid) >= 0)
  85. if (typeof this.$route.params.textid !== 'undefined') {
  86. return this.$route.params.textid.indexOf(this.item.uuid) >= 0
  87. } else {
  88. return false
  89. }
  90. },
  91. isLoaded () {
  92. return this.loadedtextsuuids.indexOf(this.item.uuid) !== -1
  93. }
  94. },
  95. // beforeCreate () {
  96. // console.log('editionid', this.editionid)
  97. // },
  98. methods: {
  99. onclick (e) {
  100. console.log('clicked on toc text', this.editionid, e)
  101. // if (this.$route.params.textid !== e.target.getAttribute('uuid')) {
  102. // this.$router.push({
  103. // name: `editiontext`,
  104. // params: {
  105. // id: this.editionid,
  106. // textid: e.target.getAttribute('uuid')
  107. // }
  108. // })
  109. // } else {
  110. this.$emit('onClickTocItem', e.target.getAttribute('uuid'))
  111. // }
  112. },
  113. onClickTocItem (uuid) {
  114. this.$emit('onClickTocItem', uuid)
  115. }
  116. }
  117. }
  118. </script>
  119. <style lang="scss" scoped>
  120. </style>