Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 427 lines (372 sloc) 19.329 kB
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jQuery contextMenu Plugin</title>
<meta name="description" content="simple contextMenu generator for interactive web applications based on jQuery" />
<script src="jquery-1.6.1.min.js" type="text/javascript"></script>
<script src="jquery.contextMenu.js" type="text/javascript"></script>
<link href="jquery.contextMenu.css" rel="stylesheet" type="text/css" />
<link href="screen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<a id="github-forkme" href="http://github.com/medialize/jQuery-contextMenu"><img src="http://s3.amazonaws.com/github/ribbons/forkme_right_darkblue_121621.png" alt="Fork me on GitHub" /></a>
<div id="container">
<h1><a href="http://github.com/medialize/jQuery-contextMenu">jQuery contextMenu</a></h1>
<ul class="menu">
<li><a href="index.html">About</a></li>
<li><a href="demo.html">Demo</a></li>
<li class="active"><a href="docs.html">Documentation</a></li>
</ul>
<ul class="toc">
<li>
<a href="#register">Register new contextMenu</a>
<ul>
<li><a href="#options">options (at registration)</a></li>
<li><a href="#items">options.items</a></li>
<li><a href="#opt">opt (options at runtime)</a></li>
</ul>
</li>
<li><a href="#disable-trigger">Disable a contextMenu trigger</a></li>
<li><a href="#enable-trigger">Enable a contextMenu trigger</a></li>
<li><a href="#show-trigger">Manually show a contextMenu</a></li>
<li><a href="#html5-polyfill">Register HTML5 context &lt;menu&gt;s</a></li>
<li><a href="#unregister-contextMenu">Unregister contextMenu</a></li>
<li><a href="#unregister-all">Unregister all contextMenus</a></li>
<li><a href="#helper-import-values">Helper: Import values for &lt;input&gt;</a></li>
<li><a href="#helper-export-values">Helper: Export values from &lt;input&gt;</a></li>
<li><a href="#events">Events</a></li>
<li><a href="#html5-import">HTML5 &lt;menu&gt; import</a></li>
<li><a href="#ui-position">$.ui.position</a></li>
</ul>
<h2 id="register">Register new contextMenu</h2>
<p>To register a new contextMenu:</p>
<pre>$.contextMenu( <em>options</em> );</pre>
<h3 id="options">options (at registration)</h3>
<dl>
<dt id="options-selector"><em>(string)</em> selector</dt>
<dd>
<p>No Default Value - specification is Mandatory</p>
<p>The selector matching the trigger objects.</p>
<p>Example: <code>"span.with-context"</code>.</p>
</dd>
<dt id="options-items"><em>(object)</em> items</dt>
<dd>
<p>No Default Value - specification is Mandatory</p>
<p>Items to be listed in contextMenu. See <a href="#items">Items</a>.</p>
<p>Example: <code>{ one: {name: "one", callback: function(key, opt){ alert("Clicked on " + key); } }}</code></p>
</dd>
<dt id="options-appendTo"><em>(selector | DOMElement)</em> appendTo</dt>
<dd>
<p>default: document.body</p>
<p>Specifies the DOMElement the generated menu is to be appended to</p>
<p>Example: <code>"#context-menus-container"</code>.</p>
</dd>
<dt id="options-trigger"><em>(string)</em> trigger</dt>
<dd>
<p>default: <em>"right"</em></p>
<p>Specifies the event to show the contextMenu</p>
<p>Possible values: "right", "left", "hover"</p>
</dd>
<dt id="options-delay"><em>(int)</em> delay</dt>
<dd>
<p>default: <em>200</em></p>
<p>Specifies the time in milliseconds to wait before showing the menu. Only applies to <code>trigger: "hover"</code></p>
</dd>
<dt id="options-zIndex"><em>(int)</em> zIndex</dt>
<dd>
<p>default: <em>1</em></p>
<p>Specifies the offset to add to the calculated zIndex of the trigger element. Set to <em>0</em> to prevent zIndex manipulation</p>
</dd>
<dt id="options-className"><em>(string)</em> className</dt>
<dd>
<p>Specifies additional classNames to add to the menu element</p>
</dd>
<dt id="options-animation"><em>(object)</em> animation</dt>
<dd>
<p>default: <em>{duration: 500, show: "slideDown", hide: "slideUp"}</em></p>
<p>
animation properties take effect on showing and hiding the menu.
<em>duration</em> specifies the duration of the animation in milliseconds.
<em>show</em> and <em>hide</em> specify jQuery methods to show and hide elements.
</p>
<p>Possible show and hide animations: <code>{show: "show", hide: "hide"}</code>, <code>{show: "fadeIn", hide: "fadeOut"}, …</code>
</dd>
<dt id="options-events"><em>(object)</em> events</dt>
<dd>
<p>The show and hide events are triggered before the menu is shown or hidden.</p>
<p>The event handlers are executed in the context of the triggering object. <em>this</em> will thus reference the jQuery handle of the trigger object.</p>
<p>A reference to the current <a href="#opt">options</a> object is passed</p>
<p>The event handlers may return <em>false</em> to prevent the show or hide process.</p>
<p>Example: <code>{show: function(opt){ this.addClass('currently-showing-menu'); alert("Selector: " + opt.selector); }}</code></p>
</dd>
<dt id="options-position"><em>(function)</em> position</dt>
<dd>
<p>Position the context menu.</p>
<p>The function is executed in the context of the trigger object.</p>
<p>The first argument ist the <em>$menu</em> jQuery object</p>
<p>The second and third arguments are <em>x</em> and <em>y</em> coordinates provided by the showing event</p>
<p><em>x</em> and <em>y</em> 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 <em>$menu</em> must be used. If the coordinates are undefined, appropriate coordinates must be determined. An example of how this can be achieved is provided with <a href="#options-determinePosition">determinePosition</a>.</p>
<p>Example: <code>{position: function($menu, x, y){$menu.css({top: 123, left: 123});}}</code></p>
</dd>
<dt id="options-determinePosition"><em>(function)</em> determinePosition</dt>
<dd>
<p>Determine the position of the menu in respect to the given trigger object.</p>
<p>Hint: <a href="http://docs.jquery.com/UI/Position">jQuery UI's position()</a> may ease things up.</p>
<p>Example: <code>$menu.css('display', 'block').position({ my: "center top", at: "center bottom", of: this, offset: "0 5"}).css('display', 'none');</code></p>
</dd>
</dl>
<h3 id="items">options.items</h3>
<p>The <em>items</em> 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).</p>
<pre>items: {
command1: <em>commandOptions</em>,
separator1: <em>"-----"</em>
command2: <em>commandOptions</em>
}</pre>
<dl>
<dt id="items-name"><em>(string)</em> name</dt>
<dd>
<p>Specified the human readable name of the command in the menu</p>
<p>This is a mandatory property</p>
</dd>
<dt id="items-callback"><em>(function)</em> callback</dt>
<dd>
<p>Specifies the callback to execute if clicked on</p>
<p>
The Callback is executed in the context of the triggering object.
The first argument is the <em>key</em> of the command.
The second argument is the <a href="#opt">options</a> object.
The Callback may return <em>false</em> to prevent the menu from being hidden.
</p>
<p>Example: { command1: {name: "Foobar", callback: function(key, opt){ alert("Clicked on " + key + " on element " + opt.$trigger.id); } }}</p>
<p>This is a mandatory property for non-input elements (no <a href="#items-type">type</a>)</p>
</dd>
<dt id="items-className"><em>(string)</em> className</dt>
<dd>
<p>Specifies additional classNames to add to the item element</p>
</dd>
<dt id="items-icon"><em>(string)</em> icon</dt>
<dd>
<p>Specifies the icon class to set for the item.</p>
<p>Icons must be defined in CSS with selectors like <code>.context-menu-item.icon-<em>edit</em></code>, where <em>edit</em> is the icon class.</p>
</dd>
<dt id="items-disabled"><em>(function|boolean)</em> disabled</dt>
<dd>
<p>Specifies if the command is disabled (<em>true</em>) or enabled (<em>false</em>).</p>
<p>
May be a callback returning a boolean.
The Callback is executed in the context of the triggering object.
The first argument is the <em>key</em> of the command.
The second argument is the <a href="#opt">options</a> object.
</p>
<p>Example: { command1: {name: "Foobar", callback: function(key, opt){ return true; } }}</p>
</dd>
<dt id="items-type"><em>(string)</em> type</dt>
<dd>
<p>Specifies the type of the command.</p>
<p><em>null</em>, <em>undefined</em> and the <em>empty-string</em> make the command a simple clickable item.</p>
<p><em>"text"</em> makes the command an &lt;input&gt; of type text. The name followed by the &lt;input&gt; are encapsulated in a &lt;label&gt;.</p>
<p><em>"textarea"</em> makes the command a &lt;textarea&gt;. The name followed by the &lt;input&gt; are encapsulated in a &lt;label&gt;.</p>
<p><em>"checkbox"</em> makes the command an &lt;input&gt; of type checkbox. The name preceeded by the &lt;input&gt; are encapsulated in a &lt;label&gt;. The checkbox-element is moved to the icon space</p>
<p><em>"radio"</em> makes the command an &lt;input&gt; of type radio. The name preceeded by the &lt;input&gt; are encapsulated in a &lt;label&gt;. The radio-element is moved to the icon space</p>
<p><em>"select"</em> makes the command a &lt;select&gt;. The name followed by the &lt;select&gt; are encapsulated in a &lt;label&gt;.</p>
<p><em>"html"</em> makes an non-command element.</p>
</dd>
<dt id="items-events"><em>(object)</em> events</dt>
<dd>
<p>Events to register on &lt;input&gt; elements</p>
<p>Only used with <a href="#items-type">types</a> <em>"text"</em>, <em>"textarea"</em>, <em>"radio"</em>, <em>"checkbox"</em> and <em>"select"</em>.</p>
<p>Example: { command1: {name: "Foobar", type: "text", events: {keyup: function(e){alert(e.keyCode);}} }}</p>
</dd>
<dt id="items-value"><em>(string)</em> value</dt>
<dd>
<p>The value of the &lt;input&gt; element.</p>
<p>Only used with <a href="#items-type">types</a> <em>"text"</em>, <em>"textarea"</em> and <em>"radio"</em>.</p>
</dd>
<dt id="items-selected"><em>(string|boolean)</em> selected</dt>
<dd>
<p>The selected option of a &lt;select&gt; element and the checked property for <em>"checkbox"</em> and <em>"radio"</em> types.</p>
<p>Expecting a boolean when used with <a href="#items-type">types</a> <em>"checkbox"</em> and <em>"radio"</em>.</p>
<p>Expecting a string when used with <a href="#items-type">type</a> <em>"select"</em></p>
</dd>
<dt id="items-radio"><em>(string)</em> radio</dt>
<dd>
<p>Specifies the group of the radio elements.</p>
<p>Only used with <a href="#items-type">type</a> <em>"radio"</em>.</p>
</dd>
<dt id="items-options"><em>(object)</em> options</dt>
<dd>
<p>Specifies the &lt;option&gt;s for the &lt;select&gt; element.</p>
<p>Only used with <a href="#items-type">type</a> <em>"select"</em>.</p>
<p>Example: <code>{name: "select box", selected: "two", options: {one: "red", two: "blue", three: "green"}}</code></p>
</dd>
<dt id="items-height"><em>(int)</em> height</dt>
<dd>
<p>The height in pixel &lt;textarea&gt; element. If not specified, the height is defined by CSS.</p>
<p>Only used with <a href="#items-type">type</a> <em>"textarea"</em>.</p>
</dd>
<dt id="items-items"><em>(object)</em> items</dt>
<dd>
<p>Commands to show in a sub-menu.</p>
</dd>
<dt id="items-node"><em>(jQuery)</em> $node</dt>
<dd>
<p>Reference to the &lt;li&gt; command element</p>
<p>created by contextMenu on registration</p>
</dd>
<dt id="items-input"><em>(jQuery)</em> $input</dt>
<dd>
<p>Reference to the &lt;input&gt; or &lt;select&gt; of the command element.</p>
<p>Only available with <a href="#items-type">type</a> <em>"text"</em>, <em>"textarea"</em>, <em>"checkbox"</em>, <em>"radio"</em> and <em>"select"</em>.</p>
<p>created by contextMenu on registration</p>
</dd>
<dt id="items-label"><em>(jQuery)</em> $label</dt>
<dd>
<p>Reference to the &lt;label&gt; of the command element</p>
<p>Only available with <a href="#items-type">type</a> <em>"text"</em>, <em>"textarea"</em>, <em>"checkbox"</em>, <em>"radio"</em> and <em>"select"</em>.</p>
<p>created by contextMenu on registration</p>
</dd>
<dt id="items-node"><em>(jQuery)</em> $menu</dt>
<dd>
<p>Reference to the &lt;ul&gt; sub-menu element</p>
<p>created by contextMenu on registration</p>
</dd>
</dl>
<h3 id="opt">opt (options at runtime)</h3>
<p><em>opt</em> is a reference to the <em>options</em> object passed at contextMenu registration</p>
<dl>
<dt id="opt-trigger"><em>(jQuery)</em> $trigger</dt>
<dd>
<p>The element triggering the menu</p>
</dd>
<dt id="opt-menu"><em>(jQuery)</em> $menu</dt>
<dd>
<p>The menu element</p>
</dd>
<dt id="opt-callbacks"><em>(object)</em> callbacks</dt>
<dd>
<p>Registered callbacks of all commands (including those of sub-menus)</p>
</dd>
<dt id="opt-commands"><em>(object)</em> commands</dt>
<dd>
<p>Registered commands (including those of sub-menus)</p>
</dd>
<dt id="opt-inputs"><em>(object)</em> inputs</dt>
<dd>
<p>Registered commands of input-type (including those of sub-menus)</p>
<p>Access a specific &lt;input&gt;: <code>opt.inputs[key].$input</code></p>
</dd>
<dt id="opt-hasTypes"><em>(boolean)</em> hasTypes</dt>
<dd>
<p>flag denoting if the menu contains input elements</p>
</dd>
<dt id="opt-ns"><em>(string)</em> ns</dt>
<dd>
<p>The namespace (including leading dot) all events for this contextMenu instance were registered under</p>
</dd>
</dl>
<h2 id="disable-trigger">Disable a contextMenu trigger</h2>
<p>disable contextMenu to be shown on specified trigger elements</p>
<pre>$(".some-selector").contextMenu(false);</pre>
<h2 id="enable-trigger">Enable a contextMenu trigger</h2>
<p>enable contextMenu to be shown on specified trigger elements</p>
<pre>$(".some-selector").contextMenu(true);</pre>
<h2 id="show-trigger">Manually show a contextMenu</h2>
<p>show the contextMenu of the first element of the selector (position determined by <a href="#options-determinePosition">determinePosition</a>):</p>
<pre>$(".some-selector").contextMenu();</pre>
<h2 id="html5-polyfill">Register HTML5 context &lt;menu&gt;s</h2>
<pre>$.contextMenu("html5");</pre>
<h2 id="unregister-contextMenu">Unregister contextMenu</h2>
<p>To unregister / destroy a specific contextMenu:</p>
<pre>$.contextMenu( 'destroy', <em>selector</em> );</pre>
<p><em>selector</em> expects the (string) selector that the contextMenu was registered to</p>
<h2 id="unregister-all">Unregister all contextMenus</h2>
<p>To unregister / destroy all contextMenus:</p>
<pre>$.contextMenu( 'destroy' );</pre>
<h2 id="helper-import-values">Helper: Import values for &lt;input&gt;</h2>
<p>To fill input commands with values from a map:</p>
<pre>{events: {
hide: function(opt){
$.contextMenu.getInputValues(opt, {command1: "foo", command2: "bar"});
}
}
}</pre>
<p>To fill input commands with values from data-attributes:</p>
<pre>{events: {
hide: function(opt){
$.contextMenu.getInputValues(opt, this.data());
}
}
}</pre>
<h2 id="helper-export-values">Helper: Export values from &lt;input&gt;</h2>
<p>To fetch values from input commands:</p>
<pre>{events: {
hide: function(opt){
var values = $.contextMenu.setInputValues(opt}
}
}</pre>
<p>To save values from input commands to data-attributes:</p>
<pre>{events: {
hide: function(opt){
$.contextMenu.setInputValues(opt, this.data()); }
}
}</pre>
<h2 id="events">Events</h2>
<dl>
<dt>contextmenu</dt>
<dd>
<p>Trigger context menu to be shown for a trigger object.</p>
<p>Available on <em>trigger object</em>. The Event must be supplied with coordinates for the menu: <code>{pageX: 123, pageY:123}</code></p>
<pre>$('.context-menu-one').first().trigger(
$.Event('contextmenu', {pageX: 123, pageY: 123})
);</pre>
<pre>$('.context-menu-one').first().trigger("contextmenu");</pre>
<p>will invoke <a href="#options-determinePosition">determinePosition</a> to position the menu</p>
</dd>
<dt>prevcommand</dt>
<dd>
<p>Select / highlight the previous possible command</p>
<p>Available on <em>context menu</em>.</p>
<pre>opt.$menu.trigger("prevcommand");</pre>
</dd>
<dt>nextcommand</dt>
<dd>
<p>Select / highlight the next possible command</p>
<p>Available on <em>context menu</em>.</p>
<pre>opt.$menu.trigger("nextcommand");</pre>
</dd>
</dl>
<h2 id="html5-import">HTML5 &lt;menu&gt; import</h2>
<p>considering the following HTML <code>$.contextMenu.fromMenu($('#html5menu'))</code> will return a proper <a href="#items">items</a> object
<pre>&lt;menu id=&quot;html5menu&quot; type=&quot;context&quot; style=&quot;display:none&quot;&gt;
&lt;command label=&quot;rotate&quot; onclick=&quot;alert('rotate')&quot;&gt;
&lt;command label=&quot;resize&quot; onclick=&quot;alert('resize')&quot;&gt;
&lt;menu label=&quot;share&quot;&gt;
&lt;command label=&quot;twitter&quot; onclick=&quot;alert('twitter')&quot;&gt;
&lt;hr&gt;
&lt;command label=&quot;facebook&quot; onclick=&quot;alert('facebook')&quot;&gt;
&lt;/menu&gt;
&lt;/menu&gt;</pre>
<p><code>$.contextMenu.fromMenu()</code> will properly import (and thus handle) the following elements. Everything else is imported as <code>{type: "html"}</code></p>
<ul>
<li>&lt;menu&gt;</li>
<li>&lt;hr&gt;</li>
<li>&lt;a&gt;</li>
<li>&lt;command type=&quot;command|radio|checkbox&quot;&gt;</li>
<li>&lt;input type=&quot;text|radio|checkbox&quot;&gt;</li>
<li>&lt;select&gt;</li>
<li>&lt;textare&gt;</li>
<li>&lt;label for="someId"&gt;</li>
<li>&lt;label&gt; the text &lt;input|textarea|select&gt;</li>
</ul>
<p>The &lt;menu&gt; must be hidden but not removed, as all command events (clicks) are passed-thru to the original command element!</p>
<h2 id="ui-position">$.ui.position</h2>
<p>
The <a href="http://jqueryui.com/demos/position/">position utility of jQuery UI</a> makes relative positioning very simple. But the real beauty is <em>collision detection</em>,
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…
</p>
</div>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.