template.js
15.2 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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/**
* 模板页js
* @author: xuqi(qi.xu@yoho.cn)
* @date;2015/4/14
*/
var $ = require('yoho.jquery'),
_ = require('underscore'),
ellipsis = require('mlellipsis'),
lazyLoad = require('yoho.lazyload'),
Mustache = require('yoho.mustache');
require('./wxshare')();
/**
* 页面加载初始化
*/
exports.init = function() {
$(function() {
var $navItem = $('#goods-nav .nav-item'),
$goodsContainer = $('#goods-content'),
$goodList = $('#goods-content > div'),
$npc = $goodList.filter('.new-patterns-container'),
$svc = $goodList.filter('.sales-volume-container'),
$pc = $goodList.filter('.price-container'),
//load-more
$loadMore = $('#load-more-info'),
$loadStatus = $loadMore.children('.status'),
$noMore = $loadStatus.filter('.no-more'),
$loading = $loadStatus.filter('.loading'),
loadMoreH = $loadMore.height(),
isLogin = $('#is-login').val(),
tpl,//商品信息模板
classification,
winH = $(window).height(),
canLoadAjax = true,
promotionId = $('#promotion').val(),
clientType = $('#client-type').val(),
contentCode = $('#content-code').val(),
$loginTip = $('#login-tip'),
winW,
tipH,
tipW,
//筛选相关变量
curFilter = {
brand: '',
msort: '',
color: '',
size: '',
price: '',
discount: ''
},
navInfo = {
newest: {
direction: 0, //排序方向;NOTE:最新保持升序排序
reload: false, //是否需要重新加载
start: 1,
end: false,
empty: false
},
sale: {
direction: 0, //销量降序排列
reload: true,
start: 0,
end: false,
empty: true
},
price: {
direction: 1,
reload: true,
start: 0,
end: false,
empty: true
}
},
classifyItemTpl = '<li class="{{^ id}}chosed{{/ id}}" data-id="{{id}}">' +
'<span class="text">{{name}}</span>' +
'<span><i class="chosed-icon iconfont"></i></span>' +
'</li>',
$screen = $('#screen-content, #screen-mask'),
$prevFocusNav = $navItem.filter('.focus');
isLogin = isLogin ? isLogin : 'N';
//定位登录提示
winH = $(window).height();
winW = $(window).width();
tipH = $loginTip.height();
tipW = $loginTip.width();
$loginTip.css({
top: (winH - tipH) / 2,
left: (winW - tipW) / 2
});
//lazyload
lazyLoad();
//ellipsis
ellipsis.init();
$('.reco .name').each(function() {
this.mlellipsis(2);
});
$('.brand-name').each(function() {
this.mlellipsis(1);
});
//read good-info template
$.get('/common/goodinfo', function(data) {
tpl = data;
Mustache.parse(tpl);
});
Mustache.parse(classifyItemTpl); //cache tpl
/**
* Mustache 渲染数组数据
* @params data Array 数据数组
* @return html html字符串
*/
function renderArrData(data) {
var i = 0,
html = '';
for (i = 0; i < data.length; i++) {
html += Mustache.render(classifyItemTpl, data[i]);
}
return html;
}
/**
* 获取当前选中导航的类别
* @return string/undefined
*/
function getFocusNavType() {
var type;
if ($prevFocusNav.hasClass('sales-volume-nav')) {
type = 'sale';
} else if ($prevFocusNav.hasClass('price-nav')) {
type = 'price';
} else if ($prevFocusNav.hasClass('new-patterns-nav')) {
type = 'newest';
}
return type;
}
/**
* 查找当前状态下的商品列表并填充HTML
* @params fromScroll Boolean 是否由scroll触发search
*/
function search(fromScroll) {
var type = getFocusNavType(),
setting = {},
hadNum = 0,
nav;
nav = navInfo[type];
//重新加载重置start和end
if (nav.reload) {
nav.start = 0;
nav.end = false;
nav.empty = false;
}
//请求数据setting
$.extend(setting, curFilter, {
type: type,
direction: nav.direction,
start: nav.start + 1,
promotionId: promotionId,
clientType: clientType,
contentCode: contentCode
});
if (nav.end) {
$loading.addClass('hide');
$noMore.removeClass('hide');
} else {
$loading.removeClass('hide');
$noMore.addClass('hide');
}
if (nav.end && nav.empty) {
$noMore.addClass('hide');
}
if (!nav.reload && nav.end) {
//不需要重新加载并且数据请求结束
return;
}
canLoadAjax = false;
$.ajax({
type: 'GET',
url: '/activity/search',
data: setting
}).then(function(data) {
var html = '',
$container,
res,
goods,
len,
i;
if (data.success) {
res = data.data;
goods = res.goods;
len = goods.length;
//插入HTML
switch (setting.type) {
case 'newest':
$container = $npc;
break;
case 'price':
$container = $pc;
break;
case 'sale':
$container = $svc;
break;
}
if (!fromScroll && len === 0) {
//由非scroll触发并且返回结果为空的代表未查到数据
nav.empty = true;
//无返回数据
html = '<p class="search-tip">未找到相关搜索结果</p>';
} else {
nav.empty = false;
hadNum = $container.find('.good-info').length;
for (i = 0; i < len; i++) {
html += Mustache.render(tpl, goods[i]);
}
}
if (nav.reload || (!fromScroll && len === 0)) {
hadNum = 0;
$container.html(html);
} else {
$container.append(html);
}
//lazyload加载新插入的图片
if (hadNum === 0) {
lazyLoad($container.find('.good-info img.lazy'));
} else {
lazyLoad($container.find('.good-info:gt(' + (hadNum - 1) + ') img.lazy'));
}
//重置navInfo
if (res.end) {
nav.end = true;
//设置加载更多显示
$loading.addClass('hide');
//若无数据项返回则不显示noMore
if (!fromScroll && len === 0) {
$noMore.addClass('hide');
} else {
$noMore.removeClass('hide');
}
}
nav.reload = false;
nav.start++;
//重置可请求标识
canLoadAjax = true;
}
});
}
//读取筛选时的分类信息
$.get('/activity/classification', {
promotionId: promotionId,
clientType: clientType
}, function(data) {
var c;
if (data.success) {
classification = data.data;
for (c in classification) {
if (classification.hasOwnProperty(c)) {
$('#sub-' + c).html(renderArrData(classification[c]));
}
}
}
});
/**
* 切换排序
* @params $cur 当前选中nav-item
*/
function toggleSort($cur) {
var type = getFocusNavType(),
nav = navInfo[type],
direction;
$cur.find('.sort i').toggleClass('cur');
if ($cur.find('.sort .cur').hasClass('up')) {
direction = 1;
} else {
direction = 0;
}
nav.direction = direction;
nav.reload = true;
search();
}
/**
* 导航 touch/click处理句柄
*/
function navTouchEvt(e) {
var $cur = $(e.currentTarget),
type;
if ($cur.hasClass('screen-nav')) {
//筛选
$screen.toggleClass('hide');
$prevFocusNav.toggleClass('focus');
$cur.toggleClass('focus');
} else {
if ($cur.hasClass('focus')) {
if ($cur.hasClass('price-nav')) {
toggleSort($cur);
}
return;
}
$prevFocusNav = $cur;
$navItem.removeClass('focus');
$cur.addClass('focus');
type = getFocusNavType(); //当前focus项(new/sale/price)
$goodList.addClass('hide');
switch (type) {
case 'newest':
$npc.removeClass('hide');
break;
case 'sale':
$svc.removeClass('hide');
break;
case 'price':
$pc.removeClass('hide');
break;
}
if (navInfo[type].reload) {
search();
}
}
}
//切换“最新”,“销量”,“价格”以及“筛选”功能
$('#goods-nav').delegate('.nav-item', 'click', function(e) {
navTouchEvt(e);
});
/**
* 筛选分类点击事件句柄
*/
function scTouchEvt(e) {
var $cur = $(e.currentTarget),
cs = ['brand', 'msort', 'color', 'size', 'price', 'discount'],
curType;
if ($cur.hasClass('active')) {
return;
}
$('#screen-content .c-item').removeClass('active');
$cur.addClass('active');
curType = _.filter(cs, function(c) {
return $cur.hasClass(c);
});
$('.sub-classify:not(.hide)').addClass('hide');
$('#sub-' + curType).removeClass('hide');
}
$('#screen-content').delegate('.c-item', 'click', function(e) {
scTouchEvt(e);
});
/**
* 筛选
* @params string 数据id
* @params string 数据类型
* @name string 值
* @navNam string 最新/销量/价格
* @direction int 0(降序)/1(升序)
*/
function doFilter(id, type, name, navName) {
var $shower = $('#show-' + type),
att;
//更新当前过滤项
curFilter[type] = id;
//更新显示值
if (id === 0) {
$shower.addClass('default');
} else {
$shower.removeClass('default');
}
$shower.text(name);
//重置reload
for (att in navInfo) {
if (navInfo.hasOwnProperty(att)) {
navInfo[att].reload = true;
}
}
search();
}
/**
* 筛选子类点击事件句柄
*/
function subScTouchEvt(e) {
var $cur = $(e.currentTarget),
$parent = $cur.closest('ul'),
id = $cur.data('id'),
type = $parent.data('type'),
name = $cur.children('.text').text();
$parent.children('li.chosed').removeClass('chosed');
$cur.addClass('chosed');
doFilter(id, type, name);
}
$('.sub-classify').delegate('li', 'click', function(e) {
subScTouchEvt(e);
//子筛选类点击后关闭筛选框
$navItem.filter('.screen-nav').click(); //触发点击筛选导航的点击事件
});
//加载更多
$(document).scroll(function(e) {
//当scroll到1/3$goodsContainer高度后继续请求
if (canLoadAjax && $(window).scrollTop() + winH >=
$(document).height() - 0.33 * $goodsContainer.height() - loadMoreH) {
search(true);
}
});
//点击蒙版关闭筛选框
$('#screen-mask').click(function() {
$navItem.filter('.screen-nav').click(); //触发点击筛选导航的点击事件
});
//Like
$('#goods-content').delegate('.good-islike', 'touchstart', function(e) {
var $cur,
$good,
id;
if (isLogin === 'Y') {
$cur = $(e.currentTarget);
$good = $cur.closest('.good-info');
id = $good.data('id');
$.ajax({
type: 'GET',
url: '/favorite/product',
data: {
product_skn: id
}
}).then(function(data) {
if (data.code === 200) {
$cur.toggleClass('good-like');
} else if (data.code === 400) {
//提示登录信息
$('#login-tip').fadeIn(500, function() {
setTimeout(function() {
$('#login-tip').fadeOut(500);
}, 1000);
});
}
});
}
}).delegate('.good-islike', 'click', function(e) {
if (isLogin === 'Y') {
e.preventDefault();
}
});
});
};