Blame view

public/js/home/favorite.page.js 7.89 KB
zhangxiaoru authored
1 2 3 4 5
/**
 * 个人中心--收藏
 * @author: zxr
 * @date: 2016/8/16
 */
郝肖肖 authored
6
require('home/favorite.page.css');
陈峰 authored
7
let $ = require('yoho-jquery'),
zhangxiaoru authored
8 9 10
    Hammer = require('yoho-hammer'),
    Swiper = require('yoho-swiper');
11
let diaLog = require('plugin/dialog');
zhangxiaoru authored
12
13
let tip = require('plugin/tip');
zhangxiaoru authored
14
陈峰 authored
15
let $navLi = $('#fav-tab > li'),
zhangxiaoru authored
16 17 18 19 20 21 22 23 24 25 26
    $favContainer = $('.fav-content > .fav-type'),
    swiperObj = {},
    favTabHammer,
    favContentHammer,
    $loadMore = $('.fav-load-more'),
    $brandLoadMore = $('.fav-brand-load-more'),
    winH = $(window).height(),
    footerH = $('#yoho-footer').height(),
    $favProductList = $('.fav-product-list'),
    $favBrandList = $('.fav-brand-swiper-wrapper'),
    pageId = 1,
zhangxiaoru authored
27
    brandPageId = 1, // 收藏品牌的当前页数
zhangxiaoru authored
28
    lockId = true,
zhangxiaoru authored
29 30
    brandLockId = true, // 收藏品牌是否可下拉加载更多
    brandTab = false; // 当前是否停留在收藏品牌页
