Skip to content

Instantly share code, notes, and snippets.

@alokito
Created September 16, 2011 14:51
Show Gist options
  • Save alokito/1222275 to your computer and use it in GitHub Desktop.
Save alokito/1222275 to your computer and use it in GitHub Desktop.
patch to jquery.contextMenu.js from jQuery Context Menu Plugin v1.01
The attached "jquery.contextMenu.js.diff" is a patch for two improvements to the jQuery Context Menu Plugin
1) instead of requiring an id for the menu div, allow the plugin to accept either the
id string or a jquery object. I have been dynamically adding menus, and it is annoying
to have to make up ids. The code change is as simple as adding the followin to the contextMenu function:
if (o.menu.substr)
o.menu = $('#' + o.menu);
and then going through the rest of the code and making sure that o.menu is used as a
jquery object instead of an id string. This was easy to do, although I ended up
changing a lot of lines.
2) With safari 5 on a macbook, control-click gets mapped to e.button == 1 && e.ctrlKey,
rather than e.button == 2. To make this plugin work with safari 5 on a macbook, I ended
up abstracting out a "isRightClick" function that checks for both these conditions,
currently
"e.button == 2 || ($.browser.webkit && e.ctrlKey)"
@@ -13,6 +13,9 @@
// and the MIT License and is copyright A Beautiful Site, LLC.
//
if(jQuery)( function() {
+ function isRightClick(e) {^M
+ return (e.button == 2 || ($.browser.webkit && e.ctrlKey)) ^M
+ }^M
$.extend($.fn, {
contextMenu: function(o, callback) {
@@ -23,25 +26,28 @@ if(jQuery)( function() {
// 0 needs to be -1 for expected results (no fade)
if( o.inSpeed == 0 ) o.inSpeed = -1;
if( o.outSpeed == 0 ) o.outSpeed = -1;
+ // replace ID with jquery object.^M
+ if (o.menu.substr)^M
+ o.menu = $('#' + o.menu);^M
// Loop each context menu
$(this).each( function() {
var el = $(this);
var offset = $(el).offset();
// Add contextMenu class
- $('#' + o.menu).addClass('contextMenu');
+ o.menu.addClass('contextMenu');^M
// Simulate a true right click
$(this).mousedown( function(e) {
var evt = e;
+ if (isRightClick(evt)) {^M
evt.stopPropagation();
$(this).mouseup( function(e) {
- e.stopPropagation();
- var srcElement = $(this);
- $(this).unbind('mouseup');
- if( evt.button == 2 ) {
+ e.stopPropagation();^M
+ var srcElement = $(this);^M
+ $(this).unbind('mouseup');^M
// Hide context menus that may be showing
$(".contextMenu").hide();
// Get this context menu
- var menu = $('#' + o.menu);
+ var menu = o.menu;^M
if( $(el).hasClass('disabled') ) return false;
@@ -69,12 +75,12 @@ if(jQuery)( function() {
// Show the menu
$(document).unbind('click');
- $(menu).css({ top: y, left: x }).fadeIn(o.inSpeed);
+ menu.css({ top: y, left: x }).fadeIn(o.inSpeed);^M
// Hover events
- $(menu).find('A').mouseover( function() {
+ menu.find('A').hover( function() {^M
$(menu).find('LI.hover').removeClass('hover');
$(this).parent().addClass('hover');
- }).mouseout( function() {
+ }, function() {^M
$(menu).find('LI.hover').removeClass('hover');
});
@@ -82,23 +88,23 @@ if(jQuery)( function() {
$(document).keypress( function(e) {
switch( e.keyCode ) {
case 38: // up
- if( $(menu).find('LI.hover').size() == 0 ) {
- $(menu).find('LI:last').addClass('hover');
+ if( menu.find('LI.hover').size() == 0 ) {^M
+ menu.find('LI:last').addClass('hover');^M
} else {
- $(menu).find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
- if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:last').addClass('hover');
+ menu.find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');^M
+ if( menu.find('LI.hover').size() == 0 ) menu.find('LI:last').addClass('hover');^M
}
break;
case 40: // down
- if( $(menu).find('LI.hover').size() == 0 ) {
- $(menu).find('LI:first').addClass('hover');
+ if( menu.find('LI.hover').size() == 0 ) {^M
+ menu.find('LI:first').addClass('hover');^M
} else {
- $(menu).find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
- if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:first').addClass('hover');
+ menu.find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');^M
+ if( menu.find('LI.hover').size() == 0 ) menu.find('LI:first').addClass('hover');^M
}
break;
case 13: // enter
- $(menu).find('LI.hover A').trigger('click');
+ menu.find('LI.hover A').trigger('click');^M
break;
case 27: // esc
$(document).trigger('click');
@@ -107,8 +113,8 @@ if(jQuery)( function() {
});
// When items are selected
- $('#' + o.menu).find('A').unbind('click');
- $('#' + o.menu).find('LI:not(.disabled) A').click( function() {
+ menu.find('A').unbind('click');^M
+ menu.find('LI:not(.disabled) A').click( function() {^M
$(document).unbind('click').unbind('keypress');
$(".contextMenu").hide();
// Callback
@@ -120,21 +126,21 @@ if(jQuery)( function() {
setTimeout( function() { // Delay for Mozilla
$(document).click( function() {
$(document).unbind('click').unbind('keypress');
- $(menu).fadeOut(o.outSpeed);
+ menu.fadeOut(o.outSpeed);^M
return false;
});
}, 0);
- }
});
+ }^M
});
// Disable text selection
if( $.browser.mozilla ) {
- $('#' + o.menu).each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
+ o.menu.each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });^M
} else if( $.browser.msie ) {
- $('#' + o.menu).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
+ o.menu.each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });^M
} else {
- $('#' + o.menu).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
+ o.menu.each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });^M
}
// Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
$(el).add($('UL.contextMenu')).bind('contextmenu', function() { return false; });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment