-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.multi_filters.js
257 lines (237 loc) · 7.28 KB
/
jquery.multi_filters.js
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
/*
* jQuery Multi Filters plugin v0.4
* http://dingyu.me/portfolio/jquery-multi-filters
*
* by Felix Ding
*
* MIT License.
*/
(function($) {
$.fn.multi_filters = function(options) {
var plugin = this;
plugin.filter_index = 0;
plugin.dom = {
add_filter: function(obj, existing_filter) {
// compile template
var filter_row = $.tmpl("filter", {name: options.name, id: plugin.filter_index, available_filters: plugin.options.available_filters});
// update dom
if(obj) {
$(obj).parent().parent().parent().after(filter_row);
}
else {
plugin.append(filter_row);
}
// give it an id
$(filter_row).data("id", plugin.filter_index);
if(existing_filter) {
$(filter_row).find(".key").first().val(existing_filter["key"]).change();
$(filter_row).find(".restriction").first().val(existing_filter["restriction"]).change();
$(filter_row).find(".value").first().val(existing_filter["value"]);
}
else {
// ask restriction to change
$(filter_row).find(".key").first().change();
}
// update index
plugin.filter_index++;
this.add_or_remove_check();
},
remove_filter: function(obj) {
$(obj).parent().parent().parent().remove();
this.add_or_remove_check();
},
add_or_remove_check: function() {
if(plugin.find(".filter").length == 1) {
plugin.find(".remove-filter").attr("disabled", "disabled");
}
else if(plugin.find(".filter").length > 1) {
plugin.find(".remove-filter").removeAttr("disabled");
}
}
}
// options
var default_options = {
types: {
"text":{
"restrictions":[
{
"title":"Contains",
"value":"contains"
},
{
"title":"Is",
"value":"is"
}
]
},
"rating":{
"restrictions":[
{
"title":"Less Than",
"value":"less_than"
},
{
"title":"Greater Than",
"value":"greater_than"
},
{
"title":"Is",
"value":"is"
}
],
"value":[
{ "title" : "1 Star",
"value" : 1
},
{ "title" : "2 Stars",
"value" : 2
},
{ "title" : "3 Stars",
"value" : 3
},
{ "title" : "4 Stars",
"value" : 4
},
{ "title" : "5 Stars",
"value" : 5
}
]
},
"date":{
"restrictions":[
{
"title":"Before",
"value":"before"
},
{
"title":"After",
"value":"after"
},
{
"title":"Is",
"value":"is"
}
]
},
"status":{
"restrictions":[
{
"title":"Is",
"value":"is"
},
{
"title":"Is Not",
"value":"is_not"
}
],
"value":[
{ "title" : "Open",
"value" : "open"
},
{ "title" : "In Progress",
"value" : "in_progress"
},
{ "title" : "Closed",
"value" : "closed"
}
]
},
"no_restrictions": {
"restrictions": null,
"value": [
{ "title" : "Set to 1",
"value" : "1"
},
{ "title" : "Set to 2",
"value" : "2"
}
]
}
},
templates: {
filter: '<li class="filter">\
<div class="row">\
<div class="col-md-3 col-sm-3 col-xs-12">\
<select class="key col-xs-12" name="model[${name}][${id}][key]">{{tmpl(available_filters) "option"}}</select>\
</div>\
<div class="col-md-3 col-sm-3 col-xs-12">\
<select class="restriction col-xs-12" name="model[${name}][${id}][restriction]"></select>\
</div>\
${restrictions}\
<div class="col-md-3 col-sm-3 col-xs-12">\
<span class="value_container"></span>\
</div>\
<div class="col-md-3 col-sm-3 col-xs-12" style="text-align:right1">\
<button class="add-filter">+</button>\
<button class="remove-filter">-</button>\
</div>\
</div>\
</li>',
option: '<option value="${value}">${title}</option>',
value_text: '<input class="value col-xs-12" type="text" name="model[${name}][${id}][value]"></input>',
value_date: '<input class="value col-xs-12" type="text" name="model[${name}][${id}][value]"></input>',
value_rating: '<select class="value col-xs-12" name="model[${name}][${id}][value]">{{tmpl(value) "option"}}</select>',
value_status: '<select class="value col-xs-12" name="model[${name}][${id}][value]">{{tmpl(value) "option"}}</select>',
value_no_restrictions: '<select class="value col-xs-12" name="model[${name}][${id}][value]">{{tmpl(value) "option"}}</select>'
},
available_filters: {},
multiple: true
};
this.options = $.extend(default_options, options);
// cache templates
$.each(this.options.templates, function(name, tpl) {
$.template(name, tpl);
});
$( "#whichkey" ).on( "keydown", function( event ) {
$( "#log" ).html( event.type + ": " + event.which );
});
// prevent hitting the Enter key from adding a new filter row
$(plugin).on("keydown", function(event) {
if(event.which == 13) {
return false;
}
});
// listen "filter key change"
$(plugin).on("change", ".key", function(event) {
var $filter = $(this).parent().parent().parent();
var type = $(this).find("option:selected").tmplItem().data.type;
// restriction
if(plugin.options.types[type]["restrictions"]) {
var restrictions = $.tmpl("option", plugin.options.types[type]["restrictions"]);
$(this).parent().parent().find(".restriction").empty().append(restrictions);
}
else {
$(this).parent().parent().find(".restriction").parent().remove();
}
// value
var value = $.tmpl("value_"+type, {id: $filter.data("id"), name: options.name, value: plugin.options.types[type].value});
$(this).parent().parent().find(".value_container").empty().append(value);
// add & remove buttons
if(!plugin.options.multiple) {
$(this).parent().parent().find("button").remove();
}
});
// any existing filters?
var existing_filters = plugin.options.existing_filters;
if(existing_filters && existing_filters.length > 0) {
// yes, iterate
for (var i in existing_filters) {
plugin.dom.add_filter(null, existing_filters[i]);
}
}
else {
// append the first filter
plugin.dom.add_filter();
}
// listen "add button"
$(plugin).on("click", ".add-filter", function() {
plugin.dom.add_filter(this);
return false;
});
// listen "remove button"
$(plugin).on("click", ".remove-filter", function() {
plugin.dom.remove_filter(this);
return false;
});
};
})(jQuery);