brand-cate.vue 11.2 KB
<template>
    <div class="cate-page" id='cate-page'>
        <header-box title="ALL CATEGORIES" ref="header" v-if="!hideHeader"></header-box>
        <div class="cate-container clearfix">
            <div class="content">
                <ul class="primary-level">
                    <li v-for="(ca, index) in cateNavLeftData" :key="index" class="ellipsis p-level-item" :class="{focus: index === leftcurrent}" @click='cateNavLeftFun(index, ca.relation_parameter.sort, ca.category_name)'>
                        {{ca.category_name}}
                    </li>
                </ul>
                <div class="sub-level-container">
                    <ul class="sub-level">
                        <li :class="{'sub-checked': rightAll.checked}">
                            <div class="block ellipsis">
                                <a v-if="jump" :href="`/product/list?sort=${rightAll.sortId || ''}&sort_name=全部${rightAll.categoryName || ''}&gender=${gender}`">全部{{rightAll.categoryName}}</a>
                                <a v-else @click="noJumpReturn(rightAll.sortId, '全部' + rightAll.categoryName, true)">全部{{rightAll.categoryName}}</a>
                            </div>
                            <div class="checked">
                                <i class="icon icon-check"></i>
                            </div>
                        </li>
                    </ul>
                    <ul class="sub-level">
                        <li v-for="(sub, index) in cateNavRightData" :key="index" :class="{'sub-checked': sub.checked}">
                            <div class="block ellipsis">
                                <a class="ellipsis" v-if="jump" :href="`/product/list?sort=${sub.relation_parameter.sort}&sort_name=${sub.category_name}&gender=${gender}`">{{sub.category_name}}</a>
                                <a v-else @click="noJumpReturn(sub.relation_parameter.sort, sub.category_name)">{{sub.category_name}}</a>
                            </div>
                            <div class="checked">
                                <i class="icon icon-check"></i>
                            </div>
                        </li>
                    </ul>
                </div>
            </div>
        </div><!--/end cate-container-->
    </div>
