TagsView.vue 5.7 KB
Newer Older
P
Pan 已提交
1
<template>
2
  <div class="tags-view-container">
3 4
    <scroll-pane ref="scrollPane" class="tags-view-wrapper">
      <router-link
P
Pan 已提交
5
        v-for="tag in visitedViews"
6 7
        ref="tag"
        :class="isActive(tag)?'active':''"
P
Pan 已提交
8
        :to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath }"
9
        :key="tag.path"
10
        tag="span"
11
        class="tags-view-item"
12
        @click.middle.native="closeSelectedTag(tag)"
13 14
        @contextmenu.prevent.native="openMenu(tag,$event)">
        {{ generateTitle(tag.title) }}
P
Pan 已提交
15
        <span class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)" />
16 17
      </router-link>
    </scroll-pane>
18
    <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
19
      <li @click="refreshSelectedTag(selectedTag)">{{ $t('tagsView.refresh') }}</li>
20 21 22
      <li @click="closeSelectedTag(selectedTag)">{{ $t('tagsView.close') }}</li>
      <li @click="closeOthersTags">{{ $t('tagsView.closeOthers') }}</li>
      <li @click="closeAllTags">{{ $t('tagsView.closeAll') }}</li>
23 24
    </ul>
  </div>
P
Pan 已提交
25 26 27
</template>

<script>
28 29
import ScrollPane from '@/components/ScrollPane'
import { generateTitle } from '@/utils/i18n'
P
Pan 已提交
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
export default {
  components: { ScrollPane },
  data() {
    return {
      visible: false,
      top: 0,
      left: 0,
      selectedTag: {}
    }
  },
  computed: {
    visitedViews() {
      return this.$store.state.tagsView.visitedViews
    }
  },
  watch: {
    $route() {
      this.addViewTags()
      this.moveToCurrentTag()
P
Pan 已提交
50
    },
51 52
    visible(value) {
      if (value) {
53
        document.body.addEventListener('click', this.closeMenu)
54
      } else {
55
        document.body.removeEventListener('click', this.closeMenu)
56 57 58 59 60 61 62 63 64
      }
    }
  },
  mounted() {
    this.addViewTags()
  },
  methods: {
    generateTitle, // generateTitle by vue-i18n
    isActive(route) {
花裤衩 已提交
65
      return route.path === this.$route.path
P
Pan 已提交
66
    },
67
    addViewTags() {
P
tweak  
Pan 已提交
68 69 70
      const { name } = this.$route
      if (name) {
        this.$store.dispatch('addView', this.$route)
71
      }
P
tweak  
Pan 已提交
72
      return false
73
    },
74 75 76 77
    moveToCurrentTag() {
      const tags = this.$refs.tag
      this.$nextTick(() => {
        for (const tag of tags) {
P
Pan 已提交
78
          if (tag.to.path === this.$route.path) {
79
            this.$refs.scrollPane.moveToTarget(tag)
80 81 82 83 84 85

            // when query is different then update
            if (tag.to.fullPath !== this.$route.fullPath) {
              this.$store.dispatch('updateVisitedView', this.$route)
            }

86 87
            break
          }
88
        }
89 90
      })
    },
91 92 93
    refreshSelectedTag(view) {
      this.$store.dispatch('delCachedView', view).then(() => {
        const { fullPath } = view
94 95 96 97
        this.$nextTick(() => {
          this.$router.replace({
            path: '/redirect' + fullPath
          })
98 99 100
        })
      })
    },
101
    closeSelectedTag(view) {
102
      this.$store.dispatch('delView', view).then(({ visitedViews }) => {
103
        if (this.isActive(view)) {
104
          const latestView = visitedViews.slice(-1)[0]
105
          if (latestView) {
106
            this.$router.push(latestView)
107 108 109 110 111 112 113
          } else {
            this.$router.push('/')
          }
        }
      })
    },
    closeOthersTags() {
114
      this.$router.push(this.selectedTag)
115 116 117
      this.$store.dispatch('delOthersViews', this.selectedTag).then(() => {
        this.moveToCurrentTag()
      })
118 119 120 121 122 123 124 125
    },
    closeAllTags() {
      this.$store.dispatch('delAllViews')
      this.$router.push('/')
    },
    openMenu(tag, e) {
      this.visible = true
      this.selectedTag = tag
126 127
      const offsetLeft = this.$el.getBoundingClientRect().left // container margin left
      this.left = e.clientX - offsetLeft + 15 // 15: margin right
128 129 130 131
      this.top = e.clientY
    },
    closeMenu() {
      this.visible = false
P
Pan 已提交
132
    }
P
Pan 已提交
133
  }
134
}
P
Pan 已提交
135 136 137
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
138
.tags-view-container {
139 140 141 142 143
  height: 34px;
  width: 100%;
  background: #fff;
  border-bottom: 1px solid #d8dce5;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
144 145 146 147
  .tags-view-wrapper {
    .tags-view-item {
      display: inline-block;
      position: relative;
148
      cursor: pointer;
149 150 151 152
      height: 26px;
      line-height: 26px;
      border: 1px solid #d8dce5;
      color: #495060;
153
      background: #fff;
154 155 156 157 158 159 160
      padding: 0 8px;
      font-size: 12px;
      margin-left: 5px;
      margin-top: 4px;
      &:first-of-type {
        margin-left: 15px;
      }
161 162 163
      &:last-of-type {
        margin-right: 15px;
      }
164 165 166 167 168 169 170 171 172 173 174 175 176
      &.active {
        background-color: #42b983;
        color: #fff;
        border-color: #42b983;
        &::before {
          content: '';
          background: #fff;
          display: inline-block;
          width: 8px;
          height: 8px;
          border-radius: 50%;
          position: relative;
          margin-right: 2px;
177 178
        }
      }
P
Pan 已提交
179
    }
180 181 182 183
  }
  .contextmenu {
    margin: 0;
    background: #fff;
184
    z-index: 100;
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    position: absolute;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #333;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
    li {
      margin: 0;
      padding: 7px 16px;
      cursor: pointer;
      &:hover {
        background: #eee;
P
Pan 已提交
199 200 201
      }
    }
  }
202
}
P
Pan 已提交
203 204 205
</style>

<style rel="stylesheet/scss" lang="scss">
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
//reset element css of el-icon-close
.tags-view-wrapper {
  .tags-view-item {
    .el-icon-close {
      width: 16px;
      height: 16px;
      vertical-align: 2px;
      border-radius: 50%;
      text-align: center;
      transition: all .3s cubic-bezier(.645, .045, .355, 1);
      transform-origin: 100% 50%;
      &:before {
        transform: scale(.6);
        display: inline-block;
        vertical-align: -3px;
      }
      &:hover {
        background-color: #b4bccc;
        color: #fff;
P
Pan 已提交
225
      }
P
Pan 已提交
226 227
    }
  }
228
}
P
Pan 已提交
229
</style>