he wei
2022-11-13 b4816f6294646157b50bb49f1d19eaf306e0ac8c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<template>
  <div class="tabs-contain">
    <div
      class="tab-item active-tab"
      v-if="currentPage && (currentPage != '/empty' || currentPage != '/')"
    >
      <div class="title">
        {{ pageName(currentPage) }}
      </div>
      <i class="el-icon-close icon-close" @click="remove(currentPage)" />
    </div>
    <div class="others">
      <div
        :class="['posA', { 'left-shadow': !isLeft, 'right-shadow': !isEnd }]"
        ref="container"
      >
        <div
          class="inner"
          ref="inner"
          :style="{ transform: 'translateX(' + translateX + 'px)' }"
        >
          <div
            class="tab-item"
            v-for="(page, idx) in list"
            :key="'list_' + idx"
          >
            <div class="title" @click="onTabClick(page)">
              {{ pageName(page) }}
            </div>
            <i class="el-icon-close icon-close" @click="remove(page)" />
          </div>
        </div>
      </div>
    </div>
    <div class="btn-grp" v-if="needScroll">
      <div
        :class="['btn', 'el-icon-caret-left', { disabled: isLeft }]"
        @click="scrollLeft"
      ></div>
      <div
        :class="['btn', 'el-icon-caret-right', { disabled: isEnd }]"
        @click="scrollRight"
      ></div>
    </div>
  </div>
</template>
 
<script>
export default {
  name: "TabsBar",
  props: {
    vnode: {
      type: Function,
      default: () => {},
    },
  },
  watch: {
    $route(newRoute) {
      // debugger;
      this.activePage = newRoute.path;
      const page = this.pageList.find((item) => item.path === newRoute.path);
      if (page) {
        page.fullPath = newRoute.fullPath;
      } else if (!page) {
        // 如果新增的是文件结果或对比页面 则清除同类
        if (newRoute.name == "result" || newRoute.name == 'compare') {
          // 清除缓存
          const clearPages = this.pageList.filter(
            (item) => item.name == "result" || item.name == 'compare'
          );
          this.$bus.$emit(
            "clearCaches",
            clearPages.map((item) => item.cachedKey)
          );
          this.pageList = this.pageList.filter(
            (item) => !clearPages.includes(item)
          );
        }
        /////////////////////
        // 添加新签
        this.pageList.push(this.createPage(newRoute));
      }
      this.$nextTick(() => {
        this.setCachedKey(newRoute);
      });
    },
    pageList(n) {
      let hasFile = n.some((v) => ["compare", "result"].includes(v.name));
      this.$bus.$emit("checkCloseAll", hasFile);
    },
  },
  computed: {
    list() {
      return this.pageList.filter(
        (v) => v.path != this.activePage && v.path != "/" && v.path != "/empty"
      );
    },
    currentPage() {
      // console.log(this.activePage, "activePage====");
      return this.pageList.filter(
        (v) => v.path == this.activePage && v.path != "/" && v.path != "/empty"
      )[0];
    },
    isLeft() {
      return this.translateX == 0;
    },
    isEnd() {
      this.needScroll;
      let containerW = this.$refs.container
        ? this.$refs.container.clientWidth
        : 0;
      let innerW = this.$refs.inner ? this.$refs.inner.clientWidth : 0;
      // console.log(this.translateX, containerW, innerW, '-=-==-=-=-=-');
      return containerW >= innerW + this.translateX;
    },
  },
  data() {
    return {
      pageList: [],
      activePage: "",
      needScroll: false,
      translateX: 0,
    };
  },
  components: {},
  methods: {
    changePage(key) {
      this.activePage = key;
      const page = this.pageList.find((item) => item.path === key);
      this.$router.push(page.fullPath);
    },
    createPage(route) {
      let title = "";
      if (route.name == "result") {
        title = route.params && route.params.filename;
      } else {
        title = route.meta.title;
      }
      return {
        keyPath: route.matched[route.matched.length - 1].path,
        fullPath: route.fullPath,
        loading: false,
        path: route.path,
        name: route.name,
        title,
        query: route.query,
        params: route.params,
      };
    },
    pageName(page) {
      // console.log(page, "pageName");
      return page.title;
    },
    /**
     * 添加监听器
     */
    addListener() {
      window.addEventListener("resize", this.checkScroll);
    },
    /**
     * 移出监听器
     */
    removeListener() {
      window.removeEventListener("resize", this.checkScroll);
    },
    clearCache(page) {
      page._init_ = false;
      this.$bus.$emit("clearCaches", [page.cachedKey]);
    },
    /**
     * 设置页面缓存的key
     * @param route 页面对应的路由
     */
    setCachedKey(route) {
      const page = this.pageList.find((item) => item.path === route.path);
      // page.unclose = route.meta && route.meta.page && (route.meta.page.closable === false)
      if (!page._init_) {
        // TODO
        const vnode = this.vnode();
        page.cachedKey = vnode.key + vnode.componentOptions.Ctor.cid;
        page._init_ = true;
      }
    },
    remove(page) {
      // if (this.pageList.length === 1) {
      //   return this.$message.warning('最后一个了');
      // }
      const { path, title } = page;
 
      let index = this.pageList.findIndex((item) => item.path === path);
 
      let remove = this.pageList.splice(index, 1);
      this.$bus.$emit(
        "clearCaches",
        remove.map((page) => page.cachedKey)
      );
      if (path === this.activePage) {
        this.pageList.sort((a, b) => (b.path == "/empty" ? -1 : 0));
        this.activePage = this.pageList.length ? this.pageList[0].path : "/";
        const nextPage = this.pageList.length
          ? this.pageList[0]
          : { path: "/" };
        if ("result" == nextPage.name) {
          this.$router.push({
            path: "/result/" + nextPage.title,
            query: nextPage.query,
          });
          this.$bus.$emit("chartResize");
        } else {
          this.$router.push({ path: nextPage.path, query: nextPage.query });
          // this.$router.push(this.activePage);
        }
      }
      this.checkScroll();
    },
    closeAll() {
      // 清除缓存
      const clearPages = this.pageList.filter((item) => item.name == "result");
      this.$bus.$emit(
        "clearCaches",
        clearPages.map((item) => item.cachedKey)
      );
      this.pageList = this.pageList.filter(
        (item) => !clearPages.includes(item)
      );
      this.pageList.sort((a, b) => (b.path == "/empty" ? -1 : 0));
      const nextPage = this.pageList.length ? this.pageList[0] : {};
      this.activePage = nextPage.path || "/";
      // 判断跳转
      this.$router.push({ path: nextPage.path, query: nextPage.query });
      // this.$router.push(this.activePage);
    },
    onTabClick(page) {
      // console.log("onTabClick");
      const { path, name, query, title } = page;
      this.activePage = path;
      // const page = this.pageList.find((item) => item.path === path);
      if ("result" == name) {
        this.$router.push({
          path: "/result/" + title,
          query,
        });
        this.$bus.$emit("chartResize");
      } else {
        this.$router.push({ path, query });
        // this.$router.push(this.activePage);
      }
    },
    // onClose(path) {
    //   this.pageList = this.pageList.filter((v) => v.path != path);
    //   this.activePage = this.pageList.length ? this.pageList[0].path : "";
    // },
    checkScroll() {
      this.$nextTick(() => {
        let container = this.$refs.container;
        let inner = this.$refs.inner;
        let containerW = container.clientWidth;
        let innerW = inner.clientWidth;
        this.needScroll = containerW < innerW;
        if (this.translateX < 0 && containerW - this.translateX > innerW) {
          this.translateX = containerW - innerW < 0 ? containerW - innerW : 0;
        }
      });
    },
    scrollLeft() {
      if (this.isLeft) {
        return false;
      }
      let innerW = this.$refs.inner.clientWidth;
      let containerW = this.$refs.container.clientWidth;
      let x = containerW > 600 ? containerW * 0.9 : containerW;
      if (this.translateX + x >= 0) {
        x = -this.translateX;
      }
      this.translateX += x;
    },
    scrollRight() {
      if (this.isEnd) {
        return false;
      }
      let containerW = this.$refs.container.clientWidth;
      let innerW = this.$refs.inner.clientWidth;
      let x = containerW > 600 ? containerW * 0.9 : containerW;
      if (containerW - this.translateX + x > innerW) {
        x = innerW + this.translateX - containerW;
      }
      this.translateX -= x;
    },
  },
 
  mounted() {
    this.addListener();
    this.$bus.$on("checkScroll", this.checkScroll);
    this.$bus.$on("closeFile", () => {
      if (this.currentPage) {
        this.remove(this.currentPage);
      }
    });
    this.$bus.$on("closeAll", this.closeAll);
  },
};
</script>
 
