forked from arnklint/jquery-contextMenu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.contextMenu.js
110 lines (94 loc) · 3.1 KB
/
jquery.contextMenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* jQuery.contextMenu - Show a custom context when right clicking something
* Jonas Arnklint, http://github.com/arnklint/jquery-contextMenu
* Released into the public domain
* Date: Jan 14, 2011
* @author Jonas Arnklint
* @version 1.7
*
*/
// Making a local '$' alias of jQuery to support jQuery.noConflict
(function($) {
jQuery.fn.contextMenu = function ( name, actions, options ) {
var me = this,
win = $(window),
menu = $('<ul id="'+name+'" class="context-menu"></ul>').hide().appendTo('body'),
activeElement = null, // last clicked element that responds with contextMenu
hideMenu = function() {
$('.context-menu:visible').each(function() {
$(this).trigger("closed");
$(this).hide();
$('body').unbind('click', hideMenu);
});
},
default_options = {
disable_native_context_menu: false, // disables the native contextmenu everywhere you click
leftClick: false // show menu on left mouse click instead of right
},
options = $.extend(default_options, options);
$(document).bind('contextmenu', function(e) {
if (options.disable_native_context_menu) {
e.preventDefault();
}
hideMenu();
});
$.each(actions, function(me, itemOptions) {
if (itemOptions.link) {
var link = itemOptions.link;
} else {
var link = '<a href="#">'+me+'</a>';
}
var menuItem = $('<li>' + link + '</li>');
if (itemOptions.klass) {
menuItem.attr("class", itemOptions.klass);
}
menuItem.appendTo(menu).bind('click', function(e) {
itemOptions.click(activeElement);
e.preventDefault();
});
});
if (options.leftClick) {
var mouseEvent = 'click';
} else {
var mouseEvent = 'contextmenu';
}
var mouseEventFunc = function(e){
// Hide any existing context menus
hideMenu();
activeElement = $(this); // set clicked element
if (options.showMenu) {
options.showMenu.call(menu, activeElement);
}
// Bind to the closed event if there is a hideMenu handler specified
if (options.hideMenu) {
menu.bind("closed", function() {
options.hideMenu.call(menu, activeElement);
});
}
menu.css({
visibility: 'hidden',
position: 'absolute',
zIndex: 1000
});
// include margin so it can be used to offset from page border.
var mWidth = menu.outerWidth(true),
mHeight = menu.outerHeight(true),
xPos = ((e.pageX - win.scrollLeft()) + mWidth < win.width()) ? e.pageX : e.pageX - mWidth,
yPos = ((e.pageY - win.scrollTop()) + mHeight < win.height()) ? e.pageY : e.pageY - mHeight;
menu.show(0, function() {
$('body').bind('click', hideMenu);
}).css({
visibility: 'visible',
top: yPos + 'px',
left: xPos + 'px',
zIndex: 1000
});
return false;
}
if (options.delegateEventTo) {
return me.on(mouseEvent, options.delegateEventTo, mouseEventFunc)
} else {
return me.bind(mouseEvent, mouseEventFunc);
}
}
})(jQuery);