Skip to content

Commit 3f87728

Browse files
committed
fix highlighting bugs introduced by disabled option support. fixes select2#765 closes select2#775
1 parent 7a006a7 commit 3f87728

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

select2.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ the specific language governing permissions and limitations under the Apache Lic
622622
search.bind("blur", function () { search.removeClass("select2-focused");});
623623

624624
this.dropdown.delegate(resultsSelector, "mouseup", this.bind(function (e) {
625-
if ($(e.target).closest(".select2-result-selectable:visible").length > 0) {
625+
if ($(e.target).closest(".select2-result-selectable").length > 0) {
626626
this.highlightUnderEvent(e);
627627
this.selectHighlighted(e);
628628
} else {
@@ -1075,7 +1075,7 @@ the specific language governing permissions and limitations under the Apache Lic
10751075
return;
10761076
}
10771077

1078-
children = results.find(".select2-result:visible");
1078+
children = this.findHighlightableChoices();
10791079

10801080
child = $(children[index]);
10811081

@@ -1101,9 +1101,14 @@ the specific language governing permissions and limitations under the Apache Lic
11011101
}
11021102
},
11031103

1104+
// abstract
1105+
findHighlightableChoices: function() {
1106+
return this.results.find(".select2-result-selectable:not(.select2-selected):not(.select2-disabled)");
1107+
},
1108+
11041109
// abstract
11051110
moveHighlight: function (delta) {
1106-
var choices = this.results.find(".select2-result:visible"),
1111+
var choices = this.findHighlightableChoices(),
11071112
index = this.highlight();
11081113

11091114
while (index > -1 && index < choices.length) {
@@ -1118,7 +1123,7 @@ the specific language governing permissions and limitations under the Apache Lic
11181123

11191124
// abstract
11201125
highlight: function (index) {
1121-
var choices = this.results.find(".select2-result:visible");
1126+
var choices = this.findHighlightableChoices();
11221127

11231128
if (arguments.length === 0) {
11241129
return indexOf(choices.filter(".select2-highlighted")[0], choices.get());
@@ -1131,19 +1136,18 @@ the specific language governing permissions and limitations under the Apache Lic
11311136

11321137
$(choices[index]).addClass("select2-highlighted");
11331138
this.ensureHighlightVisible();
1134-
11351139
},
11361140

11371141
// abstract
11381142
countSelectableResults: function() {
1139-
return this.results.find(".select2-result-selectable").not(".select2-disabled").not(".select2-selected").length;
1143+
return this.findHighlightableChoices().length;
11401144
},
11411145

11421146
// abstract
11431147
highlightUnderEvent: function (event) {
11441148
var el = $(event.target).closest(".select2-result-selectable");
11451149
if (el.length > 0 && !el.is(".select2-highlighted")) {
1146-
var choices = this.results.find('.select2-result:visible');
1150+
var choices = this.findHighlightableChoices();
11471151
this.highlight(choices.index(el));
11481152
} else if (el.length == 0) {
11491153
// if we are over an unselectable item remove al highlights
@@ -1346,8 +1350,8 @@ the specific language governing permissions and limitations under the Apache Lic
13461350
// abstract
13471351
selectHighlighted: function (options) {
13481352
var index=this.highlight(),
1349-
highlighted=this.results.find(".select2-highlighted:visible"),
1350-
data = highlighted.closest('.select2-result:visible').data("select2-data");
1353+
highlighted=this.results.find(".select2-highlighted"),
1354+
data = highlighted.closest('.select2-result').data("select2-data");
13511355
if (data) {
13521356
highlighted.addClass("select2-selected");
13531357
this.highlight(index);
@@ -1694,7 +1698,7 @@ the specific language governing permissions and limitations under the Apache Lic
16941698

16951699
// find the selected element in the result list
16961700

1697-
this.results.find(".select2-result:visible").each2(function (i, elm) {
1701+
this.findHighlightableChoices().each2(function (i, elm) {
16981702
if (equal(self.id(elm.data("select2-data")), self.opts.element.val())) {
16991703
selected = i;
17001704
return false;
@@ -2211,34 +2215,31 @@ the specific language governing permissions and limitations under the Apache Lic
22112215
// multi
22122216
postprocessResults: function () {
22132217
var val = this.getVal(),
2214-
choices = this.results.find(".select2-result:visible"),
2218+
choices = this.results.find(".select2-result"),
22152219
compound = this.results.find(".select2-result-with-children"),
22162220
self = this;
22172221

22182222
choices.each2(function (i, choice) {
22192223
var id = self.id(choice.data("select2-data"));
22202224
if (indexOf(id, val) >= 0) {
2221-
choice.addClass("select2-selected").removeClass("select2-result-selectable");
2225+
choice.addClass("select2-selected");
22222226
} else {
2223-
choice.removeClass("select2-selected").addClass("select2-result-selectable");
2227+
choice.removeClass("select2-selected");
22242228
}
22252229
});
22262230

2227-
compound.each2(function(i, e) {
2228-
if (!e.is('.select2-result-selectable') && e.find(".select2-result-selectable").length==0) { // FIX FOR HIERARCHICAL DATA
2229-
e.addClass("select2-selected");
2231+
compound.each2(function(i, choice) {
2232+
// hide an optgroup if it doesnt have any selectable children
2233+
if (!choice.is('.select2-result-selectable')
2234+
&& choice.find(".select2-result-selectable:not(.select2-selected)").length === 0) {
2235+
choice.addClass("select2-selected");
22302236
} else {
2231-
e.removeClass("select2-selected");
2237+
choice.removeClass("select2-selected");
22322238
}
22332239
});
22342240

22352241
if (this.highlight() == -1){
2236-
choices.each2(function (i, choice) {
2237-
if (!choice.hasClass("select2-selected") &&!choice.hasClass("select2-disabled") && choice.hasClass("select2-result-selectable")) {
2238-
self.highlight(0);
2239-
return false;
2240-
}
2241-
});
2242+
self.highlight(0);
22422243
}
22432244

22442245
},

0 commit comments

Comments
 (0)