Skip to content

Commit ecb9a54

Browse files
SteveTheTechiemlh758
authored andcommitted
Eliminate replacement _toggleChecked code in multiselect.filter.js (#745)
Replaces the separate _toggleChecked function code in multiselect.filter.js with instead using a parameter that changes the context for _toggleChecked in multiselect.js . Also, a few housekeeping updates.
1 parent 7002ad8 commit ecb9a54

File tree

4 files changed

+348
-369
lines changed

4 files changed

+348
-369
lines changed

src/jquery.multiselect.filter.js

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@
7171
else if(e.which === 27) {
7272
$element.multiselect('close');
7373
e.preventDefault();
74-
}
74+
}
7575
else if(e.which === 9 && e.shiftKey) {
7676
$element.multiselect('close');
7777
e.preventDefault();
78-
}
78+
}
7979
else if(e.altKey) {
8080
switch(e.which) {
8181
case 82:
@@ -100,7 +100,7 @@
100100
// automatically reset the widget on close?
101101
if (this.options.autoReset)
102102
$element.on('multiselectclose', $.proxy(this._reset, this));
103-
103+
104104
// rebuild cache when multiselect is updated
105105
$element.on('multiselectrefresh', $.proxy(function() {
106106
this.updateCache();
@@ -113,47 +113,19 @@
113113
.prependTo(this.$header);
114114

115115
// reference to the actual inputs
116-
this.$inputs = this.instance.$menu.find('input[type="checkbox"], input[type="radio"]');
116+
this.$inputs = this.instance.$inputs;
117117

118118
// cache input values for searching
119119
this.updateCache();
120120

121-
// rewrite internal _toggleChecked fn so that when checkAll/uncheckAll is fired,
122-
// only the currently filtered $elements are checked
123-
this.instance._toggleChecked = function(flag, group) {
124-
var self = this;
125-
var $element = this.element;
126-
var $inputs = (group && group.length) ? group : this.$inputs;
127-
128-
// do not include hidden elems if the menu isn't open.
129-
var selector = self._isOpen ? ':disabled, :hidden' : ':disabled';
130-
131-
$inputs = $inputs
132-
.not(selector)
133-
.each(this._toggleState('checked', flag));
134-
135-
// update text
136-
this.update();
137-
138-
// gather an array of the values that actually changed
139-
var values = {};
140-
var inputCount = $inputs.length;
141-
for (var x = 0; x < inputCount; x++) {
142-
values[ $inputs.get(x).value ] = true;
143-
}
144-
145-
// select option tags
146-
$element.find('option').filter(function() {
147-
if(!this.disabled && values[this.value]) {
148-
self._toggleState('selected', flag).call(this);
149-
}
150-
});
151-
152-
// trigger the change event on the select
153-
if(inputCount)
154-
$element.trigger('change');
155-
121+
// Change the normal _toggleChecked fxn behavior so that when checkAll/uncheckAll
122+
// is fired, only the currently displayed filtered inputs are checked
123+
var $instance = this.instance;
124+
$instance._oldToggleChecked = $instance._toggleChecked;
125+
$instance._toggleChecked = function(flag, group) {
126+
$instance._oldToggleChecked(flag, group, true);
156127
};
128+
157129
},
158130

159131
// thx for the logic here ben alman

src/jquery.multiselect.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
* 3. If still do not have a valid DOM element to append to, then append to the document body.
6969
*
7070
* NOTE: this.element and this.document are jQuery objects per the jQuery UI widget API.
71-
* @returns {object} jQuery object to append to or document body.
71+
* @returns {object} jQuery object for the DOM element to append to.
7272
*/
7373
_getAppendEl: function() {
7474
var elem = this.options.appendTo; // jQuery object or selector, DOM element or null.
@@ -80,7 +80,7 @@
8080
elem = this.element.closest(".ui-front, dialog");
8181
}
8282
if (!elem.length) {
83-
elem = document.body; // Position at end of body. Note that this returns a DOM element.
83+
elem = $(document.body); // Position at end of body. Note that this returns a DOM element.
8484
}
8585
return elem;
8686
},
@@ -169,16 +169,15 @@
169169
var $checkboxes = (this.$checkboxes = $( document.createElement('ul') ) )
170170
.addClass('ui-multiselect-checkboxes ui-helper-reset' + (/\bmenu\b/i.test(wrapText) ? '' : ' ui-multiselect-nowrap'));
171171

172-
// This is the menu that will hold all the options.
172+
// This is the menu container that will hold all the options added via refresh().
173173
var $menu = (this.$menu = $( document.createElement('div') ) )
174174
.addClass('ui-multiselect-menu ui-widget ui-widget-content ui-corner-all'
175175
+ (elSelect.multiple ? '' : ' ui-multiselect-single ')
176176
+ (classes ? ' ' + classes : ''))
177177
.append($header, $checkboxes);
178178

179179
$button.insertAfter($element);
180-
// This is an empty menu at this point.
181-
$menu.appendTo( this._getAppendEl() );
180+
this._getAppendEl().append($menu);
182181

183182
this._bindEvents();
184183

@@ -731,7 +730,7 @@
731730
/**
732731
* Converts dimensions specified in options to pixel values.
733732
* Determines if specified value is a minimum, maximum or exact value.
734-
* The value can be a number or a string with px, pts, ems, or % units.
733+
* The value can be a number or a string with px, pts, ems, in, cm, mm, or % units.
735734
* Number/Numeric string treated as pixel measurements
736735
* - 30
737736
* - '30'
@@ -936,7 +935,7 @@
936935
/**
937936
* Calculate accurate outerWidth(false) using getBoundingClientRect()
938937
* Note that this presumes that the element is visible in the layout.
939-
* @param {node} DOM node or jQuery equivalent get width for.
938+
* @param {node} DOM node or jQuery equivalent to get width for.
940939
* @returns {float} Decimal floating point value for the width.
941940
*/
942941
_getBCRWidth: function(elem) {
@@ -950,7 +949,7 @@
950949
/**
951950
* Calculate accurate outerHeight(false) using getBoundingClientRect()
952951
* Note that this presumes that the element is visible in the layout.
953-
* @param {node} DOM node or jQuery equivalent get height for.
952+
* @param {node} DOM node or jQuery equivalent to get height for.
954953
* @returns {float} Decimal floating point value for the height.
955954
*/
956955
_getBCRHeight: function(elem) {
@@ -964,7 +963,7 @@
964963
/**
965964
* Calculate jQuery width correction factor to fix floating point round-off errors.
966965
* Note that this presumes that the element is visible in the layout.
967-
* @param {node} DOM node or jQuery equivalent get width for.
966+
* @param {node} DOM node or jQuery equivalent to get width for.
968967
* @returns {float} Correction value for the width--typically a decimal < 1.0
969968
*/
970969
_jqWidthFix: function(elem) {
@@ -979,7 +978,7 @@
979978
/**
980979
* Calculate jQuery height correction factor to fix floating point round-off errors.
981980
* Note that this presumes that the element is visible in the layout.
982-
* @param {node} DOM node or jQuery equivalent get height for.
981+
* @param {node} DOM node or jQuery equivalent to get height for.
983982
* @returns {float} Correction value for the height--typically a decimal < 1.0
984983
*/
985984
_jqHeightFix: function(elem) {
@@ -1090,12 +1089,21 @@
10901089
* Potentially scoped down to visible elements from filteredInputs
10911090
* @param {string} flag checked property to set
10921091
* @param {object} group option group that was clicked, if any
1093-
* @param {array} filteredInputs visible elements with the filter applied
1092+
* @param {boolean} filteredInputs does not toggle hidden inputs if filtering.
10941093
*/
10951094
_toggleChecked: function(flag, group, filteredInputs) {
10961095
var self = this;
10971096
var $element = self.element;
1098-
var $inputs = (group && group.length) ? group : (filteredInputs || self.$inputs);
1097+
var $inputs = (group && group.length) ? group : self.$inputs;
1098+
1099+
if (filteredInputs) {
1100+
// Do not include hidden inputs if the menu isn't open.
1101+
$inputs = $inputs.not( self._isOpen ? ':disabled, :hidden' : ':disabled' );
1102+
}
1103+
else {
1104+
// If not filtering, then the underlying select is cleared out each time.
1105+
$element[0].selectedIndex = -1;
1106+
}
10991107

11001108
// toggle state on inputs
11011109
$inputs.each(self._toggleState('checked', flag));
@@ -1113,7 +1121,6 @@
11131121
});
11141122

11151123
// toggle state on original option tags
1116-
$element[0].selectedIndex = -1;
11171124
$element.find('option')
11181125
.each( function() {
11191126
if (!this.disabled && values[this.value]) {

0 commit comments

Comments
 (0)