indexControl.vue 7.61 KB
<template>
  <div class="layout">
    <div
      class="tab"
      ref="tabbox"
    >
      <div
        :class="['tab-item', { 'item-active': windowId == -1 }]"
        @click="windowId = -1"
      >
        盯防首页
      </div>
      <template v-for="(i, index) in tab">
        <template>
          <div
            :key="i.id"
            :class="['tab-item', { 'item-active': windowId == i.id }]"
            @click="windowId = i.id"
            :ref="`tab-${index}`"
          >
            {{ i.tabTitle }}
            <div
              class="close"
              @click.stop="close(index, i.id)"
            >
              <i class="el-icon-close"></i>
            </div>
          </div>
        </template>
      </template>
      <template v-if="dropdownList.length">
        <div class="arrow-box">
          <el-dropdown
            trigger="click"
            @command="windowId = $event"
          >
            <div class="dropdown-link">
              <i class="el-icon-arrow-down"></i>
            </div>
            <el-dropdown-menu slot="dropdown">
              <template v-for="(i, index) in dropdownList">
                <el-dropdown-item
                  :class="{ 'dropdown-item-active': windowId == i.id }"
                  :key="i.id"
                  :command="i.id"
                >
                  <div class="dropdown-item">
                    {{ i.tabTitle }}
                    <div
                      class="close"
                      @click.stop="closeDrop(index)"
                    >
                      <i class="el-icon-close"></i>
                    </div>
                  </div>
                </el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </template>
    </div>
    <div class="content">
      <shop-guard-index
        v-show="windowId == -1"
        @openWindow="openWindow"
        :show="windowId == -1"
        ref="shopGuardIndex"
      />
      <template v-for="i in [...tab, ...dropdownList]">
        <component
          :ref="`page-${i.id}`"
          :key="i.id"
          :is="i.component"
          v-show="windowId == i.id"
          :show="windowId == i.id"
          :pageData="i.data"
          @openWindow="openWindow"
        />
      </template>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
import shopGuardIndex from "./components/shopGuardIndex/index"
import boardDetail from './components/boardDetail/index'
import shopDetail from "./components/shopDetail/index"
import dataMore from './components/dataMore/index'
export default {
  name: "",
  components: {
    shopGuardIndex,
    boardDetail,
    shopDetail,
    dataMore
  },
  data () {
    return {
      tab: [],
      dropdownList: [],
      windowId: -1,
      tabboxWidth: 0,
    }
  },

  // 在beforeDestroy钩子中移除事件监听
  beforeDestroy () {
    window.removeEventListener('popstate', this.preventBack)
    window.onresize = null
  },
  // 在你的Vue组件的mounted钩子中添加事件监听
  mounted () {
    // window.onresize = () => {
    //   this.tabboxWidth = this.$refs.tabbox.offsetWidth
    //   this.checkEl()
    // }
    history.pushState(null, null, document.URL)
    window.addEventListener('popstate', this.preventBack)
    if (this.tabboxWidth == 0) this.tabboxWidth = this.$refs.tabbox.offsetWidth
  },
  methods: {
    preventBack () {
      if (this.windowId !== -1) {
        const [recep] = this.$refs[`page-${this.windowId}`]
        if (recep && recep.isQualityVisible) recep.closeDialog()
        else this.windowId = -1
        history.pushState(null, null, document.URL)
      } else this.$router.go(-1)
    },
    openWindow (data) {
      console.log(data)
      // data.merchantId = 44211895
      // data.merchantId = 44210895
      // data.merchantId = 44209510
      // data.businessType = 4

      let id = data.pageId,
        windowList = [...this.tab, ...this.dropdownList],
        windowItem = windowList.find((e) => e.id == id),
        item = {
          component: data.componentName,
          data,
          id,
          tabTitle: data.tabTitle
        }
      if (windowItem) {
        let tabIndex = this.tab.findIndex(e => e.id == id)
        if (tabIndex == -1) {
          let dropdownIndex = this.dropdownList.findIndex(e => e.id == id)
          this.dropdownList[dropdownIndex] = item
        } else this.tab[tabIndex] = item
        this.windowId = id
        return
      }
      if (windowList.length == 20) return this.$message.error("最多同时打开20个tabs")

      if (this.dropdownList.length) this.dropdownList.push(item)
      else {
        this.tab.push(item)
        this.checkEl()
      }
      this.windowId = id
    },
    close (index) {
      this.tab.splice(index, 1)
      if (this.dropdownList.length) {
        this.tab.push(this.dropdownList[0])
        this.closeDrop(0)
      }
      this.windowId = -1
      this.checkEl()
    },
    closeDrop (index) {
      this.dropdownList.splice(index, 1)
    },
    checkEl () {
      let { tab, tabboxWidth } = this,
        len = tab.length - 1
      if (len == -1) return
      this.$nextTick(() => {
        let [el] = this.$refs[`tab-${len}`]
        if (el) {
          const { offsetWidth, offsetLeft } = el
          if (offsetLeft + offsetWidth / 2 > tabboxWidth - 54) {
            this.dropdownList.push(tab[len])
            this.tab.splice(len, 1)
          }
          if (this.dropdownList.length && offsetLeft + offsetWidth / 2 < tabboxWidth - 54) {
            this.tab.push(this.dropdownList[0])
            this.closeDrop(0)
          }
        }
      })
    },

  }
}
</script>

<style scoped lang='scss'>
.layout {
  .tab {
    display: flex;
    align-items: center;
    margin-left: 20px;
    width: calc(100% - 58px);
    box-sizing: border-box;
    &-item {
      width: fit-content;
      height: 40px;
      padding: 0 20px;
      background: #fafafa;
      border-radius: 4px 4px 0 0;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      color: #666666;
      cursor: pointer;
      line-height: 40px;
      margin-right: 1px;
      flex-shrink: 0;
      .close {
        border-radius: 100%;
        padding: 4px;
        margin-left: 4px;
        display: flex;
        align-items: center;
        justify-content: center;
        &:hover {
          background-color: #666;
          .el-icon-close {
            color: #fff;
          }
        }
      }
      .el-icon-close {
        font-size: 12px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #666666;
        transform: translateY(1px);
      }
    }
    .item-active {
      font-weight: 500;
      color: #2a58ad;
      background: #ffffff;
    }
    .arrow-box {
      width: 32px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      background: #fafafa;
      cursor: pointer;
      flex-shrink: 0;
      .el-dropdown {
        width: 100%;
      }
    }
  }
  .content {
    position: relative;
    width: 100%;
    height: calc(100vh - 110px);
    // overflow-y: scroll;
    box-sizing: border-box;
    padding-bottom: 70px;
  }
}
::v-deep.dropdown-item {
  display: flex;
  align-items: center;
  justify-content: center;
  .close {
    border-radius: 100%;
    padding: 4px;
    margin-left: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    &:hover {
      background-color: #666;
      .el-icon-close {
        color: #fff;
      }
    }
    i {
      margin-right: 0;
    }
  }
  .el-icon-close {
    font-size: 12px;

    color: #666666;
  }
}
.dropdown-item-active {
  background-color: #eaeef7;
  color: #5579bd;
}
</style>