TocItem.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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}"
  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 :item="child" :level="nextLevel" :editionid="editionid" />
  29. </li>
  30. </ul>
  31. </section>
  32. </template>
  33. <script>
  34. import TocItem from './TocItem'
  35. export default {
  36. name: 'TocItem',
  37. components: {
  38. TocItem
  39. },
  40. props: {
  41. item: Object,
  42. level: Number,
  43. editionid: String
  44. },
  45. // data: () => ({
  46. //
  47. // })
  48. computed: {
  49. children () {
  50. // check if children exists and if it is an array
  51. // this shoudn't be necessary
  52. return this.item.children ? Array.isArray(this.item.children) ? this.item.children : [this.item.children] : []
  53. },
  54. title () {
  55. // this shoudn't be necessary
  56. if (this.item.title && Array.isArray(this.item.title)) {
  57. return this.item.title.join(' ')
  58. } else {
  59. return this.item.title
  60. }
  61. },
  62. titlelevel () {
  63. return 'h' + this.level
  64. },
  65. nextLevel () {
  66. return this.level + 1
  67. },
  68. isActive () {
  69. // console.log('Active', this.$route.params.textid, this.item.uuid)
  70. if (typeof this.$route.params.textid !== 'undefined') {
  71. return this.$route.params.textid === this.item.uuid
  72. } else {
  73. return false
  74. }
  75. },
  76. isOpened () {
  77. // console.log('opened', this.$route.params.textid.indexOf(this.item.uuid) >= 0)
  78. if (typeof this.$route.params.textid !== 'undefined') {
  79. return this.$route.params.textid.indexOf(this.item.uuid) >= 0
  80. } else {
  81. return false
  82. }
  83. }
  84. },
  85. // beforeCreate () {
  86. // console.log('editionid', this.editionid)
  87. // },
  88. methods: {
  89. onclick (e) {
  90. console.log('clicked on toc text', this.editionid, e)
  91. this.$router.push({
  92. name: `editiontext`,
  93. params: {
  94. id: this.editionid,
  95. textid: e.target.getAttribute('uuid')
  96. }
  97. })
  98. }
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. </style>