shop.page.js 4.56 KB
/**
 * 首页
 * @author: bikai<kai.bi@yoho.cn>
 * @date: 2016/4/26
 */

var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload'),
    Handlebars = require('yoho-handlebars');

var product = require('./index/product');

var $shopIntro = $('.shop-intro'),
    $shopCollect = $('.shop-collect'),
    $colloectIcon = $shopCollect.find('.shop-collect-ico'),
    $colloectText = $shopCollect.find('.shop-collect-text'),
    $searchForm = $('#shop-search-form'),
    $sliderLeft = $('.slider-left'),
    $allGoods = $('.all-goods'),
    $fixedArea = $allGoods.find('.fixed-area'),
    fixedAreaTop = $fixedArea.offset() ? $fixedArea.offset().top : 0;

var shopId = $shopCollect.data('id');

// Pjax
require('yoho-jquery-pjax');

require('../common');
require('../plugins/filter');
require('../plugins/sort-pager');

require('../plugins/slider');
require('./index/top-coupon');

// require('../product/list');

product.init(4);

lazyLoad($('img.lazy'));

if ($sliderLeft.length) {
    $sliderLeft.slider();
}

if ($shopCollect && $shopCollect.length) {
    $.ajax({
        type: 'POST',
        url: '/product/index/isFavoriteShop',
        data: {
            shopId: shopId
        }
    }).then(function(data) {
        if (data.code === 200 && data.data) {
            // 已收藏
            $colloectIcon.addClass('on');
            $colloectText.html('已收藏');
            return;
        }

        $colloectIcon.removeClass('on');
        $colloectText.html('收藏');
    });
}

$shopIntro.on('click', function() {
    $('.pop-shop-intro').show();
    $('.mask').show();
});

$('.close-btn, .mask').on('click', function() {
    $('.pop-shop-intro').hide();
    $('.mask').hide();
});

$('.shop-query-submit').on('click', function() {
    $searchForm.submit();
});

(function() {
    var $adBox = $('#brand-shop-ad');
    var html = $('#pic-link-tpl').html() || '';

    $.ajax({
        type: 'POST',
        url: '/product/index/getAdnav',
        data: {shopId: shopId}
    }).then(function(data) {
        if (data) {
            $adBox.parent().append($adBox);
            $adBox.html(Handlebars.compile(html)({
                picLink: data
            }));
        }
    });
}());

// 收藏店铺
function colloectAction() {
    var isFavorite = $colloectIcon.hasClass('on'),
        needColloect = window.cookie('needColloect');

    $.ajax({
        type: 'post',
        url: '/product/shop/togglecollect',
        data: {
            isFavorite: isFavorite ? 0 : 1,
            needColloect: needColloect,
            shopId: shopId
        },
        success: function(res) {
            if (res.code === 200) {
                if (isFavorite) {
                    $colloectIcon.removeClass('on');
                    $colloectText.html('收藏');
                } else {
                    $colloectIcon.addClass('on');
                    $colloectText.html('已收藏');
                }

                if (needColloect) {
                    $colloectIcon.addClass('on');
                    $colloectText.html('已收藏');
                }
            } else if (res.code === 401) {
                window.setCookie('needColloect', '1', {
                    path: '/',
                    domain: '.yohobuy.com',
                    expires: 90
                });
                location.href = res.data.url;
            }
        }
    });
}

$shopCollect.on('click', function() {
    colloectAction();
});

if (window.cookie('needColloect') * 1 === 1 && window.getUid()) {
    colloectAction();
    window.setCookie('needColloect', '', {
        path: '/',
        domain: '.yohobuy.com',
        expires: 1
    });
}

// 全部商品使用 pjax 翻页
$allGoods.pjax('a.pjax', '.goods-wrap', {
    timeout: 5000,
    scrollTo: false // 默认滚动没有动画,禁止掉
}).on('pjax:end', function() {
    product.init(4);
    lazyLoad($('img.lazy'));

    $fixedArea = $allGoods.find('.fixed-area'); // 翻页后 fixed-area 区域有变化,需要更新选择器

    // 分页后移动到全部商品
    $('html, body').animate({
        scrollTop: $allGoods.offset().top - 30
    });
}).on('click', '.menu-list li', function() {
    var $this = $(this);

    $this.siblings().removeClass('on');
    $this.addClass('on');
}).on('click', 'a.checked', function(e) {
    e.preventDefault();
});

$(window).on('scroll', function() {
    var scrollTop = $(this).scrollTop();

    if (scrollTop > fixedAreaTop) {
        $fixedArea.css({
            position: 'fixed',
            top: 0
        });
    } else {
        $fixedArea.css({
            position: 'static',
            top: 0
        });
    }
});