index.vue 8.1 KB
Newer Older
郝先瑞 已提交
1
<script setup lang="ts">
2
import {
郝先瑞 已提交
3 4 5 6
  getCurrentInstance,
  nextTick,
  ref,
  watch,
7
  onMounted,
8
  ComponentInternalInstance
郝先瑞 已提交
9
} from 'vue';
10
import { storeToRefs } from 'pinia';
11

郝先瑞 已提交
12
import path from 'path-browserify';
郝先瑞 已提交
13

14
import { useRoute, useRouter } from 'vue-router';
15

16
import { translateRouteTitleI18n } from '@/utils/i18n';
郝先瑞 已提交
17

18 19
import { usePermissionStore } from '@/store/modules/permission';
import { useTagsViewStore, TagView } from '@/store/modules/tagsView';
20
import ScrollPane from './ScrollPane.vue';
郝先瑞 已提交
21

22
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
郝先瑞 已提交
23
const router = useRouter();
24 25
const route = useRoute();

26 27 28 29 30
const permissionStore = usePermissionStore();
const tagsViewStore = useTagsViewStore();

const { visitedViews } = storeToRefs(tagsViewStore);

31
const selectedTag = ref({});
32
const scrollPaneRef = ref();
33 34
const left = ref(0);
const top = ref(0);
35
const affixTags = ref<TagView[]>([]);
有来技术 已提交
36

37 38 39 40 41 42 43 44
watch(
  route,
  () => {
    addTags();
    moveToCurrentTag();
  },
  {
    //初始化立即执行
45
    immediate: true
46 47
  }
);
有来技术 已提交
48

49 50
const tagMenuVisible = ref(false); // 标签操作菜单显示状态
watch(tagMenuVisible, value => {
郝先瑞 已提交
51
  if (value) {
52
    document.body.addEventListener('click', closeTagMenu);
郝先瑞 已提交
53
  } else {
54
    document.body.removeEventListener('click', closeTagMenu);
郝先瑞 已提交
55
  }
郝先瑞 已提交
56
});
有来技术 已提交
57

58
function filterAffixTags(routes: any[], basePath = '/') {
郝先瑞 已提交
59 60
  let tags: TagView[] = [];

61
  routes.forEach(route => {
郝先瑞 已提交
62 63 64 65 66 67
    if (route.meta && route.meta.affix) {
      const tagPath = path.resolve(basePath, route.path);
      tags.push({
        fullPath: tagPath,
        path: tagPath,
        name: route.name,
68
        meta: { ...route.meta }
郝先瑞 已提交
69 70 71 72 73 74 75 76 77 78 79
      });
    }

    if (route.children) {
      const childTags = filterAffixTags(route.children, route.path);
      if (childTags.length >= 1) {
        tags = tags.concat(childTags);
      }
    }
  });
  return tags;
80
}
有来技术 已提交
81

82
function initTags() {
83
  const tags: TagView[] = filterAffixTags(permissionStore.routes);
84 85
  affixTags.value = tags;
  for (const tag of tags) {
郝先瑞 已提交
86
    // Must have tag name
87
    if (tag.name) {
88
      tagsViewStore.addVisitedView(tag);
郝先瑞 已提交
89 90
    }
  }
91 92 93
}

function addTags() {
郝先瑞 已提交
94
  if (route.name) {
95
    tagsViewStore.addView(route);
郝先瑞 已提交
96
  }
97
}
有来技术 已提交
98

99
function moveToCurrentTag() {
郝先瑞 已提交
100
  nextTick(() => {
101
    for (const r of tagsViewStore.visitedViews) {
102 103
      if (r.path === route.path) {
        scrollPaneRef.value.moveToTarget(r);
郝先瑞 已提交
104
        // when query is different then update
105
        if (r.fullPath !== route.fullPath) {
106
          tagsViewStore.updateVisitedView(route);
郝先瑞 已提交
107 108 109
        }
      }
    }
110
  });
111
}
有来技术 已提交
112

113
function isActive(tag: TagView) {
郝先瑞 已提交
114
  return tag.path === route.path;
115
}
116

