Blame view

antis-ncc-admin/src/layout/blend/menu/index.vue 3.06 KB
4aba9b59   wwk   1
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
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
  <template>
    <div :class="classObj" class="top-menu" id="topMenu">
      <el-menu mode="horizontal" :unique-opened="true" :default-active="activeName">
        <sidebar-item v-for="route in list" :key="route.enCode" :item="route" :base-path="route.path"
          ref="sidebarItem" />
      </el-menu>
    </div>
  </template>
  
  <script>
  import { mapGetters, mapState } from 'vuex'
  import SidebarItem from './SidebarItem'
  import variables from '@/styles/variables.scss'
  import elementResizeDetectorMaker from 'element-resize-detector'
  
  export default {
    components: { SidebarItem },
    computed: {
      ...mapGetters([
        'permission_routes',
        'sidebar',
        'menuList'
      ]),
      ...mapState({
        slideClass: state => state.settings.slideClass,
      }),
      classObj() {
        return {
          // [this.slideClass]: true
        };
      },
      activeMenu() {
        const route = this.$route
        const { meta, path } = route
        // if (meta.activeMenu) {
        //   return meta.activeMenu
        // }
        return path
      },
      showLogo() {
        return this.$store.state.settings.sidebarLogo
      },
      variables() {
        return variables
      },
      isCollapse() {
        return !this.sidebar.opened
      }
    },
    data() {
      return {
        activeName: '',
        list: []
      }
    },
    created() {
      let _this = this
      this.$nextTick(() => {
        // 监听页面宽度变化
        let erd = elementResizeDetectorMaker({ strategy: "scroll" })
        erd.listenTo(document.getElementById('topMenu'), function (element) {
          let width = element.offsetWidth
          let newList = []
          let moreItem = {
            path: "/menuMore",
            newChildren: [],
            fullName: '更多 ...',
            vueName: 'moreMenu',
            icon: ''
          }
          let number = Math.floor(width / 116)
          let length = _this.menuList.length
          if (number >= length) return _this.list = _this.menuList
          for (let i = 0; i < length; i++) {
            const item = _this.menuList[i];
            if (i < number - 1) {
              newList.push(item)
            } else {
              moreItem.newChildren.push(item)
            }
          }
          if (moreItem.newChildren.length) newList.push(moreItem)
          _this.list = newList
        });
        this.$nextTick(() => {
          this.setDefault()
        })
      })
    },
    methods: {
      setDefault() {
        const currPath = this.$route.path
        const modelId = this.$route.meta.modelId || ''
        if (!modelId) return
        for (let i = 0; i < this.menuList.length; i++) {
          const e = this.menuList[i];
          let boo = false
          const loop = data => {
            if (modelId === data.id) {
              boo = true
              this.activeName = e.path
              if (e.type === 1) this.$store.commit('user/SET_LEFTMENULIST', e.children || [])
              return
            }
            if (data.children && Array.isArray(data.children) && data.children.length) {
              for (let j = 0; j < data.children.length; j++) {
                loop(data.children[j])
              }
            }
          }
          loop(e)
          if (boo) break
        }
      }
    },
  }
  </script>