1 // jQuery Context Menu Plugin
6 // A Beautiful Site (http://abeautifulsite.net/)
8 // Visit http://abeautifulsite.net/notebook/80 for usage and more information
12 // This software is licensed under a Creative Commons License and is copyrighted
13 // (C)2008 by Cory S.N. LaViska.
15 // For details, visit http://creativecommons.org/licenses/by/3.0/us/
20 contextMenu: function(o
, callback
) {
22 if( o
.menu
== undefined ) return false;
23 if( o
.inSpeed
== undefined ) o
.inSpeed
= 150;
24 if( o
.outSpeed
== undefined ) o
.outSpeed
= 75;
25 // 0 needs to be -1 for expected results (no fade)
26 if( o
.inSpeed
== 0 ) o
.inSpeed
= -1;
27 if( o
.outSpeed
== 0 ) o
.outSpeed
= -1;
28 // Loop each context menu
29 $(this).each( function() {
31 var offset
= $(el
).offset();
32 // Add contextMenu class
33 $('#' + o
.menu
).addClass('contextMenu');
34 // Simulate a true right click
35 $(this).mousedown( function(e
) {
37 $(this).mouseup( function(e
) {
38 var srcElement
= $(this);
39 $(this).unbind('mouseup');
40 if( evt
.button
== 2 ) {
41 // Hide context menus that may be showing
42 $(".contextMenu").hide();
43 // Get this context menu
44 var menu
= $('#' + o
.menu
);
46 if( $(el
).hasClass('disabled') ) return false;
48 // Detect mouse position
50 if( self
.innerHeight
) {
51 d
.pageYOffset
= self
.pageYOffset
;
52 d
.pageXOffset
= self
.pageXOffset
;
53 d
.innerHeight
= self
.innerHeight
;
54 d
.innerWidth
= self
.innerWidth
;
55 } else if( document
.documentElement
&&
56 document
.documentElement
.clientHeight
) {
57 d
.pageYOffset
= document
.documentElement
.scrollTop
;
58 d
.pageXOffset
= document
.documentElement
.scrollLeft
;
59 d
.innerHeight
= document
.documentElement
.clientHeight
;
60 d
.innerWidth
= document
.documentElement
.clientWidth
;
61 } else if( document
.body
) {
62 d
.pageYOffset
= document
.body
.scrollTop
;
63 d
.pageXOffset
= document
.body
.scrollLeft
;
64 d
.innerHeight
= document
.body
.clientHeight
;
65 d
.innerWidth
= document
.body
.clientWidth
;
67 (e
.pageX
) ? x
= e
.pageX
: x
= e
.clientX
+ d
.scrollLeft
;
68 (e
.pageY
) ? y
= e
.pageY
: x
= e
.clientY
+ d
.scrollTop
;
71 $(document
).unbind('click');
72 $(menu
).css({ top
: y
, left
: x
}).fadeIn(o
.inSpeed
);
74 $(menu
).find('A').mouseover( function() {
75 $(menu
).find('LI.hover').removeClass('hover');
76 $(this).parent().addClass('hover');
77 }).mouseout( function() {
78 $(menu
).find('LI.hover').removeClass('hover');
82 $(document
).keypress( function(e
) {
85 if( $(menu
).find('LI.hover').size() == 0 ) {
86 $(menu
).find('LI:last').addClass('hover');
88 $(menu
).find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
89 if( $(menu
).find('LI.hover').size() == 0 ) $(menu
).find('LI:last').addClass('hover');
93 if( $(menu
).find('LI.hover').size() == 0 ) {
94 $(menu
).find('LI:first').addClass('hover');
96 $(menu
).find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
97 if( $(menu
).find('LI.hover').size() == 0 ) $(menu
).find('LI:first').addClass('hover');
101 $(menu
).find('LI.hover A').trigger('click');
104 $(document
).trigger('click');
109 // When items are selected
110 $('#' + o
.menu
).find('A').unbind('click');
111 $('#' + o
.menu
).find('LI:not(.disabled) A').click( function() {
112 $(document
).unbind('click').unbind('keypress');
113 $(".contextMenu").hide();
115 if( callback
) callback( $(this).attr('href').substr(1), $(srcElement
), {x
: x
- offset
.left
, y
: y
- offset
.top
, docX
: x
, docY
: y
} );
120 setTimeout( function() { // Delay for Mozilla
121 $(document
).click( function() {
122 $(document
).unbind('click').unbind('keypress');
123 $(menu
).fadeOut(o
.outSpeed
);
131 // Disable text selection
132 if( $.browser
.mozilla
) {
133 $('#' + o
.menu
).each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
134 } else if( $.browser
.msie
) {
135 $('#' + o
.menu
).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
137 $('#' + o
.menu
).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
139 // Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
140 $(el
).add('UL.contextMenu').bind('contextmenu', function() { return false; });
146 // Disable context menu items on the fly
147 disableContextMenuItems: function(o
) {
148 if( o
== undefined ) {
150 $(this).find('LI').addClass('disabled');
153 $(this).each( function() {
154 if( o
!= undefined ) {
155 var d
= o
.split(',');
156 for( var i
= 0; i
< d
.length
; i
++ ) {
157 $(this).find('A[href="' + d
[i
] + '"]').parent().addClass('disabled');
165 // Enable context menu items on the fly
166 enableContextMenuItems: function(o
) {
167 if( o
== undefined ) {
169 $(this).find('LI.disabled').removeClass('disabled');
172 $(this).each( function() {
173 if( o
!= undefined ) {
174 var d
= o
.split(',');
175 for( var i
= 0; i
< d
.length
; i
++ ) {
176 $(this).find('A[href="' + d
[i
] + '"]').parent().removeClass('disabled');
184 // Disable context menu(s)
185 disableContextMenu: function() {
186 $(this).each( function() {
187 $(this).addClass('disabled');
192 // Enable context menu(s)
193 enableContextMenu: function() {
194 $(this).each( function() {
195 $(this).removeClass('disabled');
200 // Destroy context menu(s)
201 destroyContextMenu: function() {
202 // Destroy specified context menus
203 $(this).each( function() {
205 $(this).unbind('mousedown').unbind('mouseup');