Skip to content

Commit 0ba4254

Browse files
committed
Closed mar10#22
1 parent aea9c47 commit 0ba4254

7 files changed

+37
-43
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# 0.5.0 / Unreleased
22

33
* [CHANGE] Changed widget namespace from 'ui' to 'moogle'.
4+
* [CHANGE] Default position is now at click event.
5+
* [CHANGE] Show-animation now faster by default.
46
* [FEATURE] `select` event has new parameters `ui.cmd` and `ui.target`.
57

68
# 0.4.0 / 2013-05-28

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,16 @@ structure (see [jQueryUI menu] for details):
131131
</dd>
132132
<dt>position</dt>
133133
<dd>
134-
Type: <code>Object | Function</code>, default: <code>{my: "left top", at: "left bottom", of: parentTarget, collision: "fit"}</code><br>
134+
Type: <code>Object | Function</code>,<br>
135+
default: <code>{my: "left top", at: "center", of: event, collision: "fit"}</code><br>
135136
Define position where popup opens. A simple <a href="http://api.jqueryui.com/position/">position</a> may be passed.<br>
136137
Also a function may be specified, to recalculate position every time:<br>
137138
<pre>
138139
$("#container").contextmenu({
139140
// position: {my: "left bottom", at: "right top"},
140141
position: function(event, ui){
141142
return {my: "left top", at: "center", of: event};
142-
},...</pre>
143+
}, ...</pre>
143144
</dd>
144145
<dt>preventSelect</dt>
145146
<dd>
@@ -150,7 +151,7 @@ structure (see [jQueryUI menu] for details):
150151
<dt>show</dt>
151152
<dd>
152153
Type: <code> Boolean | Number | String | Object</code>,
153-
default: <code>{ effect: "slideDown", duration: "slow"}</code><br>
154+
default: <code>{ effect: "slideDown", duration: "fast"}</code><br>
154155
Effect applied when showing the popup.<br>
155156
See <a href="http://api.jqueryui.com/jQuery.widget/#option-show">sample</a>
156157
for possible option values.

demo/index.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
]}
8181
],
8282
select: function(event, ui) {
83-
var $target = $(event.relatedTarget);
83+
var $target = ui.target;
8484
switch(ui.cmd){
8585
case "copy":
8686
CLIPBOARD = $target.text();
@@ -89,12 +89,11 @@
8989
CLIPBOARD = "";
9090
break
9191
}
92-
alert("select " + ui.cmd + " on " + ui.target.text());
92+
alert("select " + ui.cmd + " on " + $target.text());
9393
},
9494
beforeOpen: function(event, ui) {
9595
var $menu = ui.menu,
9696
$target = ui.target;
97-
// console.log("beforeOpen", this, event, ui, "" + CLIPBOARD);
9897
$(document)
9998
// .contextmenu("replaceMenu", [{title: "aaa"}, {title: "bbb"}])
10099
// .contextmenu("setEntry", "cut", {title: "Cuty", uiIcon: "ui-icon-heart", disabled: true})
@@ -111,9 +110,9 @@
111110
$("#container").contextmenu({
112111
delegate: ".hasmenu2",
113112
menu: "#options",
114-
// position: {my: "left bottom", at: "right top"},
113+
// position: {my: "left top", at: "left bottom"},
115114
position: function(event, ui){
116-
return {my: "left top", at: "center", of: event};
115+
return {my: "left top", at: "left.bottom", of: event.target};
117116
},
118117
preventSelect: true,
119118
taphold: true,

jquery.ui-contextmenu.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
options: {
2222
delegate: "[data-menu]", // selector
2323
hide: { effect: "fadeOut", duration: "fast"},
24-
show: { effect: "slideDown", duration: "slow"},
25-
position: null, // specify positional preferences (added for issue #18 and #13).
2624
ignoreParentSelect: true, // Don't trigger 'select' for sub-menu parents
2725
menu: null, // selector or jQuery or a function returning such
26+
position: null, // popup positon
2827
preventSelect: false, // disable text selection of target
29-
taphold: false, // open menu on taphold events (requires external plugins)
28+
show: { effect: "slideDown", duration: "fast"},
29+
taphold: false, // open menu on taphold events (requires external plugins)
3030
// Events:
3131
beforeOpen: $.noop, // menu about to open; return `false` to prevent opening
3232
blur: $.noop, // menu option lost focus
@@ -42,8 +42,6 @@
4242
var eventNames, targetId,
4343
opts = this.options;
4444

45-
// console.log(this.element)
46-
4745
this.$headStyle = null;
4846
this.orgMenu = null;
4947
this.currentTarget = null;
@@ -87,12 +85,10 @@
8785
create: $.proxy(this.options.create, this),
8886
focus: $.proxy(this.options.focus, this),
8987
select: $.proxy(function(event, ui){
90-
// Also pass the target that the menu was triggered on:
91-
event.relatedTarget = this.currentTarget;
92-
ui.cmd = normCommand(ui.item.find(">a").attr("href"));
93-
ui.target = $(this.currentTarget);
94-
// ignore clicks, if they only open a sub-menu
9588
var isParent = (ui.item.has(">a[aria-haspopup='true']").length > 0);
89+
ui.cmd = normCommand(ui.item.find(">a").attr("href"));
90+
ui.target = $(this.currentTarget);
91+
// ignore clicks, if they only open a sub-menu
9692
if( !isParent || !this.options.ignoreParentSelect){
9793
if( this._trigger.call(this, "select", event, ui) !== false ){
9894
this._closeMenu.call(this);
@@ -129,17 +125,10 @@
129125
posOption = opts.position,
130126
self = this,
131127
$menu = this._getMenu(),
132-
openEvent = event,
133-
// if called by 'open' method, 'relatedTarget' is the requested target object
134-
parentTarget = openEvent.target ? openEvent.target : openEvent,
135-
ui = {menu: $menu, target: $(openEvent.target)};
136-
this.currentTarget = openEvent.target;
128+
ui = {menu: $menu, target: $(event.target)};
129+
this.currentTarget = event.target;
137130
// Prevent browser from opening the system context menu
138131
event.preventDefault();
139-
// Also pass the target that the menu was triggered on as 'relatedTarget'.
140-
// This is required because our _trigger() calls will create events
141-
// that refer to the contextmenu's context (which is the target *container*)
142-
event.relatedTarget = this.currentTarget;
143132

144133
if( this._trigger("beforeOpen", event, ui) === false ){
145134
return false;
@@ -163,18 +152,20 @@
163152
posOption = $.extend({
164153
my: "left top",
165154
at: "left bottom",
166-
of: parentTarget,
155+
// if called by 'open' method, event does not have pageX/Y
156+
of: (event.pageX === undefined) ? event.target : event,
167157
collision: "fit"
168158
}, posOption);
169159

170160
// Finally display the popup
171161
$menu
172-
.show() // required to fix positioning error (issue #3)
162+
.show() // required to fix positioning error
173163
.css({
174164
position: "absolute",
175165
left: 0,
176166
top: 0
177-
}).position(posOption).hide();
167+
}).position(posOption)
168+
.hide();
178169

179170
this._show($menu, this.options.show, function(){
180171
self._trigger.call(self, "open", event, ui);
@@ -206,10 +197,14 @@
206197
},
207198
/** Return ui-menu root element as jQuery object. */
208199
_getMenu: function(){
209-
// this.options.menu may be a string, jQuery or a function returning that.
210200
var $menu = this.options.menu;
211201
return (typeof $menu === "string") ? $($menu) : $menu;
212202
},
203+
/** Return ui-menu entry (<A> or <LI> tag). */
204+
_getMenuEntry: function(cmd, wantLi){
205+
var $entry = this._getMenu().find("li a[href=#" + normCommand(cmd) + "]");
206+
return wantLi ? $entry.closest("li") : $entry;
207+
},
213208
/** Open context menu on a specific target (must match options.delegate) */
214209
open: function(target){
215210
// Fake a 'contextmenu' event
@@ -222,9 +217,7 @@
222217
},
223218
/** Enable or disable the menu command. */
224219
enableEntry: function(cmd, flag){
225-
// TODO: should be $menu.find(...)!
226-
var $entry = this.element.find("a[href=#" + normCommand(cmd) + "]");
227-
$entry.toggleClass("ui-state-disabled", (flag === false));
220+
this._getMenuEntry(cmd, true).toggleClass("ui-state-disabled", (flag === false));
228221
},
229222
/** Redefine the whole menu. */
230223
replaceMenu: function(data){
@@ -254,7 +247,7 @@
254247
/** Redefine menu entry (title or all of it). */
255248
setEntry: function(cmd, titleOrData){
256249
var $parent,
257-
$entry = this.element.find("a[href=#" + normCommand(cmd) + "]");
250+
$entry = this._getMenuEntry(cmd, false);
258251

259252
if(typeof titleOrData === "string"){
260253
// Replace <a> text without removing <span> child
@@ -270,8 +263,7 @@
270263
},
271264
/** Show or hide the menu command. */
272265
showEntry: function(cmd, flag){
273-
var $entry = this.element.find("a[href=#" + normCommand(cmd) + "]");
274-
$entry.toggle(flag !== false);
266+
this._getMenuEntry(cmd, true).toggle(flag !== false);
275267
}
276268
});
277269

jquery.ui-contextmenu.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
"title": "jQuery UI context menu plugin",
66
"description": "Turn a jQuery UI Menu widget into a context menu.",
7-
"keywords": ["context-menu", "contextmenu", "delegate",
8-
"jquery-ui-menu", "menu", "navigation", "popup",
7+
"keywords": ["context-menu", "contextmenu", "delegate",
8+
"jquery-ui-menu", "menu", "navigation", "popup",
99
"right-click", "right-click-menu"],
1010

1111
"author": {

ui-contextmenu.jquery.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
"title": "jQuery UI context menu plugin",
66
"description": "Turn a jQuery UI Menu widget into a context menu.",
7-
"keywords": ["context-menu", "contextmenu", "delegate",
8-
"jquery-ui-menu", "menu", "navigation", "popup",
7+
"keywords": ["context-menu", "contextmenu", "delegate",
8+
"jquery-ui-menu", "menu", "navigation", "popup",
99
"right-click", "right-click-menu"],
1010

1111
"author": {

0 commit comments

Comments
 (0)