Skip to content

Commit 6f2ced1

Browse files
committed
Improved documention
1 parent 8e571de commit 6f2ced1

File tree

2 files changed

+110
-22
lines changed

2 files changed

+110
-22
lines changed

README.md

Lines changed: 108 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,47 @@ A jQuery plugin that provides a context menu (based on the standard [jQueryUI me
99

1010

1111
## Status
12-
Pre-alpha *not* ready for production
12+
Beta: *not* ready for production.
13+
1314

1415
## Demo
1516

16-
[Sample page](http://mar10.github.com/jquery-contextmenu/sample-widget.html)
17+
[Live demo page](http://mar10.github.com/jquery-contextmenu/sample-widget.html)
1718

1819

1920
## Example
2021

2122
Say we have some HTML elements that we want to attach a popup menu to:
2223

2324
```html
24-
<div id="container">
25-
<div class="hasmenu">AAA</div>
26-
<div class="hasmenu">BBB</div>
27-
<div class="hasmenu">CCC</div>
28-
</div>
25+
<div id="container">
26+
<div class="hasmenu">AAA</div>
27+
<div class="hasmenu">BBB</div>
28+
<div class="hasmenu">CCC</div>
29+
</div>
2930
```
3031

3132

3233
now we can enable a contextmenu like so:
3334

3435
```js
3536
$("#container").contextmenu({
36-
delegate : ".hasmenu",
37-
menu : "#options",
38-
select : function(event, ui) {
39-
var menuId = ui.item.find(">a").attr("href");
40-
alert("select " + menuId);
41-
}
42-
});
37+
delegate: ".hasmenu",
38+
menu: "#options",
39+
select: function(event, ui) {
40+
var menuId = ui.item.find(">a").attr("href"),
41+
target = event.relatedTarget;
42+
alert("select " + menuId + " on " + $(target).text());
43+
}
44+
});
45+
```
46+
47+
To apply the selector globally, pass `document` as context:
48+
49+
```js
50+
$(document).contextmenu({
51+
delegate: ".hasmenu",
52+
[...]
4353
});
4454
```
4555

@@ -61,4 +71,88 @@ structure (see [jQueryUI menu] for details):
6171
```
6272

6373

74+
## API documentation
75+
### Options
76+
<dl>
77+
<dt>delegate</dt>
78+
<dd>
79+
`{String}` jQuery selector describing the elements that trigger the context menu.
80+
</dd>
81+
<dt>menu</dt>
82+
<dd>
83+
`{String | jQuery | function}` jQuery object or selector (or function returning such) describing the menu definition.
84+
</dd>
85+
</dl>
86+
87+
88+
### Methods
89+
<dl>
90+
<dt>open(target)</dt>
91+
<dd>
92+
Open context menu on a specific target (must match options.delegate).
93+
</dd>
94+
</dl>
95+
96+
97+
### Events
98+
jquery-contextmenu exposes events from [jQueryUI menu]: `blur`, `create`, `focus`, `select`.
99+
However, since the `event.target` parameter contains the menu item, we additionally pass the element
100+
that was right-clicked in `event.relatedTarget`.
101+
102+
Events may be handled by defining a handler option:
103+
```js
104+
$("#container").contextmenu({
105+
[...]
106+
select: function(event, ui) {
107+
var menuId = ui.item.find(">a").attr("href"),
108+
target = event.relatedTarget;
109+
alert("select " + menuId + " on " + $(target).text());
110+
}
111+
});
112+
```
113+
114+
Alternatively a handler may be bound, so this is equivaent:
115+
```js
116+
$("#container").bind("contextmenuselect", function(event, ui) {
117+
var menuId = ui.item.find(">a").attr("href"),
118+
target = event.relatedTarget;
119+
alert("select " + menuId + " on " + $(target).text());
120+
}
121+
```
122+
123+
<dl>
124+
<dt>beforeopen(event)</dt>
125+
<dd>
126+
Menu about to open; return `false` to prevent opening.
127+
</dd>
128+
<dt>blur(event, ui)</dt>
129+
<dd>
130+
menu option lost focus
131+
</dd>
132+
<dt>close(event, ui)</dt>
133+
<dd>
134+
menu was closed
135+
</dd>
136+
<dt>create(event, ui)</dt>
137+
<dd>
138+
menu was initialized
139+
</dd>
140+
<dt>focus(event, ui)</dt>
141+
<dd>
142+
menu option got focus
143+
</dd>
144+
<dt>init(event, ui)</dt>
145+
<dd>
146+
ui-contextmenu was initialized
147+
</dd>
148+
<dt>open(event, ui)</dt>
149+
<dd>
150+
menu was opened
151+
</dd>
152+
<dt>select(event, ui)</dt>
153+
<dd>
154+
menu option was selected; return `false` to prevent closing.
155+
</dd>
156+
</dl>
157+
64158
[jQueryUI menu]: http://jqueryui.com/menu/

jquery.contextmenu.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
return $menu.data("ui-menu") || $menu.data("menu");
1616
}
1717
$.widget("ui.contextmenu", {
18-
version: "0.0.1pre",
18+
version: "0.1.0",
1919
options: {
2020
delegate: "[data-menu]", // selector
2121
menu: null, // selector or jQuery or a function returning such
22-
// preventBuiltinMenu: true,
2322
// Events:
2423
beforeopen: $.noop, // menu about to open; return `false` to prevent opening
2524
blur: $.noop, // menu option lost focus
@@ -32,11 +31,6 @@
3231
},
3332
_create: function () {
3433
this.element.delegate(this.options.delegate, "contextmenu.contextmenu", $.proxy(this._openMenu, this));
35-
// if(this.options.preventBuiltinMenu){
36-
// this.element.delegate(this.options.delegate, "contextmenu.contextmenu", function(event){
37-
// return false;
38-
// });
39-
// }
4034
this._trigger("init");
4135
},
4236
/** Return menu jQuery object. */
@@ -78,7 +72,7 @@
7872
create: $.proxy(this.options.create, this),
7973
focus: $.proxy(this.options.focus, this),
8074
select: function(event, ui){
81-
// // Also pass the target that the menu was triggered on:
75+
// Also pass the target that the menu was triggered on:
8276
event.relatedTarget = openEvent.target;
8377
if( self._trigger.call(self, "select", event, ui) !== false ){
8478
self._closeMenu.call(self);

0 commit comments

Comments
 (0)