TabLayout.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <template>
  2. <global-layout @dynamicRouterShow="dynamicRouterShow" @removeTab="remove">
  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 @removeTab="remove"/>
  22. </keep-alive>
  23. <router-view v-else @removeTab="remove"/>
  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. this.$bus.$on('remove',this.remove)
  65. if (this.$route.path != indexKey) {
  66. this.pageList.push({
  67. name: 'dashboard-analysis',
  68. path: indexKey,
  69. fullPath: indexKey,
  70. meta: {
  71. icon: 'dashboard',
  72. title: '首页'
  73. }
  74. })
  75. this.linkList.push(indexKey)
  76. }
  77. this.pageList.push(this.$route)
  78. this.linkList.push(this.$route.fullPath)
  79. this.activePage = this.$route.fullPath
  80. if(!this.linkList.includes("/account/stepForm")){
  81. localStorage.removeItem("step")
  82. localStorage.removeItem("campaignId")
  83. localStorage.removeItem("pans")
  84. }
  85. if(!this.linkList.includes("/account/advertisingGroup")){
  86. localStorage.removeItem("advertisingGroup")
  87. localStorage.removeItem("advertisingGroupKey")
  88. }
  89. },
  90. watch: {
  91. '$route': function (newRoute) {
  92. this.activePage = newRoute.fullPath
  93. if (!this.multipage) {
  94. this.linkList = [newRoute.fullPath]
  95. this.pageList = [Object.assign({}, newRoute)]
  96. } else if (this.linkList.indexOf(newRoute.fullPath) < 0) {
  97. this.linkList.push(newRoute.fullPath)
  98. this.pageList.push(Object.assign({}, newRoute))
  99. } else if (this.linkList.indexOf(newRoute.fullPath) >= 0) {
  100. let oldIndex = this.linkList.indexOf(newRoute.fullPath)
  101. let oldPositionRoute = this.pageList[oldIndex]
  102. this.pageList.splice(oldIndex, 1, Object.assign({}, newRoute, {meta: oldPositionRoute.meta}))
  103. }
  104. if(!this.linkList.includes("/account/stepForm")){
  105. localStorage.removeItem("step")
  106. localStorage.removeItem("campaignId")
  107. localStorage.removeItem("pans")
  108. }
  109. if(!this.linkList.includes("/account/advertisingGroup")){
  110. localStorage.removeItem("advertisingGroup")
  111. localStorage.removeItem("advertisingGroupKey")
  112. }
  113. if(!this.linkList.includes("/account/originality")){
  114. localStorage.removeItem("originality")
  115. localStorage.removeItem("originalityKey")
  116. }
  117. if(!this.linkList.includes("/account/accountIndex")){
  118. localStorage.removeItem("accountId")
  119. }
  120. },
  121. 'activePage': function (key) {
  122. let index = this.linkList.lastIndexOf(key)
  123. let waitRouter = this.pageList[index]
  124. this.$router.push(Object.assign({}, waitRouter));
  125. },
  126. 'multipage': function (newVal) {
  127. if (!newVal) {
  128. this.linkList = [this.$route.fullPath]
  129. this.pageList = [this.$route]
  130. }
  131. }
  132. },
  133. methods: {
  134. changePage(key) {
  135. this.activePage = key
  136. },
  137. editPage(key, action) {
  138. this[action](key)
  139. },
  140. remove(key) {
  141. if (key == indexKey) {
  142. this.$message.warning('首页不能关闭!')
  143. return
  144. }
  145. if (this.pageList.length === 1) {
  146. this.$message.warning('这是最后一页,不能再关闭了啦')
  147. return
  148. }
  149. if(key == "/account/advertisingGroup"){
  150. localStorage.removeItem("advertisingGroup")
  151. localStorage.removeItem("advertisingGroupKey")
  152. }
  153. if(key == "/account/stepForm"){
  154. localStorage.removeItem("step")
  155. localStorage.removeItem("pans")
  156. }
  157. if(key == "/account/originality"){
  158. localStorage.removeItem("originality")
  159. localStorage.removeItem("originalityKey")
  160. }
  161. if(key == "/modules/material/materialList"){
  162. localStorage.removeItem("key")
  163. }
  164. this.pageList = this.pageList.filter(item => item.fullPath !== key)
  165. let index = this.linkList.indexOf(key)
  166. this.linkList = this.linkList.filter(item => item !== key)
  167. index = index >= this.linkList.length ? this.linkList.length - 1 : index
  168. this.activePage = this.linkList[index]
  169. },
  170. onContextmenu(e) {
  171. const pagekey = this.getPageKey(e.target)
  172. if (pagekey !== null) {
  173. e.preventDefault()
  174. this.menuVisible = true
  175. }
  176. },
  177. getPageKey(target, depth) {
  178. depth = depth || 0
  179. if (depth > 2) {
  180. return null
  181. }
  182. let pageKey = target.getAttribute('pagekey')
  183. pageKey = pageKey || (target.previousElementSibling ? target.previousElementSibling.getAttribute('pagekey') : null)
  184. return pageKey || (target.firstElementChild ? this.getPageKey(target.firstElementChild, ++depth) : null)
  185. },
  186. onMenuSelect(key, target) {
  187. let pageKey = this.getPageKey(target)
  188. switch (key) {
  189. case '1':
  190. this.closeLeft(pageKey)
  191. break
  192. case '2':
  193. this.closeRight(pageKey)
  194. break
  195. case '3':
  196. this.closeOthers(pageKey)
  197. break
  198. default:
  199. break
  200. }
  201. },
  202. closeOthers(pageKey) {
  203. let index = this.linkList.indexOf(pageKey)
  204. if (pageKey == indexKey) {
  205. this.linkList = this.linkList.slice(index, index + 1)
  206. this.pageList = this.pageList.slice(index, index + 1)
  207. this.activePage = this.linkList[0]
  208. } else {
  209. let indexContent = this.pageList.slice(0, 1)[0]
  210. this.linkList = this.linkList.slice(index, index + 1)
  211. this.pageList = this.pageList.slice(index, index + 1)
  212. this.linkList.unshift(indexKey)
  213. this.pageList.unshift(indexContent)
  214. this.activePage = this.linkList[1]
  215. }
  216. },
  217. closeLeft(pageKey) {
  218. if (pageKey == indexKey) {
  219. return
  220. }
  221. let tempList = [...this.pageList]
  222. let indexContent = tempList.slice(0, 1)[0]
  223. let index = this.linkList.indexOf(pageKey)
  224. this.linkList = this.linkList.slice(index)
  225. this.pageList = this.pageList.slice(index)
  226. this.linkList.unshift(indexKey)
  227. this.pageList.unshift(indexContent)
  228. if (this.linkList.indexOf(this.activePage) < 0) {
  229. this.activePage = this.linkList[0]
  230. }
  231. },
  232. closeRight(pageKey) {
  233. let index = this.linkList.indexOf(pageKey)
  234. this.linkList = this.linkList.slice(0, index + 1)
  235. this.pageList = this.pageList.slice(0, index + 1)
  236. if (this.linkList.indexOf(this.activePage < 0)) {
  237. this.activePage = this.linkList[this.linkList.length - 1]
  238. }
  239. },
  240. //update-begin-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  241. dynamicRouterShow(key, title) {
  242. let keyIndex = this.linkList.indexOf(key)
  243. if (keyIndex >= 0) {
  244. let currRouter = this.pageList[keyIndex]
  245. let meta = Object.assign({}, currRouter.meta, {title: title})
  246. this.pageList.splice(keyIndex, 1, Object.assign({}, currRouter, {meta: meta}))
  247. }
  248. }
  249. //update-end-author:taoyan date:20190430 for:动态路由title显示配置的菜单title而不是其对应路由的title
  250. }
  251. }
  252. </script>
  253. <style lang="scss">
  254. /*
  255. * The following styles are auto-applied to elements with
  256. * transition="page-transition" when their visibility is toggled
  257. * by Vue.js.
  258. *
  259. * You can easily play with the page transition by editing
  260. * these styles.
  261. */
  262. .page-transition-enter {
  263. opacity: 0;
  264. }
  265. .page-transition-leave-active {
  266. opacity: 0;
  267. }
  268. .page-transition-enter .page-transition-container,
  269. .page-transition-leave-active .page-transition-container {
  270. -webkit-transform: scale(1.1);
  271. transform: scale(1.1);
  272. }
  273. /*美化弹出Tab样式*/
  274. .ant-tabs-nav-container {
  275. margin-top: 4px;
  276. }
  277. /* 修改 ant-tabs 样式 */
  278. .tab-layout-tabs.ant-tabs {
  279. border-bottom: 1px solid #ccc;
  280. border-left: 1px solid #ccc;
  281. background-color: white;
  282. padding: 0 20px;
  283. .ant-tabs-bar {
  284. margin: 4px 0 0;
  285. border: none;
  286. }
  287. }
  288. .ant-tabs {
  289. &.ant-tabs-card .ant-tabs-tab {
  290. padding: 0 24px !important;
  291. background-color: white !important;
  292. margin-right: 10px !important;
  293. .ant-tabs-close-x {
  294. width: 12px !important;
  295. height: 12px !important;
  296. opacity: 0 !important;
  297. cursor: pointer !important;
  298. font-size: 12px !important;
  299. margin: 0 !important;
  300. position: absolute;
  301. top: 36%;
  302. right: 6px;
  303. }
  304. &:hover .ant-tabs-close-x {
  305. opacity: 1 !important;
  306. }
  307. }
  308. }
  309. .ant-tabs.ant-tabs-card > .ant-tabs-bar {
  310. .ant-tabs-tab {
  311. border: none !important;
  312. border-bottom: 1px solid transparent !important;
  313. }
  314. .ant-tabs-tab-active {
  315. border-color: #1890ff !important;
  316. }
  317. }
  318. </style>