recommend-product.js
3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* 为您优选 | 店铺推荐 | 最近浏览
* @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;