Skip to content

Commit 6b0e2bd

Browse files
committed
adding reposition config flag - Issue #104
1 parent c5ba852 commit 6b0e2bd

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

docs.html

+9-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,15 @@ <h3 id="options">options (at registration)</h3>
9595
<p>Specifies the event to show the contextMenu</p>
9696
<p>Possible values: "right", "left", "hover", "none"</p>
9797
</dd>
98-
98+
99+
<dt id="options-reposition"><em>(boolean)</em> reposition</dt>
100+
<dd>
101+
<p>
102+
Specifies if a menu should be repositioned (true) or rebuilt (false) if a second trigger event (like a right click) is performed
103+
on the same trigger element (or its children) while the menu is still visible.
104+
</p>
105+
</dd>
106+
99107
<dt id="options-delay"><em>(int)</em> delay</dt>
100108
<dd>
101109
<p>default: <em>200</em></p>

src/jquery.contextMenu.js

+14-9
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ var // currently active contextMenu trigger
8585
autoHide: false,
8686
// ms to wait before showing a hover-triggered context menu
8787
delay: 200,
88+
// flag denoting if a second trigger should simply move (true) or rebuild (false) an open menu
89+
// as long as the trigger happened on one of the trigger-element's child nodes
90+
reposition: true,
8891
// determine position to show menu at
8992
determinePosition: function($menu) {
9093
// position to the lower middle of the trigger element
@@ -352,13 +355,16 @@ var // currently active contextMenu trigger
352355

353356
setTimeout(function() {
354357
var $window, hideshow, possibleTarget;
355-
// test if we need to reposition the menu
356-
if ((root.trigger == 'left' && button == 0) || (root.trigger == 'right' && button == 2)) {
358+
359+
// find the element that would've been clicked, wasn't the layer in the way
360+
if (document.elementFromPoint) {
361+
root.$layer.hide();
362+
target = document.elementFromPoint(x - $win.scrollLeft(), y - $win.scrollTop());
363+
root.$layer.show();
364+
}
365+
366+
if (root.reposition && ((root.trigger == 'left' && button == 0) || (root.trigger == 'right' && button == 2))) {
357367
if (document.elementFromPoint) {
358-
root.$layer.hide();
359-
target = document.elementFromPoint(x - $win.scrollLeft(), y - $win.scrollTop());
360-
root.$layer.show();
361-
362368
if (root.$trigger.is(target) || root.$trigger.has(target).length) {
363369
root.position.call(root.$trigger, root, x, y);
364370
return;
@@ -385,9 +391,8 @@ var // currently active contextMenu trigger
385391
}
386392
}
387393
}
388-
389-
if (target && target.length) {
390-
target.contextMenu({x: x, y: y});
394+
if (target) {
395+
$(target).contextMenu({x: x, y: y});
391396
} else {
392397
// TODO: it would be nice if we could prevent animations here
393398
root.$menu.trigger('contextmenu:hide');

0 commit comments

Comments
 (0)