template.js
9.73 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
/**
* 模板页js
* @author: xuqi(qi.xu@yoho.cn)
* @date;2015/4/14
*/
var $ = require('jquery'),
_ = require('underscore'),
Mustache = require('mustache'),
timer = null;
require('lazyload');
/**
* 页面加载初始化
*/
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'),
tpl; //商品信息模板
//筛选相关变量
var curFilter = {
brand: 0,
msort: 0,
color: 0,
size: 0,
price: 0,
discount: 0
},
navInfo = {
newest: {
direction: 1, //排序方向;NOTE:最新保持升序排序
reload: false, //是否需要重新加载
deviation: $npc.children('.good-info').length,
end: false
},
sale: {
direction: 1,
reload: true,
deviation: 0,
end: false
},
price: {
direction: 1,
reload: true,
deviation: 0,
end: false
}
},
classifyItemTpl = '<li data-type="{{type}}" data-id="{{id}}"><span class="text">{{name}}</span></li>',
$screen = $('#screen-content, #screen-mask'),
$prevFocusNav = $navItem.filter('.focus'), //初始化为已选项
classification; //分类数据
//read good-info template
$.get('/tpl/readTpl', function(data) {
if (data.success) {
tpl = data.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
*/
function search() {
var type = getFocusNavType(),
setting = {},
nav;
nav = navInfo[type];
//请求数据setting
$.extend(setting, curFilter, {
type: type,
direction: nav.direction,
deviation: nav.deviation
});
if (nav.reload) {
setting.deviation = 0;
} else if (nav.end) {
//不需要重新加载并且数据请求结束
return;
}
$.ajax({
type: 'GET',
url: '/tpl/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;
}
for (i = 0; i < len; i++) {
html += Mustache.render(tpl, goods[i]);
}
if (nav.reload) {
$container.html(html);
} else {
$container.append(html);
}
//lazyload
$('img.lazy').lazyload({
container: $container
});
//手动加载前4个
$container.find('img.lazy:lt(4)').lazyload({
event: 'load'
});
//重置navInfo
if (res.end) {
nav.end = true;
}
nav.reload = false;
nav.deviation = setting.deviation + len;
}
});
}
//读取筛选时的分类信息
$.get('/tpl/classification', function(data) {
var c,
html;
if (data.success) {
classification = data.data;
for (c in classification) {
if(classification.hasOwnProperty(c)) {
$('#sub-' + c).html(renderArrData(classification[c]));
}
}
}
});
//img lazyload
$('img.lazy').lazyload({
skip_invisible : false
});
/**
* 导航 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')) {
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', 'touchstart', function(e) {
e.preventDefault();
navTouchEvt(e);
}).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', 'touchstart', function(e) {
e.preventDefault();
scTouchEvt(e);
}).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,
req;
//更新当前过滤项
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,
$cur = $(cur),
$parent = $cur.closest('ul'),
id = cur.dataset.id * 1,
type = $parent[0].dataset.type,
name = $cur.children('.text').text();
doFilter(id, type, name);
}
$('.sub-classify').delegate('li', 'click', function(e) {
subScTouchEvt(e);
});
});
};