Skip to content

Commit 503e915

Browse files
committed
Allow custom classes per entry
Close mar10#85
1 parent 18f39cb commit 503e915

File tree

3 files changed

+80
-6
lines changed

3 files changed

+80
-6
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.8.2-0 / Unreleased
22

3-
* [CHANGE ] #82 Add "jquery-ui/menu" as AMD dependency
3+
* [CHANGE] #82 Add "jquery-ui/menu" as AMD dependency
4+
* [FEATURE] #85 Allow custom classes per entry
45

56
# 1.8.1 / 2014-12-21
67

README.md

+69-2
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,80 @@ $("#container").bind("contextmenuselect", function(event, ui) {
373373
<dt>select(event, ui)</dt>
374374
<dd>
375375
Triggered when a menu item is selected.<br>
376-
<code>ui.cmd</code> contains thecommand id.
376+
<code>ui.cmd</code> contains the command id.
377377
Return <code>false</code> to prevent closing the menu.
378378
</dd>
379379
</dl>
380380
381381
382+
### Menu Entry Definition
383+
384+
The menu structure is defined as (nested) list of plain objects:
385+
```js
386+
$(...).contextmenu({
387+
...
388+
menu: [
389+
{title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy"},
390+
{title: "----"},
391+
{title: "More", children: [
392+
{title: "Sub 1", cmd: "sub1"},
393+
{title: "Sub 2", cmd: "sub1"}
394+
]}
395+
],
396+
...
397+
});
398+
```
399+
400+
Follwing a list of available menu definition properties:
401+
<dl>
402+
<dt>action</dt>
403+
<dd>
404+
Type: <code>Function</code>, default: n.a.<br>
405+
Optional callback that will be executed when the entry is selected.
406+
</dd>
407+
<dt>addClass</dt>
408+
<dd>
409+
Type: <code>String</code>, default: ""<br>
410+
Additional class to be added to the entries html element. Separate multiple
411+
classes with space.<br>
412+
Custom CSS may be applied like <code>.ui-menu .my-class { color: red; }</code>.
413+
</dd>
414+
<dt>cmd</dt>
415+
<dd>
416+
Type: <code>String</code>, default: ""<br>
417+
Optional identifier associated with the menu entry.
418+
It can later be accessed in the *select* event by <code>ui.cmd</code>.
419+
</dd>
420+
<dt>data</dt>
421+
<dd>
422+
Type: <code>Object</code>, default: {}<br>
423+
Optional hash of additional properties that will be added to the entries *data*
424+
attribute.<br>
425+
It can later be accessed in the *select* event by <code>ui.item.data()</code>.
426+
</dd>
427+
<dt>disabled</dt>
428+
<dd>
429+
Type: <code>Boolean</code>, default: false<br>
430+
Pass <i>true</i> to disable the entry.
431+
</dd>
432+
<dt>title</dt>
433+
<dd>
434+
Type: <code>String</code>, default: <code>""</code><br>
435+
The displayed name of the menu entry. Use <code>"---"</code> to define a
436+
separator.
437+
</dd>
438+
<dt>uiIcon</dt>
439+
<dd>
440+
Type: <code>String</code>, default: ""<br>
441+
If defined, an icon is added to the menu entry. For example passing
442+
<code>"ui-icon-copy"</code> will generate this element:
443+
<code>&lt;span class='ui-icon ui-icon-copy' /></code>.<br>
444+
See also [Icon Overview](http://api.jqueryui.com/theming/icons/).
445+
</dd>
446+
447+
</dl>
448+
449+
382450
# Tips and Tricks
383451
### [Howto] Add right-aligned shortcut hints
384452
@@ -401,7 +469,6 @@ and make it right aligned via CSS:
401469
402470
### [Howto] Modify the menu using an asynchronous request
403471
404-
405472
```js
406473
$(document).contextmenu({
407474
...

jquery.ui-contextmenu.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,16 @@ if ( uiVersion.major < 2 && uiVersion.minor < 11 ) {
406406
if ( $.isFunction(entry.action) ) {
407407
$parentLi.data("actionHandler", entry.action);
408408
}
409-
if (entry.uiIcon) {
409+
if ( entry.uiIcon ) {
410410
$a.append($("<span class='ui-icon' />").addClass(entry.uiIcon));
411411
}
412-
if (entry.disabled) {
412+
if ( entry.disabled ) {
413413
$parentLi.addClass("ui-state-disabled");
414414
}
415-
if ($.isPlainObject(entry.data)) {
415+
if ( entry.addClass ) {
416+
$parentLi.addClass(entry.addClass);
417+
}
418+
if ( $.isPlainObject(entry.data) ) {
416419
$parentLi.data(entry.data);
417420
}
418421
}
@@ -447,6 +450,9 @@ if ( uiVersion.major < 2 && uiVersion.minor < 11 ) {
447450
if ( entry.disabled ) {
448451
$parentLi.addClass("ui-state-disabled");
449452
}
453+
if ( entry.addClass ) {
454+
$parentLi.addClass(entry.addClass);
455+
}
450456
if ( $.isPlainObject(entry.data) ) {
451457
$parentLi.data(entry.data);
452458
}

0 commit comments

Comments
 (0)