index.vue 14.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
<template>
  <div
    id="tabs-bar-container"
    class="tabs-bar-container"
    :class="{ 'is-horizontal': layout === 'horizontal' }"
    :style="{
      background: variables[`${style}-top-bar-background`],
      '--borderColor': variables[`${style}-container-background`],
    }"
  >
    <el-tabs
      v-model="tabActive"
      type="card"
      class="tabs-content"
      :style="{
        '--color': variables[`${style}-tab-color`],
      }"
      @tab-click="handleTabClick"
      @tab-remove="handleTabRemove"
    >
      <el-tab-pane
        v-for="item in visitedMenus"
        :key="item.id"
        :label="item.name"
        :name="item.id"
        :closable="isCloseable"
      ></el-tab-pane>
    </el-tabs>

    <!-- 获取系统最新布局 -->
    <div v-if="isShowDefaultLayoutBtn" class="layout-content">
      <el-button class="new-layout add-column" @click="handleAddColumn">
        <i class="icon-f-plus"></i>
        添加栏目
      </el-button>
      <el-button
        v-if="isNewLayout || isDragColumn"
        class="new-layout"
        @click="getDefaultLayout"
      >
        <span class="btn-text">获取最新门户</span>
      </el-button>
    </div>

    <ul
      v-if="contextMenuVisible"
      :style="{ left: left + 'px', top: '108px' }"
      class="context-menu"
    >
      <li class="context-menu-item">
        <el-button type="text" @click="handleOperateTabs('all')">
          关闭全部标签页
        </el-button>
      </li>
      <li class="context-menu-item">
        <el-button
          type="text"
          :disabled="isDisabledCloseLeftBtnFlag"
          @click="handleOperateTabs('left')"
        >
          关闭左侧标签页
        </el-button>
      </li>
      <li class="context-menu-item">
        <el-button
          type="text"
          :disabled="isDisabledCloseRightBtnFlag"
          @click="handleOperateTabs('right')"
        >
          关闭右侧标签页
        </el-button>
      </li>
      <li class="context-menu-item">
        <el-button type="text" @click="handleOperateTabs('other')">
          关闭其他标签页
        </el-button>
      </li>
    </ul>
  </div>
</template>

