Skip to content

Commit 4ee469b

Browse files
committed
Disable shouldn't take so long
Going pure JavaScript with the _toggleDisabled method, yields a massive performance increase in exchange for being uglier. Added the disableInputsOnToggle option in case the consumer doesn't want to disable the inputs at all.
1 parent 806f887 commit 4ee469b

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

src/jquery.multiselect.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
position: {},
4747
appendTo: null,
4848
menuWidth:null,
49-
selectedListSeparator: ', '
49+
selectedListSeparator: ', ',
50+
disableInputsOnToggle: true
5051
},
5152

5253
_getAppendEl: function() {
@@ -637,23 +638,34 @@
637638
_toggleDisabled: function(flag) {
638639
this.button.prop({ 'disabled':flag, 'aria-disabled':flag })[ flag ? 'addClass' : 'removeClass' ]('ui-state-disabled');
639640

640-
var inputs = this.menu.find('input');
641-
var key = "ech-multiselect-disabled";
642-
643-
if(flag) {
644-
// remember which elements this widget disabled (not pre-disabled)
645-
// elements, so that they can be restored if the widget is re-enabled.
646-
inputs = inputs.filter(':enabled').data(key, true);
647-
} else {
648-
inputs = inputs.filter(function() {
649-
return $.data(this, key) === true;
650-
}).removeData(key);
641+
if(this.options.disableInputsOnToggle) {
642+
var checkboxes = this.menu.find(".ui-multiselect-checkboxes").get(0);
643+
var matchedInputs = [];
644+
var key = "ech-multiselect-disabled";
645+
var i = 0;
646+
if(flag) {
647+
// remember which elements this widget disabled (not pre-disabled)
648+
// elements, so that they can be restored if the widget is re-enabled.
649+
matchedInputs = checkboxes.querySelectorAll("input:enabled");
650+
for(i = 0; i < matchedInputs.length; i++) {
651+
matchedInputs[i].setAttribute(key, true);
652+
matchedInputs[i].setAttribute("disabled", "disabled");
653+
matchedInputs[i].setAttribute("aria-disabled", "disabled");
654+
matchedInputs[i].parentNode.className = matchedInputs[i].parentNode.className + " ui-state-disabled";
655+
}
656+
} else {
657+
matchedInputs = checkboxes.querySelectorAll("input:disabled");
658+
for(i = 0; i < matchedInputs.length; i++) {
659+
if(matchedInputs[i].hasAttribute(key)) {
660+
matchedInputs[i].removeAttribute(key);
661+
matchedInputs[i].removeAttribute("disabled");
662+
matchedInputs[i].removeAttribute("aria-disabled");
663+
matchedInputs[i].parentNode.className = matchedInputs[i].parentNode.className.replace(" ui-state-disabled", "");
664+
}
665+
}
666+
}
651667
}
652668

653-
inputs
654-
.prop({ 'disabled':flag, 'arial-disabled':flag })
655-
.parent()[ flag ? 'addClass' : 'removeClass' ]('ui-state-disabled');
656-
657669
this.element.prop({
658670
'disabled':flag,
659671
'aria-disabled':flag

tests/unit/methods.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@
4646
.appendTo(document.body)
4747
.multiselect();
4848

49-
var boxes = menu().find("input")
49+
var boxes = menu().find("input");
5050
var disabled = boxes.first();
5151
var enabled = boxes.last();
5252
var key = "ech-multiselect-disabled";
5353

5454
equals(disabled.is(":disabled"), true, "The first option is disabled");
5555
el.multiselect("disable");
56-
equals(disabled.data(key), undefined, "After disabling the widget, the pre-disabled option is not flagged to re-enable");
57-
equals(enabled.data(key), true, "and the enabled option is flagged to be re-enable");
56+
equals(disabled.attr(key), undefined, "After disabling the widget, the pre-disabled option is not flagged to re-enable");
57+
equals(enabled.attr(key), "true", "and the enabled option is flagged to be re-enable");
5858
el.multiselect("enable");
5959
equals(disabled.is(":disabled"), true, "After enabling, the first option is still disabled");
60-
equals(disabled.data(key), undefined, "and the option no longer has the stored data flag");
60+
equals(disabled.attr(key), undefined, "and the option no longer has the stored data flag");
6161
el.multiselect("destroy").remove();
6262
});
6363

0 commit comments

Comments
 (0)