good.js 2.07 KB
/**
 * 购物车商品
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/20
 */

var $ = require('jquery'),
    ellipsis = require('mlellipsis'),
    lazyLoad = require('yoho.lazyload');

var chosePanel = require('./chose-panel');

function docTouchEvt(e) {
    var $tar = $(e.target);

    if ($tar.closest('.opt-panel').length === 0) {
        $('.opt-panel:not(.hide)').addClass('hide');

        //
        $(document).unbind('tap', docTouchEvt);
    }
}

// function unbindDocTouchEvt(e) {
//     $(document).unbind('tap', docTouchEvt);
// }

ellipsis.init();

lazyLoad($('.lazy'));

$('.name')[0].mlellipsis(2);

//TIP:事件委托在.cart-goods,商品列表的容器统一需要有.cart-goods
$('.cart-goods').delegate('.checkbox', 'tap', function() {
    var $this = $(this);

    if ($this.hasClass('icon-cb-checked')) {
        $this.removeClass('icon-cb-checked').addClass('icon-checkbox');
    } else {
        $this.removeClass('icon-checkbox').addClass('icon-cb-checked');
    }
}).delegate('.icon-edit', 'tap', function() {
    var id = $(this).closest('.shopping-cart-good').data('id');

    $.ajax({
        type: 'GET',
        url: '/shoppingCart/goodinfo',
        data: {
            id: id
        },
        success: function(data) {
            if (data.code === 200) {
                chosePanel.show(data.data);
            }
        }
    });
}).delegate('.icon-del', 'tap', function(e) {
    e.stopPropagation();

    $(this).closest('.shopping-cart-good').children('.opt-panel').removeClass('hide');

    $(document).bind('tap', docTouchEvt);
}).delegate('.opt-panel', 'tap', function() {
    var $this = $(this),
        id = $this.closest('.shopping-cart-good').data('id');

    if ($this.closest('.put-in-favorite')) {

        //移入收藏夹
        $.ajax({
            type: 'POST',
            url: '/shoppingCart/col',
            data: {
                id: id
            }
        });
    } else {

        //删除
        $.ajax({
            type: 'POST',
            url: '/shoppingCart/del',
            data: {
                id: id
            }
        });
    }
});