chose-panel.js 10.6 KB
/**
 * 购物车选择尺寸、颜色和数量面板
 * 显示时构造当前商品信息的HTML插入yoho-page;消失则是直接清除HTML
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/21
 */

// bikai
// 增加init函数,异步请求的接口需要重新初始化一下选择列表
// 异步渲染的模板统一插入 #chose-panel ,兼容页面多个选择框

var $ = require('jquery'),
    tip = require('../plugin/tip'),
    loading = require('../plugin/loading');

var $chosePanel = $('#chose-panel'),
    $num,
    $chosed,
    $imgsThumb,
    $leftNum,
    leftNum,
    confirming,
    curColorIndex,
    curSizeIndex,
    hasChooseColor,
    hasChooseSize,
    $curSizeBlock,
    $sizeRowList,
    $curColorBlock,
    $colorRowList,
    cbFn,
    $allChoseItems,
    queryString,
    $soonSoldOut = $('.soonSoldOut-tag'),
    $yohoPage = $('.yoho-page');

//初始化购物车面板显示
function init() {
    hasChooseColor = false;
    hasChooseSize = false;
    $curSizeBlock = null;
    queryString = $.queryString();
    $imgsThumb = $('.chose-panel').find('.thumb');
    $allChoseItems = $('.chose-items');
    $sizeRowList = $('.size-list ul');
    $colorRowList = $('.color-list ul');
    $leftNum = $('#left-num');
    curColorIndex = 0;
    curSizeIndex = 0;
}

function checkColorSizeNum() {
    if (!hasChooseColor && !hasChooseSize) {
        tip.show('请选择颜色和尺码~');
        return false;
    } else if (!hasChooseColor) {
        tip.show('请选择颜色~');
        return false;
    } else if (!hasChooseSize) {
        tip.show('请选择尺码~');
        return false;
    }
    return true;
}


function show(html, cb) {
    var scrollPosition = [
            document.documentElement.scrollLeft || document.body.scrollLeft,
            document.documentElement.scrollTop || document.body.scrollTop
        ],
        $html = $('html, body'); // it would make more sense to apply this to body, but IE7 won't have that

    $html.data('scroll-position', scrollPosition);
    $html.data('previous-overflow', $html.css('overflow'));
    $html.css('overflow', 'hidden');
    $html.css('height', '100%');
    window.scrollTo(scrollPosition[0], scrollPosition[1]);

    if (html) {
        $chosePanel.html(html);
        if ($('#promotionId').val() !== '') {
            $yohoPage.off('touchstart', '.btn-minus');
            $yohoPage.off('touchstart', '.btn-plus');
        }
        init();
    }
    $('.chose-panel').show();
    $num = $('#good-num');
    cbFn = cb;
}

//隐藏当前Panel
function hide() {
    var $html = $('html, body'),
        scrollPosition = $html.data('scroll-position');

    // un-lock scroll position
    $html.css('overflow', $html.data('previous-overflow'));
    $html.css('height', 'auto');
    window.scrollTo(scrollPosition[0], scrollPosition[1]);
    $('.chose-panel').hide();
}

//修改加入购物车的文字和背景
function updateConformButtonClassAndText() {
    $chosed = $allChoseItems.find('.chosed');
    if (2 === $chosed.closest('.zero-stock').length) {
        $('#chose-btn-sure').css('background-color', '#c0c0c0').html('已售罄');
    } else {
        $('#chose-btn-sure').css('background-color', '#eb0313').html('加入购物车');
    }
}

//显示剩余件数
function displayGoodNum(curGoodNum) {

    //数量大于0
    if (curGoodNum > 0) {
        if ($soonSoldOut.length > 0) {
            $allChoseItems.find('.num .left-num').html('即将售罄');
        } else {
            $allChoseItems.find('.num .left-num').html('剩余' + curGoodNum + '件');
        }

        $leftNum.val(curGoodNum);

        //数量小于0
    } else {
        $allChoseItems.find('.num .left-num').html('');
        $leftNum.val(0);
    }
}

//老的选中尺码去掉勾选,新的选中尺码加上勾选
function changeSizeChosed(oldSizeIndex) {
    if (curColorIndex && $curSizeBlock && $curSizeBlock.length > 0) {
        $curSizeBlock.removeClass('chosed');
        $curSizeBlock = $($sizeRowList.eq(oldSizeIndex).children().get(curColorIndex - 1));
        $curSizeBlock.addClass('chosed');
        return $curSizeBlock.data('num');
    }
    return 0;
}

//老的选中颜色去掉勾选,新的选中颜色加上勾选
function changeColorChosed(oldColorIndex) {
    if (curSizeIndex && $curColorBlock && $curColorBlock.length > 0) {
        $curColorBlock.removeClass('chosed');
        $curColorBlock = $($colorRowList.eq(oldColorIndex).children().get(curSizeIndex - 1));
        $curColorBlock.addClass('chosed');
        return $curColorBlock.data('num');
    }
    return 0;
}

init();

$yohoPage.on('touchstart', '.chose-panel', function(e) {
    var $cur = $(e.target);

    if ($cur.closest('.main').length > 0) {
        return;
    }

    //点击蒙版消失
    hide();
});

