From c6b842d5cca5c42d9bc54b1693c1d1366a4977d6 Mon Sep 17 00:00:00 2001
From: "Martin@MBP"
Demo with jQuery UI 1.11
—
+ Demo with jQuery UI 1.12
+ —
Demo with jQuery UI 1.10
+ Demo with jQuery UI 1.11
+ —
+ Demo with jQuery UI 1.12
+ —
+ Demo with jQuery UI 1.10
+ Right-click in an element to open the context menu: Open context menu using
Demo with jQuery UI 1.11
—
+ Demo with jQuery UI 1.12
+ —
Demo with jQuery UI 1.10
` element or definition list (i.e.
`[{title: "Paste", cmd: "paste"}, ...]`).
@@ -27,7 +27,7 @@ See also the [Change Log](https://github.com/mar10/jquery-ui-contextmenu/blob/ma
[  ](http://wwwendt.de/tech/demo/jquery-contextmenu/demo/ "Live demo")
-## Tutorial
+## Getting Started
First, include dependencies:
@@ -40,10 +40,10 @@ First, include dependencies:
```html
-
-
-
+
+
```
@@ -76,491 +76,13 @@ $("#container").contextmenu({
});
```
-The `delegate` option defines a CSS selector, which is evaluated for all
-elements inside the context element (`#container` in our example).
-In order to attach menus to *all* matching elements on the page that have
-`class="hasmenu"`, we may use `document` as context:
-```js
-$(document).contextmenu({
- delegate: ".hasmenu",
- ...
-});
-```
-**Note:** only one contextmenu widget instance can be bound to one element.
-See the *Howto* below for a solution to this problem.
-
-The `menu` options may contain a (nested) array of entry defiitions.
-Following a list of available properties:
-
-
-
-Instead of handling all menu commands in the `select` event, it is also possible
-to attach callbacks directly to menu entries:
-```js
-$(document).contextmenu({
- delegate: ".hasmenu",
- menu: [
- {title: "Copy", uiIcon: "ui-icon-copy", action: function(event, ui){
- alert("Copy " + ui.target.text());
- }
- },
- ...
-});
-```
-
-### Initialize menu from an existing `Function
, default: n.a.
- Optional callback that will be executed when the entry is selected.
-String
, default: ""
- Additional class name(s) to be added to the entries <li> element.
- Separate multiple class names with a space.
- Custom CSS may be applied like .ui-menu .my-class { color: red; }
.
-String
, default: ""
- Optional identifier associated with the menu entry.
- It can later be accessed in the select event as ui.cmd
.
-Object
, default: {}
- Optional hash of additional properties that will be added to the entry's
- data attribute.
- It can later be accessed in the select event as ui.item.data()
.
-Boolean
, default: false
- Pass true to disable the entry.
-String
, default: ""
- The displayed name of the menu entry. Use dashes ("---"
) to
- define a separator.
-String
, default: ""
- If defined, an icon is added to the menu entry. For example passing
- "ui-icon-copy"
will generate this element:
- <span class='ui-icon ui-icon-copy' />
.
- See also <Icon Overview.
-` element
-
-In this case `menu` must point to the markup:
-
-```js
-$(document).contextmenu({
- delegate: ".hasmenu",
- menu: "#options",
- select: function(event, ui) {
- ...
- }
-});
-```
-We also have to provide some HTML markup that defines the context menu structure,
-see [jQueryUI menu] for details:
-
-```html
-
-
-```
-
-**Note:** until and including jQuery UI 1.10 the use of anchors (``) in menu
-items was required:
-```html
-
-
-
-
-```
+For more information:
-
-### Modify the menu depending on the context
-
-Often we need to modify the menu before it is displayed, in order to reflect the
-current context.
-This can be done in the `beforeOpen` event:
-
-```js
-$(document).contextmenu({
- delegate: ".hasmenu",
- menu: [
- {title: "Cut", cmd: "cut", uiIcon: "ui-icon-scissors"},
- {title: "Copy", cmd: "copy", uiIcon: "ui-icon-copy"},
- {title: "Paste", cmd: "paste", uiIcon: "ui-icon-clipboard", disabled: true },
- ...
- ],
- beforeOpen: function(event, ui) {
- var $menu = ui.menu,
- $target = ui.target,
- extraData = ui.extraData; // optionally passed when menu was opened by call to open()
-
- // Optionally return false, to prevent opening the menu
-// return false;
-
- // En/disable single entries
- $(document).contextmenu("enableEntry", "paste", false);
- // Show/hide single entries
- $(document).contextmenu("showEntry", "cut", false);
- // Redefine the title of single entries
- $(document).contextmenu("setEntry", "copy", "Copy '" + $target.text() + "'")
- // Redefine all attributes of single entries
- $(document).contextmenu("setEntry", "cut", {title: "Cuty", uiIcon: "ui-icon-heart", disabled: true});
- // Redefine the whole menu
- $(document).contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}, ...]);
- // Redefine the whole menu from another HTML definition
- $(document).contextmenu("replaceMenu", "#options2");
- },
- ...
-});
-```
-
-
-## API documentation
-### Options
-
-
-
-
-
-### Methods
-
-String
,
- default: "ui-contextmenu"
- This class is added to the outer ul element.
-Boolean
,
- default: false
- Set keyboard focus to first menu entry on open.
-Boolean
,
- default: true
- Set `false` to prevent opening on a browser's `contextmenu` event, which is
- normally triggered by a mouse rightclick.
- The menu can still be opened by calling the `open()` method.
-String
- A selector to filter the elements that trigger the context menu.
- Boolean | Number | String | Object
,
- default: { effect: "fadeOut", duration: "fast" }
- Effect applied when hiding the popup.
- See sample
- for possible option values.
-Boolean
, default: true
- If true
, a click on a menu item that contains a sub-menu, will
- not trigger the select
event.
-Object[] | String | jQuery
- jQuery object or selector of HTML markup that
- defines the context menu structure (see
- jQueryUI menu for details).
-
- If an array of objects is passed, it will be used to generate
- such markup on the fly.
-Object | Function
,
- default: {my: "left top", at: "center", of: event, collision: "fit"}
- Define position where popup opens. A simple position may be passed.
- Also a function may be specified, to recalculate position every time:
-
- $("#container").contextmenu({
- position: function(event, ui){
- return {my: "left top", at: "left bottom", of: ui.target};
- }, ...
-Boolean
, default: false
- Prevent that a right click inside an open popup menu will open the browser's
- system context menu.
-Boolean
, default: false
- Prevent accidental text selection of potential menu targets on doubleclick
- or drag.
- Boolean | Number | String | Object
,
- default: { effect: "slideDown", duration: "fast"}
- Effect applied when showing the popup.
- See sample
- for possible option values.
-Boolean
, default: false
- Open menu on taphold events,
- which is especially useful for touch devices (but may require external
- plugins to generate taphold
events).
-String
, optional
- Add a title
attribute to the menu markup, which will be displayed
- as tooltip by most browser (or external plugins).
-Object
, default: {}
- Custom options passed to UI Menu, when the widget is created.
- Especially useful to tweak the position of submenus.
-
-
-
-
-### Events
-
-jquery-contextmenu exposes events from [jQueryUI menu]: `blur`, `create`, `focus`, `select`.
-However, since the `event.target` parameter contains the menu item, we additionally pass the element
-that was right-clicked in `ui.target`.
-
-Events may be handled by passing a handler callback option:
-```js
-$("#container").contextmenu({
- [...]
- select: function(event, ui) {
- alert("select " + ui.cmd + " on " + ui.target.text());
- }
-});
-```
-
-Alternatively a handler may be bound, so this is equivalent:
-```js
-$("#container").bind("contextmenuselect", function(event, ui) {
- alert("select " + ui.cmd + " on " + ui.target.text());
-});
-```
-
-
- Call like $(...).contextmenu("close");
.
-
- Call like $(...).contextmenu("enableEntry", "paste", false);
.
-UL
element.
-
- Call like $(...).contextmenu("open", $(target)[, extraData]);
.
- Optional `extraData` will be available in event handlers as ui.extraData
.
-
- Call like $(...).contextmenu("replaceMenu", "#menu2");
.
- or $(...).contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}, ...]);
.
-
- `data` may be a title string or a menu definition object.
- Call like $(...).contextmenu("setEntry", "paste", "Paste link");
.
-
- Call like $(...).contextmenu("showEntry", "paste", false);
.
-
-
-
-
-# Tips and Tricks
-### [Howto] Add right-aligned shortcut hints
-
-Simply add a tag of your choice to the title (for example ``)
-```js
-$(document).contextmenu({
- delegate: ".hasmenu",
- menu: [
- {title: "Edit title[F2]", cmd: "rename"},
- {title: "Copy [Ctrl+C]", cmd: "copy"}, ...
- ],
-```
-and make it right aligned via CSS:
-```css
-.ui-menu kbd {
- float: right;
-}
-```
-
-### [Howto] Enable keyboard control
-
-In order open a context menu with the keyboard, make sure the target elements
-are tabbable, for example by adding a `tabindex="0"` attribute.
-Also make sure the `autoFocus: true` option is set.
-This will allow to Use Tab and the Windows Menu keys.
-
-
-### [Howto] Modify the menu using an asynchronous request
-
-```js
-$(document).contextmenu({
- ...
- beforeOpen: function(event, ui) {
- // Immediate menu changes
- $(document).contextmenu("setEntry", "test", "(loading...)");
- // Menu opens, then we submit a request and wait for the resonse
- $.ajax({
- ...
- }).done(function(data) {
- // Modify the menu from the ajax response. The menu will be updated
- // while open
- $(document).contextmenu("setEntry", "test", {
- title: "New entry", cmd: "test",
- children: [ ... ]
- });
- });
- },
-```
-
-Alternatively we can delay the opening until the response arrives:
-```js
-$(document).contextmenu({
- ...
- beforeOpen: function(event, ui) {
- var dfd = new $.Deferred();
-
- $.ajax({
- ...
- }).done(function(data) {
- // Modify the menu from the ajax response. The menu will be opened
- // afterwards
- $(document).contextmenu("setEntry", "test", {
- title: "New entry", cmd: "test",
- children: [ ... ]
- });
- dfd.resolve(); // Notify about finished response
- });
-
- // Return a promise to delay opening until an async response becomes
- // available
- ui.result = dfd.promise();
- },
-```
-
-
-### [Howto] Bind different contextmenus to the same DOM element
-
-This is especially useful if we want to bind contextmenus for different selectors
-to the `document` element, in order to make them global:
-
-```js
-$(document).contextmenu({
- delegate: ".hasmenu",
- menu: ...,
- select: function(event, ui) {
- alert("select contextmenu 1" + ui.cmd + " on " + ui.target.text());
- }
-});
-```
-
-Another call to `$(document).contextmenu({...})` would destroy the previous
-instance, because the [jQuery Widget Factory](https://learn.jquery.com/jquery-ui/widget-factory/)
-only allows one instance per element.
-
-The soulution is to create new widget with another name but identical functionality:
-
-```js
-// 1. Create and register another widget that inherits directly from
-// jquery-ui-contextmenu:
-$.widget("moogle.contextmenu2", $.moogle.contextmenu, {});
-// 2. Now we can bind this new widget to the same DOM element without
-// destroying a previous widget.
-$(document).contextmenu2({
- delegate: ".hasmenu2",
- menu: ...,
- select: function(event, ui) {
- alert("select contextmenu2" + ui.cmd + " on " + ui.target.text());
- }
-});
-```
+ * [Read the Tutorial](https://github.com/mar10/jquery-ui-contextmenu/wiki) and
+ [API Reference](https://github.com/mar10/jquery-ui-contextmenu/wiki/ApiRef)
+ * Have a look at the [Live demo page](http://wwwendt.de/tech/demo/jquery-contextmenu/demo/)
+ * Ask questions on [Stackoverflow](http://stackoverflow.com/questions/tagged/jquery-ui-contextmenu)
+ * [Play with the jsFiddle](http://jsfiddle.net/mar10/6o3u8a88/)
# Credits
From 5fb91bded3adf8aa9e1b92e7470fa4a672978a26 Mon Sep 17 00:00:00 2001
From: "Martin@MBP"
- Return false
to prevent opening.
- This is also a good place to modify the menu (i.e. hiding, disabling, or
- renaming entries, or replace the menu altogether).
-
- ui.cmd
contains the command id.
- Return false
to prevent closing the menu.
-").appendTo(e),a.moogle.contextmenu.createMenuMarkup(c.children,d)),this.getMenu().menu("refresh"))},showEntry:function(a,b){this._getMenuEntry(a).toggle(b!==!1)}}),a.extend(a.moogle.contextmenu,{createEntryMarkup:function(b,c){var d=null;c.attr("data-command",b.cmd),/[^\-\u2014\u2013\s]/.test(b.title)?(e?(d=a("",{html:""+b.title,href:"#"}).appendTo(c),b.uiIcon&&d.append(a("").addClass(b.uiIcon))):(c.html(""+b.title),a.isFunction(b.action)&&c.data("actionHandler",b.action),b.uiIcon&&c.append(a("").addClass(b.uiIcon))),a.isFunction(b.action)&&c.data("actionHandler",b.action),b.disabled&&c.addClass("ui-state-disabled"),b.addClass&&c.addClass(b.addClass),a.isPlainObject(b.data)&&c.data(b.data),null!=b.tooltip&&c.attr("title",b.tooltip)):c.text(b.title)},createMenuMarkup:function(b,c){var d,e,f,g;for(null==c&&(c=a("
").appendTo("body")),d=0;d
").appendTo(g),a.moogle.contextmenu.createMenuMarkup(e.children,f));return c},isMenu:function(a){return e?a.has(">a[aria-haspopup='true']").length>0:a.is("[aria-haspopup='true']")},replaceFirstTextNodeChild:function(a,b){a.contents().filter(function(){return 3===this.nodeType}).first().replaceWith(b)},updateTitle:function(b,c){e?a.moogle.contextmenu.replaceFirstTextNodeChild(a("a",b),c):a.moogle.contextmenu.replaceFirstTextNodeChild(b,c)}})});
//# sourceMappingURL=jquery.ui-contextmenu.min.js.map
\ No newline at end of file
diff --git a/test/index.html b/test/index.html
index 50fcd84..e1148fe 100644
--- a/test/index.html
+++ b/test/index.html
@@ -1,5 +1,4 @@
-
-
+
From de70862a9f1d8337b6b18be5ed104aad86ca4b2e Mon Sep 17 00:00:00 2001
From: "Martin@MBP"
;Wc0QE8B-cUPf;YxEm02E}=f34QGBBstiNVRijcvKpc(vrAUCI9B
UiAid
zL`BQ&qbNJhbop*d3e3ad(ca4eU|1b4dz4~XQ%#I!U?fSWHdFGF{sYP%azjquery.ui-contextmenu.js
jquery.ui-contextmenu.js
+
+
+ Sample 1
+
+
+ Sample 2
+
+
+
+
+
+ Sample 3
+ $("#container").contextmenu("open", $(".hasmenu:first"))
and close after 2 sec.:
+
+
diff --git a/demo/index.html b/demo/index.html
index b5d02aa..a621766 100644
--- a/demo/index.html
+++ b/demo/index.html
@@ -187,6 +187,8 @@
jquery.ui-contextmenu.js