<style lang="less" scoped>
.tabs-contain {
  height: 32px;
  box-shadow: 0 1px 4px -2px #333;
  align-items: center;
  background: #ddd;
  display: flex;
  .tab-item {
    position: relative;
    align-self: flex-end;
    border: 1px solid #1aafea;
    border-radius: 5px 5px 0 0;
    padding: 6px 26px 0 4px;
    cursor: pointer;
    &.active-tab {
      background: #1aafea;
    }
    .icon-close {
      position: absolute;
      width: 14px;
      height: 14px;
      font-size: 10px;
      line-height: 14px;
      text-align: center;
      right: 4px;
      top: 4px;
      border-radius: 50%;
      background: rgba(122, 122, 122, 0.6);
      color: #fff;
      &:hover {
        background: rgba(255, 39, 39, 0.6);
      }
    }
  }
  .others {
    flex: 1;
    align-self: stretch;
    position: relative;
    .posA {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      overflow: hidden;
      display: flex;
      align-items: flex-end;
      &.left-shadow::before {
        content: "";
        position: absolute;
        left: 0;
        top: 0;
        bottom: 0;
        box-shadow: 0 0 1px #333;
      }
      &.right-shadow::after {
        content: "";
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        box-shadow: 0 0 1px #333;
      }
      .inner {
        white-space: nowrap;
        text-align: left;
        transition: transform 300ms;
        .tab-item {
          display: inline-block;
          border: 1px #ccc solid;
          // min-width: 160px;
        }
      }
    }
  }
  .btn-grp {
    align-self: center;
    .btn {
      display: inline-block;
      cursor: pointer;
      font-size: 22px;
      &.disabled {
        background: #eee;
        color: #aaa;
        cursor: not-allowed;
      }
    }
  }
}
</style>