Skip to content

Commit 105bb21

Browse files
SteveTheTechiemlh758
authored andcommitted
Improve performance and fix issues (#728)
* Improve performance and fix issues 1. Add htmlButtonText option 2. Use more local variables instead of frequently referencing widget properties/options. 3. Use document.createElement() to more quickly create new DOM elements. 4. Eliminate use of href="#" in header links. Add cursor: pointer to jquery.multiselect.css for these links. 5. Move insertions to end of _create to limit browser reflowing lags. 6. Create _updateCache() internal function... called from multiple places. 7. Add check in update() to reposition menu if button height has changed from saved value and the menu is open. 8. Save the current button height in position(). Tests Updated 1. Added test for namespace separation to core.js 2. Added selectedList:0 to tests in core.js to ensure that button text is correct for tests. 3. Fixed minWidth percentage test in options.js that was failing for non-Chrome browsers.
1 parent 1676043 commit 105bb21

File tree

5 files changed

+595
-557
lines changed

5 files changed

+595
-557
lines changed

jquery.multiselect.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
.ui-multiselect-header ul { font-size:0.9em }
88
.ui-multiselect-header ul li { float:left; padding:0 10px 0 0; }
99
.ui-multiselect-header a { text-decoration:none; }
10-
.ui-multiselect-header a:hover { text-decoration:underline; }
10+
.ui-multiselect-header a:hover { text-decoration:underline; cursor: pointer;}
1111
.ui-multiselect-header span.ui-icon { float:left; }
1212
.ui-multiselect-header .ui-multiselect-close { float:right; padding-right:0; text-align:right; }
1313

src/jquery.multiselect.filter.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
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 3.0.0
3+
* jQuery MultiSelect UI Widget Filtering Plugin 2.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+
109
* - jQuery UI MultiSelect widget
1110
*
1211
* Dual licensed under the MIT and GPL licenses:
@@ -49,45 +48,48 @@
4948

5049
_create: function() {
5150
var opts = this.options;
52-
var $elem = this.element;
51+
var $element = this.element;
5352

5453
// get the multiselect instance
55-
this.instance = $elem.multiselect('instance');
54+
this.instance = $element.multiselect('instance');
5655

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

60-
// wrapper $elem
61-
this.$input = $("<input/>").attr({
59+
// wrapper $element
60+
this.$input = $(document.createElement('input'))
61+
.attr({
6262
placeholder: opts.placeholder,
6363
type: "search"
64-
}).css({
65-
width: (/\d/.test(opts.width) ? opts.width + 'px' : null)
66-
}).on({
64+
})
65+
.css({ width: (/\d/.test(opts.width) ? opts.width + 'px' : null) })
66+
.on({
6767
keydown: function(e) {
6868
// prevent the enter key from submitting the form / closing the widget
69-
if(e.which === 13) {
69+
if(e.which === 13)
7070
e.preventDefault();
71-
} else if(e.which === 27) {
72-
$elem.multiselect('close');
71+
else if(e.which === 27) {
72+
$element.multiselect('close');
7373
e.preventDefault();
74-
} else if(e.which === 9 && e.shiftKey) {
75-
$elem.multiselect('close');
74+
}
75+
else if(e.which === 9 && e.shiftKey) {
76+
$element.multiselect('close');
7677
e.preventDefault();
77-
} else if(e.altKey) {
78+
}
79+
else if(e.altKey) {
7880
switch(e.which) {
7981
case 82:
8082
e.preventDefault();
8183
$(this).val('').trigger('input', '');
8284
break;
8385
case 65:
84-
$elem.multiselect('checkAll');
86+
$element.multiselect('checkAll');
8587
break;
8688
case 85:
87-
$elem.multiselect('uncheckAll');
89+
$element.multiselect('uncheckAll');
8890
break;
8991
case 76:
90-
$elem.multiselect('instance').$labels.first().trigger("mouseenter");
92+
$element.multiselect('instance').$labels.first().trigger("mouseenter");
9193
break;
9294
}
9395
}
@@ -96,15 +98,19 @@
9698
search: $.proxy(this._handler, this)
9799
});
98100
// automatically reset the widget on close?
99-
if(this.options.autoReset) {
100-
$elem.on('multiselectclose', $.proxy(this._reset, this));
101-
}
101+
if (this.options.autoReset)
102+
$element.on('multiselectclose', $.proxy(this._reset, this));
103+
102104
// rebuild cache when multiselect is updated
103-
$elem.on('multiselectrefresh', $.proxy(function() {
105+
$element.on('multiselectrefresh', $.proxy(function() {
104106
this.updateCache();
105107
this._handler();
106108
}, this));
107-
this.$wrapper = $("<div/>").addClass("ui-multiselect-filter").text(opts.label).append(this.$input).prependTo(this.$header);
109+
this.$wrapper = $(document.createElement('div'))
110+
.addClass(' ui-multiselect-filter')
111+
.text(opts.label)
112+
.append(this.$input)
113+
.prependTo(this.$header);
108114

109115
// reference to the actual inputs
110116
this.$inputs = this.instance.$menu.find('input[type="checkbox"], input[type="radio"]');
@@ -116,10 +122,11 @@
116122
// only the currently filtered $elements are checked
117123
this.instance._toggleChecked = function(flag, group) {
118124
var $inputs = (group && group.length) ? group : this.$labels.find('input');
119-
var _self = this;
125+
var self = this;
126+
var $element = this.element;
120127

121128
// do not include hidden elems if the menu isn't open.
122-
var selector = _self._isOpen ? ':disabled, :hidden' : ':disabled';
129+
var selector = self._isOpen ? ':disabled, :hidden' : ':disabled';
123130

124131
$inputs = $inputs
125132
.not(selector)
@@ -135,16 +142,16 @@
135142
});
136143

137144
// select option tags
138-
this.element.find('option').filter(function() {
145+
$element.find('option').filter(function() {
139146
if(!this.disabled && values[this.value]) {
140-
_self._toggleState('selected', flag).call(this);
147+
self._toggleState('selected', flag).call(this);
141148
}
142149
});
143150

144151
// trigger the change event on the select
145-
if($inputs.length) {
146-
this.element.trigger('change');
147-
}
152+
if($inputs.length)
153+
$element.trigger('change');
154+
148155
};
149156
},
150157

@@ -153,19 +160,19 @@
153160
var term = $.trim(this.$input[0].value.toLowerCase()),
154161

155162
// speed up lookups
156-
rows = this.rows, $inputs = this.$inputs, $cache = this.$cache;
163+
$rows = this.$rows, $inputs = this.$inputs, $cache = this.$cache;
157164
var $groups = this.instance.$menu.find(".ui-multiselect-optgroup");
158165
$groups.show();
159166
if(!term) {
160-
rows.show();
167+
$rows.show();
161168
} else {
162-
rows.hide();
169+
$rows.hide();
163170

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

166173
this._trigger("filter", e, $.map($cache, function(v, i) {
167174
if(v.search(regex) !== -1) {
168-
rows.eq(i).show();
175+
$rows.eq(i).show();
169176
return $inputs.get(i);
170177
}
171178

@@ -176,9 +183,8 @@
176183
// show/hide optgroups
177184
$groups.each(function() {
178185
var $this = $(this);
179-
if(!$this.children("li:visible").length) {
186+
if (!$this.children("li:visible").length)
180187
$this.hide();
181-
}
182188
});
183189
this.instance._setMenuHeight();
184190
},
@@ -189,18 +195,18 @@
189195

190196
updateCache: function() {
191197
// each list item
192-
this.rows = this.instance.$labels.parent();
198+
this.$rows = this.instance.$labels.parent();
193199

194200
// cache
195201
this.$cache = this.element.children().map(function() {
196-
var $elem = $(this);
202+
var $element = $(this);
197203

198204
// account for optgroups
199205
if(this.tagName.toLowerCase() === "optgroup") {
200-
$elem = $elem.children();
206+
$element = $element.children();
201207
}
202208

203-
return $elem.map(function() {
209+
return $element.map(function() {
204210
return this.innerHTML.toLowerCase();
205211
}).get();
206212
}).get();

0 commit comments

Comments
 (0)