longCourseCenter.vue 7.44 KB
<template>
  <div class="layout">
    <div class="tab" ref="tabbox">
      <div
        :class="['tab-item', { 'item-active': windowId == -1 }]"
        @click="windowId = -1"
      >
        长课程配置
      </div>
      <div
        :class="['tab-item', { 'item-active': windowId == -2 }]"
        @click="windowId = -2"
      >
        已配置课程
      </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">
      <configure-task
        v-show="windowId == -1"
        ref="configureTask"
        @openWindow="openWindow"
      />
      <courseList
        v-show="windowId == -2"
        ref="configureTask"
        @openWindow="openWindow"
      />
      <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"
          :query-data="i.data"
          @backIndex="backIndex"
        />
      </template>
    </div>
  </div>
</template>

<script type="text/ecmascript-6">
// import clockSalaryIndex from './components/clockSalaryIndex/index'
import configureTask from "./configureTask"
import courseList from "./courseList"
import storeInfo from './components/storeInfo.vue'
import indicatorInfo from './components/indicatorInfo.vue'
export default {
  name: "",
  components: {
    configureTask,
    courseList,
    storeInfo,
    indicatorInfo
  },
  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[`page-${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) {
      console.log(data)
      const id = data.pageId ? data.pageId : `${data.metricName}_${data.itemBrand}`
      const windowList = [...this.tab, ...this.dropdownList]
      const windowItem = windowList.find((e) => e.id == id)
      // windowItem.
      let component = data.activeName,
        title = data.metricName+data.itemBrand || '门店详情'
      if (data.componentName) component = data.componentName
      if (windowItem) {
        windowItem.data=data
        this.$refs[`page-${id}`]?.[0]?.setActiveName && this.$refs[`page-${id}`]?.[0]?.setActiveName(data)
        return this.windowId = id
      }
      if (windowList.length == 20) return this.$message.error("最多同时打开20个tab")
      const item = {
        text: title,
        data,
        id,
        component
      }
      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
      this.$refs['inStoreManageIndex']?.handleSearch()
    }
  }
}
</script>

<style scoped lang="scss">
.layout {
  height: 100%;
  .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(100% - 40px);
    // 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>