yangys
2025-05-27 81060ec4cc3ead9080d4bdb3875920e257583de4
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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<template>
  <div class="avue-tags" v-if="setting.tag" @click="contextmenuFlag = false">
    <!-- tag盒子 -->
    <div
      v-if="contextmenuFlag"
      class="avue-tags__contentmenu"
      :style="{ left: contentmenuX + 'px', top: contentmenuY + 'px' }"
    >
      <div class="item" @click="closeOthersTags">{{ $t('tagsView.closeOthers') }}</div>
      <div class="item" @click="closeAllTags">{{ $t('tagsView.closeAll') }}</div>
      <div class="item" @click="clearCacheTags">{{ $t('tagsView.clearCache') }}</div>
    </div>
    <div class="avue-tags__box">
      <el-tabs
        v-model="active"
        type="card"
        @contextmenu="handleContextmenu"
        :closable="tagLen !== 1"
        @tab-click="openTag"
        @edit="menuTag"
      >
        <el-tab-pane
          v-for="(item, index) in tagList"
          :key="index"
          :label="generateTitle(item)"
          :name="item.fullPath"
        >
          <template #label>
            <span>
              {{ generateTitle(item) }}
              <i
                class="el-icon-refresh"
                :class="{ turn: refresh }"
                @click="handleRefresh"
                v-if="active === item.fullPath"
              ></i>
            </span>
          </template>
        </el-tab-pane>
      </el-tabs>
      <el-dropdown class="avue-tags__menu">
        <el-button type="primary">
          {{ $t('tagsView.menu') }}
          <i class="el-icon-arrow-down el-icon--right"></i>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item @click="openSearch"
              ><i class="icon-fangda" /> {{ $t('tagsView.search') }}</el-dropdown-item
            >
            <el-dropdown-item @click="closeOthersTags"
              ><i class="icon-fangkuai1" /> {{ $t('tagsView.closeOthers') }}
            </el-dropdown-item>
            <el-dropdown-item @click="closeAllTags"
              ><i class="icon-cuowukongxin" /> {{ $t('tagsView.closeAll') }}</el-dropdown-item
            >
            <el-dropdown-item @click="clearCacheTags"
              ><i class="icon-dingwei" /> {{ $t('tagsView.clearCache') }}
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import { clearCache } from '@/api/user';
 
export default {
  name: 'tags',
  data() {
    return {
      refresh: false,
      active: '',
      contentmenuX: '',
      contentmenuY: '',
      contextmenuFlag: false,
    };
  },
  watch: {
    tag: {
      handler(val) {
        this.active = val.fullPath;
      },
      immediate: true,
    },
    contextmenuFlag() {
      window.addEventListener('mousedown', this.watchContextmenu);
    },
  },
  computed: {
    ...mapGetters(['tagWel', 'tagList', 'tag', 'setting']),
    tagLen() {
      return this.tagList.length || 0;
    },
  },
  methods: {
    openSearch() {
      this.$store.commit('SET_IS_SEARCH', true);
    },
    handleRefresh() {
      this.refresh = true;
      this.$store.commit('SET_IS_REFRESH', false);
      setTimeout(() => {
        this.$store.commit('SET_IS_REFRESH', true);
      }, 100);
      setTimeout(() => {
        this.refresh = false;
      }, 500);
    },
    generateTitle(item) {
      return this.$router.$avueRouter.generateTitle({
        ...item,
        ...{
          label: item.name,
        },
      });
    },
    watchContextmenu(event) {
      if (!this.$el.contains(event.target) || event.button !== 0) {
        this.contextmenuFlag = false;
      }
      window.removeEventListener('mousedown', this.watchContextmenu);
    },
    handleContextmenu(event) {
      let target = event.target;
      let flag = false;
      if (target.className.indexOf('el-tabs__item') > -1) flag = true;
      else if (target.parentNode.className.indexOf('el-tabs__item') > -1) {
        target = target.parentNode;
        flag = true;
      }
      if (flag) {
        event.preventDefault();
        event.stopPropagation();
        this.contentmenuX = event.clientX;
        this.contentmenuY = event.clientY;
        this.tagName = target.getAttribute('aria-controls').slice(5);
        this.contextmenuFlag = true;
      }
    },
    menuTag(value, action) {
      if (action === 'remove') {
        let { tag, key } = this.findTag(value);
        this.$store.commit('DEL_TAG', tag);
        if (tag.fullPath === this.tag.fullPath) {
          tag = this.tagList[key - 1];
          this.$router.push({
            path: tag.path,
            query: tag.query,
          });
        }
      }
    },
    openTag(item) {
      let value = item.props.name;
      let { tag } = this.findTag(value);
      this.$router.push({
        path: tag.path,
        query: tag.query,
      });
    },
    findTag(fullPath) {
      let tag = this.tagList.find(item => item.fullPath === fullPath);
      let key = this.tagList.findIndex(item => item.fullPath === fullPath);
      return { tag, key };
    },
    closeOthersTags() {
      this.contextmenuFlag = false;
      this.$store.commit('DEL_TAG_OTHER');
    },
    closeAllTags() {
      this.contextmenuFlag = false;
      this.$store.commit('DEL_ALL_TAG');
      this.$router.push(this.tagWel);
    },
    clearCacheTags() {
      this.$confirm('是否需要清除缓存?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        clearCache().then(() => {
          this.contextmenuFlag = false;
          this.$message.success('清除完毕');
        });
      });
    },
  },
};
</script>