history.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div class="router-history">
  3. <el-tabs
  4. :closable="!(historys.length==1&&this.$route.name==defaultRouter)"
  5. @contextmenu.prevent.native="openContextMenu($event)"
  6. @tab-click="changeTab"
  7. @tab-remove="removeTab"
  8. type="card"
  9. v-model="activeValue"
  10. >
  11. <el-tab-pane
  12. :key="item.name"
  13. :label="item.meta.title"
  14. :name="item.name"
  15. :tab="item"
  16. v-for="item in historys"
  17. ></el-tab-pane>
  18. </el-tabs>
  19. <!--自定义右键菜单html代码-->
  20. <ul :style="{left:left+'px',top:top+'px'}" class="contextmenu" v-show="contextMenuVisible">
  21. <li @click="closeAll">关闭所有</li>
  22. <li @click="closeLeft">关闭左侧</li>
  23. <li @click="closeRight">关闭右侧</li>
  24. <li @click="closeOther">关闭其他</li>
  25. </ul>
  26. </div>
  27. </template>
  28. <script>
  29. import {mapGetters} from "vuex"
  30. export default {
  31. name: 'HistoryComponent',
  32. data() {
  33. return {
  34. historys: [],
  35. activeValue: '',
  36. contextMenuVisible: false,
  37. left: 0,
  38. top: 0,
  39. isCollapse: false,
  40. isMobile: false,
  41. rightActive: ''
  42. }
  43. },
  44. computed:{
  45. ...mapGetters("user",["userInfo"]),
  46. defaultRouter(){
  47. return this.userInfo.authority.defaultRouter
  48. }
  49. },
  50. created() {
  51. this.activeValue = this.defaultRouter
  52. this.$bus.on('mobile', isMobile => {
  53. this.isMobile = isMobile
  54. })
  55. this.$bus.on('collapse', isCollapse => {
  56. this.isCollapse = isCollapse
  57. })
  58. const initHistorys = [
  59. {
  60. name: this.defaultRouter,
  61. meta: {
  62. title: '首页'
  63. }
  64. }
  65. ]
  66. this.historys =
  67. JSON.parse(sessionStorage.getItem('historys')) || initHistorys
  68. this.setTab(this.$route)
  69. },
  70. beforeDestroy() {
  71. this.$bus.off('collapse')
  72. this.$bus.off('mobile')
  73. },
  74. methods: {
  75. openContextMenu(e) {
  76. if (this.historys.length == 1 && this.$route.name == this.defaultRouter) {
  77. return false
  78. }
  79. if (e.srcElement.id) {
  80. this.contextMenuVisible = true
  81. let width
  82. if (this.isCollapse) {
  83. width = 54
  84. } else {
  85. width = 220
  86. }
  87. if (this.isMobile) {
  88. width = 0
  89. }
  90. this.left = e.clientX - width
  91. this.top = e.clientY + 10
  92. this.rightActive = e.srcElement.id.split('-')[1]
  93. }
  94. },
  95. closeAll() {
  96. this.historys = [
  97. {
  98. name: this.defaultRouter,
  99. meta: {
  100. title: '首页'
  101. }
  102. }
  103. ]
  104. this.$router.push({ name: this.defaultRouter })
  105. this.contextMenuVisible = false
  106. sessionStorage.setItem('historys', JSON.stringify(this.historys))
  107. },
  108. closeLeft() {
  109. let right
  110. const rightIndex = this.historys.findIndex(
  111. item => {
  112. if(item.name == this.rightActive){
  113. right = item
  114. }
  115. return item.name == this.rightActive
  116. }
  117. )
  118. const activeIndex = this.historys.findIndex(
  119. item => item.name == this.activeValue
  120. )
  121. this.historys.splice(0, rightIndex)
  122. if (rightIndex > activeIndex) {
  123. this.$router.push(right)
  124. }
  125. sessionStorage.setItem('historys', JSON.stringify(this.historys))
  126. },
  127. closeRight() {
  128. let right
  129. const leftIndex = this.historys.findIndex(
  130. item => {
  131. if(item.name == this.rightActive){
  132. right = item
  133. }
  134. return item.name == this.rightActive
  135. }
  136. )
  137. const activeIndex = this.historys.findIndex(
  138. item => item.name == this.activeValue
  139. )
  140. this.historys.splice(leftIndex + 1, this.historys.length)
  141. if (leftIndex < activeIndex) {
  142. this.$router.push(right)
  143. }
  144. sessionStorage.setItem('historys', JSON.stringify(this.historys))
  145. },
  146. closeOther() {
  147. let right
  148. this.historys = this.historys.filter(
  149. item => {
  150. if(item.name == this.rightActive){
  151. right = item
  152. }
  153. return item.name == this.rightActive
  154. }
  155. )
  156. this.$router.push(right)
  157. sessionStorage.setItem('historys', JSON.stringify(this.historys))
  158. },
  159. setTab(route) {
  160. if (!this.historys.some(item => item.name == route.name)) {
  161. const obj = {}
  162. obj.name = route.name
  163. obj.meta = route.meta
  164. obj.query = route.query
  165. obj.params = route.params
  166. this.historys.push(obj)
  167. }
  168. this.activeValue = this.$route.name
  169. },
  170. changeTab(component) {
  171. const tab = component.$attrs.tab
  172. this.$router.push({ name: tab.name,query:tab.query,params:tab.params })
  173. },
  174. removeTab(tab) {
  175. const index = this.historys.findIndex(item => item.name == tab)
  176. if (this.$route.name == tab) {
  177. if (this.historys.length == 1) {
  178. this.$router.push({ name: this.defaultRouter })
  179. } else {
  180. if (index < this.historys.length - 1) {
  181. this.$router.push({ name: this.historys[index + 1].name,query:this.historys[index + 1].query,params:this.historys[index + 1].params })
  182. } else {
  183. this.$router.push({ name: this.historys[index - 1].name,query:this.historys[index - 1].query,params:this.historys[index - 1].params })
  184. }
  185. }
  186. }
  187. this.historys.splice(index, 1)
  188. }
  189. },
  190. watch: {
  191. contextMenuVisible() {
  192. if (this.contextMenuVisible) {
  193. document.body.addEventListener('click', () => {
  194. this.contextMenuVisible = false
  195. })
  196. } else {
  197. document.body.removeEventListener('click', () => {
  198. this.contextMenuVisible = false
  199. })
  200. }
  201. },
  202. $route(to) {
  203. this.historys = this.historys.filter(item => !item.meta.hidden)
  204. this.setTab(to)
  205. sessionStorage.setItem('historys', JSON.stringify(this.historys))
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss">
  211. .contextmenu {
  212. width: 100px;
  213. margin: 0;
  214. border: 1px solid #ccc;
  215. background: #fff;
  216. z-index: 3000;
  217. position: absolute;
  218. list-style-type: none;
  219. padding: 5px 0;
  220. border-radius: 4px;
  221. font-size: 14px;
  222. color: #333;
  223. box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.2);
  224. }
  225. .contextmenu li {
  226. margin: 0;
  227. padding: 7px 16px;
  228. }
  229. .contextmenu li:hover {
  230. background: #f2f2f2;
  231. cursor: pointer;
  232. }
  233. </style>