menus.vue 4.05 KB
<template>
    <Menu theme="dark" width="auto" :accordion="true" :open-names="openNames" @on-select="open" :active-name="activeName">
        <div class="layout-logo-left"></div>
        <Submenu :name="purview.id" v-for="purview in purviews" :key="purview.id" >
            <template slot="title">
                <div class="nor">
                    <Icon type="ios-navigate"></Icon>
                    {{purview.menu_name}}
                </div>
                <div class="collapse">
                    {{purview.menu_name[0]}}
                </div>
            </template>
            <Menu-item :name="subPurview.id" v-for="subPurview in purview.sub" :key="subPurview.id">{{subPurview.menu_name}}</Menu-item>
        </Submenu>
    </Menu>
</template>

<script>
import _ from 'lodash';

export default {
    name: 'PurviewMenu',
    data() {
        return {
            purviews: this.$oriPurviews, // TODO 根据用户权限,初始化菜单
            openNames: [],
            activeName: ''
        };
    },
    created() {
        let path = _.get(this.$router, 'history.current.path');
        let menu = _.find(this.$purviews, purview => purview.menu_url === path);

        if (menu) {
            this.activeName = menu.id;
            if (menu.pMenu) {
                this.openNames = [menu.pMenu.id];
            }
        }

        this.$root.$on('menu-trigger', () => {
              
        });
    },
    methods: {
        open(menuId) {
            let menu = _.find(this.$purviews, purview => purview.id === menuId);

            if (menu) {
                setTimeout(() => {
                    this.$router.push(menu.menu_url);
                }, 100);
            }
        }
    }
};
</script>

<style lang="scss">
$collapseMenuWidth: 200px;
$collapseWidth: 50px;
$collapseLeftBorder: 2px;
@mixin submenuHover {
    .ivu-menu-submenu-title {
        margin-left: 0px;
        border-left: solid 2px #fff;
        background-color: #3d4350 !important;
        transition: border 0s;

    }
}
.ivu-menu-submenu-title {
    .collapse {
        display: none;
    }
    .nor {
        display: inline-block;
    }
}

.menu-collapse {
    .layout-logo-left {
        background: none;
        color: #fff;
        font-size: 18px;
        text-align: center;
        font-style: italic;

        &:before {
            content: 'Yoho!';
        }
    }
    .ivu-menu-submenu {
        position: relative;
        margin-left: 2px;
        
        &:hover {
            .ivu-menu {
                display: block !important;
            }
            .nor {
                display: block;
            }
        }
        &:hover, &.ivu-menu-item-active {
            @include submenuHover;
        }
        .ivu-menu {
            display: none;
            position: absolute;
            top: 49px;
            left: $collapseWidth - $collapseLeftBorder;
            z-index: 2;
            background: #464c5b;
            width: $collapseMenuWidth;
            
        }
    }
    .ivu-menu-submenu-title {
        position: initial !important;
        padding: 0px !important;
        width: 100%;
        height: $collapseWidth - $collapseLeftBorder;
        justify-content: center;
        align-items: center;
        display: flex;

        .ivu-icon-ios-arrow-down {
            display: none;
        }
        .nor {
            display: none;
            position: absolute;
            height: 49px;
            width: $collapseMenuWidth;
            left: $collapseWidth - $collapseLeftBorder;
            top: 0;
            background-color: #3d4350;
            z-index: 2;
            line-height: 49px;
            padding-left: 43px;
            color: #fff !important;
            cursor: initial;
        }
        .collapse {
            width: 30px;
            height: 30px;
            margin: 0 auto;
            display: block;
            line-height: 30px;
            text-align: center;
            font-size: 20px;
            background-color: #fff;
            color: #464c5b;
            border-radius: 50%;
            overflow: hidden;
        }
    }
}
</style>