-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathbootstrap-list-filter.src.js
executable file
·183 lines (151 loc) · 4.76 KB
/
bootstrap-list-filter.src.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
/*
* OPTIONS
*
* delay *millisecond before apply filter*
* minLength *min string lentgh searched*
* initial *search only initial text (default: true)*
* eventKey *event digit (default: 'keyup')*
* resetOnBlur *auto reset selection*
* sourceData *function generate data source(receive: text, callback)*
* sourceTmpl *html template contains {title} placeholder*
* sourceNode *function builder DOM html fragment (default: sourceTmpl)*
* emptyNode *function builder for empty result*
* itemEl *item selector (default: .list-group-item)*,
* itemChild *sub item selector (default: .list-group-item)*,
* itemFilter *function for filter results(receive: text, item)*
*/
(function($) {
$.fn.btsListFilter = function(inputEl, opts) {
'use strict';
var self = this,
searchlist$ = $(this),
inputEl$ = $(inputEl),
cancelEl$,
items$ = searchlist$,
callData,
callReq; //last callData execution
function tmpl(str, data) {
return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
return data[key] || '';
});
}
function defaultItemFilter(item, val) {
val = val && val.replace(new RegExp("[({[^.$*+?\\\]})]","g"),'');
//sanitize regexp
var text = $(item).text(),
i = opts.initial ? '^' : '',
regSearch = new RegExp(i + val, opts.casesensitive ? '' : 'i');
return regSearch.test( text );
}
opts = $.extend({
delay: 300,
minLength: 1,
initial: true,
casesensitive: false,
eventKey: 'keyup',
resetOnBlur: true,
sourceData: null,
sourceTmpl: '<a class="list-group-item" href="#"><span>{title}</span></a>',
sourceNode: function(data) {
return tmpl(opts.sourceTmpl, data);
},
emptyNode: function(data) {
return '<a class="list-group-item well" href="#"><span>No Results</span></a>';
},
cancelNode: function() {
return '<span class="btn glyphicon glyphicon-remove form-control-feedback" aria-hidden="true"></span>';
},
loadingClass: 'bts-loading-list',
itemClassTmp: 'bts-dynamic-item',
itemEl: '.list-group-item',
itemChild: null,
itemFilter: defaultItemFilter
}, opts);
function debouncer(func, timeout) {
var timeoutID;
timeout = timeout || 300;
return function () {
var scope = this , args = arguments;
clearTimeout( timeoutID );
timeoutID = setTimeout( function () {
func.apply( scope , Array.prototype.slice.call( args ) );
}, timeout);
};
}
self.reset = function() {
inputEl$.val('').trigger(opts.eventKey);
};
if($.isFunction(opts.cancelNode)) {
cancelEl$ = $(opts.cancelNode.call(self)).hide();
inputEl$.after( cancelEl$ );
inputEl$.parents('.form-group').addClass('has-feedback');
if(!inputEl$.prev().is('.control-label'))
cancelEl$.css({top: 0});
cancelEl$.css({'pointer-events': 'auto'});
cancelEl$.on('click', self.reset);
}
inputEl$.on(opts.eventKey, debouncer(function(e) {
var val = $(this).val();
if(opts.itemEl)
items$ = searchlist$.find(opts.itemEl);
if(opts.itemChild)
items$ = items$.find(opts.itemChild);
var contains = items$.filter(function(){
return opts.itemFilter.call(self, this, val);
}),
containsNot = items$.not(contains);
if (opts.itemChild){
contains = contains.parents(opts.itemEl);
containsNot = containsNot.parents(opts.itemEl).hide();
}
if(val!=='' && val.length >= opts.minLength)
{
contains.show();
containsNot.hide();
cancelEl$.show();
if($.type(opts.sourceData)==='function')
{
contains.hide();
containsNot.hide();
if(callReq)
{
if($.isFunction(callReq.abort))
callReq.abort();
else if($.isFunction(callReq.stop))
callReq.stop();
}
searchlist$.addClass(opts.loadingClass);
callReq = opts.sourceData.call(self, val, function(data) {
callReq = null;
contains.hide();
containsNot.hide();
searchlist$.find('.'+opts.itemClassTmp).remove();
if(!data || data.length===0)
$( opts.emptyNode.call(self, val) ).addClass(opts.itemClassTmp).appendTo(searchlist$);
else
for(var i in data)
$( opts.sourceNode.call(self, data[i]) ).addClass(opts.itemClassTmp).appendTo(searchlist$);
searchlist$.removeClass(opts.loadingClass);
});
}
else {
searchlist$.find('.'+opts.itemClassTmp).remove();
if(contains.length===0)
$( opts.emptyNode.call(self, val) ).addClass(opts.itemClassTmp).appendTo(searchlist$);
}
}
else
{
contains.show();
containsNot.show();
cancelEl$.hide();
searchlist$.find('.'+opts.itemClassTmp).remove();
}
}, opts.delay));
if(opts.resetOnBlur)
inputEl$.on('blur', function(e) {
self.reset();
});
return searchlist$;
};
})(jQuery);