Rap 原分销系统代码Web
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

287 строки
7.4KB

  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 }"
  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.routers
  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. tags.push({
  76. fullPath: tagPath,
  77. path: tagPath,
  78. name: route.name,
  79. meta: { ...route.meta }
  80. })
  81. }
  82. if (route.children) {
  83. const tempTags = this.filterAffixTags(route.children, route.path)
  84. if (tempTags.length >= 1) {
  85. tags = [...tags, ...tempTags]
  86. }
  87. }
  88. })
  89. return tags
  90. },
  91. initTags() {
  92. const affixTags = this.affixTags = this.filterAffixTags(this.routes)
  93. for (const tag of affixTags) {
  94. // Must have tag name
  95. if (tag.name) {
  96. this.$store.dispatch('tagsView/addVisitedView', tag)
  97. }
  98. }
  99. },
  100. addTags() {
  101. const { name } = this.$route
  102. if (name) {
  103. this.$store.dispatch('tagsView/addView', this.$route)
  104. }
  105. return false
  106. },
  107. moveToCurrentTag() {
  108. const tags = this.$refs.tag
  109. this.$nextTick(() => {
  110. for (const tag of tags) {
  111. if (tag.to.path === this.$route.path) {
  112. this.$refs.scrollPane.moveToTarget(tag)
  113. // when query is different then update
  114. if (tag.to.fullPath !== this.$route.fullPath) {
  115. this.$store.dispatch('tagsView/updateVisitedView', this.$route)
  116. }
  117. break
  118. }
  119. }
  120. })
  121. },
  122. refreshSelectedTag(view) {
  123. this.$store.dispatch('tagsView/delCachedView', view).then(() => {
  124. const { fullPath } = view
  125. this.$nextTick(() => {
  126. this.$router.replace({
  127. path: '/redirect' + fullPath
  128. })
  129. })
  130. })
  131. },
  132. closeSelectedTag(view) {
  133. this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
  134. if (this.isActive(view)) {
  135. this.toLastView(visitedViews, view)
  136. }
  137. })
  138. },
  139. closeOthersTags() {
  140. this.$router.push(this.selectedTag)
  141. this.$store.dispatch('tagsView/delOthersViews', this.selectedTag).then(() => {
  142. this.moveToCurrentTag()
  143. })
  144. },
  145. closeAllTags(view) {
  146. this.$store.dispatch('tagsView/delAllViews').then(({ visitedViews }) => {
  147. if (this.affixTags.some(tag => tag.path === view.path)) {
  148. return
  149. }
  150. this.toLastView(visitedViews, view)
  151. })
  152. },
  153. toLastView(visitedViews, view) {
  154. const latestView = visitedViews.slice(-1)[0]
  155. if (latestView) {
  156. this.$router.push(latestView)
  157. } else {
  158. // now the default is to redirect to the home page if there is no tags-view,
  159. // you can adjust it according to your needs.
  160. if (view.name === 'Dashboard') {
  161. // to reload home page
  162. this.$router.replace({ path: '/redirect' + view.fullPath })
  163. } else {
  164. this.$router.push('/')
  165. }
  166. }
  167. },
  168. openMenu(tag, e) {
  169. const menuMinWidth = 105
  170. const offsetLeft = this.$el.getBoundingClientRect().left // container margin left
  171. const offsetWidth = this.$el.offsetWidth // container width
  172. const maxLeft = offsetWidth - menuMinWidth // left boundary
  173. const left = e.clientX - offsetLeft + 15 // 15: margin right
  174. if (left > maxLeft) {
  175. this.left = maxLeft
  176. } else {
  177. this.left = left
  178. }
  179. this.top = e.clientY
  180. this.visible = true
  181. this.selectedTag = tag
  182. },
  183. closeMenu() {
  184. this.visible = false
  185. }
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. .tags-view-container {
  191. height: 34px;
  192. width: 100%;
  193. background: #fff;
  194. border-bottom: 1px solid #d8dce5;
  195. box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
  196. .tags-view-wrapper {
  197. .tags-view-item {
  198. display: inline-block;
  199. position: relative;
  200. cursor: pointer;
  201. height: 26px;
  202. line-height: 26px;
  203. border: 1px solid #d8dce5;
  204. color: #495060;
  205. background: #fff;
  206. padding: 0 8px;
  207. font-size: 12px;
  208. margin-left: 5px;
  209. margin-top: 4px;
  210. &:first-of-type {
  211. margin-left: 15px;
  212. }
  213. &:last-of-type {
  214. margin-right: 15px;
  215. }
  216. &.active {
  217. background-color: #42b983;
  218. color: #fff;
  219. border-color: #42b983;
  220. &::before {
  221. content: '';
  222. background: #fff;
  223. display: inline-block;
  224. width: 8px;
  225. height: 8px;
  226. border-radius: 50%;
  227. position: relative;
  228. margin-right: 2px;
  229. }
  230. }
  231. }
  232. }
  233. .contextmenu {
  234. margin: 0;
  235. background: #fff;
  236. z-index: 3000;
  237. position: absolute;
  238. list-style-type: none;
  239. padding: 5px 0;
  240. border-radius: 4px;
  241. font-size: 12px;
  242. font-weight: 400;
  243. color: #333;
  244. box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
  245. li {
  246. margin: 0;
  247. padding: 7px 16px;
  248. cursor: pointer;
  249. &:hover {
  250. background: #eee;
  251. }
  252. }
  253. }
  254. }
  255. </style>
  256. <style lang="scss">
  257. //reset element css of el-icon-close
  258. .tags-view-wrapper {
  259. .tags-view-item {
  260. .el-icon-close {
  261. width: 16px;
  262. height: 16px;
  263. vertical-align: 2px;
  264. border-radius: 50%;
  265. text-align: center;
  266. transition: all .3s cubic-bezier(.645, .045, .355, 1);
  267. transform-origin: 100% 50%;
  268. &:before {
  269. transform: scale(.6);
  270. display: inline-block;
  271. vertical-align: -3px;
  272. }
  273. &:hover {
  274. background-color: #b4bccc;
  275. color: #fff;
  276. }
  277. }
  278. }
  279. }
  280. </style>