c982ff9af02f66c93aa8f6baa9e2209ead9bd9f4.svn-base 7.7 KB

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