SidebarLinks.js 1.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
import Vue from "vue";
import SidebarGroup from "@theme/components/Sidebar/SidebarGroup.vue";
import SidebarLink from "@theme/components/Sidebar/SidebarLink.vue";
import { isActive } from "@theme/utils/path";
const descendantIsActive = (route, item) => {
    if (item.type === "group")
        return item.children.some((child) => {
            if (child.type === "group")
                return descendantIsActive(route, child);
            return child.type === "page" && isActive(route, child.path);
        });
    return false;
};
const resolveOpenGroupIndex = (route, items) => {
    for (let i = 0; i < items.length; i++)
        if (descendantIsActive(route, items[i]))
            return i;
    return -1;
};
export default Vue.extend({
    name: "SidebarLinks",
    components: { SidebarGroup, SidebarLink },
    props: {
        items: {
            type: Array,
            required: true,
        },
        depth: { type: Number, required: true },
    },
    data: () => ({
        openGroupIndex: 0,
    }),
    watch: {
        $route() {
            this.refreshIndex();
        },
    },
    created() {
        this.refreshIndex();
    },
    methods: {
        refreshIndex() {
            const index = resolveOpenGroupIndex(this.$route, this.items);
            if (index > -1)
                this.openGroupIndex = index;
        },
        toggleGroup(index) {
            this.openGroupIndex = index === this.openGroupIndex ? -1 : index;
        },
        isActive(page) {
            return isActive(this.$route, page.regularPath);
        },
    },
});
//# sourceMappingURL=SidebarLinks.js.map