fav.js
7.02 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**
* 个人中心--收藏
* @author: chenglong<chenglong.wang@yoho.cn>
* @date: 2015/11/12
*/
var $ = require('jquery'),
Hammer = require('yoho.hammer'),
Swiper = require('yoho.iswiper');
var diaLog = require('./dialog');
var $navLi = $('#fav-tab > li'),
$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,
brandPageId = 1, //收藏品牌的当前页数
lockId = true,
brandLockId = true, //收藏品牌是否可下拉加载更多
brandTab = false; //当前是否停留在收藏品牌页
function showFavTab(index) {
$navLi.filter('.active').removeClass('active');
$navLi.eq(index).addClass('active');
$favContainer.filter('.show').removeClass('show');
$favContainer.eq(index).addClass('show');
}
//初始化swiper
function initSwiper(data) {
var i,
idStrReg = /container-(\d+)['"]{1}/gi,
idReg = /\d+/,
idArr = data.match(idStrReg),
idArrLen = idArr.length,
containerId;
//$swiperList = $('.swiper-container');
for (i = 0; i < idArrLen; i++) {
/*id = $swiperList.eq(i).attr('data-id');
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: 'post',
url: '/home/' + url,
data: {
page: page
},
success: function(data) {
//setTimeout(function() { //模拟延时
if (url === 'favBrand') {
$brandLoadMore.addClass('hide');
} else {
$loadMore.addClass('hide');
}
if (data === ' ') {
$parent.closest('.fav-type').find('.fav-content-loading').addClass('hide');
$parent.closest('.fav-type').find('.fav-null-box').removeClass('hide');
} else if (data === 'end') {
$parent.closest('.fav-type').find('.fav-load-background')
.removeClass('fav-load-background').html('没有更多了');
} else if (data.length > 10) {
$parent.append(data);
$parent.closest('.fav-type').find('.fav-content-loading').remove();
if (url === 'favBrand') {
initSwiper(data);//如果是收藏品牌需要初始化swiper
brandLockId = false;//请求成功后解锁品牌收藏page++
} else {
lockId = false;//请求成功后解锁商品收藏page++
}
window.rePosFooter();
} else {
return;
}
//},1000);
}
});
}
// 如果从品牌收藏入口进入
if ($('#fav-tab').hasClass('brand-tab')) {
showFavTab(1);
loadData($favBrandList, 'favBrand', 1);
brandTab = true;
window.rePosFooter();
} else {
showFavTab(0);
loadData($favProductList, 'favProduct', 1);
brandTab = false;
window.rePosFooter();
}
favTabHammer = new Hammer(document.getElementById('fav-tab'));
favTabHammer.on('tap', function(e) {
var $cur = $(e.target).closest('li'),
index;
if ($cur.length === 0 || $cur.hasClass('active')) {
return;
}
index = $cur.index();
if (index === 0) {
brandTab = false;
if ($favProductList.find('li').length === 0 &&
$favProductList.closest('.fav-type').find('.fav-null-box').hasClass('hide')) {
loadData($favProductList, 'favProduct', 1);
}
} else {
brandTab = true;
if ($favBrandList.find('div').length === 0 &&
$favBrandList.closest('.fav-type').find('.fav-null-box').hasClass('hide')) {
loadData($favBrandList, 'favBrand', 1);
}
}
showFavTab(index);
window.rePosFooter();
});
//删除收藏的商品
favContentHammer = new Hammer(document.getElementById('fav-content'));
favContentHammer.on('tap', function(e) {
var id = '';
if (!$(e.target).hasClass('del-fav')) {
return;
}
diaLog.showDialog({
dialogText: '您确定要取消收藏吗?',
hasFooter: {
leftBtnText: '取消',
rightBtnText: '确定'
}
}, function() {
id = $(e.target).closest('li').attr('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();
} else if (data.code === 400) {
diaLog.showDialog({
autoHide: true,
fast: true,
dialogText: data.message
});
} else {
diaLog.showDialog({
autoHide: true,
fast: true,
dialogText: '取消收藏失败'
});
}
}).fail(function() {
//TODO
diaLog.showDialog({
autoHide: true,
dialogText: '网络错误~'
});
});
});
});
function scrollHandler() {
//距离底部未1/4列表高度+底部高度的时候加载更多
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);
}
}
}
}
//srcoll to load more
$(window).scroll(function() {
window.requestAnimationFrame(scrollHandler);
});