Skip to content

Commit e738733

Browse files
committed
Fix for destroying specific contextmenu, added unit test. Fixes #397
1 parent 98012af commit e738733

5 files changed

+49
-12
lines changed

dist/jquery.contextMenu.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* MIT License http://www.opensource.org/licenses/mit-license
1313
* GPL v3 http://opensource.org/licenses/GPL-3.0
1414
*
15-
* Date: 2016-07-17T19:45:35.567Z
15+
* Date: 2016-08-26T11:25:39.329Z
1616
*/
1717

1818
(function (factory) {
@@ -1469,6 +1469,7 @@
14691469

14701470
// manage contextMenu instances
14711471
$.contextMenu = function (operation, options) {
1472+
14721473
if (typeof operation !== 'string') {
14731474
options = operation;
14741475
operation = 'create';
@@ -1492,7 +1493,7 @@
14921493
// you never know what they throw at you...
14931494
$context = $(o.context).first();
14941495
o.context = $context.get(0);
1495-
_hasContext = o.context !== document;
1496+
_hasContext = !$(o.context).is(document);
14961497
}
14971498

14981499
switch (operation) {
@@ -1591,7 +1592,9 @@
15911592
// get proper options
15921593
var context = o.context;
15931594
$.each(menus, function (ns, o) {
1594-
if (o.context !== context) {
1595+
1596+
// Is this menu equest to the context called from
1597+
if (!$(context).is(o.selector)) {
15951598
return true;
15961599
}
15971600

dist/jquery.contextMenu.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/jquery.contextMenu.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/jquery.contextMenu.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,7 @@
14691469

14701470
// manage contextMenu instances
14711471
$.contextMenu = function (operation, options) {
1472+
14721473
if (typeof operation !== 'string') {
14731474
options = operation;
14741475
operation = 'create';
@@ -1492,7 +1493,7 @@
14921493
// you never know what they throw at you...
14931494
$context = $(o.context).first();
14941495
o.context = $context.get(0);
1495-
_hasContext = o.context !== document;
1496+
_hasContext = !$(o.context).is(document);
14961497
}
14971498

14981499
switch (operation) {
@@ -1591,7 +1592,9 @@
15911592
// get proper options
15921593
var context = o.context;
15931594
$.each(menus, function (ns, o) {
1594-
if (o.context !== context) {
1595+
1596+
// Is this menu equest to the context called from
1597+
if (!$(context).is(o.selector)) {
15951598
return true;
15961599
}
15971600

test/unit/test-events.js

+35-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ var menuRuntime = null;
66
function testQUnit(name, itemClickEvent, triggerEvent) {
77
QUnit.module(name);
88
// before each test
9-
function createContextMenu(items) {
9+
function createContextMenu(items, classname) {
10+
if(typeof(classname) == 'undefined'){
11+
classname = 'context-menu';
12+
}
1013
var $fixture = $('#qunit-fixture');
1114

1215
// ensure `#qunit-fixture` exists when testing with karma runner
@@ -15,7 +18,7 @@ function testQUnit(name, itemClickEvent, triggerEvent) {
1518
$fixture = $('#qunit-fixture');
1619
}
1720

18-
$fixture.append("<div class='context-menu'>right click me!</div>");
21+
$fixture.append("<div class='" + classname + "'>right click me!</div>");
1922

2023
if(!items){
2124
items = {
@@ -25,7 +28,7 @@ function testQUnit(name, itemClickEvent, triggerEvent) {
2528
}
2629

2730
$.contextMenu({
28-
selector: '.context-menu',
31+
selector: '.' + classname,
2932
events: {
3033
show: function(opt) {
3134
menuRuntime = opt;
@@ -130,6 +133,34 @@ function testQUnit(name, itemClickEvent, triggerEvent) {
130133
});
131134

132135

136+
QUnit.test('do not open context menu with no visible items', function(assert) {
137+
createContextMenu({
138+
copy: {name: 'Copy', icon: 'copy'},
139+
paste: {name: 'Paste', icon: 'paste'}
140+
});
141+
createContextMenu({
142+
copy: {name: 'Copy', icon: 'copy'},
143+
paste: {name: 'Paste', icon: 'paste'}
144+
}, 'context-menu-two');
145+
146+
$(".context-menu").contextMenu();
147+
$(".context-menu").contextMenu('hide');
148+
$(".context-menu-two").contextMenu();
149+
$(".context-menu-two").contextMenu('hide');
150+
151+
assert.equal(menuOpenCounter, 2, 'contextMenu was opened twice');
152+
153+
$(".context-menu-two").contextMenu('destroy');
154+
155+
$(".context-menu").contextMenu();
156+
$(".context-menu").contextMenu('hide');
157+
$(".context-menu-two").contextMenu();
158+
$(".context-menu-two").contextMenu('hide');
159+
160+
assert.equal(menuOpenCounter, 3, 'destroyed contextMenu was not opened');
161+
162+
destroyContextMenuAndCleanup();
163+
});
133164

134165
QUnit.test('do not open context menu with no visible items', function(assert) {
135166
createContextMenu({
@@ -191,4 +222,4 @@ function testQUnit(name, itemClickEvent, triggerEvent) {
191222
};
192223

193224
testQUnit('contextMenu events', '', 'mouseup')
194-
testQUnit('contextMenu events - click handler', 'click', 'click')
225+
testQUnit('contextMenu events - click handler', 'click', 'click')

0 commit comments

Comments
 (0)