PageSidebarToc.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <DropdownTransition>
  3. <ul class="toc-sidebar-links" v-if="items[0].children.length">
  4. <li v-for="(item, i) in items[0].children" :key="i">
  5. <PageSidebarTocLink :sidebarDepth="sidebarDepth" :item="item"/>
  6. </li>
  7. </ul>
  8. </DropdownTransition>
  9. </template>
  10. <script>
  11. import PageSidebarTocLink from '@theme/components/PageSidebarTocLink.vue'
  12. import DropdownTransition from '@theme/components/DropdownTransition.vue'
  13. import { isActive } from '../util'
  14. export default {
  15. name: 'PageSidebarToc',
  16. components: { PageSidebarTocLink, DropdownTransition },
  17. props: [
  18. 'items',
  19. 'depth', // depth of current sidebar links
  20. 'sidebarDepth' // depth of headers to be extracted
  21. ],
  22. data () {
  23. return {
  24. openGroupIndex: 0
  25. }
  26. },
  27. created () {
  28. this.refreshIndex()
  29. },
  30. watch: {
  31. '$route' () {
  32. this.refreshIndex()
  33. }
  34. },
  35. methods: {
  36. refreshIndex () {
  37. const index = resolveOpenGroupIndex(
  38. this.$route,
  39. this.items[0].children
  40. )
  41. if (index > -1) {
  42. this.openGroupIndex = index
  43. }
  44. },
  45. toggleGroup (index) {
  46. this.openGroupIndex = index === this.openGroupIndex ? -1 : index
  47. },
  48. isActive (page) {
  49. return isActive(this.$route, page.regularPath)
  50. }
  51. }
  52. }
  53. function resolveOpenGroupIndex (route, items) {
  54. for (let i = 0; i < items.length; i++) {
  55. const item = items[i]
  56. if (descendantIsActive(route, item)) {
  57. return i
  58. }
  59. }
  60. return -1
  61. }
  62. function descendantIsActive (route, item) {
  63. if (item.type === 'group') {
  64. return item.children.some(child => {
  65. if (child.type === 'group') {
  66. return descendantIsActive(route, child)
  67. } else {
  68. return child.type === 'page' && isActive(route, child.path)
  69. }
  70. })
  71. }
  72. return false
  73. }
  74. </script>