recommend-product.js 3.21 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'),
            $dom: $dom
        });

        _this.getProducts({
            url: url,
            index: 0,
            params: params
        });

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

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

            if ($tabCont.find('.goods-group').length === 0 && index === 1) {
                _this.getProducts({
                    url: '/product/recentPreview',
                    index: index,
                    params: {
                        limit: 20
                    }
                });
            }

            _this.isShowChangeBtn($tabCont.find('.goods-group').length);
            $tabCont.removeClass('hide').siblings().addClass('hide');
        });

        _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;

        $.get(obj.url, obj.params, function(result) {
            if (result) {
                _this.$dom.find('.tab-cont-' + obj.index).html(recProduct({result: result}));
                _this.lazyImage();

                _this.isShowChangeBtn(result.length);
            }
        });
    },
    isShowChangeBtn: function(currGoodsLen) {
        if (currGoodsLen <= 1) {
            this.$changeBtn.hide();
        } else {
            this.$changeBtn.show();
        }
    },
    lazyImage: function() {
        $('img.lazy').lazyload({
            effect: 'fadeIn'
        });
    }
};

require('yoho-jquery-lazyload');

module.exports = recommmendProduct;