Skip to content

Commit fa51311

Browse files
committed
The command must be specified in the data-command attribute of the <li> element instead of the anchor's href.
1 parent 24a958c commit fa51311

File tree

4 files changed

+20
-24
lines changed

4 files changed

+20
-24
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,19 @@ $(document).contextmenu({
9494
});
9595
```
9696

97-
We also have to provide some HTML markup that defines the context menu
98-
structure (see [jQueryUI menu] for details):
97+
We also have to provide some HTML markup that defines the context menu structure, see
98+
[jQueryUI menu] for details. jQuery UI 1.11 removed the requirement to use anchors in menu
99+
items, so the `<a>` tags should be omitted:
99100

100101
```html
101102
<ul id="options" class="ui-helper-hidden">
102-
<li><a href="#copy"><span class="ui-icon ui-icon-copy"></span>Copy</a>
103-
<li class="ui-state-disabled"><a href="#paste">Paste</a>
103+
<li data-command="copy"><a href="#"><span class="ui-icon ui-icon-copy"></span>Copy</a>
104+
<li data-command="paste" class="ui-state-disabled"><a href="#">Paste</a>
104105
<li>----
105106
<li><a>More</a>
106107
<ul>
107-
<li><a href="#sub1">Sub 1</a>
108-
<li><a href="#sub2">Sub 2</a>
108+
<li data-command="sub1"><a href="#">Sub 1</a>
109+
<li data-command="sub2"><a href="#">Sub 2</a>
109110
</ul>
110111
</ul>
111112
```

jquery.ui-contextmenu.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
"use strict";
1212
var supportSelectstart = "onselectstart" in document.createElement("div");
1313

14-
/** Return command without leading '#' (default to ""). */
15-
function normCommand(cmd){
16-
return (cmd && cmd.match(/^#/)) ? cmd.substring(1) : (cmd || "");
17-
}
18-
19-
2014
$.widget("moogle.contextmenu", {
2115
version: "1.4.0-1",
2216
options: {
@@ -144,7 +138,7 @@
144138
isParent = (ui.item.has(">a[aria-haspopup='true']").length > 0),
145139
$a = ui.item.find(">a"),
146140
actionHandler = $a.data("actionHandler");
147-
ui.cmd = normCommand($a.attr("href"));
141+
ui.cmd = ui.item.attr("data-command");
148142
ui.target = $(this.currentTarget);
149143
// ignore clicks, if they only open a sub-menu
150144
if( !isParent || !this.options.ignoreParentSelect){
@@ -254,7 +248,7 @@
254248
},
255249
/** Return ui-menu entry (<A> or <LI> tag). */
256250
_getMenuEntry: function(cmd, wantLi){
257-
var $entry = this.$menu.find("li a[href=#" + normCommand(cmd) + "]");
251+
var $entry = this.$menu.find("li[data-command=" + cmd + "] a");
258252
return wantLi ? $entry.closest("li") : $entry;
259253
},
260254
/** Close context menu. */
@@ -331,10 +325,11 @@ $.extend($.moogle.contextmenu, {
331325
// hyphen, em dash, en dash: separator as defined by UI Menu 1.10
332326
$parentLi.text(entry.title);
333327
}else{
328+
$parentLi.attr("data-command", entry.cmd);
334329
$a = $("<a/>", {
335330
// text: "" + entry.title,
336331
html: "" + entry.title, // allow to pass HTML markup
337-
href: "#" + normCommand(entry.cmd)
332+
href: "#"
338333
}).appendTo($parentLi);
339334
if( $.isFunction(entry.action) ){
340335
$a.data("actionHandler", entry.action);

test/index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ <h3>Sample Markup</h3>
3030
<!-- This element will be copied as #sampleMenu for every test: -->
3131

3232
<ul id="sampleMenuTemplate" style="display: none;">
33-
<li><a href="#cut"><span class="ui-icon ui-icon-scissors"></span>Cut</a>
34-
<li><a href="#copy"><span class="ui-icon ui-icon-copy"></span>Copy</a>
35-
<li class="ui-state-disabled ui-icon-clipboard"><a href="#paste">Paste</a>
33+
<li data-command="cut"><a href="#"><span class="ui-icon ui-icon-scissors"></span>Cut</a>
34+
<li data-command="copy"><a href="#"><span class="ui-icon ui-icon-copy"></span>Copy</a>
35+
<li data-command="paste" class="ui-state-disabled ui-icon-clipboard"><a href="#">Paste</a>
3636
<li>----
3737
<li><a>More</a>
3838
<ul>
39-
<li><a href="#sub1">Sub Item 1</a>
40-
<li><a href="#sub2">Sub Item 2</a>
39+
<li data-command="sub1"><a href="#">Sub Item 1</a>
40+
<li data-command="sub2"><a href="#">Sub Item 2</a>
4141
</ul>
4242
</ul>
4343
</body>

test/tests.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ function _clickTest(menu){
275275
// },
276276
select: function(event, ui){
277277
// window.console.log("select");
278-
var t = ui.item ? $(ui.item).find("a:first").attr("href") : ui.item;
278+
var t = ui.item ? $(ui.item).attr("data-command") : ui.item;
279279
log("select(" + t + ")");
280280
equal( ui.cmd, "cut", "select: ui.cmd is set" );
281281
equal( ui.target.text(), "AAA", "select: ui.target is set" );
@@ -301,7 +301,7 @@ function _clickTest(menu){
301301

302302
setTimeout(function(){
303303
// TODO: why is focus() called twice?
304-
equal(logOutput(), "createMenu,create,open(),beforeOpen(AAA),after open(),open,select(#cut),close",
304+
equal(logOutput(), "createMenu,create,open(),beforeOpen(AAA),after open(),open,select(cut),close",
305305
"Event sequence OK.");
306306
start();
307307
}, 1000);
@@ -349,7 +349,7 @@ asyncTest("Array menu", function(){
349349
}, 10);
350350
},
351351
select: function(event, ui){
352-
var t = ui.item ? $(ui.item).find("a:first").attr("href") : ui.item;
352+
var t = ui.item ? $(ui.item).attr("data-command") : ui.item;
353353
log("select(" + t + ")");
354354
equal( ui.cmd, "cut", "select: ui.cmd is set" );
355355
equal( ui.target.text(), "AAA", "select: ui.target is set" );
@@ -367,7 +367,7 @@ asyncTest("Array menu", function(){
367367
log("after open()");
368368

369369
setTimeout(function(){
370-
equal(logOutput(), "open(),after open(),open,select(#cut),cut action,close",
370+
equal(logOutput(), "open(),after open(),open,select(cut),cut action,close",
371371
"Event sequence OK.");
372372
start();
373373
}, 500);

0 commit comments

Comments
 (0)