Skip to content

Commit 545f4b6

Browse files
author
ehynds
committed
code consistency/whitespace changes to the filter widget
1 parent 6744477 commit 545f4b6

File tree

1 file changed

+151
-153
lines changed

1 file changed

+151
-153
lines changed

src/jquery.multiselect.filter.js

Lines changed: 151 additions & 153 deletions
Original file line numberDiff line numberDiff line change
@@ -12,163 +12,161 @@
1212
* http://www.opensource.org/licenses/mit-license.php
1313
* http://www.gnu.org/licenses/gpl.html
1414
*
15-
*/
16-
(function($){
17-
var rEscape = /[\-\[\]{}()*+?.,\\\^$|#\s]/g;
18-
19-
$.widget("ech.multiselectfilter", {
20-
21-
options: {
22-
label: "Filter:",
23-
width: null, /* override default width set in css file (px). null will inherit */
24-
placeholder: "Enter keywords",
25-
autoReset: false
26-
},
27-
28-
_create: function(){
29-
var self = this,
30-
opts = this.options,
31-
instance = (this.instance = $(this.element).data("multiselect")),
32-
33-
// store header; add filter class so the close/check all/uncheck all links can be positioned correctly
34-
header = (this.header = instance.menu.find(".ui-multiselect-header").addClass("ui-multiselect-hasfilter")),
35-
36-
// wrapper elem
37-
wrapper = (this.wrapper = $('<div class="ui-multiselect-filter">'+(opts.label.length ? opts.label : '')+'<input placeholder="'+opts.placeholder+'" type="search"' + (/\d/.test(opts.width) ? 'style="width:'+opts.width+'px"' : '') + ' /></div>').prependTo( this.header ));
38-
39-
// reference to the actual inputs
40-
this.inputs = instance.menu.find('input[type="checkbox"], input[type="radio"]');
41-
42-
// build the input box
43-
this.input = wrapper
44-
.find("input")
45-
.bind({
46-
keydown: function( e ){
47-
// prevent the enter key from submitting the form / closing the widget
48-
if( e.which === 13 ){
49-
e.preventDefault();
50-
}
51-
},
52-
keyup: $.proxy(self._handler, self),
53-
click: $.proxy(self._handler, self)
54-
});
55-
56-
// cache input values for searching
57-
this.updateCache();
58-
59-
// rewrite internal _toggleChecked fn so that when checkAll/uncheckAll is fired,
60-
// only the currently filtered elements are checked
61-
instance._toggleChecked = function(flag, group){
62-
var $inputs = (group && group.length) ? group : this.labels.find('input');
63-
var _self = this;
15+
*/
16+
(function($) {
17+
var rEscape = /[\-\[\]{}()*+?.,\\\^$|#\s]/g;
18+
19+
$.widget('ech.multiselectfilter', {
20+
21+
options: {
22+
label: 'Filter:',
23+
width: null, /* override default width set in css file (px). null will inherit */
24+
placeholder: 'Enter keywords',
25+
autoReset: false
26+
},
27+
28+
_create: function() {
29+
var opts = this.options;
30+
31+
// get the multiselect instance
32+
var instance = (this.instance = $(this.element).data('multiselect'));
33+
34+
// store header; add filter class so the close/check all/uncheck all links can be positioned correctly
35+
var header = (this.header = instance.menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter'));
36+
37+
// wrapper elem
38+
var wrapper = (this.wrapper = $('<div class="ui-multiselect-filter">' + (opts.label.length ? opts.label : '') + '<input placeholder="'+opts.placeholder+'" type="search"' + (/\d/.test(opts.width) ? 'style="width:'+opts.width+'px"' : '') + ' /></div>').prependTo(this.header));
39+
40+
// reference to the actual inputs
41+
this.inputs = instance.menu.find('input[type="checkbox"], input[type="radio"]');
42+
43+
// build the input box
44+
this.input = wrapper.find('input').bind({
45+
keydown: function(e) {
46+
// prevent the enter key from submitting the form / closing the widget
47+
if(e.which === 13) {
48+
e.preventDefault();
49+
}
50+
},
51+
keyup: $.proxy(this._handler, this),
52+
click: $.proxy(this._handler, this)
53+
});
54+
55+
// cache input values for searching
56+
this.updateCache();
57+
58+
// rewrite internal _toggleChecked fn so that when checkAll/uncheckAll is fired,
59+
// only the currently filtered elements are checked
60+
instance._toggleChecked = function(flag, group) {
61+
var $inputs = (group && group.length) ? group : this.labels.find('input');
62+
6463
// do not include hidden elems if the menu isn't open.
65-
var selector = self.instance._isOpen ? ":disabled, :hidden" : ":disabled";
66-
67-
$inputs = $inputs
68-
.not( selector )
69-
.each(this._toggleState('checked', flag));
70-
71-
// update text
72-
this.update();
73-
64+
var selector = instance._isOpen ? ':disabled, :hidden' : ':disabled';
65+
66+
$inputs = $inputs
67+
.not(selector)
68+
.each(this._toggleState('checked', flag));
69+
70+
// update text
71+
this.update();
72+
7473
// gather an array of the values that actually changed
75-
var values = $inputs.map(function(){
76-
return this.value;
77-
}).get();
78-
79-
// select option tags
80-
this.element
81-
.find('option')
82-
.filter(function(){
83-
if( !this.disabled && $.inArray(this.value, values) > -1 ){
84-
_self._toggleState('selected', flag).call( this );
85-
}
86-
});
74+
var values = $inputs.map(function() {
75+
return this.value;
76+
}).get();
77+
78+
// select option tags
79+
this.element.find('option').filter(function() {
80+
if(!this.disabled && $.inArray(this.value, values) > -1) {
81+
_self._toggleState('selected', flag).call(this);
82+
}
83+
});
8784

8885
// trigger the change event on the select
8986
if($inputs.length) {
90-
this.element.trigger("change");
87+
this.element.trigger('change');
88+
}
89+
};
90+
91+
// rebuild cache when multiselect is updated
92+
var doc = $(document).bind('multiselectrefresh', $.proxy(function() {
93+
this.updateCache();
94+
this._handler();
95+
}, this));
96+
97+
// automatically reset the widget on close?
98+
if(this.options.autoReset) {
99+
doc.bind('multiselectclose', $.proxy(this._reset, this));
100+
}
101+
},
102+
103+
// thx for the logic here ben alman
104+
_handler: function(e) {
105+
var term = $.trim(this.input[0].value.toLowerCase()),
106+
107+
// speed up lookups
108+
rows = this.rows, inputs = this.inputs, cache = this.cache;
109+
110+
if(!term) {
111+
rows.show();
112+
} else {
113+
rows.hide();
114+
115+
var regex = new RegExp(term.replace(rEscape, "\\$&"), 'gi');
116+
117+
this._trigger("filter", e, $.map(cache, function(v, i) {
118+
if(v.search(regex) !== -1) {
119+
rows.eq(i).show();
120+
return inputs.get(i);
121+
}
122+
123+
return null;
124+
}));
125+
}
126+
127+
// show/hide optgroups
128+
this.instance.menu.find(".ui-multiselect-optgroup-label").each(function() {
129+
var $this = $(this);
130+
var isVisible = $this.nextUntil('.ui-multiselect-optgroup-label').filter(function() {
131+
return $.css(this, "display") !== 'none';
132+
}).length;
133+
134+
$this[isVisible ? 'show' : 'hide']();
135+
});
136+
},
137+
138+
_reset: function() {
139+
this.input.val('').trigger('keyup');
140+
},
141+
142+
updateCache: function() {
143+
// each list item
144+
this.rows = this.instance.menu.find(".ui-multiselect-checkboxes li:not(.ui-multiselect-optgroup-label)");
145+
146+
// cache
147+
this.cache = this.element.children().map(function() {
148+
var elem = $(this);
149+
150+
// account for optgroups
151+
if(this.tagName.toLowerCase() === "optgroup") {
152+
elem = elem.children();
91153
}
92-
};
93-
94-
// rebuild cache when multiselect is updated
95-
var doc = $(document).bind("multiselectrefresh", function(){
96-
self.updateCache();
97-
self._handler();
98-
});
99-
100-
// automatically reset the widget on close?
101-
if(this.options.autoReset) {
102-
doc.bind("multiselectclose", $.proxy(this._reset, this));
103-
}
104-
},
105-
106-
// thx for the logic here ben alman
107-
_handler: function( e ){
108-
var term = $.trim( this.input[0].value.toLowerCase() ),
109-
110-
// speed up lookups
111-
rows = this.rows, inputs = this.inputs, cache = this.cache;
112-
113-
if( !term ){
114-
rows.show();
115-
} else {
116-
rows.hide();
117-
118-
var regex = new RegExp(term.replace(rEscape, "\\$&"), 'gi');
119-
120-
this._trigger( "filter", e, $.map(cache, function(v, i){
121-
if( v.search(regex) !== -1 ){
122-
rows.eq(i).show();
123-
return inputs.get(i);
124-
}
125-
126-
return null;
127-
}));
128-
}
129-
130-
// show/hide optgroups
131-
this.instance.menu.find(".ui-multiselect-optgroup-label").each(function(){
132-
var $this = $(this);
133-
var isVisible = $this.nextUntil('.ui-multiselect-optgroup-label').filter(function(){
134-
return $.css(this, "display") !== 'none';
135-
}).length;
136-
137-
$this[ isVisible ? 'show' : 'hide' ]();
138-
});
139-
},
140-
141-
_reset: function() {
142-
this.input.val('').trigger('keyup');
143-
},
144-
145-
updateCache: function(){
146-
// each list item
147-
this.rows = this.instance.menu.find(".ui-multiselect-checkboxes li:not(.ui-multiselect-optgroup-label)");
148-
149-
// cache
150-
this.cache = this.element.children().map(function(){
151-
var self = $(this);
152-
153-
// account for optgroups
154-
if( this.tagName.toLowerCase() === "optgroup" ){
155-
self = self.children();
156-
}
157-
158-
return self.map(function(){
159-
return this.innerHTML.toLowerCase();
160-
}).get();
161-
}).get();
162-
},
163-
164-
widget: function(){
165-
return this.wrapper;
166-
},
167-
168-
destroy: function(){
169-
$.Widget.prototype.destroy.call( this );
170-
this.input.val('').trigger("keyup");
171-
this.wrapper.remove();
172-
}
173-
});
154+
155+
return elem.map(function() {
156+
return this.innerHTML.toLowerCase();
157+
}).get();
158+
}).get();
159+
},
160+
161+
widget: function() {
162+
return this.wrapper;
163+
},
164+
165+
destroy: function() {
166+
$.Widget.prototype.destroy.call(this);
167+
this.input.val('').trigger("keyup");
168+
this.wrapper.remove();
169+
}
170+
});
171+
174172
})(jQuery);

0 commit comments

Comments
 (0)