SidebarItem.vue 3.33 KB
Newer Older
Pan's avatar
Pan committed
1
<template>
yanzhongrong's avatar
yanzhongrong committed
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
  <div v-if="!item.hidden" class="menu-wrapper">
    <template
      v-if="
        hasOneShowingChild(item.children, item) &&
          (!onlyOneChild.children || onlyOneChild.noShowingChildren) &&
          !item.alwaysShow
      "
    >
      <app-link
        v-if="onlyOneChild.meta"
        :to="
          resolvePath(
            onlyOneChild.path +
              (onlyOneChild.meta.querys ? '?' + onlyOneChild.meta.querys : '')
          )
        "
      >
        <el-menu-item
          :index="resolvePath(onlyOneChild.path)"
          :class="{ 'submenu-title-noDropdown': !isNest }"
        >
          <icon-item
            :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
            :title="onlyOneChild.meta.title"
          />
27
        </el-menu-item>
Pan's avatar
Pan committed
28
      </app-link>
29
    </template>
Pan's avatar
Pan committed
30

yanzhongrong's avatar
yanzhongrong committed
31
    <template v-else>
yanzhongrong's avatar
yanzhongrong committed
32
      <el-submenu id="subid" ref="subMenu" :index="resolvePath(item.path)">
yanzhongrong's avatar
yanzhongrong committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        <template slot="title">
          <icon-item
            v-if="item.meta"
            :icon="item.meta && item.meta.icon"
            :title="item.meta.title"
          />
        </template>
        <sidebar-item
          v-for="child in item.children"
          :key="child.path"
          :is-nest="true"
          :item="child"
          :base-path="resolvePath(child.path)"
          class="nest-menu"
        />
      </el-submenu>
    </template>
Pan's avatar
Pan committed
50
  </div>
Pan's avatar
Pan committed
51 52 53
</template>

<script>
yanzhongrong's avatar
yanzhongrong committed
54 55 56 57
import path from "path";
import { isExternal } from "@/utils/validate";
import IconItem from "./Item.vue";
import AppLink from "./Link";
58

Pan's avatar
Pan committed
59
export default {
yanzhongrong's avatar
yanzhongrong committed
60 61
  name: "SidebarItem",
  components: { IconItem, AppLink },
Pan's avatar
Pan committed
62
  props: {
63
    // route object
64 65
    item: {
      type: Object,
yanzhongrong's avatar
yanzhongrong committed
66
      required: true,
67 68 69
    },
    isNest: {
      type: Boolean,
yanzhongrong's avatar
yanzhongrong committed
70
      default: false,
71 72 73
    },
    basePath: {
      type: String,
yanzhongrong's avatar
yanzhongrong committed
74 75
      default: "",
    },
76
  },
77
  data() {
78 79
    // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
    // TODO: refactor with render function
yanzhongrong's avatar
yanzhongrong committed
80 81
    this.onlyOneChild = null;
    return {};
82
  },
yanzhongrong's avatar
yanzhongrong committed
83 84
  mounted() {
  },
85
  methods: {
Pan's avatar
Pan committed
86
    hasOneShowingChild(children = [], parent) {
yanzhongrong's avatar
yanzhongrong committed
87
      
88
      const showingChildren = children.filter(item => {
89
        if (item.hidden) {
yanzhongrong's avatar
yanzhongrong committed
90
          return false;
91
        } else {
92
          // Temp set(will be used if only has one showing child)
yanzhongrong's avatar
yanzhongrong committed
93 94
          this.onlyOneChild = item;
          return true;
95
        }
yanzhongrong's avatar
yanzhongrong committed
96
      });
97 98

      // When there is only one child router, the child router is displayed by default
99
      if (showingChildren.length === 1) {
yanzhongrong's avatar
yanzhongrong committed
100
        return true;
101
      }
102 103 104

      // Show parent if there are no child router to display
      if (showingChildren.length === 0) {
yanzhongrong's avatar
yanzhongrong committed
105 106
        this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
        return true;
107 108
      }

yanzhongrong's avatar
yanzhongrong committed
109
      return false;
110
    },
111
    resolvePath(routePath) {
Pan's avatar
Pan committed
112
      if (isExternal(routePath)) {
yanzhongrong's avatar
yanzhongrong committed
113
        return routePath;
Pan's avatar
Pan committed
114
      }
花裤衩's avatar
花裤衩 committed
115
      if (isExternal(this.basePath)) {
yanzhongrong's avatar
yanzhongrong committed
116
        return this.basePath;
花裤衩's avatar
花裤衩 committed
117
      }
yanzhongrong's avatar
yanzhongrong committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
      return path.resolve(this.basePath, routePath);
    },
  },
};
</script>

<style lang="scss" scoped>
.menu-wrapper {
  &:focus {
    outline: none;
  }
  a {
    text-decoration: none;
    color: inherit;
    display: inline-block;
    width: 100%;
Pan's avatar
Pan committed
134 135
  }
}
yanzhongrong's avatar
yanzhongrong committed
136 137 138 139 140 141 142
.el-menu--collapse .el-menu .el-submenu, .el-menu--popup{
  min-width: 120px!important;
}
.el-menu .el-menu--popup .el-menu--popup-bottom-start ::v-deep{
  width: 1020px;
}

yanzhongrong's avatar
yanzhongrong committed
143
</style>