Skip to content
This repository was archived by the owner on Aug 14, 2021. It is now read-only.

Commit 0ece4f8

Browse files
committed
Update readme
1 parent 42a84ae commit 0ece4f8

File tree

1 file changed

+74
-87
lines changed

1 file changed

+74
-87
lines changed

README.md

Lines changed: 74 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,27 @@ See also the [Change Log](https://github.com/mar10/jquery-ui-contextmenu/blob/ma
2727
[ ![sample](demo/teaser.png?raw=true) ](http://wwwendt.de/tech/demo/jquery-contextmenu/demo/ "Live demo")
2828

2929

30-
## Example
30+
## Tutorial
3131

32-
First, include dependencies, i.e.
32+
First, include dependencies:
3333

3434
* 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
3636
* One of the ThemeRoller CSS themes or a custom one
3737
* jquery.ui-contextmenu.js (also available as CDN on
3838
[jsdelivr](http://www.jsdelivr.com/#!jquery.ui-contextmenu)
3939
or [cdnjs](https://cdnjs.com/libraries/jquery.ui-contextmenu))
4040

4141
```html
4242
<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" />
4445
<script src="//code.jquery.com/jquery-1.11.1.min.js" type="text/javascript"></script>
4546
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.min.js" type="text/javascript"></script>
4647
<script src="assets/jquery.ui-contextmenu.min.js"></script>
4748
```
4849

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:
5051

5152
```html
5253
<div id="container">
@@ -75,6 +76,67 @@ $("#container").contextmenu({
7576
});
7677
```
7778

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 &lt;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>&lt;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+
78140
Instead of handling all menu commands in the `select` event, it is also possible
79141
to attach callbacks directly to menu entries:
80142
```js
@@ -89,17 +151,6 @@ $(document).contextmenu({
89151
});
90152
```
91153

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-
103154
### Initialize menu from an existing `<ul>` element
104155

105156
In this case `menu` must point to the markup:
@@ -130,7 +181,8 @@ see [jQueryUI menu] for details:
130181
</ul>
131182
```
132183

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:
134186
```html
135187
<ul id="options" class="ui-helper-hidden">
136188
<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:
141193

142194
### Modify the menu depending on the context
143195

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.
145198
This can be done in the `beforeOpen` event:
146199

147200
```js
@@ -181,6 +234,7 @@ $(document).contextmenu({
181234

182235
## API documentation
183236
### Options
237+
184238
<dl>
185239
<dt>addClass</dt>
186240
<dd>
@@ -274,6 +328,7 @@ $(document).contextmenu({
274328

275329

276330
### Methods
331+
277332
<dl>
278333
<dt>close()</dt>
279334
<dd>
@@ -320,6 +375,7 @@ $(document).contextmenu({
320375

321376

322377
### Events
378+
323379
jquery-contextmenu exposes events from [jQueryUI menu]: `blur`, `create`, `focus`, `select`.
324380
However, since the `event.target` parameter contains the menu item, we additionally pass the element
325381
that was right-clicked in `ui.target`.
@@ -382,75 +438,6 @@ $("#container").bind("contextmenuselect", function(event, ui) {
382438
</dl>
383439
384440
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 &lt;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>&lt;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-
454441
# Tips and Tricks
455442
### [Howto] Add right-aligned shortcut hints
456443

0 commit comments

Comments
 (0)