Skip to content

Commit a5d4b50

Browse files
committed
More Reliable Position
Position after opening, not before. Actually make use of the jQuery ui position utility if available, otherwise use offsets. Fix #675 Fix #660 Fix #647 Fix #562 Fix #552 Fix #527 Fix #505 Fix #484
1 parent 2477039 commit a5d4b50

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

src/jquery.multiselect.filter.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,18 @@
5151
var elem = $(this.element);
5252

5353
// get the multiselect instance
54-
var instance = (this.instance = (elem.data('echMultiselect') || elem.data("multiselect") || elem.data("ech-multiselect")));
54+
this.instance = elem.multiselect('instance');
5555

5656
// store header; add filter class so the close/check all/uncheck all links can be positioned correctly
57-
var header = (this.header = instance.menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter'));
57+
this.header = this.instance.menu.find('.ui-multiselect-header').addClass('ui-multiselect-hasfilter');
5858

5959
// wrapper elem
60-
var wrapper = (this.wrapper = $('<div class="ui-multiselect-filter">' + (opts.label.length ? opts.label : '') + '<input placeholder="'+opts.placeholder+'" type="search"' + (/\d/.test(opts.width) ? 'style="width:'+opts.width+'px"' : '') + ' /></div>').prependTo(this.header));
61-
62-
// reference to the actual inputs
63-
this.inputs = instance.menu.find('input[type="checkbox"], input[type="radio"]');
64-
65-
// build the input box
66-
this.input = wrapper.find('input').bind({
60+
this.input = $("<input/>").attr({
61+
placeholder: opts.placeholder,
62+
type: "search"
63+
}).css({
64+
width: (/\d/.test(opts.width) ? opts.width + 'px' : null)
65+
}).addClass("ui-front").bind({
6766
keydown: function(e) {
6867
// prevent the enter key from submitting the form / closing the widget
6968
if(e.which === 13) {
@@ -73,13 +72,17 @@
7372
input: $.proxy(debounce(this._handler, opts.debounceMS), this),
7473
search: $.proxy(this._handler, this)
7574
});
75+
this.wrapper = $("<div/>").addClass("ui-multiselect-filter").text(opts.label).append(this.input).prependTo(this.header);
76+
77+
// reference to the actual inputs
78+
this.inputs = this.instance.menu.find('input[type="checkbox"], input[type="radio"]');
7679

7780
// cache input values for searching
7881
this.updateCache();
7982

8083
// rewrite internal _toggleChecked fn so that when checkAll/uncheckAll is fired,
8184
// only the currently filtered elements are checked
82-
instance._toggleChecked = function(flag, group) {
85+
this.instance._toggleChecked = function(flag, group) {
8386
var $inputs = (group && group.length) ? group : this.labels.find('input');
8487
var _self = this;
8588

@@ -113,14 +116,14 @@
113116
};
114117

115118
// rebuild cache when multiselect is updated
116-
var doc = $(document).bind('multiselectrefresh.'+ instance._namespaceID, $.proxy(function() {
119+
var doc = $(document).bind('multiselectrefresh.'+ this.instance._namespaceID, $.proxy(function() {
117120
this.updateCache();
118121
this._handler();
119122
}, this));
120123

121124
// automatically reset the widget on close?
122125
if(this.options.autoReset) {
123-
doc.bind('multiselectclose.'+ instance._namespaceID, $.proxy(this._reset, this));
126+
doc.bind('multiselectclose.'+ this.instance._namespaceID, $.proxy(this._reset, this));
124127
}
125128
},
126129

src/jquery.multiselect.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -623,12 +623,12 @@
623623
// set the scroll of the checkbox container
624624
$container.scrollTop(0).height(o.height);
625625

626-
// positon
627-
this.position();
628-
629626
// show the menu, maybe with a speed/effect combo
630627
$.fn.show.apply(menu, args);
631628

629+
// positon
630+
this.position();
631+
632632
// select the first not disabled option
633633
// triggering both mouseover and mouseover because 1.4.2+ has a bug where triggering mouseover
634634
// will actually trigger mouseenter. the mouseenter trigger is there for when it's eventually fixed
@@ -720,25 +720,22 @@
720720
},
721721

722722
position: function() {
723-
var o = this.options;
724-
725-
// use the position utility if it exists and options are specifified
726-
if($.ui.position && !$.isEmptyObject(o.position)) {
727-
o.position.of = o.position.of || this.button;
728-
729-
this.menu
730-
.show()
731-
.position(o.position)
732-
.hide();
733-
734-
// otherwise fallback to custom positioning
723+
var pos = {
724+
my: "top",
725+
at: "bottom",
726+
of: this.button
727+
};
728+
if(!$.isEmptyObject(this.options.position)) {
729+
pos.my = this.options.position.my || pos.my;
730+
pos.at = this.options.position.at || pos.at;
731+
pos.of = this.options.position.of || pos.of;
732+
}
733+
if($.ui && $.ui.position) {
734+
this.menu.position(pos);
735735
} else {
736-
var pos = this.button.offset();
737-
738-
this.menu.css({
739-
top: pos.top + this.button.outerHeight(),
740-
left: pos.left
741-
});
736+
pos = this.button.position();
737+
pos.top += this.button.outerHeight(false);
738+
this.menu.offset(pos);
742739
}
743740
},
744741

0 commit comments

Comments
 (0)