117
function isAffix(tag: TagView) {
郝先瑞 已提交
118
  return tag.meta && tag.meta.affix;
119 120 121
}

function isFirstView() {
郝先瑞 已提交
122 123 124
  try {
    return (
      (selectedTag.value as TagView).fullPath ===
125
        tagsViewStore.visitedViews[1].fullPath ||
郝先瑞 已提交
126 127 128 129 130
      (selectedTag.value as TagView).fullPath === '/index'
    );
  } catch (err) {
    return false;
  }
131 132 133
}

function isLastView() {
郝先瑞 已提交
134 135 136
  try {
    return (
      (selectedTag.value as TagView).fullPath ===
137
      tagsViewStore.visitedViews[tagsViewStore.visitedViews.length - 1].fullPath
郝先瑞 已提交
138 139 140 141
    );
  } catch (err) {
    return false;
  }
142 143 144
}

function refreshSelectedTag(view: TagView) {
145
  tagsViewStore.delCachedView(view);
郝先瑞 已提交
146 147
  const { fullPath } = view;
  nextTick(() => {
148
    router.replace({ path: '/redirect' + fullPath }).catch(err => {
郝先瑞 已提交
149 150 151
      console.warn(err);
    });
  });
152
}
153

154
function toLastView(visitedViews: TagView[], view?: any) {
郝先瑞 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
  const latestView = visitedViews.slice(-1)[0];
  if (latestView && latestView.fullPath) {
    router.push(latestView.fullPath);
  } else {
    // now the default is to redirect to the home page if there is no tags-view,
    // you can adjust it according to your needs.
    if (view.name === 'Dashboard') {
      // to reload home page
      router.replace({ path: '/redirect' + view.fullPath });
    } else {
      router.push('/');
    }
  }
168 169 170
}

function closeSelectedTag(view: TagView) {
171
  tagsViewStore.delView(view).then((res: any) => {
郝先瑞 已提交
172 173 174 175
    if (isActive(view)) {
      toLastView(res.visitedViews, view);
    }
  });
176
}
有来技术 已提交
177

178
function closeLeftTags() {
179
  tagsViewStore.delLeftViews(selectedTag.value).then((res: any) => {
郝先瑞 已提交
180 181 182 183 184 185
    if (
      !res.visitedViews.find((item: any) => item.fullPath === route.fullPath)
    ) {
      toLastView(res.visitedViews);
    }
  });
186 187
}
function closeRightTags() {
188
  tagsViewStore.delRightViews(selectedTag.value).then((res: any) => {
郝先瑞 已提交
189 190 191 192 193 194
    if (
      !res.visitedViews.find((item: any) => item.fullPath === route.fullPath)
    ) {
      toLastView(res.visitedViews);
    }
  });
195 196
}

197
function closeOtherTags() {
198
  router.push(selectedTag.value);
199
  tagsViewStore.delOtherViews(selectedTag.value).then(() => {
郝先瑞 已提交
200 201
    moveToCurrentTag();
  });
202 203 204
}

function closeAllTags(view: TagView) {
205
  tagsViewStore.delAllViews().then((res: any) => {
郝先瑞 已提交
206 207
    toLastView(res.visitedViews, view);
  });
208 209
}

210
function openTagMenu(tag: TagView, e: MouseEvent) {
郝先瑞 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223
  const menuMinWidth = 105;
  const offsetLeft = proxy?.$el.getBoundingClientRect().left; // container margin left
  const offsetWidth = proxy?.$el.offsetWidth; // container width
  const maxLeft = offsetWidth - menuMinWidth; // left boundary
  const l = e.clientX - offsetLeft + 15; // 15: margin right

  if (l > maxLeft) {
    left.value = maxLeft;
  } else {
    left.value = l;
  }

  top.value = e.clientY;
224
  tagMenuVisible.value = true;
郝先瑞 已提交
225
  selectedTag.value = tag;
226 227
}

228 229
function closeTagMenu() {
  tagMenuVisible.value = false;
230 231 232
}

function handleScroll() {
233
  closeTagMenu();
234 235 236
}

