Skip to content

Commit 4cddd55

Browse files
committed
Added 'onFilterSelect' callback to process selected values
- Added timer for search and filter events
1 parent b870592 commit 4cddd55

18 files changed

Lines changed: 80 additions & 194 deletions

dist/filter.js

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
22
* filter.js
3-
* 2.0.0 (2014-12-23)
3+
* 2.0.0 (2015-01-20)
44
*
55
* Released under the MIT license
66
* http://opensource.org/licenses/MIT
77
*
8-
* Copyright 2011-2014 Jiren Patel[jirenpatel@gmail.com]
8+
* Copyright 2011-2015 Jiren Patel[jirenpatel@gmail.com]
99
*
1010
* Dependency:
1111
* jQuery(v1.9 >=)
@@ -845,9 +845,9 @@ F.template = function(str, data) {
845845
};
846846

847847
//Callback
848-
F.execCallback = function(name, records){
848+
F.execCallback = function(name, args){
849849
if(this.callbacks[name]) {
850-
this.callbacks[name].call(this, records);
850+
this.callbacks[name].call(this, args);
851851
}
852852
};
853853

@@ -956,12 +956,6 @@ var setDefaultCriteriaOpts = function(criteria){
956956
return criteria;
957957
};
958958

959-
var bindFilterEvent = function(criteria, context){
960-
$('body').on(criteria.event, criteria.ele, function(e) {
961-
context.filter();
962-
});
963-
};
964-
965959
F.addCriteria = function(criterias){
966960
var self = this;
967961

@@ -992,7 +986,7 @@ var addFilterCriteria = function(criteria){
992986
}
993987

994988
criteria = setDefaultCriteriaOpts(criteria);
995-
bindFilterEvent(criteria, this);
989+
this.bindEvent(criteria.ele, criteria.event);
996990

997991
criteria._q = criteria.field + (criteria.type == 'range' ? '.$bt' : '')
998992
criteria.active = true;
@@ -1045,30 +1039,36 @@ F.activateCriteria = function(names){
10451039
changeCriteriaStatus.call(this, names, true);
10461040
};
10471041

1048-
var getSelectedValues = function(criteria){
1042+
var getSelectedValues = function(criteria, context){
10491043
var vals = [];
10501044

10511045
criteria.$ele.filter(criteria.selector).each(function() {
10521046
vals.push($(this).val());
10531047
});
10541048

1049+
if($.isArray(vals[0])){
1050+
vals = [].concat(vals);
1051+
}
1052+
10551053
if(criteria.type == 'range'){
10561054
vals = vals[0].split('-');
10571055
}
10581056

1059-
return vals;
1057+
return context.execCallback('onFilterSelect', {criteria: criteria, values: vals}) || vals;
10601058
};
10611059

10621060
F.lastResult = function(){
10631061
return (this.last_result || this.records);
10641062
};
10651063

10661064
F.filter = function(){
1067-
var query = {}, vals, _q;
1065+
var query = {},
1066+
vals, _q,
1067+
self = this;
10681068

10691069
$.each(this.criterias, function(){
10701070
if(this.active){
1071-
vals = getSelectedValues(this);
1071+
vals = getSelectedValues(this, self);
10721072

10731073
if(vals || vals.length){
10741074
_q = ($.isArray(vals) && !this.type) ? (this._q + '.$in') : this._q;
@@ -1098,16 +1098,24 @@ F.show = function(result, type){
10981098
}
10991099
};
11001100

1101+
F.filterTimer = function(timeout){
1102+
var self = this;
1103+
1104+
if (this.filterTimeoutId) {
1105+
clearTimeout(this.filterTimeoutId);
1106+
}
1107+
1108+
this.filterTimeoutId = setTimeout(function() {
1109+
self.filter();
1110+
}, timeout);
1111+
};
1112+
11011113
//Search
1102-
var bindSearchEvent = function(searchBox, timeout, context){
1103-
$('body').on('keyup', searchBox, function(e){
1104-
if (context.searchTimeoutId) {
1105-
clearTimeout(context.searchTimeoutId);
1106-
}
1107-
context.searchTimeoutId = setTimeout(function() {
1108-
context.filter();
1109-
}, timeout);
1110-
//context.searchFilter(true);
1114+
F.bindEvent = function(ele, eventName){
1115+
var self = this;
1116+
1117+
$(document).on(eventName, ele, function(e){
1118+
self.filterTimer(self.opts.timeout || 70);
11111119
});
11121120
};
11131121

@@ -1125,7 +1133,7 @@ F.initSearch = function(opts){
11251133
if(this.$search_ele.length){
11261134
this.has_search = true;
11271135
this.searchFn = this.buildSearchFn(opts.fields);
1128-
bindSearchEvent(opts.ele, opts.timeout || 0, this);
1136+
this.bindEvent(opts.ele, 'keyup');
11291137
}
11301138
};
11311139

dist/filter.min.js

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ $(document).ready(function(){
2929
}
3030
});
3131

32+
/*
3233
FJS.setStreaming({
3334
data_url: 'data/stream_movies.json',
3435
stream_after: 1,
3536
batch_size: 50
3637
});
38+
*/
3739

3840
FJS.addCriteria({field: 'year', ele: '#year_filter', type: 'range'});
3941
FJS.addCriteria({field: 'rating', ele: '#rating_filter', type: 'range'});

src/filter.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ F.template = function(str, data) {
9292
};
9393

9494
//Callback
95-
F.execCallback = function(name, records){
95+
F.execCallback = function(name, args){
9696
if(this.callbacks[name]) {
97-
this.callbacks[name].call(this, records);
97+
this.callbacks[name].call(this, args);
9898
}
9999
};
100100

@@ -203,12 +203,6 @@ var setDefaultCriteriaOpts = function(criteria){
203203
return criteria;
204204
};
205205

206-
var bindFilterEvent = function(criteria, context){
207-
$('body').on(criteria.event, criteria.ele, function(e) {
208-
context.filter();
209-
});
210-
};
211-
212206
F.addCriteria = function(criterias){
213207
var self = this;
214208

@@ -239,7 +233,7 @@ var addFilterCriteria = function(criteria){
239233
}
240234

241235
criteria = setDefaultCriteriaOpts(criteria);
242-
bindFilterEvent(criteria, this);
236+
this.bindEvent(criteria.ele, criteria.event);
243237

244238
criteria._q = criteria.field + (criteria.type == 'range' ? '.$bt' : '')
245239
criteria.active = true;
@@ -292,30 +286,36 @@ F.activateCriteria = function(names){
292286
changeCriteriaStatus.call(this, names, true);
293287
};
294288

295-
var getSelectedValues = function(criteria){
289+
var getSelectedValues = function(criteria, context){
296290
var vals = [];
297291

298292
criteria.$ele.filter(criteria.selector).each(function() {
299293
vals.push($(this).val());
300294
});
301295

296+
if($.isArray(vals[0])){
297+
vals = [].concat(vals);
298+
}
299+
302300
if(criteria.type == 'range'){
303301
vals = vals[0].split('-');
304302
}
305303

306-
return vals;
304+
return context.execCallback('onFilterSelect', {criteria: criteria, values: vals}) || vals;
307305
};
308306

309307
F.lastResult = function(){
310308
return (this.last_result || this.records);
311309
};
312310

313311
F.filter = function(){
314-
var query = {}, vals, _q;
312+
var query = {},
313+
vals, _q,
314+
self = this;
315315

316316
$.each(this.criterias, function(){
317317
if(this.active){
318-
vals = getSelectedValues(this);
318+
vals = getSelectedValues(this, self);
319319

320320
if(vals || vals.length){
321321
_q = ($.isArray(vals) && !this.type) ? (this._q + '.$in') : this._q;
@@ -345,16 +345,24 @@ F.show = function(result, type){
345345
}
346346
};
347347

348+
F.filterTimer = function(timeout){
349+
var self = this;
350+
351+
if (this.filterTimeoutId) {
352+
clearTimeout(this.filterTimeoutId);
353+
}
354+
355+
this.filterTimeoutId = setTimeout(function() {
356+
self.filter();
357+
}, timeout);
358+
};
359+
348360
//Search
349-
var bindSearchEvent = function(searchBox, timeout, context){
350-
$('body').on('keyup', searchBox, function(e){
351-
if (context.searchTimeoutId) {
352-
clearTimeout(context.searchTimeoutId);
353-
}
354-
context.searchTimeoutId = setTimeout(function() {
355-
context.filter();
356-
}, timeout);
357-
//context.searchFilter(true);
361+
F.bindEvent = function(ele, eventName){
362+
var self = this;
363+
364+
$(document).on(eventName, ele, function(e){
365+
self.filterTimer(self.opts.timeout || 70);
358366
});
359367
};
360368

@@ -372,7 +380,7 @@ F.initSearch = function(opts){
372380
if(this.$search_ele.length){
373381
this.has_search = true;
374382
this.searchFn = this.buildSearchFn(opts.fields);
375-
bindSearchEvent(opts.ele, opts.timeout || 0, this);
383+
this.bindEvent(opts.ele, 'keyup');
376384
}
377385
};
378386

src/pagination.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/views/1/_first_page.html.erb

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/views/1/_gap.html.erb

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/views/1/_last_page.html.erb

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/views/1/_next_page.html.erb

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/views/1/_page.html.erb

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)