Skip to content

Commit

Permalink
Fix for destroying specific contextmenu, added unit test. Fixes #397
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrala committed Aug 26, 2016
1 parent 98012af commit e738733
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
9 changes: 6 additions & 3 deletions dist/jquery.contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* MIT License http://www.opensource.org/licenses/mit-license
* GPL v3 http://opensource.org/licenses/GPL-3.0
*
* Date: 2016-07-17T19:45:35.567Z
* Date: 2016-08-26T11:25:39.329Z
*/

(function (factory) {
Expand Down Expand Up @@ -1469,6 +1469,7 @@

// manage contextMenu instances
$.contextMenu = function (operation, options) {

if (typeof operation !== 'string') {
options = operation;
operation = 'create';
Expand All @@ -1492,7 +1493,7 @@
// you never know what they throw at you...
$context = $(o.context).first();
o.context = $context.get(0);
_hasContext = o.context !== document;
_hasContext = !$(o.context).is(document);
}

switch (operation) {
Expand Down Expand Up @@ -1591,7 +1592,9 @@
// get proper options
var context = o.context;
$.each(menus, function (ns, o) {
if (o.context !== context) {

// Is this menu equest to the context called from
if (!$(context).is(o.selector)) {
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.contextMenu.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/jquery.contextMenu.min.js.map

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/jquery.contextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,7 @@

// manage contextMenu instances
$.contextMenu = function (operation, options) {

if (typeof operation !== 'string') {
options = operation;
operation = 'create';
Expand All @@ -1492,7 +1493,7 @@
// you never know what they throw at you...
$context = $(o.context).first();
o.context = $context.get(0);
_hasContext = o.context !== document;
_hasContext = !$(o.context).is(document);
}

switch (operation) {
Expand Down Expand Up @@ -1591,7 +1592,9 @@
// get proper options
var context = o.context;
$.each(menus, function (ns, o) {
if (o.context !== context) {

// Is this menu equest to the context called from
if (!$(context).is(o.selector)) {
return true;
}

Expand Down
39 changes: 35 additions & 4 deletions test/unit/test-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ var menuRuntime = null;
function testQUnit(name, itemClickEvent, triggerEvent) {
QUnit.module(name);
// before each test
function createContextMenu(items) {
function createContextMenu(items, classname) {
if(typeof(classname) == 'undefined'){
classname = 'context-menu';
}
var $fixture = $('#qunit-fixture');

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

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

if(!items){
items = {
Expand All @@ -25,7 +28,7 @@ function testQUnit(name, itemClickEvent, triggerEvent) {
}

$.contextMenu({
selector: '.context-menu',
selector: '.' + classname,
events: {
show: function(opt) {
menuRuntime = opt;
Expand Down Expand Up @@ -130,6 +133,34 @@ function testQUnit(name, itemClickEvent, triggerEvent) {
});


QUnit.test('do not open context menu with no visible items', function(assert) {
createContextMenu({
copy: {name: 'Copy', icon: 'copy'},
paste: {name: 'Paste', icon: 'paste'}
});
createContextMenu({
copy: {name: 'Copy', icon: 'copy'},
paste: {name: 'Paste', icon: 'paste'}
}, 'context-menu-two');

$(".context-menu").contextMenu();
$(".context-menu").contextMenu('hide');
$(".context-menu-two").contextMenu();
$(".context-menu-two").contextMenu('hide');

assert.equal(menuOpenCounter, 2, 'contextMenu was opened twice');

$(".context-menu-two").contextMenu('destroy');

$(".context-menu").contextMenu();
$(".context-menu").contextMenu('hide');
$(".context-menu-two").contextMenu();
$(".context-menu-two").contextMenu('hide');

assert.equal(menuOpenCounter, 3, 'destroyed contextMenu was not opened');

destroyContextMenuAndCleanup();
});

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

testQUnit('contextMenu events', '', 'mouseup')
testQUnit('contextMenu events - click handler', 'click', 'click')
testQUnit('contextMenu events - click handler', 'click', 'click')

0 comments on commit e738733

Please sign in to comment.