Skip to content

Commit 5853a76

Browse files
committed
New option autoTrigger: true can be set to false to prevent opening on browser's contextmenu event.
Closed mar10#49
1 parent c5923bb commit 5853a76

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.3.0 / unreleased
22

33
* [FEATURE] New optional parameter open(..., extraData).
4+
* [FEATURE] New option `autoTrigger: true` can be set to `false` to prevent opening on browser's `contextmenu` event.
45
* [CHANGE] `setEntry()` and `replaceMenu()` now allow to define titles with HTML markup.
56

67
# 1.2.4 / 2013-12-25

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,56 @@ structure (see [jQueryUI menu] for details):
114114
```
115115

116116

117+
### Modify the menu depending on the context
118+
119+
The menu can be modified before display in order to reflect the current context.
120+
121+
```js
122+
$(document).contextmenu({
123+
delegate: ".hasmenu",
124+
menu: [
125+
{title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"},
126+
{title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy"},
127+
{title: "Paste", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true },
128+
...
129+
],
130+
beforeOpen: function(event, ui) {
131+
var $menu = ui.menu,
132+
$target = ui.target,
133+
extraData = ui.extraData; // optionally passed when menu was opened by call to open()
134+
135+
// Optionally return false, to prevent opening the menu
136+
// return false;
137+
138+
// En/disable single entries
139+
$(document).contextmenu("enableEntry", "paste", false);
140+
// Show/hide single entries
141+
$(document).contextmenu("showEntry", "cut", false);
142+
// Redefine the title of single entries
143+
$(document).contextmenu("setEntry", "copy", "Copy '" + $target.text() + "'")
144+
// Redefine all attributes of single entries
145+
$(document).contextmenu("setEntry", "cut", {title: "Cuty", uiIcon: "ui-icon-heart", disabled: true});
146+
// Redefine the whole menu
147+
$(document).contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}, ...]);
148+
// Redefine the whole menu from another HTML definition
149+
$(document).contextmenu("replaceMenu", "#options2");
150+
},
151+
...
152+
});
153+
```
154+
155+
117156
## API documentation
118157
### Options
119158
<dl>
159+
<dt>autoTrigger</dt>
160+
<dd>
161+
Type: <code>Boolean</code>,
162+
default: <code>true</code><br>
163+
Set `false` to prevent opening on a browser's `contextmenu` event, which is
164+
normally triggered by a mouse rightclick.<br>
165+
The menu can still be opened by calling the `open()` method.
166+
</dd>
120167
<dt>delegate</dt>
121168
<dd>
122169
Type: <code>String</code><br>

demo/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
preventSelect: true,
8282
taphold: true,
8383
menu: [
84-
{title: "<strong>Cut</strong>", cmd: "cut", uiIcon: "ui-icon-scissors"},
84+
{title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"},
8585
{title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy"},
8686
{title: "Paste", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true },
8787
{title: "----"},
@@ -213,7 +213,7 @@ <h3>Sample 2</h3>
213213

214214
<ul id="options" style="display: none;">
215215
<li><a href="#action1"><span class="ui-icon custom-icon-firefox"></span>Action 1</a>
216-
<li><a href="#action2"><span class="ui-icon ui-icon-heart"></span><strong>Action</strong> 2</a>
216+
<li><a href="#action2"><span class="ui-icon ui-icon-heart"></span>Action 2</a>
217217
<li class="ui-state-disabled"><a href="#action3">Action 3</a>
218218
<li>----
219219
<li><a>Extra</a>

jquery.ui-contextmenu.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
$.widget("moogle.contextmenu", {
2121
version: "1.3.0-1",
2222
options: {
23+
autoTrigger: true, // open menu on browser's `contextmenu` event
2324
delegate: null, // selector
2425
hide: { effect: "fadeOut", duration: "fast"},
2526
ignoreParentSelect: true, // Don't trigger 'select' for sub-menu parents
@@ -160,8 +161,15 @@
160161
var opts = this.options,
161162
posOption = opts.position,
162163
self = this,
163-
ui = {menu: this.$menu, target: $(event.target), extraData: event.extraData};
164+
manualTrigger = !!event.isTrigger,
165+
ui = {menu: this.$menu, target: $(event.target), extraData: event.extraData, originalEvent: event};
166+
167+
if( !opts.autoTrigger && !manualTrigger ) {
168+
// ignore browser's `contextmenu` events
169+
return;
170+
}
164171
this.currentTarget = event.target;
172+
165173
// Prevent browser from opening the system context menu
166174
event.preventDefault();
167175

0 commit comments

Comments
 (0)