TabLayout.vue 11 KB

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