To register a new contextMenu:
$.contextMenu( options );
No Default Value - specification is Mandatory
The selector matching the trigger objects.
Example: "span.with-context"
.
No Default Value - specification is Mandatory
Items to be listed in contextMenu. See Items.
Example: { one: {name: "one", callback: function(key, opt){ alert("Clicked on " + key); } }}
default: document.body
Specifies the DOMElement the generated menu is to be appended to
Example: "#context-menus-container"
.
default: "right"
Specifies the event to show the contextMenu
Possible values: "right", "left", "hover"
default: false
Specifies if the regular right click context menu trigger should be ignored. Only applies to "left" and "hover" triggers
default: 200
Specifies the time in milliseconds to wait before showing the menu. Only applies to trigger: "hover"
default: false
Specifies if the menu must be hidden when the mouse pointer is moved out of the trigger and menu elements
default: 1
Specifies the offset to add to the calculated zIndex of the trigger element. Set to 0 to prevent zIndex manipulation
Specifies additional classNames to add to the menu element
default: {duration: 500, show: "slideDown", hide: "slideUp"}
animation properties take effect on showing and hiding the menu. duration specifies the duration of the animation in milliseconds. show and hide specify jQuery methods to show and hide elements.
Possible show and hide animations: {show: "show", hide: "hide"}
, {show: "fadeIn", hide: "fadeOut"}, ???
The show and hide events are triggered before the menu is shown or hidden.
The event handlers are executed in the context of the triggering object. this will thus reference the jQuery handle of the trigger object.
A reference to the current options object is passed
The event handlers may return false to prevent the show or hide process.
Example: {show: function(opt){ this.addClass('currently-showing-menu'); alert("Selector: " + opt.selector); }}
Position the context menu.
The function is executed in the context of the trigger object.
The first argument ist the $menu jQuery object
The second and third arguments are x and y coordinates provided by the showing event
x and y may either be integers denoting the offset from the top left corner, undefined, or the string "maintain". If the string "maintain" is provided, the current position of the $menu must be used. If the coordinates are undefined, appropriate coordinates must be determined. An example of how this can be achieved is provided with determinePosition.
Example: {position: function($menu, x, y){$menu.css({top: 123, left: 123});}}
Determine the position of the menu in respect to the given trigger object.
Hint: jQuery UI's position() may ease things up.
Example: $menu.css('display', 'block').position({ my: "center top", at: "center bottom", of: this, offset: "0 5"}).css('display', 'none');
Specifies the default callback to be used in case an item does not expose its own callback.
The default callback behaves just like item.callback.
Example: {callback: callback: function(key, opt){ alert("Clicked on " + key + " on element " + opt.$trigger.id); }}
The items map contains the commands to list in the menu. Each command has a unique key identifying an item object. The value may either be an item (properties explained below), or a string (which will insert a separator, disregarding the string's content).
items: { command1: commandOptions, separator1: "-----" command2: commandOptions }
Specified the human readable name of the command in the menu
This is a mandatory property
Specifies the callback to execute if clicked on
The Callback is executed in the context of the triggering object. The first argument is the key of the command. The second argument is the options object. The Callback may return false to prevent the menu from being hidden.
Example: { command1: {name: "Foobar", callback: function(key, opt){ alert("Clicked on " + key + " on element " + opt.$trigger.id); } }}
If no callback and no default callback is specified, the item will not have an action
Specifies additional classNames to add to the item element
Specifies the icon class to set for the item.
Icons must be defined in CSS with selectors like .context-menu-item.icon-edit
, where edit is the icon class.
Specifies if the command is disabled (true) or enabled (false).
May be a callback returning a boolean. The Callback is executed in the context of the triggering object. The first argument is the key of the command. The second argument is the options object.
Example: { command1: {name: "Foobar", callback: function(key, opt){ return true; } }}
Specifies the type of the command.
null, undefined and the empty-string make the command a simple clickable item.
"text" makes the command an of type text. The name followed by the are encapsulated in a
"textarea" makes the command a
"checkbox" makes the command an of type checkbox. The name preceeded by the are encapsulated in a
"radio" makes the command an of type radio. The name preceeded by the are encapsulated in a
"select" makes the command a
"html" makes an non-command element.
Events to register on elements
Only used with types "text", "textarea", "radio", "checkbox" and "select".
Example: { command1: {name: "Foobar", type: "text", events: {keyup: function(e){alert(e.keyCode);}} }}
The value of the element.
Only used with types "text", "textarea" and "radio".
The selected option of a
Expecting a boolean when used with types "checkbox" and "radio".
Expecting a string when used with type "select"
Specifies the group of the radio elements.
Only used with type "radio".
Specifies the
Only used with type "select".
Example: {name: "select box", selected: "two", options: {one: "red", two: "blue", three: "green"}}
The height in pixel
Only used with type "textarea".
Commands to show in a sub-menu.
Reference to the
created by contextMenu on registration
Reference to the or
Only available with type "text", "textarea", "checkbox", "radio" and "select".
created by contextMenu on registration
Reference to the
Only available with type "text", "textarea", "checkbox", "radio" and "select".
created by contextMenu on registration
Reference to the
created by contextMenu on registration
opt is a reference to the options object passed at contextMenu registration
The element triggering the menu
The menu element
Registered callbacks of all commands (including those of sub-menus)
Registered commands (including those of sub-menus)
Registered commands of input-type (including those of sub-menus)
Access a specific : opt.inputs[key].$input
flag denoting if the menu contains input elements
The namespace (including leading dot) all events for this contextMenu instance were registered under
disable contextMenu to be shown on specified trigger elements
$(".some-selector").contextMenu(false);
enable contextMenu to be shown on specified trigger elements
$(".some-selector").contextMenu(true);
show the contextMenu of the first element of the selector (position determined by determinePosition):
$(".some-selector").contextMenu();
To unregister / destroy a specific contextMenu:
$.contextMenu( 'destroy', selector );
selector expects the (string) selector that the contextMenu was registered to
To unregister / destroy all contextMenus:
$.contextMenu( 'destroy' );
To fill input commands with values from a map:
{events: { hide: function(opt){ $.contextMenu.getInputValues(opt, {command1: "foo", command2: "bar"}); } } }
To fill input commands with values from data-attributes:
{events: { hide: function(opt){ $.contextMenu.getInputValues(opt, this.data()); } } }
To fetch values from input commands:
{events: { hide: function(opt){ var values = $.contextMenu.setInputValues(opt} } }
To save values from input commands to data-attributes:
{events: { hide: function(opt){ $.contextMenu.setInputValues(opt, this.data()); } } }
Trigger context menu to be shown for a trigger object.
Available on trigger object. The Event must be supplied with coordinates for the menu: {pageX: 123, pageY:123}
$('.context-menu-one').first().trigger( $.Event('contextmenu', {pageX: 123, pageY: 123}) );
$('.context-menu-one').first().trigger("contextmenu");
will invoke determinePosition to position the menu
Select / highlight the previous possible command
Available on context menu.
opt.$menu.trigger("prevcommand");
Select / highlight the next possible command
Available on context menu.
opt.$menu.trigger("nextcommand");
considering the following HTML $.contextMenu.fromMenu($('#html5menu'))
will return a proper items object
$.contextMenu.fromMenu()
will properly import (and thus handle) the following elements. Everything else is imported as {type: "html"}
The
Note: While the specs note
Engaging the HTML5 polyfill (ignoring $.contextMenu if context menus are available natively):
$(function(){ $.contextMenu("html5"); });
Engaging the HTML5 polyfill (ignoring browser native implementation):
$(function(){ $.contextMenu("html5", true); });
The position utility of jQuery UI makes relative positioning very simple. But the real beauty is collision detection, allowing the contextMenu to be shown to the left side of the pointer, if there's not enough space to the right. Its use is optional, if you want to shape off some more bytes by sacrificing usability: go ahead???