history.vue 5.3 KB

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