pullDownNav.vue 7.98 KB
<template>
    <div class="nav" ref="navCntrRef">
      <div style="display: inline-block;">
        <div class="nav-item" :ref="'navRef'+index"  v-for="(item,index) in rowList"  :key="item[defaultProps.id]" @click="selectNav(item)"  @contextmenu.prevent="editNav(item)" :class="{'cur-nav':value==item[defaultProps.id]}">
            {{ item[defaultProps.name] }}
            <i class="el-icon-close del-icon" @click.stop="delNav(item)"></i>
        </div>
      </div>
      <div v-if="showInput" class="input-area"><el-input  maxlength="20" show-word-limit ref="addInput" @blur="inputBlur" v-model="addValue" class="input"></el-input></div>
      <div v-else class="nav-item add-icon" style="cursor: pointer;" @click="addNav"><i class="el-icon-plus border"></i></div>
      <div v-if="colList && colList.length" class="nav-item pull-btn" style="cursor: pointer;">
        <i class="el-icon-arrow-down border"></i>
        <div class="pull-down-wrap">
          <div class="pull-down-item"  @contextmenu.prevent="editNav(item)" @click="selectNav(item, 'sort')" :class="{'cur-nav':value==item[defaultProps.id]}" v-for="item in colList" :key="item[defaultProps.id]">
            {{ item[defaultProps.name] }}
            <i class="el-icon-close del-icon" @click.stop="delNav(item)"></i>
        </div>
        </div>
      </div>
      <GLDialog title="重命名" mode="small" :visible.sync="navVisible" @onOk="commitDialog" :before-close="closeDialog" @onCancel="closeDialog">
        <div class="menu-dialog-body">
            <el-form ref="menuRef" label-width="154px" :rules="navRules" :model="navForm">
                <el-form-item label="名称:" prop="name" class="m-t-20">
                    <el-input maxlength="20" show-word-limit style="width: 284px;" :size="size" type="text" v-model="navForm.name" placeholder="请输入"></el-input>
                </el-form-item>
            </el-form>
        </div>
      </GLDialog>
    </div>
</template>
<script>
export default{
    name: 'nav',
    model: {
        prop: 'value', // 默认是value
        event: 'changeValue', // 默认是input
    },
    props: {
        value: {
            type: String
        },
        tabList:{
            type: Array,
            default: () => {
                return []
            }
        },
        defaultProps: {
            type: Object,
            default: ()=>{
                return {
                    id: 'id',
                    name: 'name'
                }
            }
        }
    },
    data(){
        return {
            navRules: {
                name: [
                    {
                    required: true,
                    validator: (rule, value, callback) => {
                        if (!value || (value && !value.trim())) {
                        return callback(new Error('请输入名称'))
                        } else {
                        return callback()
                        }
                    },
                    trigger: 'change'
                    }
                ]
            },
            navForm: {
                name: '',
                id: ''
            },
            navVisible: false,
            curNavId: '',
            addValue: '',
            showInput: false,
            rowList: [],
            colList: []
        }
    },
    watch:{
        tabList:{
            handler(val, oldVal) {
                this.calcWidth()
            },
            deep: true
        }
    },
    mounted(){
        window.addEventListener('resize', this.resizeDom);
    },
    beforeDestroy(){
      window.removeEventListener('resize', this.resizeDom)
    },
    methods:{
        resizeDom(){
            this.calcWidth()
        },
        calcWidth(){
            this.rowList = this.tabList
            let calculated = false
            let mainW = this.$refs?.navCntrRef?.offsetWidth || 0
            let curIdx = -1
            this.$nextTick(()=>{
                this.rowList.reduce((pre, cur,index) => {
                    let curW = this.$refs?.['navRef'+index]?.[0]?.offsetWidth
                    if((pre + curW) > mainW-500 && !calculated) {
                        curIdx = index
                        calculated = true
                    }
                    return pre + curW
                },0);
                this.colList = curIdx >-1 ? this.tabList.slice(curIdx) : []
                this.rowList = curIdx >-1 ? this.tabList.slice(0, curIdx) : this.tabList
            })
        },
        selectNav(item){
            this.$emit('changeValue', item[this.defaultProps.id])
        },
        editNav(item){
           console.log(item)
           this.navForm = {
                id: item[this.defaultProps.id],
                name: item[this.defaultProps.name]
           }
           this.navVisible = true
        },
        commitDialog(){
            const params = {
                [this.defaultProps.id] : this.navForm.id,
                [this.defaultProps.name]: this.navForm.name
            }
            this.$emit('confirmDialog', params)
            this.closeDialog()
        },
        closeDialog(){
            this.navVisible = false
        },
        delNav(item){
            this.$emit('delete', item)
        },
        inputBlur(){
            if(this.addValue) {
                this.$emit('inputBlur', {value: this.addValue})
            }else{
                this.$message.error('请输入内容')
            }
            this.clearData()
        },
        clearData(){
            this.showInput = false
            this.addValue = ''
        },
        addNav(){
            this.showInput = true
            this.$nextTick(()=>{
                this.$refs.addInput.focus()
            })
        }
    }
}
</script>
<style scoped lang="scss">
.nav{
    .input-area{
        display: inline-block;
        height: 40px;
        line-height: 40px;
        border-radius: 4px 4px 0 0;
        margin-right: 1px;
        .input{
        width: 245px;
        /deep/ .el-input__inner{
            vertical-align: baseline;
            box-sizing: border-box;
            border: none;
        }
        }
    }
    .nav-item{
        display: inline-block;
        padding: 0 20px;
        height: 40px;
        line-height: 40px;
        background-color: #F8F8F8;
        border-radius: 4px 4px 0 0;
        margin-right: 1px;
        color: #666666;
        letter-spacing: 0;
        font-size: 14px;
        text-align: center;
        cursor: pointer;
        .del-icon{
        font-size: 12px;
        color: #666;
        margin-left: 12px;
        }
    }
    .cur-nav{
        background-color: #fff;
        color: #5850FF;
        font-weight: 500;
    }
    .add-icon, .pull-btn{
        width: 50px;
        box-sizing: border-box;
        padding: 0;
        position: relative;
        .border{
        border: 1px dashed #999;
        color: #999;
        }
    }
    .pull-btn{
        &:hover{
            .pull-down-wrap{
                display: block;
            }
        }
        .pull-down-wrap{
            display: none;
            position: absolute;
            width: 200px;
            max-height: 200px;
            overflow-y: auto;
            right: 0;
            background-color: #fff;
            z-index: 2;
            box-shadow: 0 3px 6px -4px #0000001f, 0 6px 16px 0 #00000014, 0 9px 28px 8px #0000000d;
            border-radius: 4px;
            .pull-down-item{
                height: 36px;
                display: flex;
                justify-content: space-between;
                align-items: center;
                font-weight: 400;
                padding: 0 15px;
                font-size: 14px;
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                color: #666666;
                background-color: #F8F8F8;
                &:hover{
                    background-color: #f5f7fa;
                }
            }
            .cur-nav{
                background-color: #fff;
                color: #5850FF;
                font-weight: 500;
            }
        }
    }
}
</style>