Skip to content

Commit bcae2e9

Browse files
author
ehynds
committed
expose a position method so that the menu can be repositioned dynamically. fixes #319
1 parent e86a738 commit bcae2e9

File tree

2 files changed

+54
-21
lines changed

2 files changed

+54
-21
lines changed

src/jquery.multiselect.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,7 @@ $.widget("ech.multiselect", {
534534
}
535535

536536
var $container = menu.find('ul').last(),
537-
effect = o.show,
538-
pos = button.offset();
537+
effect = o.show;
539538

540539
// figure out opening effects/speeds
541540
if( $.isArray(o.show) ){
@@ -552,22 +551,8 @@ $.widget("ech.multiselect", {
552551
// set the scroll of the checkbox container
553552
$container.scrollTop(0).height(o.height);
554553

555-
// position and show menu
556-
if( $.ui.position && !$.isEmptyObject(o.position) ){
557-
o.position.of = o.position.of || button;
558-
559-
menu
560-
.show()
561-
.position( o.position )
562-
.hide();
563-
564-
// if position utility is not available...
565-
} else {
566-
menu.css({
567-
top: pos.top + button.outerHeight(),
568-
left: pos.left
569-
});
570-
}
554+
// positon
555+
this.position();
571556

572557
// show the menu, maybe with a speed/effect combo
573558
$.fn.show.apply(menu, args);
@@ -654,11 +639,34 @@ $.widget("ech.multiselect", {
654639
return this.button;
655640
},
656641

642+
position: function() {
643+
var o = this.options;
644+
645+
// use the position utility if it exists and options are specifified
646+
if( $.ui.position && !$.isEmptyObject(o.position) ){
647+
o.position.of = o.position.of || button;
648+
649+
this.menu
650+
.show()
651+
.position( o.position )
652+
.hide();
653+
654+
// otherwise fallback to custom positioning
655+
} else {
656+
var pos = this.button.offset();
657+
658+
this.menu.css({
659+
top: pos.top + this.button.outerHeight(),
660+
left: pos.left
661+
});
662+
}
663+
},
664+
657665
// react to option changes after initialization
658666
_setOption: function( key, value ){
659667
var menu = this.menu;
660668

661-
switch(key){
669+
switch(key) {
662670
case 'header':
663671
menu.find('div.ui-multiselect-header')[ value ? 'show' : 'hide' ]();
664672
break;
@@ -669,10 +677,10 @@ $.widget("ech.multiselect", {
669677
menu.find('a.ui-multiselect-none span').eq(-1).text(value);
670678
break;
671679
case 'height':
672-
menu.find('ul').last().height( parseInt(value,10) );
680+
menu.find('ul').last().height( parseInt(value, 10) );
673681
break;
674682
case 'minWidth':
675-
this.options[ key ] = parseInt(value,10);
683+
this.options[ key ] = parseInt(value, 10);
676684
this._setButtonWidth();
677685
this._setMenuWidth();
678686
break;
@@ -690,6 +698,8 @@ $.widget("ech.multiselect", {
690698
this.options.multiple = value;
691699
this.element[0].multiple = value;
692700
this.refresh();
701+
case 'position':
702+
this.position();
693703
}
694704

695705
$.Widget.prototype._setOption.apply( this, arguments );

tests/unit/methods.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,27 @@
147147

148148
el.multiselect("destroy").remove();
149149
});
150+
151+
test("position", function() {
152+
expect(2);
153+
154+
var left = "500px";
155+
156+
el = $("select").clone().appendTo(body).multiselect();
157+
158+
// move the button
159+
button().css({ position: "absolute", left: left });
160+
161+
// sanity check the fact that the menu and button are out of sync
162+
notEqual(menu().css("left"), left, "After moving the button, the menu remains in its old position");
163+
164+
// update the menu position
165+
el.multiselect("position");
166+
167+
// make sure the new position is accurate
168+
equals(menu().css("left"), left, "After calling position(), the menu has updated to the same left value as the button");
169+
170+
el.multiselect("destroy").remove();
171+
});
172+
150173
})(jQuery);

0 commit comments

Comments
 (0)