TagsView.vue 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <div class="tags-view-container">
  3. <!-- <scroll-pane class='tags-view-wrapper' ref='scrollPane'>
  4. <router-link
  5. ref='tag'
  6. class="tags-view-item"
  7. :class="isActive(tag)?'active':''"
  8. v-for="tag in Array.from(visitedViews)"
  9. :to="tag"
  10. :key="tag.path"
  11. @contextmenu.prevent.native="openMenu(tag,$event)">
  12. {{generateTitle(tag.title)}}
  13. <span class='el-icon-close' @click.prevent.stop='closeSelectedTag(tag)'></span>
  14. </router-link>
  15. </scroll-pane>
  16. <ul class='contextmenu' v-show="visible" :style="{left:left+'px',top:top+'px'}">
  17. <li @click="closeSelectedTag(selectedTag)">{{$t('tagsView.close')}}</li>
  18. <li @click="closeOthersTags">{{$t('tagsView.closeOthers')}}</li>
  19. <li @click="closeAllTags">{{$t('tagsView.closeAll')}}</li>
  20. </ul> -->
  21. <div class="tags-view-wrapper">
  22. <div class="tagsBox">
  23. <div class="tagsOne"
  24. v-if="permission_routers[newIndex].meta"
  25. v-for="(item, i) in permission_routers[newIndex].children"
  26. :key="i"
  27. >
  28. <router-link :to="item.path" v-if="!item.hidden && !item.is_menu">
  29. <p
  30. class="newTags"
  31. :class="activeClass == i ? 'active' : ''"
  32. @click="onClick(i)"
  33. >{{ generateTitle(item.meta.title) }}</p>
  34. </router-link>
  35. </div>
  36. <div v-else class="tagsOne">
  37. <p class="newTags" :class="activeClass == i ? 'active' : ''" @click="onClick(i)">
  38. {{
  39. generateTitle(permission_routers[newIndex].children[0].meta.title)
  40. }}
  41. </p>
  42. </div>
  43. </div>
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. import ScrollPane from '@/components/ScrollPane'
  49. import { generateTitle } from '@/utils/i18n'
  50. import { mapGetters } from "vuex";
  51. export default {
  52. components: { ScrollPane },
  53. props: {
  54. index: Number,
  55. },
  56. data() {
  57. return {
  58. visible: false,
  59. top: 0,
  60. left: 0,
  61. selectedTag: {},
  62. newIndex: 0,
  63. activeClass: 0,
  64. }
  65. },
  66. computed: {
  67. // visitedViews() {
  68. // return this.$store.state.tagsView.visitedViews
  69. // },
  70. ...mapGetters(["permission_routers"])
  71. },
  72. watch: {
  73. // $route() {
  74. // this.addViewTags()
  75. // this.moveToCurrentTag()
  76. // },
  77. // visible(value) {
  78. // if (value) {
  79. // document.body.addEventListener('click', this.closeMenu)
  80. // } else {
  81. // document.body.removeEventListener('click', this.closeMenu)
  82. // }
  83. // }
  84. index(newVal) {
  85. console.log(newVal)
  86. // if (this.newIndex != newVal) {
  87. // this.activeClass = 0;
  88. // }
  89. this.newIndex = newVal;
  90. },
  91. },
  92. created(){
  93. console.log(this.permission_routers)
  94. this.newIndex = 3
  95. },
  96. mounted() {
  97. this.addViewTags()
  98. },
  99. methods: {
  100. generateTitle, // generateTitle by vue-i18n
  101. generateRoute() {
  102. if (this.$route.name) {
  103. return this.$route
  104. }
  105. return false
  106. },
  107. isActive(route) {
  108. return route.path === this.$route.path
  109. },
  110. addViewTags() {
  111. const route = this.generateRoute()
  112. if (!route) {
  113. return false
  114. }
  115. this.$store.dispatch('addVisitedViews', route)
  116. },
  117. moveToCurrentTag() {
  118. const tags = this.$refs.tag
  119. this.$nextTick(() => {
  120. for (const tag of tags) {
  121. if (tag.to.path === this.$route.path) {
  122. this.$refs.scrollPane.moveToTarget(tag.$el)
  123. break
  124. }
  125. }
  126. })
  127. },
  128. closeSelectedTag(view) {
  129. this.$store.dispatch('delVisitedViews', view).then((views) => {
  130. if (this.isActive(view)) {
  131. const latestView = views.slice(-1)[0]
  132. if (latestView) {
  133. this.$router.push(latestView)
  134. } else {
  135. this.$router.push('/')
  136. }
  137. }
  138. })
  139. },
  140. closeOthersTags() {
  141. this.$router.push(this.selectedTag)
  142. this.$store.dispatch('delOthersViews', this.selectedTag).then(() => {
  143. this.moveToCurrentTag()
  144. })
  145. },
  146. closeAllTags() {
  147. this.$store.dispatch('delAllViews')
  148. this.$router.push('/')
  149. },
  150. openMenu(tag, e) {
  151. this.visible = true
  152. this.selectedTag = tag
  153. const offsetLeft = this.$el.getBoundingClientRect().left // container margin left
  154. this.left = e.clientX - offsetLeft + 15 // 15: margin right
  155. this.top = e.clientY
  156. },
  157. closeMenu() {
  158. this.visible = false
  159. },
  160. onClick(index) {
  161. // console.log(index);
  162. this.activeClass = index;
  163. },
  164. }
  165. }
  166. </script>
  167. <style rel="stylesheet/scss" lang="scss" scoped>
  168. .tags-view-container {
  169. height: 60px;
  170. .tags-view-wrapper {
  171. width: 100%;
  172. background: #fff;
  173. height: 50px;
  174. border-bottom: 1px solid #d8dce5;
  175. box-shadow: 0 1px 3px 0 rgba(0, 0, 0, .12), 0 0 3px 0 rgba(0, 0, 0, .04);
  176. position: fixed;
  177. z-index: 1000;
  178. top: 50px;
  179. overflow-x: scroll;
  180. /* 针对缺省样式 (必须的) */
  181. &::-webkit-scrollbar {
  182. width: 6px;
  183. height: 2px;
  184. }
  185. /* 滚动条的滑轨背景颜色 */
  186. &::-webkit-scrollbar-track {
  187. background-color: #fff;
  188. border-radius: 3px;
  189. -moz-border-radius: 3px;
  190. -webkit-border-radius: 3px;
  191. }
  192. /* 滑块颜色 */
  193. &::-webkit-scrollbar-thumb {
  194. background: #fff;
  195. border-radius: 3px;
  196. -moz-border-radius: 3px;
  197. -webkit-border-radius: 3px;
  198. }
  199. /*内层轨道的颜色*/
  200. &::-webkit-scrollbar-track-piece {
  201. background-color: #fff;
  202. border-radius: 3px;
  203. -moz-border-radius: 3px;
  204. -webkit-border-radius: 3px;
  205. }
  206. /* 滑轨两头的监听按钮颜色 */
  207. &::-webkit-scrollbar-button {
  208. background-color: #fff;
  209. width: 0;
  210. height: 0;
  211. }
  212. /* 横向滚动条和纵向滚动条相交处尖角的颜色 */
  213. &::-webkit-scrollbar-corner {
  214. background-color: #fff;
  215. }
  216. .tags-view-item {
  217. display: inline-block;
  218. position: relative;
  219. border: 1px solid #d8dce5;
  220. color: #495060;
  221. background: #fff;
  222. padding: 0 4px 0 8px;
  223. font-size: 12px;
  224. margin: 12px 0 12px 5px;
  225. border-radius: 4px;
  226. height: 28px;
  227. line-height: 28px;
  228. &:first-of-type {
  229. margin-left: 15px;
  230. }
  231. &.active {
  232. background-color: #409efe;
  233. color: #fff;
  234. border-color: #409efe;
  235. &::before {
  236. content: '';
  237. background: #fff;
  238. display: inline-block;
  239. width: 8px;
  240. height: 8px;
  241. border-radius: 50%;
  242. position: relative;
  243. margin-right: 2px;
  244. }
  245. }
  246. }
  247. }
  248. .contextmenu {
  249. margin: 0;
  250. background: #fff;
  251. z-index: 100;
  252. position: absolute;
  253. list-style-type: none;
  254. padding: 5px 0;
  255. border-radius: 4px;
  256. font-size: 12px;
  257. font-weight: 400;
  258. color: #333;
  259. box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, .3);
  260. li {
  261. margin: 0;
  262. padding: 7px 16px;
  263. cursor: pointer;
  264. &:hover {
  265. background: #eee;
  266. }
  267. }
  268. }
  269. .tagsBox {
  270. display: flex;
  271. align-items: center;
  272. height: 100%;
  273. .tagsOne {
  274. .newTags {
  275. border: 1px solid rgba(235, 238, 240, 1);
  276. border-radius: 2px;
  277. padding: 8px 12px;
  278. margin-left: 10px;
  279. font-size: 14px;
  280. }
  281. }
  282. .active {
  283. border: 1px solid rgba(64, 158, 255, 1);
  284. background: rgba(64, 158, 255, 1);
  285. border-radius: 2px;
  286. color: #fff;
  287. }
  288. }
  289. }
  290. </style>
  291. <style rel="stylesheet/scss" lang="scss">
  292. //reset element css of el-icon-close
  293. .tags-view-wrapper {
  294. .tags-view-item {
  295. .el-icon-close {
  296. width: 16px;
  297. height: 16px;
  298. vertical-align: 2px;
  299. border-radius: 50%;
  300. text-align: center;
  301. transition: all .3s cubic-bezier(.645, .045, .355, 1);
  302. transform-origin: 100% 50%;
  303. &:before {
  304. transform: scale(.6);
  305. display: inline-block;
  306. vertical-align: -3px;
  307. }
  308. &:hover {
  309. background-color: #b4bccc;
  310. color: #fff;
  311. }
  312. }
  313. }
  314. }
  315. </style>