recommend-product.js 3.83 KB
/**
 * 为您优选 | 店铺推荐 | 最近浏览
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/11/21
 */
var recProduct = require('../../../tpl/product/recommend-product.hbs'),
    recTemplet = require('../../../tpl/product/recommend-templet.hbs');

var recommmendProduct = {
    recommendPage: 0,
    recentPreviewPage: 0,
    init: function(obj) {
        var _this = this,
            params = obj.params || {},
            $dom = $(obj.dom),
            url = obj.url || '/product/getRecommendProduct';

        $dom.append(recTemplet({
            isGoodsDetail: obj.isGoodsDetail ? true : false
        }));

        $.extend(_this, {
            $changeBtn: $dom.find('.change-btn'),
            $recommendHeader: $('.recommend-header'),
            $dom: $dom
        });

        // 为您优选||店铺推荐
        _this.getProducts({
            url: url,
            index: 0,
            params: params
        });

        // tab切换
        $dom.find('.head-tab').on('click', function() {
            var index = $(this).index(),
                $tabCont = $('.tab-cont-' + index);

            $('.head-tab').removeClass('active');
            $(this).addClass('active');
            $tabCont.removeClass('hide').siblings().addClass('hide');

            _this.lazyImage();
            _this.isShowChangeBtn($tabCont.find('.goods-group').length);
        });

        _this.$changeBtn.on('click', function() {
            var index = $('.recommend-header .active').index(),
                $goodsGroup = $('.tab-cont-' + index).find('.goods-group'),
                currPage = 0;

            if ($goodsGroup.length > 1) {
                if (index === 1) {
                    currPage = _this.countPage(_this.recentPreviewPage, $goodsGroup);
                    _this.recentPreviewPage = currPage;
                } else {
                    currPage = _this.countPage(_this.recommendPage, $goodsGroup);
                    _this.recommendPage = currPage;
                }
            }

            $goodsGroup.eq(currPage).show().siblings().hide();
            _this.lazyImage();
        });
    },
    countPage: function(page, $goodsGroup) {
        var currPage = 0;

        page++;

        if (page > $goodsGroup.length - 1) {
            currPage = 0;
        } else {
            currPage = page;
        }

        return currPage;
    },
    getProducts: function(obj) {
        var _this = this;
        var $headTab = $('.head-tab');

        $.get(obj.url, obj.params, function(result) {
            var curTab = $headTab.eq(obj.index);

            if (result.length) {
                if (_this.$recommendHeader.hasClass('hide')) {
                    _this.$recommendHeader.removeClass('hide');
                }

                if (curTab.hasClass('hide')) {
                    curTab.removeClass('hide');
                }

                _this.$dom.find('.tab-cont-' + obj.index).html(recProduct({result: result}));
                _this.lazyImage();
                _this.isShowChangeBtn.bind(result.length);

                if (obj.index === 1 && $headTab.eq(0).hasClass('hide')) {
                    curTab.click();
                }
            }

            if (obj.index === 0) {
                // 最近预览
                _this.getProducts({
                    url: '/product/recentPreview',
                    index: 1,
                    params: {
                        limit: 20
                    }
                });
            }
        });
    },
    isShowChangeBtn: function(currGoodsLen) {
        if (currGoodsLen <= 1) {
            recommmendProduct.$changeBtn.hide();
        } else {
            recommmendProduct.$changeBtn.show();
        }
    },
    lazyImage: function() {
        $('img.lazy').lazyload({
            effect: 'fadeIn'
        });
    }
};

require('yoho-jquery-lazyload');

module.exports = recommmendProduct;