$yohoPage.on('touchstart', '.color-list .block', function() {
    var $this = $(this),
        index = $this.index(),
        curGoodNum;

    // 当前颜色已经是选中状态,再点击时
    if ($this.hasClass('chosed')) {

        //清空剩余件数的提示
        $allChoseItems.find('.num .left-num').html('');
        $leftNum.val(0);
        hasChooseColor = false;

        //当前尺码行隐藏
        $sizeRowList.eq(curSizeIndex).removeClass('show').addClass('hide');

        //目标尺码行显示
        $sizeRowList.eq(0).removeClass('hide').addClass('show');

        curSizeIndex = 0;

        //老的选中尺码去掉勾选,新的选中尺码加上勾选
        changeSizeChosed(0);

        // 当前颜色不是选中状态,选中时
    } else {
        hasChooseColor = true;

        //老的选中尺码去掉勾选,新的选中尺码加上勾选
        curGoodNum = changeSizeChosed(index + 1);

        // 显示剩余数量
        displayGoodNum(curGoodNum);

        // 尺码行当前行隐藏
        $sizeRowList.eq(curSizeIndex).removeClass('show').addClass('hide');

        //尺码对应行显示
        $sizeRowList.eq(index + 1).removeClass('hide').addClass('show');

        curSizeIndex = index + 1;
        $curColorBlock = $this;

        // 修改颜色时修改商品图片
        $imgsThumb.addClass('hide').eq(index).removeClass('hide');
    }

    // 颜色块切换勾选样式
    $this.siblings('.chosed').removeClass('chosed');
    $this.toggleClass('chosed');
    $('#good-num').val(1);

    // 设置按钮的样式和文字
    updateConformButtonClassAndText();
}).on('touchstart', '.size-list .block', function() {
    var $this = $(this),
        index = $this.index(),
        curGoodNum;

    // 当前尺码已经是选中状态,再点击时
    if ($this.hasClass('chosed')) {

        //清空剩余件数的提示
        $allChoseItems.find('.num .left-num').html('');
        $leftNum.val(0);
        hasChooseSize = false;

        //当前颜色行隐藏
        $colorRowList.eq(curColorIndex).removeClass('show').addClass('hide');

        //目标颜色行显示
        $colorRowList.eq(0).removeClass('hide').addClass('show');

        curColorIndex = 0;

        //老的选中颜色去掉勾选,新的选中颜色加上勾选
        changeColorChosed(0);

        // 当前尺码不是选中状态,选中时
    } else {
        hasChooseSize = true;

        //老的选中颜色去掉勾选,新的选中颜色加上勾选
        curGoodNum = changeColorChosed(index + 1);

        // 显示剩余数量
        displayGoodNum(curGoodNum);

        // 颜色当前行隐藏
        $colorRowList.eq(curColorIndex).removeClass('show').addClass('hide');

        //颜色对应行显示
        $colorRowList.eq(index + 1).removeClass('hide').addClass('show');

        curColorIndex = index + 1;
        $curSizeBlock = $this;
    }

    // 颜色块切换勾选样式
    $this.siblings('.chosed').removeClass('chosed');
    $this.toggleClass('chosed');
    $('#good-num').val(1);

    // 设置按钮的样式和文字
    updateConformButtonClassAndText();
});

$yohoPage.on('touchstart', '.btn-minus', function() {
    var num = parseInt($num.val(), 10);

    leftNum = $('#left-num').val();

    if (!checkColorSizeNum()) {
        return;
    }

    if (num === 1 || 0 === leftNum - 0) {
        tip.show('您选择的数量不能为零~');
        return;
    }
    if (num < 0) {
        tip.show('您选择的数量不能为负数~');
        return;
    }

    $num.val(num - 1);
}).on('touchstart', '.btn-plus', function() {
    var num = parseInt($num.val(), 10);

    leftNum = $('#left-num').val();

    if (!checkColorSizeNum()) {
        return;
    }

    if (num - 0 === leftNum || 0 === leftNum) {
        return;
    }

    //TODO:库存数验证
    if (num > leftNum - 1) {
        tip.show('您选择的数量超过了最大库存量~');
        return;
    }
    $num.val(num + 1);
}).on('touchstart', '#chose-btn-sure', function() {

    var productSku,
        buyNumber = $('#good-num').val() - 0,

        promotionId,
        isEdit = 0,
        num = parseInt($num.val(), 10);

    //颜色尺码没有选择
    if (!checkColorSizeNum()) {
        return;
    }

    //TODO status change
    if ($('#chose-btn-sure').html() === '已售罄') {
        return;
    }

    leftNum = $('#left-num').val() - 0;

    if (num > leftNum) {
        tip.show('您选择的数量超过了最大库存量~');
        return;
    }
    if (num < 0) {
        tip.show('您选择的数量小于一件~');
        return;
    }
    $chosed = $('.block-list>ul>li.chosed');

    if (2 === $chosed.length && 0 === $chosed.closest('.zero-stock').length) {
        productSku = $curSizeBlock.data('skuid');
        promotionId = $('#promotionId').val();
        if (confirming) {
            return false;
        }

        confirming = true;
        loading.showLoadingMask();
        $.ajax({
            method: 'POST',
            url: '/cart/index/add',
            data: {
                productSku: productSku,
                buyNumber: buyNumber,
                promotionId: promotionId,
                isEdit: isEdit,
                cartType: queryString.cartType
            }
        }).done(function(res) {
            var cartNum;

            loading.hideLoadingMask();
            if (res.code === 200) {
                cartNum = res.data.goods_count;
                if (cartNum > 99) {
                    cartNum = '99+';
                }
                $('.num-tag').html(cartNum).removeClass('hide');
                confirming = false;

                if (cbFn) {
                    cbFn();
                }
            }
            if (res.message) {
                tip.show(res.message);
            }

            hide();
        }).fail(function() {
            tip.show('网络出了点问题~');
        }).always(function() {
            confirming = false;
        });
    }

});

exports.init = init;
exports.show = show;