</template>
<style>
.cate-page {
    font-size: 36px;
    font-family: helvetica, Arial, "黑体";

    .cate-container {
    }

    ul,
    li {
        position: relative;
        margin: 0;
        padding: 0;
        list-style: none;
    }

    .search-input {
        position: relative;
        background-color: #f8f8f8;
        padding: 13px 20px;

        p {
            box-sizing: border-box;
            width: 100%;
            height: 60px;
            line-height: 60px;
            border: none;
            padding-left: 66px;
            border-radius: 60px;
            font-size: 26px;
            background: #fff;
            color: #999;
        }
    }

    .search-icon {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 43px;
        line-height: 86px;
        color: #999;
    }

    .cate-nav {
        height: 120px;
        border-bottom: 1px solid #e6e6e6;
        font-size: 24px;

        li {
            display: block;
            box-sizing: border-box;
            float: left;
            height: 100%;
            padding: 20px 0;
            width: 25%;
            text-align: center;
            color: #999;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;

            &:last-child {
                border-right: none;
            }

            &.focus {
                color: #000;

                span {
                    border-bottom: 4px solid #000;
                    font-weight: bold;
                }
            }

            &.bytouch {
                background: #eee;
            }
        }

        span {
            line-height: 80px;
            padding-bottom: 10px;
        }

        li:last-child span {
            border-right: 0;
        }
    }

    .content {
        &.hide {
            display: none;
        }
    }

    .primary-level {
        float: left;
        box-sizing: border-box;
        width: 50%;
        height: 100%;
        font-size: 34px;
        background-color: #fff;

        > li {
            height: 108px;
            line-height: 108px;
            margin-left: 30px;
            border-bottom: 1px solid #eee;
            user-select: none;
            color: #000;

            &.focus {
                background-color: #fff;

                &:after {
                    content: "";
                    width: 0;
                    height: 0;
                    border-top: 20px solid transparent;
                    border-bottom: 20px solid transparent;
                    border-right: 20px solid #f6f6f6;
                    position: absolute;
                    margin-top: 32px;
                    right: 0%;
                }
            }

            &.highlight {
                background-color: #eee;
            }
        }
    }

    .sub-level-container {
        float: left;
        box-sizing: border-box;
        background: #f6f6f6;
        width: 50%;
        height: 100%;
    }

    .sub-level {
        width: 100%;
        background-color: #f6f6f6;
        position: relative;
        font-size: 32px;

        &.hide {
            display: none;
        }

        > li {
            box-sizing: border-box;
            height: 88px;
            line-height: 88px;
            border-bottom: 1px solid #e6e6e6;
            padding-left: 30px;
            user-select: none;
            white-space: nowrap;
            overflow-y: auto;
            display: flex;

            &.highlight {
                background: #eee;
            }

            &.sub-checked {
                .icon {
                    display: inline !important;
                }
            }

            &:hover {
                background-color: #efefef;
            }

            .block {
                flex: 1;
            }

            .checked {
                width: 100px;
                text-align: center;
                color: #000;

                .icon {
                    display: none;
                }
            }

            a {
                color: #b0b0b0;
                display: block;
                width: 100%;
            }
        }
    }
}
</style>
<script>
    import $ from 'jquery';
    import HeaderBox from 'component/header.vue';

    export default {
        props: {
            category: {
                type: Array
            },
            jump: {
                type: Boolean
            },
            gender: {
                type: String
            },
            value: {
                type: Array,
                default() {
                    return [];
                }
            },
            hideHeader: Boolean
        },
        data() {
            return {
                cateNavLeftData: [], // 左侧分类数据
                cateNavRightData: [], // 右侧分类数据
                leftcurrent: 0, // 标记当前左侧选中条目
                rightAll: {}, // 全部XX
                currentValue: this.value
            };
        },
        watch: {
            value(val) {
                this.currentValue = val;
                this.categoryChangeHandler();
            },
            category() {
                this.categoryChangeHandler();
            }
        },
        methods: {
            cateNavLeftFun(index, categoryId, categoryName) {
                this.leftcurrent = index;
                this.cateNavRightData = this.cateNavLeftData[index].sub;
                let allSorts = this.getAllSortId(this.cateNavRightData);

                this.rightAll = {
                    sortId: allSorts,
                    categoryName: categoryName ? categoryName : '',
                    checked: this.currentValue.some(cate => {
                        return cate.id.toString() === allSorts;
                    })
                };
                this.cateNavRightData.forEach(right => {
                    right.checked = this.currentValue.some(cate => {
                        return cate.id.toString() === right.relation_parameter.sort;
                    });
                });
            },

            /* 筛选列表使用返回值 */
            noJumpReturn(categoryId, categoryName, isall) {
                let cateIndex = this.currentValue.findIndex(cate => cate.id.toString() === categoryId);

                if (cateIndex >= 0) {
                    this.currentValue.splice(cateIndex, 1);
                } else {
                    this.currentValue.push({
                        id: categoryId,
                        name: categoryName
                    });
                }
                if (isall) {
                    this.cateNavRightData.forEach(cate => {
                        let cateRmIndex = this.currentValue
                            .findIndex(c => c.id.toString() === cate.relation_parameter.sort);

                        if (cateRmIndex >= 0) {
                            this.currentValue.splice(cateRmIndex, 1);
                        }
                    });
                } else if (this.rightAll.checked) {
                    let cateRmIndex = this.currentValue.findIndex(c => c.id.toString() === this.rightAll.sortId);

                    if (cateRmIndex >= 0) {
                        this.currentValue.splice(cateRmIndex, 1);
                    }
                }
                this.$emit('select', {
                    value: this.currentValue,
                    subType: 'group_sort'
                });
            },

            categoryChangeHandler() {
                if (!this.category || !this.category.length) {
                    return;
                }
                this.cateNavLeftData = this.category;
                let selectLeftCate = this.cateNavLeftData[this.leftcurrent] || {};

                this.cateNavRightData = [];
                this.$nextTick(() => {
                    this.cateNavRightData = selectLeftCate.sub;

                    this.cateNavRightData.forEach(cate => {
                        cate.checked = this.currentValue
                            .some(selCate =>
                                selCate.id.toString() === cate.relation_parameter.sort);
                    });

                    let allSorts = this.getAllSortId(this.cateNavRightData);

                    this.rightAll = this.cateNavLeftData ? {
                        sortId: allSorts,
                        categoryName: selectLeftCate.category_name,
                        checked: this.currentValue.some(cate => cate.id.toString() === allSorts)
                    } : {};
                });
            },
            getAllSortId(sub) {
                return sub.map(sort=>sort.relation_parameter.sort).join(',') + ',';
            }
        },
        created() {
            this.categoryChangeHandler();

            window.addEventListener('touchstart', () => {
                const c = $('.content');
                const h = c.height();
                const h1 = document.body.offsetHeight;

                if (h <= h1) {
                    c.css('height', h1);
                }
            });
        },
        components: {HeaderBox}
    };
</script>