Skip to content

Commit df80572

Browse files
committed
worked on unit tests, tracking a bug with async events
1 parent a27f2b0 commit df80572

10 files changed

+415
-51
lines changed

CHANGELOG.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# 0.6.0 / Unreleased
22

3+
* [CHANGE] Removed `init` event, added `createMenu` event.
4+
* [CHANGE] Added unit tests.
5+
* [FEATURE] Added `getMenu` method.
36
* [BUGFIX] Fixed custom position sample.
47

58
# 0.5.0 / 2013-06-02
@@ -12,11 +15,11 @@
1215

1316
# 0.4.0 / 2013-05-28
1417

15-
* [FEATURE] `position` option (thanks to Jeffrey Dean Altemus)
18+
* [FEATURE] `position` option (thanks to Jeffrey Dean Altemus).
1619

1720
# 0.3.0 / 2013-05-20
1821

19-
* [CHANGE] Renamed project from "jquery-contextmenu" to "jquery-ui-contextmenu"
20-
* [FEATURE] Support `taphold` events
21-
* [FEATURE] Show / hide effects configurable
22-
* [BUGFIX] Fixed markup for disabled entries
22+
* [CHANGE] Renamed project from "jquery-contextmenu" to "jquery-ui-contextmenu".
23+
* [FEATURE] Support `taphold` events.
24+
* [FEATURE] Show / hide effects configurable.
25+
* [BUGFIX] Fixed markup for disabled entries.

Gruntfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ module.exports = function (grunt) {
2424
jshint: {
2525
files: [
2626
"Gruntfile.js",
27-
"jquery.ui-contextmenu.js"
27+
"jquery.ui-contextmenu.js",
28+
"test/tests.js"
2829
],
2930
options: {
3031
jshintrc: ".jshintrc"

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ A jQuery plugin that provides a context menu (based on the standard [jQueryUI me
1313

1414
## Status
1515

16-
Beta. Please report issues.
17-
18-
Latest release is available for download at
16+
The latest release is available for download at
1917
[The jQuery Plugin Registry](http://plugins.jquery.com/ui-contextmenu/).
18+
2019
See also the [Change Log](https://github.com/mar10/jquery-ui-contextmenu/blob/master/CHANGELOG.md).
2120

2221

@@ -177,6 +176,10 @@ structure (see [jQueryUI menu] for details):
177176
Enable or disable the entry. `flag` defaults to `true`<br>
178177
Call like <code>$(...).contextmenu("enableEntry", "paste", false);</code>.
179178
</dd>
179+
<dt>getMenu()</dt>
180+
<dd>
181+
Return the jQuery object for the menu's <code>UL</code> element.
182+
</dd>
180183
<dt>open(target)</dt>
181184
<dd>
182185
Open context menu on a specific target (target must match the options.delegate filter).<br>
@@ -233,23 +236,23 @@ $("#container").bind("contextmenuselect", function(event, ui) {
233236
</dd>
234237
<dt>blur(event, ui)</dt>
235238
<dd>
236-
Triggered when the menu loses focus.
239+
Triggered when the menu loses focus (original jQuery UI Menu event).
237240
</dd>
238241
<dt>close(event)</dt>
239242
<dd>
240243
Triggered when the menu is closed.
241244
</dd>
242245
<dt>create(event, ui)</dt>
243246
<dd>
244-
Triggered when the menu is created.
247+
Triggered when the contextmenu widget is created.
245248
</dd>
246-
<dt>focus(event, ui)</dt>
249+
<dt>createMenu(event, ui)</dt>
247250
<dd>
248-
Triggered when a menu gains focus or when any menu item is activated.
251+
Triggered when the popup menu is created (original jQuery UI Menu `create` event).
249252
</dd>
250-
<dt>init(event)</dt>
253+
<dt>focus(event, ui)</dt>
251254
<dd>
252-
Triggered when the contextmenu widget is initialized.
255+
Triggered when a menu gains focus or when any menu item is activated (original jQuery UI Menu event).
253256
</dd>
254257
<dt>open(event)</dt>
255258
<dd>

demo/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@
123123
focus: function(event, ui) {
124124
var menuId = ui.item.find(">a").attr("href");
125125
$("#info").text("focus " + menuId);
126+
console.log("focus", ui.item);
126127
},
127128
blur: function(event, ui) {
128129
$("#info").text("");
130+
console.log("blur", ui.item);
129131
},
130132
beforeOpen: function(event, ui) {
131133
// $("#container").contextmenu("replaceMenu", "#options2");

jquery.ui-contextmenu.js

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818

1919
$.widget("moogle.contextmenu", {
20-
version: "0.6.0",
20+
version: "1.0.0",
2121
options: {
22-
delegate: "[data-menu]", // selector
22+
delegate: null, // selector
2323
hide: { effect: "fadeOut", duration: "fast"},
2424
ignoreParentSelect: true, // Don't trigger 'select' for sub-menu parents
2525
menu: null, // selector or jQuery pointing to <UL>, or a definition hash
@@ -28,17 +28,19 @@
2828
show: { effect: "slideDown", duration: "fast"},
2929
taphold: false, // open menu on taphold events (requires external plugins)
3030
// Events:
31-
beforeOpen: $.noop, // menu about to open; return `false` to prevent opening
32-
blur: $.noop, // menu option lost focus
33-
close: $.noop, // menu was closed
34-
create: $.noop, // menu was initialized
35-
focus: $.noop, // menu option got focus
36-
init: $.noop, // ui-contextmenu was initialized
37-
open: $.noop, // menu was opened
38-
select: $.noop // menu option was selected; return `false` to prevent closing
31+
beforeOpen: $.noop, // menu about to open; return `false` to prevent opening
32+
blur: $.noop, // menu option lost focus
33+
close: $.noop, // menu was closed
34+
create: $.noop, // menu was initialized
35+
createMenu: $.noop, // menu was initialized (original UI Menu)
36+
focus: $.noop, // menu option got focus
37+
// init: $.noop, // ui-contextmenu was initialized
38+
open: $.noop, // menu was opened
39+
select: $.noop // menu option was selected; return `false` to prevent closing
3940
},
40-
/** Construtcor */
41+
/** Constructor */
4142
_create: function () {
43+
console.log("_create()");
4244
var eventNames, targetId,
4345
opts = this.options;
4446

@@ -53,7 +55,7 @@
5355
// If the contextmenu was bound to `document`, we apply the
5456
// selector relative to the <body> tag instead
5557
targetId = ($(this.element).is(document) ? $("body") : this.element).uniqueId().attr("id");
56-
this.$headStyle = $("<style class='ui-contextmenu-style'>")
58+
this.$headStyle = $("<style class='moogle-contextmenu-style'>")
5759
.prop("type", "text/css")
5860
.html("#" + targetId + " " + opts.delegate + " { " +
5961
"-webkit-user-select: none; " +
@@ -78,10 +80,17 @@
7880
}
7981
this.element.delegate(opts.delegate, eventNames, $.proxy(this._openMenu, this));
8082

81-
this._trigger("init");
83+
console.log("_create() - delegate");
84+
85+
// this._trigger("init");
8286
},
8387
/** Destructor, called on $().contextmenu("destroy"). */
84-
_destroy: function(key, value){
88+
_destroy: function(){
89+
console.log("_destroy() undelegate NS menu=", this.$menu);
90+
this.element.undelegate(this.ns);
91+
if(this.isOpen()){
92+
this._closeMenu();
93+
}
8594
this._createUiMenu(null);
8695
if(this.$headStyle){
8796
this.$headStyle.remove();
@@ -91,10 +100,13 @@
91100
/** (Re)Create jQuery UI Menu. */
92101
_createUiMenu: function(menuDef){
93102
// Remove temporary <ul> if any
103+
console.log("_createUiMenu()", menuDef);
94104
if(this.menuIsTemp){
105+
console.log("_createUiMenu - remove ", this.$menu);
95106
this.$menu.remove(); // this will also destroy ui.menu
96107
this.menuIsTemp = false;
97108
} else if(this.$menu){
109+
console.log("_createUiMenu - destroy ", this.$menu);
98110
this.$menu.menu("destroy").hide();
99111
}
100112
// If a menu definition array was passed, create a hidden <ul>
@@ -113,13 +125,14 @@
113125
// Create - but hide - the jQuery UI Menu widget
114126
this.$menu
115127
.hide()
116-
.addClass("ui-contextmenu")
128+
// .addClass("moogle-contextmenu")
117129
// Create a menu instance that delegates events to our widget
118130
.menu({
119131
blur: $.proxy(this.options.blur, this),
120-
create: $.proxy(this.options.create, this),
132+
create: $.proxy(this.options.createMenu, this),
121133
focus: $.proxy(this.options.focus, this),
122134
select: $.proxy(function(event, ui){
135+
// window.console.log("select proxy");
123136
var isParent = (ui.item.has(">a[aria-haspopup='true']").length > 0);
124137
ui.cmd = normCommand(ui.item.find(">a").attr("href"));
125138
ui.target = $(this.currentTarget);
@@ -135,6 +148,7 @@
135148
},
136149
/** Open popup (called on 'contextmenu' event). */
137150
_openMenu: function(event){
151+
console.log("_openMenu()");
138152
var opts = this.options,
139153
posOption = opts.position,
140154
self = this,
@@ -148,6 +162,7 @@
148162
}
149163
ui.menu = this.$menu; // Might have changed in beforeOpen
150164
// Register global event handlers that close the dropdown-menu
165+
console.log("_openMenu() - document.bind: " + this.ns, this.menuIsTemp, this.$menu);
151166
$(document).bind("keydown" + this.ns, function(event){
152167
if( event.which === $.ui.keyCode.ESCAPE ){
153168
self._closeMenu();
@@ -189,15 +204,16 @@
189204
_closeMenu: function(){
190205
var self = this;
191206

192-
this._hide(this.$menu, this.options.hide, function() {
207+
console.log("_closeMenu() - document.unbind: " + this.ns, this.menuIsTemp, this.$menu);
208+
$(document)
209+
.unbind("mousedown" + this.ns)
210+
.unbind("touchstart" + this.ns)
211+
.unbind("keydown" + this.ns);
212+
213+
this._hide(this.$menu, this.options.hide, function() {
193214
self._trigger("close");
194-
this.currentTarget = null;
215+
self.currentTarget = null;
195216
});
196-
197-
$(document)
198-
.unbind("mousedown" + this.ns)
199-
.unbind("touchstart" + this.ns)
200-
.unbind("keydown" + this.ns);
201217
},
202218
/** Handle $().contextmenu("option", key, value) calls. */
203219
_setOption: function(key, value){
@@ -213,6 +229,15 @@
213229
var $entry = this.$menu.find("li a[href=#" + normCommand(cmd) + "]");
214230
return wantLi ? $entry.closest("li") : $entry;
215231
},
232+
/** Return Menu element (UL). */
233+
getMenu: function(){
234+
return this.$menu;
235+
},
236+
/** Return true if menu is open. */
237+
isOpen: function(){
238+
// return this.$menu && this.$menu.is(":visible");
239+
return !!this.$menu && !!this.currentTarget;
240+
},
216241
/** Open context menu on a specific target (must match options.delegate) */
217242
open: function(target){
218243
// Fake a 'contextmenu' event

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ui-contextmenu",
3-
"version": "0.6.0",
3+
"version": "1.0.0",
44

55
"title": "jQuery UI context menu plugin",
66
"description": "Turn a jQuery UI Menu widget into a context menu.",

test/index.html

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,31 @@
88
<script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
99
<script src="http://code.jquery.com/qunit/qunit-1.10.0.js"></script>
1010
<script src="../jquery.ui-contextmenu.js"></script>
11-
<script src="tests.js"></script>
11+
<script src="tests2.js"></script>
1212
</head>
1313
<body>
1414
<h1 id="qunit-header">jquery.ui-contextmenu Test Suite</h1>
1515
<h2 id="qunit-banner"></h2>
1616
<div id="qunit-testrunner-toolbar"></div>
1717
<h2 id="qunit-userAgent"></h2>
1818
<ol id="qunit-tests"></ol>
19+
<hr>
20+
<h3>Sample Markup</h3>
21+
<div id="container">
22+
<span class="hasmenu">AAA</span>
23+
<span class="hasmenu">BBB</span>
24+
<span class="hasmenu">CCC</span>
25+
</div>
26+
<ul id="sampleMenu1" style="display: none;">
27+
<li><a href="#cut"><span class="ui-icon ui-icon-scissors"></span>Cut</a>
28+
<li><a href="#copy"><span class="ui-icon ui-icon-copy"></span>Copy</a>
29+
<li class="ui-state-disabled ui-icon-clipboard"><a href="#paste">Paste</a>
30+
<li>----
31+
<li><a>More</a>
32+
<ul>
33+
<li><a href="#sub1">Sub Item 1</a>
34+
<li><a href="#sub2">Sub Item 2</a>
35+
</ul>
36+
</ul>
1937
</body>
2038
</html>

0 commit comments

Comments
 (0)