Skip to content

Commit 32c949d

Browse files
committed
fixing menu and submenu width calculation - Issue #18
1 parent fa4e281 commit 32c949d

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ $.contextMenu is published under the [MIT license](http://www.opensource.org/lic
105105

106106
* adding [DOM Element bound context menus](http://medialize.github.com/jQuery-contextMenu/demo/on-dom-element.html) - ([Issue 88](https://github.com/medialize/jQuery-contextMenu/issues/88))
107107
* fixing key "watch" might translate to Object.prototype.watch in callbacks map - ([Issue 93](https://github.com/medialize/jQuery-contextMenu/issues/93))
108+
* fixing menu and submenu width calculation - ([Issue 18](https://github.com/medialize/jQuery-contextMenu/issues/18))
108109

109110
### 1.5.25 ###
110111

src/jquery.contextMenu.js

+33-8
Original file line numberDiff line numberDiff line change
@@ -1065,18 +1065,43 @@ var // currently active contextMenu trigger
10651065
}
10661066
opt.$menu.appendTo(opt.appendTo || document.body);
10671067
},
1068+
resize: function($menu, nested) {
1069+
// determine widths of submenus, as CSS won't grow them automatically
1070+
// position:absolute within position:absolute; min-width:100; max-width:200; results in width: 100;
1071+
// kinda sucks hard...
1072+
1073+
// determine width of absolutely positioned element
1074+
$menu.css({position: 'absolute', display: 'block'});
1075+
// don't apply yet, because that would break nested elements' widths
1076+
$menu.data('width', $menu.width());
1077+
// reset styles so they allow nested elements to grow/shrink naturally
1078+
$menu.css({
1079+
position: 'static',
1080+
minWidth: '0px',
1081+
maxWidth: '100000px'
1082+
});
1083+
// identify width of nested menus
1084+
$menu.find('> li > ul').each(function() {
1085+
op.resize($(this), true);
1086+
});
1087+
// reset and apply changes in the end because nested
1088+
// elements' widths wouldn't be calculatable otherwise
1089+
if (!nested) {
1090+
$menu.find('ul').andSelf().css({
1091+
position: '',
1092+
display: '',
1093+
minWidth: '',
1094+
maxWidth: ''
1095+
}).width(function() {
1096+
return $(this).data('width');
1097+
});
1098+
}
1099+
},
10681100
update: function(opt, root) {
10691101
var $this = this;
10701102
if (root === undefined) {
10711103
root = opt;
1072-
// determine widths of submenus, as CSS won't grow them automatically
1073-
// position:absolute > position:absolute; min-width:100; max-width:200; results in width: 100;
1074-
// kinda sucks hard...
1075-
opt.$menu.find('ul').andSelf().css({position: 'static', display: 'block'}).each(function(){
1076-
var $this = $(this);
1077-
$this.width($this.css('position', 'absolute').width())
1078-
.css('position', 'static');
1079-
}).css({position: '', display: ''});
1104+
op.resize(opt.$menu);
10801105
}
10811106
// re-check disabled for each item
10821107
opt.$menu.children().each(function(){

0 commit comments

Comments
 (0)