fav.js 3.05 KB
/**
 * 个人中心--收藏
 * @author: chenglong<chenglong.wang@yoho.cn>
 * @date: 2015/11/12
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
    Swiper = require('yoho.iswiper');

var diaLog = require('./dialog');

var $navLi = $('#fav-tab > li'),
    $favContainer = $('.fav-content > .fav-type'),
    $swiperList = $('.swiper-container'),
    swiperObj = {},
    favTabHammer,
    favContentHammer;

function showFavTab(index) {
    var i,
        id;

    $navLi.filter('.active').removeClass('active');
    $navLi.eq(index).addClass('active');

    $favContainer.filter('.show').removeClass('show');
    $favContainer.eq(index).addClass('show');

    if (index === 1) {

        //导航
        for (i = 0; i < $swiperList.length; i++) {
            id = $swiperList.eq(i).attr('data-id');
            swiperObj[id] = new Swiper('#swiper-container-' + id, {
                slidesPerView: 'auto',
                grabCursor: true,
                slideElement: 'li',
                wrapperClass: 'swiper-wrapper-' + id,
                lazyLoading: true,
                watchSlidesVisibility: true
            });
        }

    }
}

// 如果从品牌收藏入口进入
if ($('#fav-tab').hasClass('brand-tab')) {
    showFavTab(1);
} else {
    showFavTab(0);
}

favTabHammer = new Hammer(document.getElementById('fav-tab'));
favTabHammer.on('tap', function(e) {
    var $cur = $(e.target).closest('li'),
        index;

    if ($cur.length === 0 || $cur.hasClass('active')) {
        return;
    }

    index = $cur.index();
    showFavTab(index);

});

//删除收藏的商品
favContentHammer = new Hammer(document.getElementById('fav-content'));

favContentHammer.on('tap', function(e) {
    var id = '';

    if (!$(e.target).hasClass('del-fav')) {
        return;
    }

    diaLog.showDialog({
        dialogText: '您确定要取消收藏吗?',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
        }
    }, function() {
        id = $(e.target).closest('li').attr('data-id');
        $.ajax({
            method: 'post',
            url: '/home/favoriteDel',
            data: {
                id: id
            }
        }).then(function(data) {

            if (data.code === 200) {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: '已经取消收藏'
                });
                $(e.target).closest('li').remove();
            } else if (data.code === 400) {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: data.message
                });
            } else {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: '取消收藏失败'
                });
            }
        }).fail(function() {

            //TODO
        });
    });
});