inStoreControlList.vue 6.48 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.text }}
            <div class="close"
                 @click.stop="close(index)">
              <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.text }}
                    <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">
      <InStoreManageIndex v-show="windowId == -1"
                          @openWindow="openWindow" />
      <template v-for="i in [...tab, ...dropdownList]">
        <editStaff :ref="`recep-${i.id}`"
                   :key="i.id"
                   v-show="windowId == i.id"
                   :query-data="i.data"
                   @backIndex="backIndex" />
      </template>
    </div>
  </div>
</template>

<script type='text/ecmascript-6'>
import InStoreManageIndex from "./index"
import editStaff from "./editStaff"
export default {
  name: "",
  components: {
    InStoreManageIndex,
    editStaff
  },
  data() {
    return {
      tab: [],
      dropdownList: [],
      windowId: -1,
      tabboxWidth: 0
    }
  },
  // 在beforeDestroy钩子中移除事件监听
  beforeDestroy() {
    window.removeEventListener('popstate', this.preventBack)
  },
  // 在你的Vue组件的mounted钩子中添加事件监听
  mounted() {
    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[`recep-${this.windowId}`]
        if (recep && recep.isQualityVisible) recep.isQualityVisible = false
        else this.windowId = -1
        history.pushState(null, null, document.URL)
      } else this.$router.go(-1)
    },
    openWindow(data) {
      const id = `${data.staffName}_${data.phoneNumber}`
      const windowList = [...this.tab, ...this.dropdownList]
      const windowItem = windowList.find((e) => e.id == id)
      if (windowItem) return this.windowId = id
      if (windowList.length == 20) return this.$message.error("最多同时打开20个tabs")
      const item = {
        text: data.staffName || '-',
        data,
        id
      }
      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() {
      const { tab, tabboxWidth } = this
      const len = tab.length - 1
      this.$nextTick(() => {
        const [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)
          }
        }
      })
    },

    backIndex() {
      this.windowId = -1
    }
  }
}
</script>

<style scoped lang='scss'>
.layout {
  .tab {
    display: flex;
    align-items: center;
    margin-left: 20px;
    width: calc(100% - 54px);
    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>