Skip to content

Commit f14d61f

Browse files
committed
A little cleanup
Breaking _bindEvents down into smaller functions. A little code linting. Fixing a unit test that failed intermittently due to rounding.
1 parent 31c87b2 commit f14d61f

File tree

2 files changed

+49
-36
lines changed

2 files changed

+49
-36
lines changed

src/jquery.multiselect.js

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,22 @@
6666
.addClass(o.classes)
6767
.attr({ 'title':el.attr('title'), 'tabIndex':el.attr('tabIndex'), 'id': el.attr('id') ? el.attr('id') + '_ms' : null })
6868
.prop('aria-haspopup', true)
69-
.insertAfter(el),
69+
.insertAfter(el);
7070

71-
buttonlabel = (this.buttonlabel = $('<span />'))
71+
this.buttonlabel = $('<span />')
7272
.html(o.noneSelectedText)
73-
.appendTo(button),
73+
.appendTo(button);
7474

75-
menu = (this.menu = $('<div />'))
75+
this.menu = $('<div />')
7676
.addClass('ui-multiselect-menu ui-widget ui-widget-content ui-corner-all')
7777
.addClass(o.classes)
78-
.appendTo($(o.appendTo)),
78+
.appendTo($(o.appendTo));
7979

80-
header = (this.header = $('<div />'))
80+
this.header = $('<div />')
8181
.addClass('ui-widget-header ui-corner-all ui-multiselect-header ui-helper-clearfix')
82-
.appendTo(menu),
82+
.appendTo(this.menu);
8383

84-
headerLinkContainer = (this.headerLinkContainer = $('<ul />'))
84+
this.headerLinkContainer = $('<ul />')
8585
.addClass('ui-helper-reset')
8686
.html(function() {
8787
if(o.header === true) {
@@ -100,11 +100,11 @@
100100
}
101101
})
102102
.append('<li class="ui-multiselect-close"><a href="#" class="ui-multiselect-close"><span class="ui-icon '+o.closeIcon+'"></span></a></li>')
103-
.appendTo(header),
103+
.appendTo(this.header);
104104

105-
checkboxContainer = (this.checkboxContainer = $('<ul />'))
105+
var checkboxContainer = (this.checkboxContainer = $('<ul />'))
106106
.addClass('ui-multiselect-checkboxes ui-helper-reset')
107-
.appendTo(menu);
107+
.appendTo(this.menu);
108108

109109
// perform event bindings
110110
this._bindEvents();
@@ -114,7 +114,7 @@
114114

115115
// some addl. logic for single selects
116116
if(!o.multiple) {
117-
menu.addClass('ui-multiselect-single');
117+
this.menu.addClass('ui-multiselect-single');
118118
}
119119

120120
// bump unique ID
@@ -269,11 +269,9 @@
269269
this.buttonlabel.text(value);
270270
},
271271

272-
// binds events
273-
_bindEvents: function() {
272+
_bindButtonEvents: function() {
274273
var self = this;
275274
var button = this.button;
276-
277275
function clickHandler() {
278276
self[ self._isOpen ? 'close' : 'open' ]();
279277
return false;
@@ -317,21 +315,10 @@
317315
$(this).removeClass('ui-state-focus');
318316
}
319317
});
318+
},
320319

321-
// header links
322-
this.header.delegate('a', 'click.multiselect', function(e) {
323-
// close link
324-
if($(this).hasClass('ui-multiselect-close')) {
325-
self.close();
326-
327-
// check all / uncheck all
328-
} else {
329-
self[$(this).hasClass('ui-multiselect-all') ? 'checkAll' : 'uncheckAll']();
330-
}
331-
332-
e.preventDefault();
333-
});
334-
320+
_bindMenuEvents: function() {
321+
var self = this;
335322
// optgroup label toggle support
336323
this.menu.delegate('li.ui-multiselect-optgroup-label a', 'click.multiselect', function(e) {
337324
e.preventDefault();
@@ -443,16 +430,42 @@
443430
// http://bugs.jquery.com/ticket/3827
444431
setTimeout($.proxy(self.update, self), 10);
445432
});
433+
},
434+
435+
_bindHeaderEvents: function() {
436+
var self = this;
437+
// header links
438+
this.header.delegate('a', 'click.multiselect', function(e) {
439+
// close link
440+
if($(this).hasClass('ui-multiselect-close')) {
441+
self.close();
442+
443+
// check all / uncheck all
444+
} else {
445+
self[$(this).hasClass('ui-multiselect-all') ? 'checkAll' : 'uncheckAll']();
446+
}
447+
448+
e.preventDefault();
449+
});
450+
},
451+
452+
// binds events
453+
_bindEvents: function() {
454+
var self = this;
455+
456+
this._bindButtonEvents();
457+
this._bindMenuEvents();
458+
this._bindHeaderEvents();
446459

447460
// close each widget when clicking on any other element/anywhere else on the page
448461
$doc.bind('mousedown.' + self._namespaceID, function(event) {
449462
var target = event.target;
450463

451-
if(self._isOpen
452-
&& target !== self.button[0]
453-
&& target !== self.menu[0]
454-
&& !$.contains(self.menu[0], target)
455-
&& !$.contains(self.button[0], target)
464+
if(self._isOpen &&
465+
target !== self.button[0] &&
466+
target !== self.menu[0] &&
467+
!$.contains(self.menu[0], target) &&
468+
!$.contains(self.button[0], target)
456469
) {
457470
self.close();
458471
}

tests/unit/options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@
167167

168168
minWidth = "50%";
169169
el.multiselect("option", "minWidth", minWidth);
170-
outerWidth = Math.round(button().outerWidth());
171-
var halfParent = Math.round(el.parent().outerWidth()/2);
170+
outerWidth = Math.floor(button().outerWidth());
171+
var halfParent = Math.floor(el.parent().outerWidth()/2);
172172
ok(outerWidth === halfParent, 'changing value to 50%');
173173

174174
minWidth = "351px";

0 commit comments

Comments
 (0)