onMounted(() => {
郝先瑞 已提交
237
  initTags();
郝先瑞 已提交
238
});
239 240
</script>

241
<template>
242 243 244 245
  <scroll-pane
    class="tags-container"
    ref="scrollPaneRef"
    @scroll="handleScroll"
H
haoxr 已提交
246
  >
247 248 249 250 251 252 253 254
    <router-link
      :class="'tags-item ' + (isActive(tag) ? 'active' : '')"
      v-for="tag in visitedViews"
      :key="tag.path"
      :data-path="tag.path"
      :to="{ path: tag.path, query: tag.query }"
      @click.middle="!isAffix(tag) ? closeSelectedTag(tag) : ''"
      @contextmenu.prevent="openTagMenu(tag, $event)"
255
    >
256 257 258 259 260
      {{ translateRouteTitleI18n(tag.meta?.title) }}
      <span
        v-if="!isAffix(tag)"
        class="rounded-[60%] hover:bg-gray-300"
        @click.prevent.stop="closeSelectedTag(tag)"
261
      >
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
        <i-ep-close class="text-[10px]" />
      </span>
    </router-link>
  </scroll-pane>

  <!-- tag标签操作菜单 -->
  <ul
    v-show="tagMenuVisible"
    class="tag-menu"
    :style="{ left: left + 'px', top: top + 'px' }"
  >
    <li @click="refreshSelectedTag(selectedTag)">
      <svg-icon icon-name="refresh" />
      刷新
    </li>
    <li v-if="!isAffix(selectedTag)" @click="closeSelectedTag(selectedTag)">
      <svg-icon icon-name="close" />
      关闭
    </li>
    <li @click="closeOtherTags">
      <svg-icon icon-name="close_other" />
      关闭其它
    </li>
    <li v-if="!isFirstView()" @click="closeLeftTags">
      <svg-icon icon-name="close_left" />
      关闭左侧
    </li>
    <li v-if="!isLastView()" @click="closeRightTags">
      <svg-icon icon-name="close_right" />
      关闭右侧
    </li>
    <li @click="closeAllTags(selectedTag)">
      <svg-icon icon-name="close_all" />
      关闭所有
    </li>
  </ul>
298 299
</template>

郝先瑞 已提交
300
<style lang="scss" scoped>
H
haoxr 已提交
301
.tags-container {
302 303 304 305 306
  height: 34px;
  width: 100%;
  border: 1px solid #eee;
  box-shadow: 0px 1px 1px #eee;

H
haoxr 已提交
307 308 309 310 311 312 313
  .tags-item {
    display: inline-block;
    cursor: pointer;
    border: 1px solid #d8dce5;
    padding: 3px 8px;
    font-size: 12px;
    margin: 4px 0 0 5px;
郝先瑞 已提交
314

H
haoxr 已提交
315 316 317
    &:first-of-type {
      margin-left: 15px;
    }
郝先瑞 已提交
318

H
haoxr 已提交
319 320 321
    &:last-of-type {
      margin-right: 15px;
    }
郝先瑞 已提交
322

H
haoxr 已提交
323 324 325
    &:hover {
      color: var(--el-color-primary);
    }
郝先瑞 已提交
326

H
haoxr 已提交
327 328
    &.active {
      background-color: var(--el-color-primary);
329
      color: #fff;
H
haoxr 已提交
330 331 332 333 334 335 336
      border-color: var(--el-color-primary);
      &::before {
        content: '';
        background: #fff;
        display: inline-block;
        width: 8px;
        height: 8px;
郝先瑞 已提交
337
        border-radius: 50%;
H
haoxr 已提交
338
        margin-right: 5px;
郝先瑞 已提交
339 340 341
      }
    }
  }
342
}
H
haoxr 已提交
343

344
.tag-menu {
H
haoxr 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
  background: #fff;
  z-index: 99;
  position: absolute;
  border-radius: 4px;
  font-size: 12px;
  box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);

  li {
    padding: 8px 16px;
    cursor: pointer;
    &:hover {
      background: #eee;
    }
  }
}
郝先瑞 已提交
360
</style>