Skip to content

Commit 1676043

Browse files
SteveTheTechiemlh758
authored andcommitted
$variable naming & comments (#725)
Mostly changing to $naming scheme for jQuery objects and adding more comments. * $variable naming and ditch bind * Update limited selectedList/checkAll test * Changed the default selectedText to be "# of # selected" so that developers will be aware that this form is available.
1 parent 804cf1a commit 1676043

File tree

4 files changed

+733
-740
lines changed

4 files changed

+733
-740
lines changed

src/jquery.multiselect.filter.js

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/* jshint forin:true, noarg:true, noempty:true, eqeqeq:true, boss:true, undef:true, curly:true, browser:true, jquery:true */
22
/*
3-
* jQuery MultiSelect UI Widget Filtering Plugin 2.0.0
3+
* jQuery MultiSelect UI Widget Filtering Plugin 3.0.0
44
* Copyright (c) 2012 Eric Hynds
55
*
66
* http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/
77
*
88
* Depends:
9+
* - jQuery 1.7+
910
* - jQuery UI MultiSelect widget
1011
*
1112
* Dual licensed under the MIT and GPL licenses:
@@ -48,30 +49,30 @@
4849

4950
_create: function() {
5051
var opts = this.options;
51-
var elem = $(this.element);
52+
var $elem = this.element;
5253

5354
// get the multiselect instance
54-
this.instance = elem.multiselect('instance');
55+
this.instance = $elem.multiselect('instance');
5556

5657
// store header; add filter class so the close/check all/uncheck all links can be positioned correctly
57-
this.header = this.instance.menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter');
58+
this.$header = this.instance.$menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter');
5859

59-
// wrapper elem
60-
this.input = $("<input/>").attr({
60+
// wrapper $elem
61+
this.$input = $("<input/>").attr({
6162
placeholder: opts.placeholder,
6263
type: "search"
6364
}).css({
6465
width: (/\d/.test(opts.width) ? opts.width + 'px' : null)
65-
}).bind({
66+
}).on({
6667
keydown: function(e) {
6768
// prevent the enter key from submitting the form / closing the widget
6869
if(e.which === 13) {
6970
e.preventDefault();
7071
} else if(e.which === 27) {
71-
elem.multiselect('close');
72+
$elem.multiselect('close');
7273
e.preventDefault();
7374
} else if(e.which === 9 && e.shiftKey) {
74-
elem.multiselect('close');
75+
$elem.multiselect('close');
7576
e.preventDefault();
7677
} else if(e.altKey) {
7778
switch(e.which) {
@@ -80,13 +81,13 @@
8081
$(this).val('').trigger('input', '');
8182
break;
8283
case 65:
83-
elem.multiselect('checkAll');
84+
$elem.multiselect('checkAll');
8485
break;
8586
case 85:
86-
elem.multiselect('uncheckAll');
87+
$elem.multiselect('uncheckAll');
8788
break;
8889
case 76:
89-
elem.multiselect('instance').labels.first().trigger("mouseenter");
90+
$elem.multiselect('instance').$labels.first().trigger("mouseenter");
9091
break;
9192
}
9293
}
@@ -96,25 +97,25 @@
9697
});
9798
// automatically reset the widget on close?
9899
if(this.options.autoReset) {
99-
elem.bind('multiselectclose', $.proxy(this._reset, this));
100+
$elem.on('multiselectclose', $.proxy(this._reset, this));
100101
}
101102
// rebuild cache when multiselect is updated
102-
elem.bind('multiselectrefresh', $.proxy(function() {
103+
$elem.on('multiselectrefresh', $.proxy(function() {
103104
this.updateCache();
104105
this._handler();
105106
}, this));
106-
this.wrapper = $("<div/>").addClass("ui-multiselect-filter").text(opts.label).append(this.input).prependTo(this.header);
107+
this.$wrapper = $("<div/>").addClass("ui-multiselect-filter").text(opts.label).append(this.$input).prependTo(this.$header);
107108

108109
// reference to the actual inputs
109-
this.inputs = this.instance.menu.find('input[type="checkbox"], input[type="radio"]');
110+
this.$inputs = this.instance.$menu.find('input[type="checkbox"], input[type="radio"]');
110111

111112
// cache input values for searching
112113
this.updateCache();
113114

114115
// rewrite internal _toggleChecked fn so that when checkAll/uncheckAll is fired,
115-
// only the currently filtered elements are checked
116+
// only the currently filtered $elements are checked
116117
this.instance._toggleChecked = function(flag, group) {
117-
var $inputs = (group && group.length) ? group : this.labels.find('input');
118+
var $inputs = (group && group.length) ? group : this.$labels.find('input');
118119
var _self = this;
119120

120121
// do not include hidden elems if the menu isn't open.
@@ -149,11 +150,11 @@
149150

150151
// thx for the logic here ben alman
151152
_handler: function(e) {
152-
var term = $.trim(this.input[0].value.toLowerCase()),
153+
var term = $.trim(this.$input[0].value.toLowerCase()),
153154

154155
// speed up lookups
155-
rows = this.rows, inputs = this.inputs, cache = this.cache;
156-
var $groups = this.instance.menu.find(".ui-multiselect-optgroup");
156+
rows = this.rows, $inputs = this.$inputs, $cache = this.$cache;
157+
var $groups = this.instance.$menu.find(".ui-multiselect-optgroup");
157158
$groups.show();
158159
if(!term) {
159160
rows.show();
@@ -162,10 +163,10 @@
162163

163164
var regex = new RegExp(term.replace(rEscape, "\\$&"), 'gi');
164165

165-
this._trigger("filter", e, $.map(cache, function(v, i) {
166+
this._trigger("filter", e, $.map($cache, function(v, i) {
166167
if(v.search(regex) !== -1) {
167168
rows.eq(i).show();
168-
return inputs.get(i);
169+
return $inputs.get(i);
169170
}
170171

171172
return null;
@@ -183,36 +184,36 @@
183184
},
184185

185186
_reset: function() {
186-
this.input.val('').trigger('input', '');
187+
this.$input.val('').trigger('input', '');
187188
},
188189

189190
updateCache: function() {
190191
// each list item
191-
this.rows = this.instance.labels.parent();
192+
this.rows = this.instance.$labels.parent();
192193

193194
// cache
194-
this.cache = this.element.children().map(function() {
195-
var elem = $(this);
195+
this.$cache = this.element.children().map(function() {
196+
var $elem = $(this);
196197

197198
// account for optgroups
198199
if(this.tagName.toLowerCase() === "optgroup") {
199-
elem = elem.children();
200+
$elem = $elem.children();
200201
}
201202

202-
return elem.map(function() {
203+
return $elem.map(function() {
203204
return this.innerHTML.toLowerCase();
204205
}).get();
205206
}).get();
206207
},
207208

208209
widget: function() {
209-
return this.wrapper;
210+
return this.$wrapper;
210211
},
211212

212213
destroy: function() {
213214
$.Widget.prototype.destroy.call(this);
214-
this.input.val('').trigger("keyup");
215-
this.wrapper.remove();
215+
this.$input.val('').trigger("keyup");
216+
this.$wrapper.remove();
216217
}
217218
});
218219

0 commit comments

Comments
 (0)