favorite.page.js 1.81 KB
/**
 *
 * @author: jiangfeng<jeff.jiang@yoho.cn>
 * @date: 2016/7/19
 */

var lazyload = require('yoho-jquery-lazyload');
var dialog = require('../plugins/dialog');
var _confirm = dialog.Confirm;
var $root = $('.favorite-products');

require('./me');
require('../plugins/check');

function _showConfirm(cb) {
    new _confirm({
        content: '<h1 class="title">确认删除</h1><p>您确定要删除该收藏么?</p>',
        cb: cb
    }).show();
}

function doCancel(ids) {
    _showConfirm(function() {
        $.post('/me/collection/cancel', {
            type: 'product',
            ids: ids
        }, function(data) {
            if (data.code === 200) {
                location.href = '/me/collection';
            }
        });
    });
}

function goodsChoose() {
    if ($(this).parent('.goods-info', $root).hasClass('choose')) {
        $(this).parent('.goods-info', $root).removeClass('choose');
    } else {
        $(this).parent('.goods-info', $root).addClass('choose');
    }
}


function eventBind() {
    $('.check-all', $root).check({
        type: 'checkbox',
        onChange: function(ele, checked) {
            if (checked) {
                $('.goods-info', $root).addClass('choose');
            } else {
                $('.goods-info', $root).removeClass('choose');
            }
        }
    });

    $('.choose-icon', $root).click(goodsChoose);
    $('.goods-img', $root).click(goodsChoose);

    $('.btn.cancel', $root).click(function() {
        var id = $(this).parents('.goods-info').data('id');

        doCancel(id);
    });

    $('.favorite-cancel', $root).click(function() {
        var ids = [];

        $('.goods-info.choose', $root).each(function() {
            ids.push($(this).data('id'));
        });

        doCancel(ids.join(','));
    });
}

$(function() {
    lazyload();
});
eventBind();