Blame view

static/js/me/fav.js 6.74 KB
lore-w authored
1 2 3 4 5 6 7 8
/**
 * 个人中心--收藏
 * @author: chenglong<chenglong.wang@yoho.cn>
 * @date: 2015/11/12
 */

var $ = require('jquery'),
    Hammer = require('yoho.hammer'),
lore-w authored
9
    Swiper = require('yoho.iswiper');
lore-w authored
10
lore-w authored
11 12
var diaLog = require('./dialog');
lore-w authored
13
var $navLi = $('#fav-tab > li'),
lore-w authored
14
    $favContainer = $('.fav-content > .fav-type'),
15
    $swiperList = '',
lore-w authored
16 17
    swiperObj = {},
    favTabHammer,
18 19 20 21 22 23
    favContentHammer,
    footerH = $('#yoho-footer').height(),
    $loadMore = $('.fav-load-more'),
    $brandLoadMore = $('.fav-brand-load-more'),
    winH = $(window).height(),
    $favProductList = $('.fav-product-list'),
24
    $favBrandList = $('.fav-brand-swiper-wrapper'),
25 26 27 28 29
    pageId = 1,
    brandPageId = 1, //收藏品牌的当前页数
    lockId = true,
    brandLockId = true, //收藏品牌是否可下拉加载更多
    brandTab = false; //当前是否停留在收藏品牌页
lore-w authored
30
lore-w authored
31
function showFavTab(index) {
lore-w authored
32
    $navLi.filter('.active').removeClass('active');
lore-w authored
33
    $navLi.eq(index).addClass('active');
lore-w authored
34
lore-w authored
35 36
    $favContainer.filter('.show').removeClass('show');
    $favContainer.eq(index).addClass('show');
37
}
lore-w authored
38
39 40 41 42 43 44 45 46 47 48 49
//初始化swiper
function initSwiper() {
    var i,
        id;

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

        if (!!swiperObj[id]) {
            swiperObj[id].destroy(true, true);
lore-w authored
50
        }
51 52 53 54 55 56 57 58 59 60
        swiperObj[id] = new Swiper('#swiper-container-' + id, {
            slidesPerView: 'auto',
            grabCursor: true,
            slideElement: 'li',
            wrapperClass: 'swiper-wrapper-' + id,
            lazyLoading: true,
            watchSlidesVisibility: true
        });
    }
}
lore-w authored
61
62 63 64 65 66 67
// 上拉加载更多
function loadData($parent, url, page) {
    if (url === 'favBrand') {
        brandLockId = true;
    } else {
        lockId = true;
lore-w authored
68
    }
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    $.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 === ' ') {
lore-w authored
86
                $parent.closest('.fav-type').find('.fav-content-loading').addClass('hide');
87 88 89 90 91 92 93
                $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);
94
                $parent.closest('.fav-type').find('.fav-content-loading').remove();
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                if (url === 'favBrand') {
                    initSwiper();//如果是收藏品牌需要初始化swiper

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

                window.rePosFooter();

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

            //},1000);
        }
    });
lore-w authored
113 114 115 116 117
}

// 如果从品牌收藏入口进入
if ($('#fav-tab').hasClass('brand-tab')) {
    showFavTab(1);
118 119 120
    loadData($favBrandList, 'favBrand', 1);
    brandTab = true;
    window.rePosFooter();
lore-w authored
121 122
} else {
    showFavTab(0);
123 124 125
    loadData($favProductList, 'favProduct', 1);
    brandTab = false;
    window.rePosFooter();
lore-w authored
126 127 128 129 130 131 132 133 134 135 136 137
}

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

    if (index === 0) {
140
        brandTab = false;
141 142 143 144 145
        if ($favProductList.find('li').length === 0 &&
            $favProductList.closest('.fav-type').find('.fav-null-box').hasClass('hide')) {
            loadData($favProductList, 'favProduct', 1);
        }
    } else {
146
        brandTab = true;
147 148 149 150 151
        if ($favBrandList.find('div').length === 0 &&
            $favBrandList.closest('.fav-type').find('.fav-null-box').hasClass('hide')) {
            loadData($favBrandList, 'favBrand', 1);
        }
    }
lore-w authored
152
    showFavTab(index);
153
    window.rePosFooter();
lore-w authored
154
lore-w authored
155
});
lore-w authored
156 157 158 159

//删除收藏的商品
favContentHammer = new Hammer(document.getElementById('fav-content'));
lore-w authored
160
favContentHammer.on('tap', function(e) {
lore-w authored
161 162
    var id = '';
lore-w authored
163 164 165
    if (!$(e.target).hasClass('del-fav')) {
        return;
    }
166
lore-w authored
167 168 169 170 171 172 173
    diaLog.showDialog({
        dialogText: '您确定要取消收藏吗?',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
        }
    }, function() {
lore-w authored
174 175 176 177 178 179 180
        id = $(e.target).closest('li').attr('data-id');
        $.ajax({
            method: 'post',
            url: '/home/favoriteDel',
            data: {
                id: id
            }
lore-w authored
181
        }).then(function(data) {
lore-w authored
182 183

            if (data.code === 200) {
lore-w authored
184 185 186 187 188
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: '已经取消收藏'
                });
lore-w authored
189
                $(e.target).closest('li').remove();
lore-w authored
190 191 192 193 194 195 196 197 198 199 200 201
            } else if (data.code === 400) {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: data.message
                });
            } else {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: '取消收藏失败'
                });
lore-w authored
202
            }
lore-w authored
203
        }).fail(function() {
lore-w authored
204 205

            //TODO
206 207 208 209 210

            diaLog.showDialog({
                autoHide: true,
                dialogText: '网络错误~'
            });
lore-w authored
211
        });
lore-w authored
212
    });
213 214
});
215
function scrollHandler() {
216 217 218 219 220 221 222 223

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

        if (brandTab) {
            $brandLoadMore.filter('.hide').removeClass('hide');

            if (!brandLockId) {
                brandPageId++;
224
                loadData($favBrandList, 'favBrand', brandPageId);
225 226 227 228 229 230 231 232 233 234 235 236
            }

        } else {

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

            if (!lockId) {
                pageId++;
                loadData($favProductList, 'favProduct', pageId);
            }
        }
    }
237 238 239 240 241
}

//srcoll to load more
$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
lore-w authored
242
});