fav.js 6.3 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 = '',
    swiperObj = {},
    favTabHammer,
    favContentHammer,
    footerH = $('#yoho-footer').height(),
    $loadMore = $('.fav-load-more'),
    $brandLoadMore = $('.fav-brand-load-more'),
    winH = $(window).height(),
    $favProductList = $('.fav-product-list'),
    $favBrandList = $('.fav-brand-swiper'),
    pageId = 1,
    brandPageId = 1, //收藏品牌的当前页数
    lockId = true,
    brandLockId = true, //收藏品牌是否可下拉加载更多
    brandTab = false; //当前是否停留在收藏品牌页

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

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

//初始化swiper
function initSwiper() {
    var i,
        id;

    $swiperList = $('.swiper-container');
    for (i = 0; i < $swiperList.length; i++) {
        id = $swiperList.eq(i).attr('data-id');
        console.log(id);

        if (!!swiperObj[id]) {
            swiperObj[id].destroy(true, true);
        }
        swiperObj[id] = new Swiper('#swiper-container-' + id, {
            slidesPerView: 'auto',
            grabCursor: true,
            slideElement: 'li',
            wrapperClass: 'swiper-wrapper-' + id,
            lazyLoading: true,
            watchSlidesVisibility: true
        });
    }
}

// 上拉加载更多
function loadData($parent, url, page) {
    if (url === 'favBrand') {
        brandLockId = true;
    } else {
        lockId = true;
    }
    $.ajax({
        method: 'post',
        url: '/home/' + url,
        data: {
            page: page
        },
        success: function(data) {

            //setTimeout(function() { //模拟延时

            if (url === 'favBrand') {
                $brandLoadMore.addClass('hide');
            } else {
                $loadMore.addClass('hide');
            }

            if (data === ' ') {
                $parent.closest('.fav-type').find('.fav-null-box').removeClass('hide');
            } else if (data === 'end') {
                $parent.closest('.fav-type').find('.fav-load-background')
                    .removeClass('fav-load-background').html('没有更多了');

            } else if (data.length > 10) {
                $parent.append(data);

                if (url === 'favBrand') {
                    initSwiper();//如果是收藏品牌需要初始化swiper

                    brandLockId = false;//请求成功后解锁品牌收藏page++
                } else {
                    lockId = false;//请求成功后解锁商品收藏page++
                }

                window.rePosFooter();

            } else {
                return;
            }
            window.rePosFooter();

            //},1000);
        }
    });
}

// 如果从品牌收藏入口进入
if ($('#fav-tab').hasClass('brand-tab')) {
    showFavTab(1);
    loadData($favBrandList, 'favBrand', 1);
    brandTab = true;
    window.rePosFooter();
} else {
    showFavTab(0);
    loadData($favProductList, 'favProduct', 1);
    brandTab = false;
    window.rePosFooter();
}

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();

    if (index === 0) {
        if ($favProductList.find('li').length === 0 &&
            $favProductList.closest('.fav-type').find('.fav-null-box').hasClass('hide')) {
            loadData($favProductList, 'favProduct', 1);
        }
    } else {
        if ($favBrandList.find('div').length === 0 &&
            $favBrandList.closest('.fav-type').find('.fav-null-box').hasClass('hide')) {
            loadData($favBrandList, 'favBrand', 1);
        }
    }
    showFavTab(index);
    window.rePosFooter();

});

//删除收藏的商品
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
        });
    });
});

// 上拉加载更多
$(document).scroll(function() {

    if ($(window).scrollTop() + winH >= $(document).height() - footerH) {

        if (brandTab) {

            $brandLoadMore.filter('.hide').removeClass('hide');

            if (!brandLockId) {

                brandPageId++;
            }

        } else {

            $loadMore.filter('.hide').removeClass('hide');

            if (!lockId) {
                console.log(222);
                pageId++;
                loadData($favProductList, 'favProduct', pageId);
            }
        }
    }
});