filter.js
5.45 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
/**
* 筛选JS
* 暴露三个接口:注册回调、显示filter、隐藏filter
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/10/19
*/
let $ = require('yoho-jquery'),
Hammer = require('yoho-hammer');
let $filter;
let fCbFn, hCbFn; // 筛选和关闭的回调
// 隐藏筛选界面
function hideFilter() {
setTimeout(function() {
$filter && $filter.addClass('hide');
}, 301);
}
// 显示筛选界面
function showFilter() {
$filter && $filter.removeClass('hide');
}
// 一级菜单点击时背景高亮
function highlightMainItem() {
let $mainItem = $('.filter-body .classify');
$mainItem.on('touchstart', '.shower', function() {
let bpIdData = $(this).attr('data-bp-id') || '';
$mainItem.removeClass('highlight');
$(this).addClass('highlight');
$(document).trigger('shouldSendBpData', [bpIdData]);
}).on('touchend touchcancel', '.shower', function() {
$(this).removeClass('highlight');
});
}
// 子菜单点击时背景高亮
function highlightSubItem() {
let $subItem = $('.filter-body .sub-item');
$subItem.on('touchstart', function() {
$subItem.removeClass('highlight');
$(this).addClass('highlight');
}).on('touchend touchcancel', function() {
$(this).removeClass('highlight');
});
}
// 主筛选项Tap事件句柄
function classifyTapEvt($this) {
if ($this.hasClass('active')) {
return;
}
$this.siblings('.active').removeClass('active');
$this.addClass('active');
}
// 子筛选项选中处理
function setSubClassify($this) {
let $sub = $this.closest('.sub-classify');
let $shower = $sub.siblings('.shower');
let html, shower;
if ($this.hasClass('chosed')) {
return $sub;
}
$sub.children('.chosed').removeClass('chosed');
$this.addClass('chosed');
html = $.trim($this.html());
shower = $.trim($shower.html());
$shower.html(
shower.substring(0, shower.indexOf('</span>') + 7) + // 拆分出shower的title
html.substring(0, html.indexOf('<i')) // 拆分选中筛选值
);
if ($this.index() === 0) {
$shower.addClass('default');
} else {
$shower.removeClass('default');
}
if (hCbFn) {
hCbFn();
}
return $sub;
}
// 子筛选项Tap句柄
function subClassifyTapEvt($this) {
let id = $this.data('id');
let $sub;
$sub = setSubClassify($this);
if (fCbFn) {
fCbFn({
type: $sub.data('type'),
id: id,
filtering: true
});
}
if (hCbFn) {
hCbFn();
}
// 防止点击穿透,延迟100 隐藏
setTimeout(hideFilter, 100);
}
// 初始化筛选
function initFilter(opt) {
let filterHammer;
// 注册回调
fCbFn = opt.fCbFn;
hCbFn = opt.hCbFn;
highlightMainItem();
highlightSubItem();
$filter = $('.filter-mask');
filterHammer = new Hammer($filter[0]);
filterHammer.on('tap', function(e) {
let $this = $(e.target),
$cur;
// 这行不能注释,iphone下会出现穿透
e.preventDefault();// 防止透点
if ($this.closest('.filter-body').length > 0) {
$cur = $this.closest('.sub-item');
if ($cur.length > 0) {
// tap subclassify
$cur = $this.closest('li');
subClassifyTapEvt($cur);
} else {
// tap classify
$cur = $this.closest('.classify-item');
e.srcEvent.stopPropagation();
classifyTapEvt($cur);
}
} else {
hideFilter();
if (hCbFn) {
hCbFn();
}
}
});
if (opt.isScroll) {
$('ul.sub-classify').each(function() {
let lastY = 0, y = 0;
let $slider = $(this);
$(this).wrap('<div class=\'sub-classify-farther\' data-type=\'' + $(this).data('type') + '\'></div>');
// $(this).removeClass("sub-classify");
$slider.on('touchstart', function(e) {
lastY = e.originalEvent.touches[0].pageY;
});
$slider.on('touchmove', function(e) {
let curY = e.originalEvent.touches[0].pageY;
let deltaY = curY - lastY;
let height = $(this)[0].scrollHeight;
let limit = height - $(this).parent().height();
let translate;
y = y + deltaY;
if (y < limit * -1) {
y = limit * -1;
}
if (y > 0) {
y = 0;
}
translate = 'translate3d(0,' + y + 'px,0)';
$(this).css({
'-moz-transform': translate,
'-ms-transform': translate,
transform: translate,
'-webkit-transform': translate
});
lastY = curY;
});
$slider.on('touchend', function() {});
});
}
}
// 重置筛选面板
function resetFilter() {
if (typeof $filter === 'undefined') {
return;
}
// 重置子筛选项
$('.sub-classify').each(function() {
setSubClassify($(this).children(':first-child'));
});
// 重置主筛选项
classifyTapEvt($('.classify > :first-child'));
}
exports.initFilter = initFilter;
exports.showFilter = showFilter;
exports.hideFilter = hideFilter;
exports.resetFilter = resetFilter;