@@ -27,26 +27,27 @@ See also the [Change Log](https://github.com/mar10/jquery-ui-contextmenu/blob/ma
27
27
[ ![ sample] ( demo/teaser.png?raw=true ) ] ( http://wwwendt.de/tech/demo/jquery-contextmenu/demo/ " Live demo ")
28
28
29
29
30
- ## Example
30
+ ## Tutorial
31
31
32
- First, include dependencies, i.e.
32
+ First, include dependencies:
33
33
34
34
* jQuery 1.7+ (1.10 or later recommended)
35
- * jQuery UI 1.9+ (at least core, widget, menu), 1.11 recommended
35
+ * jQuery UI 1.9+ (at least core, widget, menu), 1.11+ recommended
36
36
* One of the ThemeRoller CSS themes or a custom one
37
37
* jquery.ui-contextmenu.js (also available as CDN on
38
38
[ jsdelivr] ( http://www.jsdelivr.com/#!jquery.ui-contextmenu )
39
39
or [ cdnjs] ( https://cdnjs.com/libraries/jquery.ui-contextmenu ) )
40
40
41
41
``` html
42
42
<head >
43
- <link href =" //code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" type =" text/css" rel =" stylesheet" />
43
+ <link href =" //code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css"
44
+ type =" text/css" rel =" stylesheet" />
44
45
<script src =" //code.jquery.com/jquery-1.11.1.min.js" type =" text/javascript" ></script >
45
46
<script src =" //code.jquery.com/ui/1.11.2/jquery-ui.min.js" type =" text/javascript" ></script >
46
47
<script src =" assets/jquery.ui-contextmenu.min.js" ></script >
47
48
```
48
49
49
- We have some HTML elements that we want to attach a popup menu to:
50
+ Assume we have some HTML elements that we want to attach a popup menu to:
50
51
51
52
``` html
52
53
<div id =" container" >
@@ -75,6 +76,67 @@ $("#container").contextmenu({
75
76
});
76
77
```
77
78
79
+ The ` delegate ` option defines a CSS selector, which is evaluated for all
80
+ elements inside the context element (` #container ` in our example).<br >
81
+ In order to attach menus to * all* matching elements on the page that have
82
+ ` class="hasmenu" ` , we may use ` document ` as context:
83
+ ``` js
84
+ $ (document ).contextmenu ({
85
+ delegate: " .hasmenu" ,
86
+ ...
87
+ });
88
+ ```
89
+ ** Note:** only one contextmenu widget instance can be bound to one element.
90
+
91
+ The ` menu ` options may contain a (nested) array of entry defiitions.
92
+ Following a list of available properties:
93
+ <dl >
94
+ <dt >action</dt >
95
+ <dd >
96
+ Type: <code>Function</code>, default: n.a.<br>
97
+ Optional callback that will be executed when the entry is selected.
98
+ </dd >
99
+ <dt >addClass</dt >
100
+ <dd >
101
+ Type: <code>String</code>, default: <code>""</code><br>
102
+ Additional class name(s) to be added to the entries <li> element.
103
+ Separate multiple class names with a space.<br>
104
+ Custom CSS may be applied like <code>.ui-menu .my-class { color: red; }</code>.
105
+ </dd >
106
+ <dt >cmd</dt >
107
+ <dd >
108
+ Type: <code>String</code>, default: <code>""</code><br>
109
+ Optional identifier associated with the menu entry.
110
+ It can later be accessed in the <i>select</i> event as <code>ui.cmd</code>.
111
+ </dd >
112
+ <dt >data</dt >
113
+ <dd >
114
+ Type: <code>Object</code>, default: <code>{}</code><br>
115
+ Optional hash of additional properties that will be added to the entry's
116
+ <i>data</i> attribute.<br>
117
+ It can later be accessed in the <i>select</i> event as <code>ui.item.data()</code>.
118
+ </dd >
119
+ <dt >disabled</dt >
120
+ <dd >
121
+ Type: <code>Boolean</code>, default: <code>false</code><br>
122
+ Pass <i>true</i> to disable the entry.
123
+ </dd >
124
+ <dt >title</dt >
125
+ <dd >
126
+ Type: <code>String</code>, default: <code>""</code><br>
127
+ The displayed name of the menu entry. Use dashes (<code>"---"</code>) to
128
+ define a separator.
129
+ </dd >
130
+ <dt >uiIcon</dt >
131
+ <dd >
132
+ Type: <code>String</code>, default: ""<br>
133
+ If defined, an icon is added to the menu entry. For example passing
134
+ <code>"ui-icon-copy"</code> will generate this element:
135
+ <code><span class='ui-icon ui-icon-copy' /></code>.<br>
136
+ See also <<a href="http://api.jqueryui.com/theming/icons/">Icon Overview</a>.
137
+ </dd >
138
+ </dl >
139
+
78
140
Instead of handling all menu commands in the ` select ` event, it is also possible
79
141
to attach callbacks directly to menu entries:
80
142
``` js
@@ -89,17 +151,6 @@ $(document).contextmenu({
89
151
});
90
152
```
91
153
92
- To attach menus to * all* elements on the page that have ` class="hasmenu" ` ,
93
- we may use ` document ` as context:
94
- ``` js
95
- $ (document ).contextmenu ({
96
- delegate: " .hasmenu" ,
97
- ...
98
- });
99
- ```
100
- ** Note:** only one contextmenu widget instance can be bound to one element.
101
-
102
-
103
154
### Initialize menu from an existing ` <ul> ` element
104
155
105
156
In this case ` menu ` must point to the markup:
@@ -130,7 +181,8 @@ see [jQueryUI menu] for details:
130
181
</ul >
131
182
```
132
183
133
- ** Note:** until and including jQuery UI 1.10 the use of anchors (` <a> ` ) in menu items was required:
184
+ ** Note:** until and including jQuery UI 1.10 the use of anchors (` <a> ` ) in menu
185
+ items was required:
134
186
``` html
135
187
<ul id =" options" class =" ui-helper-hidden" >
136
188
<li data-command =" copy" ><a href =" #" ><span class =" ui-icon ui-icon-copy" ></span >Copy</a >
@@ -141,7 +193,8 @@ see [jQueryUI menu] for details:
141
193
142
194
### Modify the menu depending on the context
143
195
144
- Often we need to modify the menu before it is displayed, in order to reflect the current context.
196
+ Often we need to modify the menu before it is displayed, in order to reflect the
197
+ current context.
145
198
This can be done in the ` beforeOpen ` event:
146
199
147
200
``` js
@@ -181,6 +234,7 @@ $(document).contextmenu({
181
234
182
235
## API documentation
183
236
### Options
237
+
184
238
<dl >
185
239
<dt >addClass</dt >
186
240
<dd >
@@ -274,6 +328,7 @@ $(document).contextmenu({
274
328
275
329
276
330
### Methods
331
+
277
332
<dl >
278
333
<dt >close()</dt >
279
334
<dd >
@@ -320,6 +375,7 @@ $(document).contextmenu({
320
375
321
376
322
377
### Events
378
+
323
379
jquery-contextmenu exposes events from [ jQueryUI menu] : ` blur ` , ` create ` , ` focus ` , ` select ` .
324
380
However, since the ` event.target ` parameter contains the menu item, we additionally pass the element
325
381
that was right-clicked in ` ui.target ` .
@@ -382,75 +438,6 @@ $("#container").bind("contextmenuselect", function(event, ui) {
382
438
</dl>
383
439
384
440
385
- ### Menu Entry Definition
386
-
387
- The menu structure is defined as (nested) list of plain objects:
388
- ` ` ` js
389
- $ (... ).contextmenu ({
390
- ...
391
- menu: [
392
- {title: " Copy" , cmd: " copy" , uiIcon: " ui-icon-copy" },
393
- {title: " ----" },
394
- {title: " More" , children: [
395
- {title: " Sub 1" , cmd: " sub1" },
396
- {title: " Sub 2" , cmd: " sub1" }
397
- ]}
398
- ],
399
- ...
400
- });
401
- ` ` `
402
-
403
- Following a list of available menu definition properties:
404
-
405
- <dl>
406
- <dt>action</dt>
407
- <dd>
408
- Type: <code>Function</code>, default: n.a.<br>
409
- Optional callback that will be executed when the entry is selected.
410
- </dd>
411
- <dt>addClass</dt>
412
- <dd>
413
- Type: <code>String</code>, default: <code>""</code><br>
414
- Additional class name(s) to be added to the entries <li> element.
415
- Separate multiple class names with a space.<br>
416
- Custom CSS may be applied like <code>.ui-menu .my-class { color: red; }</code>.
417
- </dd>
418
- <dt>cmd</dt>
419
- <dd>
420
- Type: <code>String</code>, default: <code>""</code><br>
421
- Optional identifier associated with the menu entry.
422
- It can later be accessed in the <i>select</i> event by <code>ui.cmd</code>.
423
- </dd>
424
- <dt>data</dt>
425
- <dd>
426
- Type: <code>Object</code>, default: <code>{}</code><br>
427
- Optional hash of additional properties that will be added to the entry's
428
- <i>data</i> attribute.<br>
429
- It can later be accessed in the <i>select</i> event as <code>ui.item.data()</code>.
430
- </dd>
431
- <dt>disabled</dt>
432
- <dd>
433
- Type: <code>Boolean</code>, default: <code>false</code><br>
434
- Pass <i>true</i> to disable the entry.
435
- </dd>
436
- <dt>title</dt>
437
- <dd>
438
- Type: <code>String</code>, default: <code>""</code><br>
439
- The displayed name of the menu entry. Use <code>"---"</code> to define a
440
- separator.
441
- </dd>
442
- <dt>uiIcon</dt>
443
- <dd>
444
- Type: <code>String</code>, default: <code>""</code><br>
445
- If defined, an icon is added to the menu entry. For example passing
446
- <code>"ui-icon-copy"</code> will generate this element:
447
- <code><span class='ui-icon ui-icon-copy' /></code>.<br>
448
- See also the <a href="http://api.jqueryui.com/theming/icons/">Icon Overview</a>.
449
- </dd>
450
-
451
- </dl>
452
-
453
-
454
441
# Tips and Tricks
455
442
### [Howto] Add right-aligned shortcut hints
456
443
0 commit comments