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

<script>
19 20
import ScrollPane from '@/components/ScrollPane'
import { generateTitle } from '@/utils/i18n'
P
Pan 已提交
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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 已提交
41
    },
42 43
    visible(value) {
      if (value) {
44
        document.body.addEventListener('click', this.closeMenu)
45
      } else {
46
        document.body.removeEventListener('click', this.closeMenu)
47 48 49 50 51 52 53 54 55 56 57
      }
    }
  },
  mounted() {
    this.addViewTags()
  },
  methods: {
    generateTitle, // generateTitle by vue-i18n
    generateRoute() {
      if (this.$route.name) {
        return this.$route
P
Pan 已提交
58
      }
59
      return false
P
Pan 已提交
60
    },
61 62
    isActive(route) {
      return route.path === this.$route.path || route.name === this.$route.name
P
Pan 已提交
63
    },
64 65 66
    addViewTags() {
      const route = this.generateRoute()
      if (!route) {
67 68
        return false
      }
69
      this.$store.dispatch('addVisitedViews', route)
70
    },
71 72 73 74 75 76 77 78
    moveToCurrentTag() {
      const tags = this.$refs.tag
      this.$nextTick(() => {
        for (const tag of tags) {
          if (tag.to === this.$route.path) {
            this.$refs.scrollPane.moveToTarget(tag.$el)
            break
          }
79
        }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      })
    },
    closeSelectedTag(view) {
      this.$store.dispatch('delVisitedViews', view).then((views) => {
        if (this.isActive(view)) {
          const latestView = views.slice(-1)[0]
          if (latestView) {
            this.$router.push(latestView.path)
          } else {
            this.$router.push('/')
          }
        }
      })
    },
    closeOthersTags() {
      this.$router.push(this.selectedTag.path)
96 97 98
      this.$store.dispatch('delOthersViews', this.selectedTag).then(() => {
        this.moveToCurrentTag()
      })
99 100 101 102 103 104 105 106 107 108 109 110 111
    },
    closeAllTags() {
      this.$store.dispatch('delAllViews')
      this.$router.push('/')
    },
    openMenu(tag, e) {
      this.visible = true
      this.selectedTag = tag
      this.left = e.clientX
      this.top = e.clientY
    },
    closeMenu() {
      this.visible = false
P
Pan 已提交
112
    }
P
Pan 已提交
113
  }
114
}
P
Pan 已提交
115 116 117
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
118 119 120 121 122 123 124 125 126 127 128 129 130
.tags-view-container {
  .tags-view-wrapper {
    background: #fff;
    height: 34px;
    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);
    .tags-view-item {
      display: inline-block;
      position: relative;
      height: 26px;
      line-height: 26px;
      border: 1px solid #d8dce5;
      color: #495060;
131
      background: #fff;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
      padding: 0 8px;
      font-size: 12px;
      margin-left: 5px;
      margin-top: 4px;
      &:first-of-type {
        margin-left: 15px;
      }
      &.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;
152 153
        }
      }
P
Pan 已提交
154
    }
155 156 157 158
  }
  .contextmenu {
    margin: 0;
    background: #fff;
159
    z-index: 100;
160 161 162 163 164 165 166 167 168 169 170 171 172 173
    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 已提交
174 175 176
      }
    }
  }
177
}
P
Pan 已提交
178 179 180
</style>

<style rel="stylesheet/scss" lang="scss">
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
//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 已提交
200
      }
P
Pan 已提交
201 202
    }
  }
203
}
P
Pan 已提交
204
</style>