zhangxiaoru authored
31
32
require('common');
zhangxiaoru authored
33 34 35 36 37 38 39 40 41

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

    $favContainer.filter('.show').removeClass('show');
    $favContainer.eq(index).addClass('show');
}
zhangxiaoru authored
42
// 初始化swiper
zhangxiaoru authored
43
function initSwiper(data) {
陈峰 authored
44
    let i,
zhangxiaoru authored
45 46 47
        idStrReg = /container-(\d+)['"]{1}/gi,
        idReg = /\d+/,
        idArr = data.match(idStrReg),
48
        idArrLen = idArr && idArr.length || 0,
zhangxiaoru authored
49 50
        containerId;
zhangxiaoru authored
51
    // $swiperList = $('.swiper-container');
zhangxiaoru authored
52 53
    for (i = 0; i < idArrLen; i++) {
zhangxiaoru authored
54
        /* id = $swiperList.eq(i).attr('data-id');
zhangxiaoru authored
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

        if (!!swiperObj[id]) {
            swiperObj[id].destroy(true, true);
        }*/

        containerId = idArr[i].match(idReg)[0];

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

// 上拉加载更多
function loadData($parent, url, page) {
    if (url === 'favBrand') {
        brandLockId = true;
    } else {
        lockId = true;
    }
    $.ajax({
        method: 'get',
        url: '/home/' + url,
        data: {
            page: page
        },
        success: function(data) {
陈峰 authored
87
            let $loadingMask = $parent.closest('.fav-type').find('.fav-content-loading');
zhangxiaoru authored
88 89 90 91 92 93 94

            if (url === 'favBrand') {
                $brandLoadMore.addClass('hide');
            } else {
                $loadMore.addClass('hide');
            }
zhangxiaoru authored
95 96
            if (data.total === 0) {
zhangxiaoru authored
97
                $loadingMask.addClass('hide');
98
                $parent.closest('.fav-type').find('.fav-null-box').removeClass('hide').addClass('show');
zhangxiaoru authored
99
                window.rePosFooter();
zhangxiaoru authored
100
            } else if (data.more === true) {
zhangxiaoru authored
101
zhangxiaoru authored
102
                // 处理data等于end时如果loadingMask存在且没有hide样式的情况
zhangxiaoru authored
103 104 105
                if ($loadingMask && !$loadingMask.hasClass('hide')) {
                    $loadingMask.addClass('hide');
zhangxiaoru authored
106
                    // $parent.closest('.fav-type').find('.fav-null-box').removeClass('hide');
zhangxiaoru authored
107 108 109 110 111 112 113 114 115 116 117
                }

                $parent.closest('.fav-type').find('.fav-load-background')
                    .removeClass('fav-load-background').html('没有更多了');

                // hf: fixes bug to 修改没有数据还调接口,加载错误页面问题
                brandLockId = true;
                lockId = true;
            } else if (data.length > 10) {
                $parent.append(data);
zhangxiaoru authored
118
                // 如果有数据loadingMask会被remove掉
zhangxiaoru authored
119 120
                $loadingMask.remove();
                if (url === 'favBrand') {
zhangxiaoru authored
121
                    initSwiper(data);// 如果是收藏品牌需要初始化swiper
zhangxiaoru authored
122
zhangxiaoru authored
123
                    brandLockId = false;// 请求成功后解锁品牌收藏page++
zhangxiaoru authored
124
                } else {
zhangxiaoru authored
125
                    lockId = false;// 请求成功后解锁商品收藏page++
zhangxiaoru authored
126 127 128 129 130 131 132 133 134
                }
            }
            window.rePosFooter();
        }
    });
}

// 如果从品牌收藏入口进入
if ($('#fav-tab').hasClass('brand-tab')) {
Jian Shi authored
135 136 137
    //  showFavTab(1);
    //  loadData($favBrandList, 'favBrand', 1);
    initSwiper($favBrandList.html());
zhangxiaoru authored
138 139
    brandTab = true;
    window.rePosFooter();
Jian Shi authored
140
    brandLockId = false;// 请求成功后解锁品牌收藏page++
zhangxiaoru authored
141
} else {
Jian Shi authored
142 143
    //  showFavTab(0);
    //  loadData($favProductList, 'favProduct', 1);
zhangxiaoru authored
144 145
    brandTab = false;
    window.rePosFooter();
Jian Shi authored
146
    lockId = false;// 请求成功后解锁商品收藏page++
zhangxiaoru authored
147 148 149 150
}

favTabHammer = new Hammer(document.getElementById('fav-tab'));
favTabHammer.on('tap', function(e) {
陈峰 authored
151
    let $cur = $(e.target).closest('li'),
zhangxiaoru authored
152 153 154 155 156 157 158 159 160 161 162
        index;

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

    index = $cur.index();

    if (index === 0) {
        brandTab = false;
        if ($favProductList.find('li').length === 0 &&
Jian Shi authored
163
            !$favProductList.closest('.fav-type').find('.fav-null-box').hasClass('show')) {
zhangxiaoru authored
164 165 166 167 168
            loadData($favProductList, 'favProduct', 1);
        }
    } else {
        brandTab = true;
        if ($favBrandList.find('div').length === 0 &&
Jian Shi authored
169
            !$favBrandList.closest('.fav-type').find('.fav-null-box').hasClass('show')) {
zhangxiaoru authored
170 171 172 173 174 175 176 177
            loadData($favBrandList, 'favBrand', 1);
        }
    }
    showFavTab(index);
    window.rePosFooter();

});
zhangxiaoru authored
178
// 删除收藏的商品
zhangxiaoru authored
179 180 181
favContentHammer = new Hammer(document.getElementById('fav-content'));

favContentHammer.on('tap', function(e) {
陈峰 authored
182
    let id = '';
zhangxiaoru authored
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

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

    diaLog.showDialog({
        dialogText: '您确定要取消收藏吗?',
        hasFooter: {
            leftBtnText: '取消',
            rightBtnText: '确定'
        }
    }, function() {
        id = $(e.target).closest('li').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();
zhangxiaoru authored
211
                location.reload();
zhangxiaoru authored
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
            } else if (data.code === 400) {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: data.message
                });
            } else {
                diaLog.showDialog({
                    autoHide: true,
                    fast: true,
                    dialogText: '取消收藏失败'
                });
            }
        }).fail(function() {
zhangxiaoru authored
227
            // TODO
zhangxiaoru authored
228 229 230 231 232 233 234 235 236 237 238

            diaLog.showDialog({
                autoHide: true,
                dialogText: '网络错误~'
            });
        });
    });
});

function scrollHandler() {
zhangxiaoru authored
239
    // 距离底部未1/4列表高度+底部高度的时候加载更多
zhangxiaoru authored
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
    if ($(window).scrollTop() + winH >= $(document).height() - 0.25 * $favBrandList.height() - footerH) {
        if (brandTab) {
            $brandLoadMore.filter('.hide').removeClass('hide');

            if (!brandLockId) {
                brandPageId++;
                loadData($favBrandList, 'favBrand', brandPageId);
            }

        } else {

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

            if (!lockId) {
                pageId++;
                loadData($favProductList, 'favProduct', pageId);
            }
        }
    }
}
zhangxiaoru authored
261
// srcoll to load more
zhangxiaoru authored
262 263 264
$(window).scroll(scrollHandler);

$(document).on('touchend', '.swiper-header', function() {
陈峰 authored
265
    let url = $(this).find('.fav-more').attr('href');
zhangxiaoru authored
266 267 268 269 270 271 272

    if (url) {
        window.location.href = url;
    }
});

$('.invalidGoods').on('touchstart touchend', function(e) {
陈峰 authored
273
    let $this = $(e.target).closest('span');
zhangxiaoru authored
274 275 276 277 278 279 280

    if ($this.hasClass('del-fav')) {
        return;
    }
    tip.show('商品已下架');
    return false;
});