@@ -9,37 +9,47 @@ A jQuery plugin that provides a context menu (based on the standard [jQueryUI me
9
9
10
10
11
11
## Status
12
- Pre-alpha * not* ready for production
12
+ Beta: * not* ready for production.
13
+
13
14
14
15
## Demo
15
16
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 )
17
18
18
19
19
20
## Example
20
21
21
22
Say we have some HTML elements that we want to attach a popup menu to:
22
23
23
24
``` 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 >
29
30
```
30
31
31
32
32
33
now we can enable a contextmenu like so:
33
34
34
35
``` js
35
36
$ (" #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
+ [... ]
43
53
});
44
54
```
45
55
@@ -61,4 +71,88 @@ structure (see [jQueryUI menu] for details):
61
71
```
62
72
63
73
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
+
64
158
[jQueryUI menu]: http://jqueryui.com/menu/
0 commit comments