all-product.js
9.93 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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* @Author: Targaryen
* @Date: 2017-03-24 13:43:55
* @Last Modified by: Targaryen
* @Last Modified time: 2017-06-20 09:50:01
*/
/** *****************************
* 红人店铺使用的全部商品页面碎片
*******************************/
const filter = require('../../plugin/filter');
const lazyLoad = require('yoho-jquery-lazyload');
let noResultHbs = require('product/search/no-result-new.hbs');
let $goodsContainer = $('#goods-container');
let $container = $goodsContainer.children('.default-goods'); // 承载商品列表的容器
let $listNav = $('#list-nav'); // 筛选项列表
let $pre = $listNav.find('.active'); // 记录进入筛选前的active项,初始为选中项
let $allGoodsTabPage = $(document.getElementById('all-goods'));
let $filterBody = $('.filter-body');
let $dropList = $('.drop-list');
const shopId = $('#shopId').val();
let navInfo = {
price: {
order: 1,
end: false
},
discount: {
order: 1,
end: false
},
default: {
order: 0,
end: false,
type: 'default'
},
new: {
order: 0,
end: false
},
sale: {
order: 0,
end: false
},
popularity: {
order: 0,
end: false
}
};
let beforeScroll = document.body.scrollTop; // 滚动前位置记录
let navType = 'default'; // 目前激活的导航页面
let defaultOpt = Object.assign({}, navInfo.default, {shop_id: shopId}); // 默认参数
let onSearching = false; // 是否正在搜索
let isScrollLoad = false; // 是否是滚动加载
let page = 1; // 页码
let nav;
let $firstText = $('.first-li-more').find('.nav-txt');
let $thisLi = '';
// 物料商品列表标记
if ($('#material-flag').val() === 'material') {
let material = true;
if ($('.first-li-more').hasClass('active')) {
page = 2; // 已在服务端渲染第一页
}
Object.assign(defaultOpt, {
material: material,
unionType: window.queryString.union_type
});
}
/**
* 处理筛选参数
*/
const handleChoseFilter = function() {
$.each($filterBody.find('.chosed'), function(index, elem) {
let choseOpt = {};
choseOpt[$(elem).parent().data('type')] = $(elem).data('id');
Object.assign(defaultOpt, choseOpt);
});
};
/**
* 获取商品列表
*/
const getGoodsList = params => {
handleChoseFilter();
if (!onSearching) {
Object.assign(defaultOpt, {
page: page++
});
}
// 有参数,参数优先,滚动加载相关参数重置
if (params && !params.filtering) {
Object.assign(defaultOpt, params);
if (params.page) {
page = params.page + 1;
}
isScrollLoad = false;
beforeScroll = document.body.scrollTop;
}
if (nav && nav.end) {
return false;
}
if (!onSearching) {
onSearching = true;
$.ajax({
type: 'GET',
url: location.protocol + '//m.yohobuy.com/product/search/search',
data: defaultOpt,
xhrFields: {
withCredentials: true
},
beforeSend: function() {
if ($('.no-result-new').length > 0) {
$('.no-result-new').remove();
}
$container.append('<div class="search-divide">正在加载...</div>');
},
success: function(result) {
// 去掉正在加载
$('.search-divide').remove();
let noResult = !result || result.length < 1 || (result.list && result.list.length < 1);
// 没有结果输出没有结果页面
if (noResult) {
if (isScrollLoad) {
$container.append('<div class="search-divide">没有更多内容了...</div>');
} else {
$container.html(noResultHbs());
}
if (nav) {
nav.end = true;
}
onSearching = false;
return false;
}
if (isScrollLoad) {
$container.append(result);
} else {
$container.html(result);
}
lazyLoad($container.find('img[class=lazy]').not('img[src]'));
onSearching = false;
},
error: function() {
let $divide = $('.search-divide');
$divide.text('加载失败,点击重试');
$divide.one('click', function() {
$divide.text('正在加载...');
getGoodsList();
});
onSearching = false;
}
});
}
};
/**
* 加载筛选数据
*/
const getFilter = function() {
$.ajax({
type: 'GET',
url: location.protocol + '//m.yohobuy.com/product/search/filter',
data: defaultOpt,
success: function(data) {
if (!data) {
return false;
}
$goodsContainer.append(data);
// 初始化filter&注册filter回调
filter.initFilter({
fCbFn: getGoodsList,
hCbFn: function() {
// 切换active状态到$pre上
$pre.addClass('active');
$pre.siblings('.filter').removeClass('active');
}
});
$filterBody = $('.filter-body');
}
});
};
$listNav.bind('contextmenu', function() {
return false;
});
const popularityFilter = function(thisLi, type) {
if (thisLi.hasClass('active')) {
$dropList.hide();
return false;
}
$thisLi.addClass('active').siblings('li').removeClass('active');
defaultOpt.type = type;
nav = navInfo[type];
if (thisLi.hasClass('asc')) {
nav.order = 1;
} else {
nav.order = 0;
}
Object.assign(defaultOpt, nav);
getGoodsList();
$dropList.hide();
};
$listNav.on('touchend touchcancel', function(e) {
page = 1;
beforeScroll = $(window).scrollTop();
isScrollLoad = false;
let $this = $(e.target).closest('li'); // 被点击的 Tab
let $active;
// 默认列表增加人气筛选
if ($this.hasClass('first-li-more') && $this.hasClass('active')) {
$this.siblings('.active').removeClass('active');
$this.addClass('active');
$dropList.toggle();
$dropList.find('.default').on('touchend touchcancel', function() {
$thisLi = $(this);
$firstText.html('默认');
popularityFilter($thisLi, 'default');
return false;
});
$dropList.find('.discount').on('touchend touchcancel', function() {
$thisLi = $(this);
$firstText.html($(this).data('text'));
popularityFilter($thisLi, 'discount');
$('.first-li-more').removeClass('new default sale discount').addClass('discount');
return false;
});
} else if ($this.hasClass('filter')) { // 筛选面板
$dropList.css('display', 'none');
// 筛选面板切换状态
if ($this.hasClass('active')) {
filter.hideFilter();
// 点击筛选前的 active 项恢复 active
$pre.addClass('active');
$this.removeClass('active');
} else {
$pre = $this.siblings('.active');
$pre.removeClass('active');
$this.addClass('active');
filter.showFilter();
}
} else { // 排序改变
$dropList.css('display', 'none');
if ($this.hasClass('new')) {
navType = 'new';
} else if ($this.hasClass('price')) {
navType = 'price';
} else if ($this.hasClass('discount')) {
navType = 'discount';
} else if ($this.hasClass('default')) {
navType = 'default';
} else if ($this.hasClass('sale')) {
navType = 'sale';
} else if ($this.hasClass('popularity')) {
navType = 'popularity';
}
// 更新当前排序:默认、最新、价格、折扣
nav = navInfo[navType];
if ($this.hasClass('active')) {
// 默认、最新无排序切换
if ($this.hasClass('default') || $this.hasClass('new') || $this.hasClass('popularity')) {
return false;
}
if ($this.hasClass('price') || $this.hasClass('discount')) {
// 价格或折扣切换排序状态
$this.find('.icon > .iconfont').toggleClass('cur');
$pre = $this; // 更新 pre 为当前项
nav.order = nav.order === 0 ? 1 : 0; // 切换排序
}
} else {
$active = $this.siblings('.active');
$pre = $this; // $pre为除筛选导航的其他导航项,若当前active的为筛选,则把$pre置为当前点击项
if ($active.hasClass('filter')) {
// 若之前active项为筛选,则隐藏筛选面板
filter.hideFilter();
}
$active.removeClass('active');
$this.addClass('active');
}
/* 排序条件更新 */
defaultOpt.type = navType;
Object.assign(defaultOpt, nav);
getGoodsList();
}
});
/**
* 当scroll到1/2$goodsContainer高度后继续请求下一页数据
*/
const scrollHandler = function() {
if ($allGoodsTabPage.hasClass('active') && $(window).scrollTop() > $goodsContainer.height() * 0.6) {
isScrollLoad = true;
getGoodsList();
}
};
/**
* 滚动加载
*/
$(window).scroll(function() {
setTimeout(function() {
let afterScroll = document.body.scrollTop;
if (afterScroll - beforeScroll > 0) {
window.requestAnimationFrame(scrollHandler);
beforeScroll = afterScroll;
} else {
return false;
}
}, 5);
});
module.exports = {
getGoodsList,
getFilter
};