Common.js 4.53 KB
Newer Older
1
import Vue from "vue";
2 3 4 5
import { getSidebarItems } from "@theme/utils/sidebar";
import Navbar from "@theme/components/Navbar/Navbar.vue";
import Sidebar from "@theme/components/Sidebar/Sidebar.vue";
import throttle from "lodash.throttle";
6
export default Vue.extend({
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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    name: "Common",
    components: {
        Navbar,
        Sidebar,
    },
    props: {
        navbar: { type: Boolean, default: true },
        sidebar: { type: Boolean, default: true },
    },
    data: () => ({
        isSidebarOpen: false,
        hideNavbar: false,
        touchStart: {
            clientX: 0,
            clientY: 0,
        },
    }),
    computed: {
        enableNavbar() {
            if (this.navbar === false)
                return false;
            const { frontmatter } = this.$page;
            if (frontmatter.navbar === false || this.$themeConfig.navbar === false)
                return false;
            return Boolean(this.$title ||
                this.$themeConfig.logo ||
                this.$themeConfig.repo ||
                this.$themeConfig.nav ||
                this.$themeLocaleConfig.nav);
        },
        enableSidebar() {
            if (this.sidebar === false)
                return false;
            return (!this.$frontmatter.home &&
                this.$frontmatter.sidebar !== false &&
                this.sidebarItems.length !== 0);
        },
        sidebarItems() {
            if (this.sidebar === false)
                return [];
            return getSidebarItems(this.$page, this.$site, this.$localePath);
        },
        pageClasses() {
            const userPageClass = this.$page.frontmatter.pageClass;
            return [
                {
                    "has-navbar": this.enableNavbar,
                    "has-sidebar": this.enableSidebar,
                    "has-anchor": this.enableAnchor,
                    "hide-navbar": this.hideNavbar,
                    "sidebar-open": this.isSidebarOpen,
                },
                userPageClass,
            ];
        },
        headers() {
            return this.getHeader(this.sidebarItems);
        },
        enableAnchor() {
            return (this.$frontmatter.anchorDisplay ||
                (this.$themeConfig.anchorDisplay !== false &&
                    this.$frontmatter.anchorDisplay !== false));
        },
    },
    mounted() {
        let lastDistance = 0;
        this.$router.afterEach(() => {
            this.isSidebarOpen = false;
        });
        window.addEventListener("scroll", throttle(() => {
            const distance = this.getScrollTop();
            // scroll down
            if (lastDistance < distance && distance > 58) {
                if (!this.isSidebarOpen)
                    this.hideNavbar = true;
                // scroll up
            }
            else
                this.hideNavbar = false;
            lastDistance = distance;
        }, 300));
    },
    methods: {
        /** Get scroll distance */
        getScrollTop() {
            return (window.pageYOffset ||
                document.documentElement.scrollTop ||
                document.body.scrollTop ||
                0);
        },
        toggleSidebar(to) {
            this.isSidebarOpen = typeof to === "boolean" ? to : !this.isSidebarOpen;
            this.$emit("toggle-sidebar", this.isSidebarOpen);
        },
        // Side swipe
        onTouchStart(event) {
            this.touchStart = {
                clientX: event.changedTouches[0].clientX,
                clientY: event.changedTouches[0].clientY,
            };
        },
        onTouchEnd(event) {
            const dx = event.changedTouches[0].clientX - this.touchStart.clientX;
            const dy = event.changedTouches[0].clientY - this.touchStart.clientY;
            if (Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > 40)
                if (dx > 0 && this.touchStart.clientX <= 80)
                    this.toggleSidebar(true);
                else
                    this.toggleSidebar(false);
        },
        getHeader(items) {
            for (let i = 0; i < items.length; i++) {
                const item = items[i];
                if (item.type === "group") {
                    const matching = this.getHeader(item.children);
                    if (matching.length !== 0)
                        return matching;
                }
                else if (item.type === "page" &&
                    item.headers &&
                    item.path === this.$route.path)
                    return item.headers;
            }
            return [];
        },
    },
});
134
//# sourceMappingURL=Common.js.map