Blame view

src/layouts/default/header/components/Breadcrumb.vue 5.48 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)">
vben authored
7
8
          {{ t(route.meta.title) }}
        </span>
9
        <router-link v-else to="" @click="handleClick(route, paths, $event)">
vben authored
10
11
12
13
14
15
16
          {{ t(route.meta.title) }}
        </router-link>
      </template>
    </a-breadcrumb>
  </div>
</template>
<script lang="ts">
vben authored
17
18
  import type { RouteLocationMatched } from 'vue-router';
vben authored
19
  import { defineComponent, ref, toRaw, watchEffect } from 'vue';
vben authored
20
  import { Breadcrumb } from 'ant-design-vue';
vben authored
21
22
23
24
25
26
27
28
  import { useI18n } from 'vue-i18n';

  import { useRouter } from 'vue-router';
  import { filter } from '/@/utils/helper/treeHelper';
  import { REDIRECT_NAME } from '/@/router/constant';
  import Icon from '/@/components/Icon';

  import { PageEnum } from '/@/enums/pageEnum';
vben authored
29
30
31
32
33

  import { useDesign } from '/@/hooks/web/useDesign';
  import { useRootSetting } from '/@/hooks/setting/useRootSetting';

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

      const { t } = useI18n();
      watchEffect(() => {
51
52
        if (currentRoute.value.name === REDIRECT_NAME) return;
53
        const matched = currentRoute.value?.matched;
vben authored
54
55
        if (!matched || matched.length === 0) return;
56
        let breadcrumbList = filterItem(toRaw(matched));
vben authored
57
58
59
60
61
62

        const filterBreadcrumbList = breadcrumbList.filter(
          (item) => item.path !== PageEnum.BASE_HOME
        );

        if (filterBreadcrumbList.length === breadcrumbList.length) {
vben authored
63
          filterBreadcrumbList.unshift(({
vben authored
64
65
66
            path: PageEnum.BASE_HOME,
            meta: {
              title: t('layout.header.home'),
67
              isLink: true,
vben authored
68
            },
vben authored
69
          } as unknown) as RouteLocationMatched);
vben authored
70
        }
71
72
73
74
75
76

        if (currentRoute.value.meta?.currentActiveMenu) {
          filterBreadcrumbList.push((currentRoute.value as unknown) as RouteLocationMatched);
        }
        // routes.value = filterBreadcrumbList.length === 1 ? [] : filterBreadcrumbList;
        routes.value = filterBreadcrumbList;
vben authored
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
134
135
136
137
138
139
140
141
142
143
144
145
146
      function filterItem(list: RouteLocationMatched[]) {
        let resultList = filter(list, (item) => {
          const { meta } = item;

          if (!meta) {
            return false;
          }
          const { title, hideBreadcrumb, hideMenu } = meta;
          if (!title || hideBreadcrumb || hideMenu) {
            return false;
          }

          return true;
        }).filter((item) => !item.meta?.hideBreadcrumb || !item.meta?.hideMenu);

        resultList = resultList.filter((item) => item.path !== PageEnum.BASE_HOME);
        return resultList;
      }

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

        // const isParent =
        //   components?.default?.name === 'DefaultLayout' || (components?.default as any)?.parentView;

        if (
          children?.length &&
          !redirect
          // && !isParent
        ) {
          e?.stopPropagation();
          return;
        }
        if (meta?.carryParam) {
          return;
        }

        const go = useGo();
        if (redirect && isString(redirect)) {
          go(redirect);
        } else {
          const ps = paths.slice(1);
          const lastPath = ps.pop() || '';
          const parentPath = ps.pop() || '';
          let path = `${parentPath}/${lastPath}`;
          path = /^\//.test(path) ? path : `/${path}`;
          go(path);
        }
      }

      function hasRedirect(routes: RouteLocationMatched[], route: RouteLocationMatched) {
        if (route?.meta?.isLink) {
          return true;
        }

        if (routes.indexOf(route) === routes.length - 1) {
          return false;
        }
        return true;
      }

      return { routes, t, prefixCls, getShowBreadCrumbIcon, handleClick, hasRedirect };
vben authored
147
148
149
    },
  });
</script>
vben authored
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<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 {
170
          color: rgba(0, 0, 0, 0.65);
vben authored
171
172
173
174
175
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

          &: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>