filter.js
2.81 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
/**
* 筛选JS
* 暴露三个接口:注册回调、显示filter、隐藏filter
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/10/19
*/
var $ = require('jquery'),
Hammer = require('hammer');
var $filter;
var fCbFn, hCbFn; //筛选和关闭的回调
var missStatus; //是否忽略选中状态
//隐藏筛选界面
function hideFilter() {
$filter.addClass('hide');
if (missStatus) {
//重置一级筛选项选中状态
$filter.find('.classify-item.active').removeClass('active');
$filter.find('.classify-item:first-child').addClass('active');
}
}
//显示筛选界面
function showFilter() {
$filter.removeClass('hide');
}
function classifyTapEvt($this) {
if ($this.hasClass('active')) {
return;
}
$this.siblings('.active').removeClass('active');
$this.addClass('active');
}
function subClassifyTapEvt($this) {
var id = $this.data('id');
var $sub = $this.closest('.sub-classify');
var $shower = $sub.siblings('.shower');
var html, shower;
if ($this.hasClass('chosed')) {
return;
}
$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 (fCbFn) {
fCbFn({
type: $sub.data('type'),
id: id
});
}
hideFilter();
//重置选中状态
if (missStatus) {
$this.removeClass('chosed');
$sub.children(':first-child').addClass('chosed');
}
}
//初始化筛选
function initFilter(opt) {
var filterHammer;
//注册回调
fCbFn = opt.fCbFn;
hCbFn = opt.hCbFn;
missStatus = opt.missStatus;
$filter = $('.filter-mask');
filterHammer = new Hammer($filter[0]);
filterHammer.on('tap', function(e) {
var $this = $(e.target),
$cur;
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();
}
}
});
}
exports.initFilter = initFilter;
exports.showFilter = showFilter;
exports.hideFilter = hideFilter;