b818a5f3f6c49a4cb7be1ced2a8b6db87327636e.svn-base 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <template>
  2. <div id="tags-view-container" class="tags-view-container">
  3. <scroll-pane ref="scrollPane" class="tags-view-wrapper">
  4. <router-link
  5. v-for="tag in visitedViews"
  6. ref="tag"
  7. :key="tag.path"
  8. :class="isActive(tag)?'active':''"
  9. :to="{ path: tag.path, query: tag.query, fullPath: tag.fullPath,id:tag.id }"
  10. tag="span"
  11. class="tags-view-item"
  12. @click.middle.native="closeSelectedTag(tag)"
  13. @contextmenu.prevent.native="openMenu(tag,$event)"
  14. >
  15. {{ tag.title }}
  16. <span v-if="!tag.meta.affix" class="el-icon-close" @click.prevent.stop="closeSelectedTag(tag)" />
  17. </router-link>
  18. </scroll-pane>
  19. <ul v-show="visible" :style="{left:left+'px',top:top+'px'}" class="contextmenu">
  20. <li @click="refreshSelectedTag(selectedTag)">刷新</li>
  21. <li v-if="!(selectedTag.meta&&selectedTag.meta.affix)" @click="closeSelectedTag(selectedTag)">关闭</li>
  22. <li @click="closeOthersTags">关闭其他</li>
  23. <li @click="closeAllTags(selectedTag)">关闭所有</li>
  24. </ul>
  25. </div>
  26. </template>
  27. <script>
  28. import ScrollPane from './ScrollPane'
  29. import path from 'path'
  30. export default {
  31. components: { ScrollPane },
  32. data() {
  33. return {
  34. visible: false,
  35. top: 0,
  36. left: 0,
  37. selectedTag: {},
  38. affixTags: []
  39. }
  40. },
  41. computed: {
  42. visitedViews() {
  43. return this.$store.state.tagsView.visitedViews
  44. },
  45. routes() {
  46. return this.$store.state.permission.routes
  47. }
  48. },
  49. watch: {
  50. $route() {
  51. this.addTags()
  52. this.moveToCurrentTag()
  53. },
  54. visible(value) {
  55. if (value) {
  56. document.body.addEventListener('click', this.closeMenu)
  57. } else {
  58. document.body.removeEventListener('click', this.closeMenu)
  59. }
  60. }
  61. },
  62. mounted() {
  63. this.initTags()
  64. this.addTags()
  65. },
  66. methods: {
  67. isActive(route) {
  68. return route.path === this.$route.path
  69. },
  70. filterAffixTags(routes, basePath = '/') {
  71. let tags = []
  72. routes.forEach(route => {
  73. if (route.meta && route.meta.affix) {
  74. const tagPath = path.resolve(basePath, route.path)
  75. console.log(route)
  76. tags.push({
  77. fullPath: tagPath,
  78. path: tagPath,
  79. name: route.name,
  80. id: route.id,
  81. meta: { ...route.meta }
  82. })
  83. }
  84. if (route.children) {
  85. const tempTags = this.filterAffixTags(route.children, route.path)
  86. if (tempTags.length >= 1) {
  87. tags = [...tags, ...tempTags]
  88. }
  89. }
  90. })
  91. return tags
  92. },
  93. initTags() {
  94. const affixTags = this.affixTags = this.filterAffixTags(this.routes)
  95. for (const tag of affixTags) {
  96. // Must have tag name
  97. if (tag.name) {
  98. this.$store.dispatch('tagsView/addVisitedView', tag)
  99. }
  100. }
  101. },
  102. addTags() {
  103. const { name } = this.$route
  104. if (name) {
  105. this.$store.dispatch('tagsView/addView', this.$route)
  106. }
  107. return false
  108. },
  109. moveToCurrentTag() {
  110. const tags = this.$refs.tag
  111. this.$nextTick(() => {
  112. for (const tag of tags) {
  113. if (tag.to.path === this.$route.path) {
  114. this.$refs.scrollPane.moveToTarget(tag)
  115. // when query is different then update
  116. if (tag.to.fullPath !== this.$route.fullPath) {
  117. this.$store.dispatch('tagsView/updateVisitedView', this.$route)
  118. }
  119. break
  120. }
  121. }
  122. })
  123. },
  124. refreshSelectedTag(view) {
  125. this.$store.dispatch('tagsView/delCachedView', view).then(() => {
  126. const { fullPath } = view
  127. this.$nextTick(() => {
  128. this.$router.replace({
  129. path: '/redirect' + fullPath
  130. })
  131. })
  132. })
  133. },
  134. closeSelectedTag(view) {
  135. this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
  136. if (this.isActive(view)) {
  137. this.toLastView(visitedViews, view)
  138. }
  139. })
  140. },
  141. closeOthersTags() {
  142. this.$router.push(this.selectedTag)
  143. this.$store.dispatch('tagsView/delOthersViews', this.selectedTag).then(() => {
  144. this.moveToCurrentTag()
  145. })
  146. },
  147. closeAllTags(view) {
  148. this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
  149. if (this.affixTags.some(tag => tag.path === view.path)) {
  150. return
  151. }
  152. this.toLastView(visitedViews, view)
  153. })
  154. },
  155. toLastView(visitedViews, view) {
  156. const latestView = visitedViews.slice(-1)[0]
  157. if (latestView) {
  158. this.$router.push(latestView)
  159. } else {
  160. // now the default is to redirect to the home page if there is no tags-view,
  161. // you can adjust it according to your needs.
  162. if (view.name === 'Dashboard') {
  163. // to reload home page
  164. this.$router.replace({ path: '/redirect' + view.fullPath })
  165. } else {
  166. this.$router.push('/')
  167. }
  168. }
  169. },
  170. openMenu(tag, e) {
  171. const menuMinWidth = 105
  172. const offsetLeft = this.$el.getBoundingClientRect().left // container margin left
  173. const offsetWidth = this.$el.offsetWidth // container width
  174. const maxLeft = offsetWidth - menuMinWidth // left boundary
  175. const left = e.clientX - offsetLeft + 15 // 15: margin right
  176. if (left > maxLeft) {
  177. this.left = maxLeft
  178. } else {
  179. this.left = left
  180. }
  181. this.top = e.clientY
  182. this.visible = true
  183. this.selectedTag = tag
  184. },
  185. closeMenu() {
  186. this.visible = false
  187. }
  188. }
  189. }
  190. </script>
  191. <style lang="scss" scoped>
  192. .tags-view-container {
  193. height: 34px;
  194. width: 100%;
  195. background: #fff;
  196. border-bottom: 1px solid #d8dce5;
  197. box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
  198. .tags-view-wrapper {
  199. .tags-view-item {
  200. display: inline-block;
  201. position: relative;
  202. cursor: pointer;
  203. height: 26px;
  204. line-height: 26px;
  205. border: 1px solid #d8dce5;
  206. color: #495060;
  207. background: #fff;
  208. padding: 0 8px;
  209. font-size: 12px;
  210. margin-left: 5px;
  211. margin-top: 4px;
  212. &:first-of-type {
  213. margin-left: 15px;
  214. }
  215. &:last-of-type {
  216. margin-right: 15px;
  217. }
  218. &.active {
  219. background-color: #409EFF;
  220. color: #fff;
  221. border-color: #409EFF;
  222. &::before {
  223. content: '';
  224. background: #fff;
  225. display: inline-block;
  226. width: 8px;
  227. height: 8px;
  228. border-radius: 50%;
  229. position: relative;
  230. margin-right: 2px;
  231. }
  232. }
  233. }
  234. }
  235. .contextmenu {
  236. margin: 0;
  237. background: #fff;
  238. z-index: 3000;
  239. position: absolute;
  240. list-style-type: none;
  241. padding: 5px 0;
  242. border-radius: 4px;
  243. font-size: 12px;
  244. font-weight: 400;
  245. color: #333;
  246. box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
  247. li {
  248. margin: 0;
  249. padding: 7px 16px;
  250. cursor: pointer;
  251. &:hover {
  252. background: #eee;
  253. }
  254. }
  255. }
  256. }
  257. </style>
  258. <style lang="scss">
  259. //reset element css of el-icon-close
  260. .tags-view-wrapper {
  261. .tags-view-item {
  262. .el-icon-close {
  263. width: 16px;
  264. height: 16px;
  265. vertical-align: 2px;
  266. border-radius: 50%;
  267. text-align: center;
  268. transition: all .3s cubic-bezier(.645, .045, .355, 1);
  269. transform-origin: 100% 50%;
  270. &:before {
  271. transform: scale(.6);
  272. display: inline-block;
  273. vertical-align: -3px;
  274. }
  275. &:hover {
  276. background-color: #b4bccc;
  277. color: #fff;
  278. }
  279. }
  280. }
  281. }
  282. </style>