Blame view

src/layouts/default/header/components/Breadcrumb.vue 5.84 KB
vben authored
1
<template>
vben authored
2
  <div :class="[prefixCls, `${prefixCls}--${theme}`]">
vben authored
3
    <a-breadcrumb :routes="routes">
4
      <template #itemRender="{ route, routes, paths }">
vben authored
5
        <Icon :icon="route.meta.icon" v-if="getShowBreadCrumbIcon && route.meta.icon" />
6
        <span v-if="!hasRedirect(routes, route)">
7
          {{ t(route.name || route.meta.title) }}
vben authored
8
        </span>
9
        <router-link v-else to="" @click="handleClick(route, paths, $event)">
10
          {{ t(route.name || route.meta.title) }}
vben authored
11
12
13
14
15
16
        </router-link>
      </template>
    </a-breadcrumb>
  </div>
</template>
<script lang="ts">
vben authored
17
  import type { RouteLocationMatched } from 'vue-router';
18
  import type { Menu } from '/@/router/types';
vben authored
19
20
  import { defineComponent, ref, watchEffect } from 'vue';
vben authored
21
22
  import { Breadcrumb } from 'ant-design-vue';
vben authored
23
24
25
  import Icon from '/@/components/Icon';

  import { PageEnum } from '/@/enums/pageEnum';
vben authored
26
27
28

  import { useDesign } from '/@/hooks/web/useDesign';
  import { useRootSetting } from '/@/hooks/setting/useRootSetting';
29
30
31
  import { useGo } from '/@/hooks/web/usePage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useRouter } from 'vue-router';
vben authored
32
33

  import { propTypes } from '/@/utils/propTypes';
34
  import { isString } from '/@/utils/is';
35
  import { filter } from '/@/utils/helper/treeHelper';
36
  import { getMenus } from '/@/router/menus';
vben authored
37
38
39
40
  import { REDIRECT_NAME } from '/@/router/constant';
  import { getAllParentPath } from '/@/router/helper/menuHelper';
vben authored
41
42
  export default defineComponent({
    name: 'LayoutBreadcrumb',
43
    components: { Icon, [Breadcrumb.name]: Breadcrumb },
vben authored
44
    props: {
vben authored
45
      theme: propTypes.oneOf(['dark', 'light']),
vben authored
46
47
48
49
    },
    setup() {
      const routes = ref<RouteLocationMatched[]>([]);
      const { currentRoute } = useRouter();
vben authored
50
51
      const { prefixCls } = useDesign('layout-breadcrumb');
      const { getShowBreadCrumbIcon } = useRootSetting();
vben authored
52
53

      const { t } = useI18n();
54
      watchEffect(async () => {
55
        if (currentRoute.value.name === REDIRECT_NAME) return;
56
        const menus = await getMenus();
57
58
59
60
61
62
63
64
        const routeMatched = currentRoute.value.matched;
        const cur = routeMatched?.[routeMatched.length - 1];
        let path = currentRoute.value.path;

        if (cur && cur?.meta?.currentActiveMenu) {
          path = cur.meta.currentActiveMenu as string;
        }
65
66
67
68

        const parent = getAllParentPath(menus, path);
        const filterMenus = menus.filter((item) => item.path === parent[0]);
        const matched = getMatched(filterMenus, parent) as any;
69
vben authored
70
71
        if (!matched || matched.length === 0) return;
72
        const breadcrumbList = filterItem(matched);
vben authored
73
74
        if (currentRoute.value.meta?.currentActiveMenu) {
75
          breadcrumbList.push(({
76
77
78
            ...currentRoute.value,
            name: currentRoute.value.meta?.title || currentRoute.value.name,
          } as unknown) as RouteLocationMatched);
79
        }
80
        routes.value = breadcrumbList;
vben authored
81
82
      });
83
84
85
86
      function getMatched(menus: Menu[], parent: string[]) {
        const metched: Menu[] = [];
        menus.forEach((item) => {
          if (parent.includes(item.path)) {
87
88
89
90
            metched.push({
              ...item,
              name: item.meta?.title || item.name,
            });
91
92
93
94
95
96
97
98
          }
          if (item.children?.length) {
            metched.push(...getMatched(item.children, parent));
          }
        });
        return metched;
      }
99
100
      function filterItem(list: RouteLocationMatched[]) {
        let resultList = filter(list, (item) => {
101
          const { meta, name } = item;
102
          if (!meta) {
103
            return !!name;
104
105
106
107
108
109
110
111
112
113
114
115
116
          }
          const { title, hideBreadcrumb, hideMenu } = meta;
          if (!title || hideBreadcrumb || hideMenu) {
            return false;
          }
          return true;
        }).filter((item) => !item.meta?.hideBreadcrumb || !item.meta?.hideMenu);

        return resultList;
      }

      function handleClick(route: RouteLocationMatched, paths: string[], e: Event) {
        e?.preventDefault();
117
118
119
        const { children, redirect, meta } = route;

        if (children?.length && !redirect) {
120
121
122
123
124
125
126
127
128
129
130
          e?.stopPropagation();
          return;
        }
        if (meta?.carryParam) {
          return;
        }

        const go = useGo();
        if (redirect && isString(redirect)) {
          go(redirect);
        } else {
131
132
133
134
135
136
          let goPath = '';
          if (paths.length === 1) {
            goPath = paths[0];
          } else {
            const ps = paths.slice(1);
            const lastPath = ps.pop() || '';
137
            goPath = `${lastPath}`;
138
139
140
          }
          goPath = /^\//.test(goPath) ? goPath : `/${goPath}`;
          go(goPath);
141
142
143
144
145
146
147
148
149
150
151
        }
      }

      function hasRedirect(routes: RouteLocationMatched[], route: RouteLocationMatched) {
        if (routes.indexOf(route) === routes.length - 1) {
          return false;
        }
        return true;
      }

      return { routes, t, prefixCls, getShowBreadCrumbIcon, handleClick, hasRedirect };
vben authored
152
153
154
    },
  });
</script>
vben authored
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<style lang="less">
  @prefix-cls: ~'@{namespace}-layout-breadcrumb';

  .@{prefix-cls} {
    display: flex;
    padding: 0 8px;
    align-items: center;

    .ant-breadcrumb-link {
      .anticon {
        margin-right: 4px;
        margin-bottom: 2px;
      }
    }

    &--light {
      .ant-breadcrumb-link {
        color: @breadcrumb-item-normal-color;

        a {
175
          color: rgba(0, 0, 0, 0.65);
vben authored
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

          &:hover {
            color: @primary-color;
          }
        }
      }

      .ant-breadcrumb-separator {
        color: @breadcrumb-item-normal-color;
      }
    }

    &--dark {
      .ant-breadcrumb-link {
        color: rgba(255, 255, 255, 0.6);

        a {
          color: rgba(255, 255, 255, 0.8);

          &:hover {
            color: @white;
          }
        }
      }

      .ant-breadcrumb-separator,
      .anticon {
        color: rgba(255, 255, 255, 0.8);
      }
    }
  }
</style>