-
Notifications
You must be signed in to change notification settings - Fork 0
/
slick.filter.js
431 lines (355 loc) · 14 KB
/
slick.filter.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
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
// slick.filter Plugin
//
// Version 0.00
//
// (c) 2009-2010 Yellow Feather Ltd (http://www.yellowfeather.co.uk)
// All rights reserved.
jQuery.SlickFilter = function(parent, options) {
var timeout = null;
var lastValue = null;
var nextCriterionIndex = 1;
buildFilterForm(parent, options.columns);
var columnSelect = $("#filter_criterion\\[0\\]_column", parent);
columnSelect.change();
function buildFilterForm(filterDiv, columns) {
var form = $(document.createElement('form'))
.attr("id", "filter_form")
.addClass('inline');
var fieldset = $(document.createElement('fieldset'));
fieldset.append(buildFilterMatchRow());
fieldset.append(buildFilterRow(0, columns));
form.append(fieldset);
filterDiv.append(form);
}
function buildFilterMatchRow() {
var div = $(document.createElement('div'))
.attr("id", "filter_match_row")
.addClass("filter_match_row");
div.append($(document.createElement('span')).text("Match "));
var select = $(document.createElement('select')).attr("id", "filter_match").attr("name", "filter.match");
select.append($(document.createElement('option')).attr("value", "any").text("Any"));
select.append($(document.createElement('option')).attr("value", "all").text("All"));
select.append($(document.createElement('option')).attr("value", "none").text("None"));
div.append(select);
select.change(function() {
onFilterChange();
});
div.append($(document.createElement('span')).text(" of the following:"));
return div;
}
function buildFilterRow(criterionIndex, columns) {
var id = "filter_criterion[" + criterionIndex + "]_row";
var div = $(document.createElement('div'))
.attr("id", id)
.addClass("filter_criterion_row");
div.append(buildFilterSelectColumn(criterionIndex, columns));
div.append(buildFilterSelectOperator(criterionIndex, columns));
div.append(buildFilterValues(criterionIndex));
div.append(buildFilterRowActions(criterionIndex, columns));
return div;
}
function buildFilterSelectColumn(criterionIndex, columns) {
var id = "filter_criterion[" + criterionIndex + "]_column";
var name = "filter.criterion[" + criterionIndex + "].column";
var select = $(document.createElement('select'))
.attr("id", id)
.attr("name", name)
.addClass("filter_criterion_column");
select.change(function() {
var criterionIndex = getCriterionIndexFromId($(this));
var column = getColumn(criterionIndex, columns);
updateFilterSelectOperatorOptions(criterionIndex, column);
var filterOperatorElement = $("#filter_criterion\\[" + criterionIndex + "\\]_operator");
updateFilterInputs(criterionIndex, filterOperatorElement, columns);
onFilterChange();
});
$.each(columns, function(index, column) {
if (column.filterable) {
select.append($(document.createElement('option')).attr("value", column.id).text(column.name));
}
});
return select;
}
function buildFilterSelectOperator(criterionIndex, columns) {
var id = "filter_criterion[" + criterionIndex + "]_operator";
var name = "filter.criterion[" + criterionIndex + "].operator";
var select = $(document.createElement('select'))
.attr("id", id)
.attr("name", name)
.addClass("filter_criterion_operator");
select.change(function() {
var criterionIndex = getCriterionIndexFromId($(this));
updateFilterInputs(criterionIndex, $(this), columns);
onFilterChange();
});
return select;
}
function buildFilterValues(criterionIndex) {
var id = "filter_criterion[" + criterionIndex + "]_values";
var name = "filter.criterion[" + criterionIndex + "].values";
var div = $(document.createElement('div'))
.addClass("filter_criterion_values")
.attr("id", id)
.attr("name", name);
return div;
}
function buildFilterRowActions(criterionIndex, columns) {
var id = "filter_criterion[" + criterionIndex + "]_actions";
var name = "filter.criterion[" + criterionIndex + "].actions";
var div = $(document.createElement('div'))
.addClass("filter_criterion_actions")
.attr("id", id)
.attr("name", name);
var addCriterion = $(document.createElement('a'))
.attr("href", "#");
div.append(addCriterion);
var addImage = $(document.createElement('img'))
.attr("id", id + "_add")
.addClass("filter_criterion_actions_add")
.attr("alt", "Add Criterion")
.attr("src", "../images/add-small.png")
.click(function() {
var criterionIndex = getCriterionIndexFromId($(this));
addFilterRow(criterionIndex, columns);
});
addCriterion.append(addImage);
var deleteCriterion = $(document.createElement('a'))
.attr("href", "#");
div.append(deleteCriterion);
var deleteImage = $(document.createElement('img'))
.attr("id", id + "_delete")
.addClass("filter_criterion_actions_delete")
.attr("alt", "Delete Criterion")
.attr("src", "../images/delete-small.png")
.click(function() {
var criterionIndex = getCriterionIndexFromId($(this));
deleteFilterRow(criterionIndex, columns);
});
deleteCriterion.append(deleteImage);
return div;
}
function updateFilterSelectOperatorOptions(criterionIndex, column) {
var select = $("#filter_criterion\\[" + criterionIndex + "\\]_operator");
select.children().remove();
var operators = getOperators(column.datatype);
$.each(operators, function(index, operator) {
select.append($(document.createElement('option')).attr("value", operator.value).text(operator.name));
});
}
function updateFilterInputs(criterionIndex, element, columns) {
var div = $("#filter_criterion\\[" + criterionIndex + "\\]_values");
div.children().remove();
var column = getColumn(criterionIndex, columns);
var operators = getOperators(column.datatype);
var numInputs = 1;
var selectedOperator = element.val();
$.each(operators, function(index, operator) {
if (selectedOperator == operator.value) {
numInputs = operator.numInputs;
}
});
for (var index = 0; index < numInputs; index++) {
if (index > 0) {
div.append($(document.createElement('span')).text(" and "));
}
var id = "filter_criterion[" + criterionIndex + "]_values[" + index + "]";
var name = "filter.criterion[" + criterionIndex + "].values[" + index + "]";
if (column.datatype == "enum") {
createSelect(div, column, id, name);
}
else {
createInput(div, column, id, name);
}
}
}
function createSelect(parent, column, id, name) {
var select = $(document.createElement('select'))
.attr("id", id)
.attr("name", name);
select.append(new Option());
$.each(column.values, function(index, value) {
select.append($(document.createElement('option')).attr("value", value.value).text(value.text));
});
parent.append(select);
select.change(function() { onFilterChange(); });
}
function createInput(parent, column, id, name) {
var input = $(document.createElement('input'))
.attr("id", id)
.attr("name", name)
.attr("type", "text");
parent.append(input);
if (column.datatype == "date") {
input.datepicker({
buttonImage: '../images/calendar.png',
buttonImageOnly: true,
dateFormat: 'dd/mm/yy',
showButtonPanel: true,
showOn: 'button',
onSelect: function(dateText, inst) { onFilterChange(); }
});
}
input.focus(function() {
lastValue = $(this).val();
})
input.keyup(function(e) {
if (timeout) {
clearTimeout(timeout);
}
var value = $(this).val();
if (value != lastValue) {
lastValue = value;
timeout = setTimeout(function() { onFilterChange(); }, options.delay);
}
})
}
function addFilterRow(criterionIndex, columns) {
var id = "#filter_criterion\\[" + criterionIndex + "\\]_row";
var newCriterionIndex = nextCriterionIndex++;
$(id).after(buildFilterRow(newCriterionIndex, columns));
var columnSelect = $("#filter_criterion\\[" + newCriterionIndex + "\\]_column");
columnSelect.change();
}
function deleteFilterRow(criterionIndex, columns) {
var id = "#filter_criterion\\[" + criterionIndex + "\\]_row";
$(id).remove();
onFilterChange();
}
function onFilterChange() {
updateCriterionIds();
options.onFilterChange();
}
function updateCriterionIds() {
var form = $("#filter_form");
var index = 0;
$(".filter_criterion_row", form).each(function() {
var div = $(this);
var baseId = "filter_criterion[" + index + "]";
var baseName = "filter.criterion[" + index + "]";
div
.attr("id", baseId + "_row")
.attr("name", baseId + ".row");
$(".filter_criterion_column", div)
.attr("id", baseId + "_column")
.attr("name", baseName + ".column");
$(".filter_criterion_operator", div)
.attr("id", baseId + "_operator")
.attr("name", baseName + ".operator");
var values = $(".filter_criterion_values", div);
values
.attr("id", baseId + "_values")
.attr("name", baseName + ".values");
var valuesIndex = 0;
$("input", values).each(function() {
$(this)
.attr("id", baseId + "_values[" + valuesIndex + "]")
.attr("name", baseName + ".values[" + valuesIndex + "]");
valuesIndex++;
});
$(".filter_criterion_actions", div)
.attr("id", baseId + "_actions")
.attr("name", baseId + ".actions");
$(".filter_criterion_actions_add", div)
.attr("id", baseId + "_actions_add");
$(".filter_criterion_actions_delete", div)
.attr("id", baseId + "_actions_delete");
index++;
});
}
function getColumn(criterionIndex, columns) {
var id = "#filter_criterion\\[" + criterionIndex + "\\]_column";
var element = $(id);
var val = element.val();
var column;
$.each(columns, function(index, value) { if (val == value.id) { column = value; } });
return column;
}
function getCriterionIndexFromId(element) {
var match = /\[(\d+)\]/.exec(element.attr("id"));
return match[1];
}
function getOperators(datatype) {
var operators;
switch (datatype) {
case 'date':
operators = options.dateOperators;
break;
case 'enum':
operators = options.enumOperators;
break;
case 'numeric':
operators = options.numericOperators;
break;
case 'string':
operators = options.stringOperators;
break;
}
return operators;
}
};
jQuery.fn.SlickFilter = function(columns, onFilterChange, options) {
// Make sure options exists
options = options || {};
options.columns = columns;
options.onFilterChange = onFilterChange;
// Set default values for required options
options = $.extend({
delay: 400,
dateOperators: [
{ value: 'equals', name: 'Equals', numInputs: 1 },
{ value: 'before', name: 'Before', numInputs: 1 },
{ value: 'after', name: 'After', numInputs: 1 },
{ value: 'between', name: 'Between', numInputs: 2 },
// { value: 'tomorrow', name: 'Tomorrow', numInputs: 1 },
// { value: 'today', name: 'Today', numInputs: 1 },
// { value: 'yesterday', name: 'Yesterday', numInputs: 1 },
// { value: 'next-week', name: 'Next Week', numInputs: 2 },
// { value: 'this-week', name: 'This Week', numInputs: 2 },
// { value: 'last-week', name: 'Last Week', numInputs: 2 },
// { value: 'next-month', name: 'Next Month', numInputs: 2 },
// { value: 'this-month', name: 'This Month', numInputs: 2 },
// { value: 'last-month', name: 'Last Month', numInputs: 2 },
// { value: 'next-quarter', name: 'Next Quarter', numInputs: 2 },
// { value: 'this-quarter', name: 'This Quarter', numInputs: 2 },
// { value: 'last-quarter', name: 'Last Quarter', numInputs: 2 },
// { value: 'next-year', name: 'Next Year', numInputs: 2 },
// { value: 'this-year', name: 'This Year', numInputs: 2 },
// { value: 'last-year', name: 'Last Year', numInputs: 2 },
// { value: 'year-to-date', name: 'Year To Date', numInputs: 2 }
// all dates in period
],
enumOperators: [
{ value: 'equals', name: 'Equals', numInputs: 1 },
{ value: 'does-not-equal', name: 'Does Not Equal', numInputs: 1 }
// list
],
numericOperators: [
{ value: 'equals', name: 'Equals', numInputs: 1 },
{ value: 'does-not-equal', name: 'Does Not Equal', numInputs: 1 },
{ value: 'greater-than', name: 'Greater Than', numInputs: 1 },
{ value: 'greater-than-or-equal-to', name: 'Greater Than Or Equal To', numInputs: 1 },
{ value: 'less-than', name: 'Less Than', numInputs: 1 },
{ value: 'less-than-or-equal-to', name: 'Less Than Or Equal To', numInputs: 1 },
{ value: 'between', name: 'Between', numInputs: 2 },
// Top 10
// Above Average
// Below Average
// list
],
stringOperators: [
{ value: 'equals', name: 'Equals', numInputs: 1 },
{ value: 'does-not-equal', name: 'Does Not Equal', numInputs: 1 },
{ value: 'begins-with', name: 'Begins With', numInputs: 1 },
{ value: 'ends-with', name: 'Ends With', numInputs: 1 },
{ value: 'contains', name: 'Contains', numInputs: 1 },
{ value: 'does-not-contain', name: 'Does Not Contain', numInputs: 1 }
// list
]
}, options);
this.each(function() {
var parent = $(this);
new jQuery.SlickFilter(parent, options);
});
// Don't break the chain
return this;
}