Blame view

src/layouts/default/header/components/Breadcrumb.vue 5.53 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
17
18
19
20
21
22
23
24
25
26
27
          {{ t(route.meta.title) }}
        </router-link>
      </template>
    </a-breadcrumb>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, toRaw, watchEffect } from 'vue';
  import { useI18n } from 'vue-i18n';

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

  import { HomeOutlined } from '@ant-design/icons-vue';
  import { PageEnum } from '/@/enums/pageEnum';
vben authored
28
29
30
31
32

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

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

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

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

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

        if (currentRoute.value.meta?.currentActiveMenu) {
          filterBreadcrumbList.push((currentRoute.value as unknown) as RouteLocationMatched);
        }
        // routes.value = filterBreadcrumbList.length === 1 ? [] : filterBreadcrumbList;
        routes.value = filterBreadcrumbList;
vben authored
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
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
      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
146
147
148
    },
  });
</script>
vben authored
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<style lang="less">
  @import (reference) '../../../../design/index.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>