TabLayout.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <template>
  2. <global-layout @dynamicRouterShow="dynamicRouterShow">
  3. <contextmenu :itemList="menuItemList" :visible.sync="menuVisible" @select="onMenuSelect"/>
  4. <a-tabs
  5. @contextmenu.native="e => onContextmenu(e)"
  6. v-if="multipage"
  7. :active-key="activePage"
  8. class="tab-layout-tabs"
  9. style="height:52px"
  10. :hide-add="true"
  11. type="editable-card"
  12. @change="changePage"
  13. @edit="editPage">
  14. <a-tab-pane :id="page.fullPath" :key="page.fullPath" v-for="page in pageList">
  15. <span slot="tab" :pagekey="page.fullPath">{{ page.meta.title }}</span>
  16. </a-tab-pane>
  17. </a-tabs>
  18. <div style="margin: 12px 12px 0;">
  19. <transition name="page-toggle">
  20. <keep-alive v-if="multipage">
  21. <router-view/>
  22. </keep-alive>
  23. <router-view v-else/>
  24. </transition>
  25. </div>
  26. </global-layout>
  27. </template>
  28. <script>
  29. import GlobalLayout from '@/components/page/GlobalLayout'
  30. import Contextmenu from '@/components/menu/Contextmenu'
  31. import {mixin, mixinDevice} from '@/utils/mixin.js'
  32. const indexKey = '/dashboard/analysis'
  33. export default {
  34. name: 'TabLayout',
  35. components: {
  36. GlobalLayout,
  37. Contextmenu
  38. },
  39. mixins: [mixin, mixinDevice],
  40. data() {
  41. return {
  42. pageList: [],
  43. linkList: [],
  44. activePage: '',
  45. menuVisible: false,
  46. menuItemList: [
  47. {key: '1', icon: 'arrow-left', text: '关闭左侧'},
  48. {key: '2', icon: 'arrow-right', text: '关闭右侧'},
  49. {key: '3', icon: 'close', text: '关闭其它'}
  50. ]
  51. }
  52. },
  53. computed: {
  54. multipage() {
  55. //判断如果是手机模式,自动切换为单页面模式
  56. if (this.isMobile()) {
  57. return false
  58. } else {
  59. return this.$store.state.app.multipage
  60. }
  61. }
  62. },
  63. created() {
  64. if (this.$route.path != indexKey) {
  65. this.pageList.push({
  66. name: 'dashboard-analysis',
  67. path: indexKey,
  68. fullPath: indexKey,
  69. meta: {
  70. icon: 'dashboard',
  71. title: '首页'
  72. }
  73. })
  74. this.linkList.push(indexKey)
  75. }
  76. this.pageList.push(this.$route)
  77. this.linkList.push(this.$route.fullPath)
  78. this.activePage = this.$route.fullPath
  79. if(!this.linkList.includes("/account/stepForm")){
  80. localStorage.removeItem("step")
  81. }
  82. },
  83. watch: {
  84. '$route': function (newRoute) {
  85. this.activePage = newRoute.fullPath
  86. if (!this.multipage) {
  87. this.linkList = [newRoute.fullPath]
  88. this.pageList = [Object.assign({}, newRoute)]
  89. } else if (this.linkList.indexOf(newRoute.fullPath) < 0) {
  90. this.linkList.push(newRoute.fullPath)
  91. this.pageList.push(Object.assign({}, newRoute))
  92. } else if (this.linkList.indexOf(newRoute.fullPath) >= 0) {
  93. let oldIndex = this.linkList.indexOf(newRoute.fullPath)
  94. let oldPositionRoute = this.pageList[oldIndex]
  95. this.pageList.splice(oldIndex, 1, Object.assign({}, newRoute, {meta: oldPositionRoute.meta}))
  96. }
  97. },
  98. 'activePage': function (key) {
  99. let index = this.linkList.lastIndexOf(key)
  100. let waitRouter = this.pageList[index]
  101. this.$router.push(Object.assign({}, waitRouter));
  102. },
  103. 'multipage': function (newVal) {
  104. if (!newVal) {
  105. this.linkList = [this.$route.fullPath]
  106. this.pageList = [this.$route]
  107. }
  108. }
  109. },
  110. methods: {
  111. changePage(key) {
  112. this.activePage = key
  113. },
  114. editPage(key, action) {
  115. this[action](key)
  116. },
  117. remove(key) {
  118. console.log(key)
  119. if (key == indexKey) {
  120. this.$message.warning('首页不能关闭!')
  121. return
  122. }
  123. if (this.pageList.length === 1) {
  124. this.$message.warning('这是最后一页,不能再关闭了啦')
  125. return
  126. }
  127. if(key == "/account/advertisingGroup"){
  128. localStorage.removeItem("advertisingGroup")
  129. localStorage.removeItem("advertisingGroupKey")
  130. }
  131. if(key == "/account/stepForm"){
  132. localStorage.removeItem("step")
  133. }
  134. if(key == "/account/originality"){
  135. localStorage.removeItem("originality")
  136. localStorage.removeItem("originalityKey")
  137. }
  138. if(key == "/modules/material/materialList"){
  139. localStorage.removeItem("key")
  140. }
  141. this.pageList = this.pageList.filter(item => item.fullPath !== key)
  142. let index = this.linkList.indexOf(key)
  143. this.linkList = this.linkList.filter(item => item !== key)
  144. index = index >= this.linkList.length ? this.linkList.length - 1 : index
  145. this.activePage = this.linkList[index]
  146. },
  147. onContextmenu(e) {
  148. const pagekey = this.getPageKey(e.target)
  149. if (pagekey !== null) {
  150. e.preventDefault()
  151. this.menuVisible = true
  152. }
  153. },
  154. getPageKey(target, depth) {n
  155. depth = depth || 0
  156. if (depth > 2) {
  157. return null
  158. }
  159. let pageKey = target.getAttribute('pagekey')
  160. pageKey = pageKey || (target.previousElementSibling ? target.previousElementSibling.getAttribute('pagekey') : null)
  161. return pageKey || (target.firstElementChild ? this.getPageKey(target.firstElementChild, ++depth) : null)
  162. },
  163. onMenuSelect(key, target) {
  164. let pageKey = this.getPageKey(target)
  165. switch (key) {
  166. case '1':
  167. this.closeLeft(pageKey)
  168. break
  169. case '2':
  170. this.closeRight(pageKey)
  171. break
  172. case '3':
  173. this.closeOthers(pageKey)
  174. break
  175. default:
  176. break
  177. }
  178. },
  179. closeOthers(pageKey) {
  180. let index = this.linkList.indexOf(pageKey)
  181. if (pageKey == indexKey) {
  182. this.linkList = this.linkList.slice(index, index + 1)
  183. this.pageList = this.pageList.slice(index, index + 1)
  184. this.activePage = this.linkList[0]
  185. } else {
  186. let indexContent = this.pageList.slice(0, 1)[0]
  187. this.linkList = this.linkList.slice(index, index + 1)
  188. this.pageList = this.pageList.slice(index, index + 1)
  189. this.linkList.unshift(indexKey)
  190. this.pageList.unshift(indexContent)
  191. this.activePage = this.linkList[1]
  192. }
  193. },
  194. closeLeft(pageKey) {
  195. if (pageKey == indexKey) {
  196. return
  197. }
  198. let tempList = [...this.pageList]
  199. let indexContent = tempList.slice(0, 1)[0]
  200. let index = this.linkList.indexOf(pageKey)
  201. this.linkList = this.linkList.slice(index)
  202. this.pageList = this.pageList.slice(index)
  203. this.linkList.unshift(indexKey)
  204. this.pageList.unshift(indexContent)
  205. if (this.linkList.indexOf(this.activePage) < 0) {
  206. this.activePage = this.linkList[0]
  207. }
  208. },
  209. closeRight(pageKey) {
  210. let index = this.linkList.indexOf(pageKey)
  211. this.linkList = this.linkList.slice(0, index + 1)
  212. this.pageList = this.pageList.slice(0, index + 1)
  213. if (this.linkList.indexOf(this.activePage < 0)) {
  214. this.activePage = this.linkList[this.linkList.length - 1]
  215. }
  216. },
  217. //update-begin-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  218. dynamicRouterShow(key, title) {
  219. let keyIndex = this.linkList.indexOf(key)
  220. if (keyIndex >= 0) {
  221. let currRouter = this.pageList[keyIndex]
  222. let meta = Object.assign({}, currRouter.meta, {title: title})
  223. this.pageList.splice(keyIndex, 1, Object.assign({}, currRouter, {meta: meta}))
  224. }
  225. }
  226. //update-end-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  227. }
  228. }
  229. </script>
  230. <style lang="scss">
  231. /*
  232. * The following styles are auto-applied to elements with
  233. * transition="page-transition" when their visibility is toggled
  234. * by Vue.js.
  235. *
  236. * You can easily play with the page transition by editing
  237. * these styles.
  238. */
  239. .page-transition-enter {
  240. opacity: 0;
  241. }
  242. .page-transition-leave-active {
  243. opacity: 0;
  244. }
  245. .page-transition-enter .page-transition-container,
  246. .page-transition-leave-active .page-transition-container {
  247. -webkit-transform: scale(1.1);
  248. transform: scale(1.1);
  249. }
  250. /*美化弹出Tab样式*/
  251. .ant-tabs-nav-container {
  252. margin-top: 4px;
  253. }
  254. /* 修改 ant-tabs 样式 */
  255. .tab-layout-tabs.ant-tabs {
  256. border-bottom: 1px solid #ccc;
  257. border-left: 1px solid #ccc;
  258. background-color: white;
  259. padding: 0 20px;
  260. .ant-tabs-bar {
  261. margin: 4px 0 0;
  262. border: none;
  263. }
  264. }
  265. .ant-tabs {
  266. &.ant-tabs-card .ant-tabs-tab {
  267. padding: 0 24px !important;
  268. background-color: white !important;
  269. margin-right: 10px !important;
  270. .ant-tabs-close-x {
  271. width: 12px !important;
  272. height: 12px !important;
  273. opacity: 0 !important;
  274. cursor: pointer !important;
  275. font-size: 12px !important;
  276. margin: 0 !important;
  277. position: absolute;
  278. top: 36%;
  279. right: 6px;
  280. }
  281. &:hover .ant-tabs-close-x {
  282. opacity: 1 !important;
  283. }
  284. }
  285. }
  286. .ant-tabs.ant-tabs-card > .ant-tabs-bar {
  287. .ant-tabs-tab {
  288. border: none !important;
  289. border-bottom: 1px solid transparent !important;
  290. }
  291. .ant-tabs-tab-active {
  292. border-color: #1890ff !important;
  293. }
  294. }
  295. </style>