<script>
  import portal from '@/api/portal'
  import { mapGetters } from 'vuex'
  import variables from '@/styles/variables.scss'
  export default {
    name: 'HtTabsBar',
    data() {
      return {
        affixtabs: [],
        tabActive: '',
        contextMenuVisible: false,
        isDisabledCloseLeftBtnFlag: true,
        isDisabledCloseRightBtnFlag: true,
        left: 0,
        top: 0,
        isNewLayout: false,
        isDragColumn: false,
        defaultMune: [
          {
            name: '/personal/message',
            store: 'settings/changeMessageColor',
          },
          {
            name: '/personal/mail',
            store: 'settings/changeEmailColor',
          },
        ],
        defaultPerMun: [
          '/personal/security',
          '/personal/information',
          '/personal/setting',
        ],
        winUrl: '',
        currentActiveName: '',
        clickTabIndex: -1,
      }
    },
    computed: {
      ...mapGetters({
        visitedMenus: 'app/visitedMenus',
        activeMenu: 'app/activeMenu',
        routes: 'routes/routes',
        style: 'app/style',
        layout: 'app/layout',
      }),
      variables() {
        return variables
      },
      isShowDefaultLayoutBtn() {
        const { path, meta } = this.$route
        return ['/index', '/index/', '/Index'].includes(path) || meta.index
      },
      isDisabled() {
        const index = this.routes.findIndex(
          (item) => ['/'].includes(item.path) && item.meta
        )
        const findCurrentLayoutIsNew = this.routes[index].children.find(
          (it) => it.meta.alias === this.$route.meta.alias
        )
        return !(findCurrentLayoutIsNew && findCurrentLayoutIsNew.isNew)
      },
      isCloseable() {
        return this.visitedMenus.length > 1
      },
    },
    watch: {
      activeMenu(val) {
        this.tabActive = val?.id
      },
      contextMenuVisible() {
        if (this.contextMenuVisible) {
          document.body.addEventListener('click', this.closeContextMenu)
        } else {
          document.body.removeEventListener('click', this.closeContextMenu)
        }
      },
    },
    mounted() {
      this.tabActive = this.activeMenu ? this.activeMenu.id : ''
      this.bindRightClickMenu()
      const layoutId = sessionStorage.getItem('layoutId')
      const localIsNewLayout = localStorage.getItem(`isNewLayout_${layoutId}`)
      const localIsDragColumn = localStorage.getItem(`isDragColumn_${layoutId}`)
      this.isNewLayout = localIsNewLayout ? JSON.parse(localIsNewLayout) : false
      this.isDragColumn = localIsDragColumn
        ? JSON.parse(localIsDragColumn)
        : false
      this.initListener()
    },
    destroyed() {
      window.removeEventListener('storage', () => {})
    },
    methods: {
      getDefaultLayout() {
        const layoutId = sessionStorage.getItem('layoutId')
        if (layoutId) {
          this.$confirm('确认获取最新门户布局吗?', '提示', {
            type: 'warning',
            closeOnClickModal: false,
          })
            .then(() => {
              const layouConfig = {
                layoutId,
                configContent: JSON.stringify({}),
                isReset: true,
              }
              portal.setLayoutConfig(layouConfig).then(() => {
                this.$message.success('获取最新布局成功')
                this.isNewLayout = false
                this.isDragColumn = false
                this.$baseEventBus.$emit('get-layout-config')
              })
            })
            .catch(() => {})
        } else {
          this.$message.error('当前布局id为空,无法获取最新布局!')
        }
      },
      handleAddColumn() {
        const layoutId = sessionStorage.getItem('layoutId')
        this.$baseEventBus.$emit('add-column', layoutId)
      },
      handleTabRemove(tabActive) {
        let view
        this.visitedMenus.forEach((item) => {
          if (tabActive === item.id) {
            view = item
          }
        })
        this.closeSelectedTab(view)
      },
      handleTabClick(tab) {
        this.selectTabById(tab.name)
        // let activeMenu = this.visitedMenus.find((item) => tab.name === item.id)
        // this.$store.dispatch('app/setActiveMenu', activeMenu)
      },
      selectTabById(id) {
        let activeMenu = this.visitedMenus.find((item) => id === item.id)
        this.$store.dispatch('app/setActiveMenu', activeMenu)
      },
      isActive(view) {
        return view.id === this.activeMenu.id
      },
      handleOperateTabs(type) {
        switch (type) {
          case 'other':
            this.closeOtherTabs()
            break
          case 'left':
            this.closeLeftTabs()
            break
          case 'right':
            this.closeRightTabs()
            break
          case 'all':
            this.closeAllTabs()
            break
        }
        this.contextMenuVisible = false
      },
      async closeSelectedTab(view) {
        await this.$store.dispatch('app/delVisitedMenus', view)
        if (this.isActive(view)) {
          this.toLastTag(this.visitedMenus, view)
        }
      },
      async closeOtherTabs() {
        let view = this.rightClickSelectedTab()
        await this.$store.dispatch('app/delOtherMenus', view)
        if (!this.isActive(view)) {
          this.$store.dispatch('app/setActiveMenu', view)
        }
      },
      async closeLeftTabs() {
        const view = this.rightClickSelectedTab()
        await this.$store.dispatch('app/delLeftMenus', this.clickTabIndex)
      },
      async closeRightTabs() {
        const view = this.rightClickSelectedTab()
        await this.$store.dispatch('app/delRightMenus', this.clickTabIndex)
      },
      async closeAllTabs() {
        const view = await this.toThisTag()
        await this.$store.dispatch('app/delAllVisitedMenus')
        this.toLastTag(this.visitedMenus, view)
      },
      toLastTag(visitedMenus, view) {
        const latestView = visitedMenus.slice(-1)[0]
        this.$store.dispatch('app/setActiveMenu', latestView)
      },
      async toThisTag() {
        return this.activeMenu
      },
      rightClickSelectedTab() {
        return this.visitedMenus.find(
          (menu) => menu.id === this.currentActiveName
        )
      },
      bindRightClickMenu() {
        const tabsDom = document.getElementById('tabs-bar-container')
        let tabsHeaderDom = tabsDom.getElementsByClassName(
          'el-tabs__header is-top'
        )
        tabsHeaderDom[0].oncontextmenu = this.handleClickContextMenu
      },
      handleClickContextMenu(e) {
        e.preventDefault()
        this.currentActiveName = e.target.id.split('-')[1]
        if (this.currentActiveName) {
          this.contextMenuVisible = true
          this.left = e.clientX - 24
          this.top = e.clientY - 10
          this.clickTabIndex = this.visitedMenus.findIndex(
            (item) => item.id === this.currentActiveName
          )
          if (this.clickTabIndex <= 0) {
            this.isDisabledCloseLeftBtnFlag = true
            this.isDisabledCloseRightBtnFlag = false
          } else if (this.clickTabIndex >= this.visitedMenus.length - 1) {
            this.isDisabledCloseLeftBtnFlag = false
            this.isDisabledCloseRightBtnFlag = true
          } else {
            this.isDisabledCloseLeftBtnFlag = false
            this.isDisabledCloseRightBtnFlag = false
          }
          this.contextMenuVisible = true
        }
      },
      closeContextMenu() {
        this.contextMenuVisible = false
      },
      initListener() {
        window.addEventListener('storage', (value) => {
          if (value.key === 'close-tab') {
            this.closeSelectedTab(this.activeMenu)
          }
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  $base-padding: 24px;
  @mixin tabs-item-bg {
    mask: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANoAAAAkBAMAAAAdqzmBAAAAMFBMVEVHcEwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlTPQ5AAAAD3RSTlMAr3DvEM8wgCBA379gj5//tJBPAAAAnUlEQVRIx2NgAAM27fj/tAO/xBsYkIHyf9qCT8iWMf6nNQhAsk2f5rYheY7Dnua2/U+A28ZEe8v+F9Ax2v7/F4DbxkUH2wzgtvHTwbYPo7aN2jZq26hto7aN2jZq25Cy7Qvctnw62PYNbls9HWz7S8/G6//PsI6H4396gAUQy1je08W2jxDbpv6nD4gB2uWp+J9eYPsEhv/0BPS1DQBvoBLVZ3BppgAAAABJRU5ErkJggg==);
    mask-size: 100% 100%;
  }
  .tabs-bar-container {
    position: relative;
    box-sizing: border-box;
    display: flex;
    align-content: center;
    // align-items: center;
    justify-content: space-between;
    height: $base-tabs-bar-height;
    padding-right: $base-padding;
    padding-left: $base-padding;
    user-select: none;
    // border-top: 1px solid var(--borderColor);
    margin-top: 1px;

    ::v-deep {
      .fold-unfold {
        margin-right: $base-padding;
      }
    }

    .tabs-content {
      width: calc(100% - 150px);
      height: $base-tabs-item-height;
      margin-top: calc(#{$base-tabs-bar-height} - #{$base-tabs-item-height});
      ::v-deep {
        .el-tabs__nav-next,
        .el-tabs__nav-prev {
          height: $base-tabs-item-height;
          line-height: $base-tabs-item-height;
        }

        .el-tabs__header {
          border-bottom: 0;
          margin: 0;

          .el-tabs__nav {
            border: 0;
          }

          .el-tabs__item {
            box-sizing: border-box;
            height: $base-tabs-item-height;
            margin-right: 5px;
            line-height: $base-tabs-item-height;
            border: none;
            color: var(--color);
            border-radius: 4px 4px 0 0;
            transition: padding 0.3s cubic-bezier(0.645, 0.045, 0.355, 1) !important;

            &.is-active {
              border: none;
              color: var(--themeColor);
              @include tabs-item-bg;
            }
            &:hover {
              color: var(--themeColor);
              @include tabs-item-bg;
              background-color: $base-tabs-item-hover-bg;
            }
          }
        }
      }
    }

    .layout-content {
      height: $base-tabs-bar-height;
      display: flex;
      align-items: center;
      .new-layout {
        display: flex;
        align-items: center;
        font-size: $base-font-size-default;
        padding: 7px 14px 7px 28px;
        border: 1px solid #dcdee0;
        border-radius: $base-border-radius;
        // color: $base-font-color;
        .circle {
          display: inline-block;
          width: 6px;
          height: 6px;
          margin-right: 7px;
          border-radius: 50%;
          background: $base-color-red;
        }
        .text {
          color: $base-font-color;
          font-size: $base-font-size-default;
        }
        .btn-text {
          position: relative;
          &::before {
            position: absolute;
            content: '';
            width: 6px;
            height: 6px;
            border-radius: 50%;
            left: -14px;
            top: 5px;
            background: $base-color-red;
          }
        }
      }
      .add-column {
        padding-left: 14px;
      }
      ::v-deep {
        .el-button {
          color: $base-dark-title-color;
        }
        .is-disabled {
          color: #c0c4cc;
          padding: 7px 14px;
        }
      }
    }

    .context-menu {
      width: 122px;
      background: $base-color-white;
      position: fixed;
      z-index: 99;
      padding: 4px 0;
      border-radius: 2px;
      box-shadow: 0px 1px 6px rgba(91, 91, 91, 0.15);
      text-align: center;
      .context-menu-item {
        ::v-deep {
          .el-button--text {
            color: $base-color-second-color;
            font-size: 14px;
            padding: 6px 10px;
            &:hover {
              color: var(--themeColor);
              background: #f9f9f9;
            }
          }
          .is-disabled {
            color: #ccc;
            &:hover {
              color: #ccc;
              background: $base-color-white;
            }
          }
        }
      }
    }
    .more {
      display: flex;
      align-content: center;
      align-items: center;
      cursor: pointer;
    }
  }
  .is-horizontal {
    width: 92% !important;
    margin: auto;
  }
</style>