1 | // jQuery Context Menu Plugin
|
---|
2 | //
|
---|
3 | // Version 1.01
|
---|
4 | //
|
---|
5 | // Cory S.N. LaViska
|
---|
6 | // A Beautiful Site (http://abeautifulsite.net/)
|
---|
7 | //
|
---|
8 | // More info: http://abeautifulsite.net/2008/09/jquery-context-menu-plugin/
|
---|
9 | //
|
---|
10 | // Terms of Use
|
---|
11 | //
|
---|
12 | // This plugin is dual-licensed under the GNU General Public License
|
---|
13 | // and the MIT License and is copyright A Beautiful Site, LLC.
|
---|
14 | //
|
---|
15 | if(jQuery)( function() {
|
---|
16 | $.extend($.fn, {
|
---|
17 |
|
---|
18 | contextMenu: function(o, callback) {
|
---|
19 | // Defaults
|
---|
20 | if( o.menu == undefined ) return false;
|
---|
21 | if( o.inSpeed == undefined ) o.inSpeed = 150;
|
---|
22 | if( o.outSpeed == undefined ) o.outSpeed = 75;
|
---|
23 | // 0 needs to be -1 for expected results (no fade)
|
---|
24 | if( o.inSpeed == 0 ) o.inSpeed = -1;
|
---|
25 | if( o.outSpeed == 0 ) o.outSpeed = -1;
|
---|
26 |
|
---|
27 | if( o.disabledItems == undefined ) o.disabledItems = null;
|
---|
28 |
|
---|
29 | // Loop each context menu
|
---|
30 | $(this).each( function() {
|
---|
31 | var el = $(this);
|
---|
32 | var offset = $(el).offset();
|
---|
33 | // Add contextMenu class
|
---|
34 | $('#' + o.menu).addClass('contextMenu');
|
---|
35 | // Simulate a true right click
|
---|
36 | $(this).mousedown( function(e) {
|
---|
37 | var evt = e;
|
---|
38 | evt.stopPropagation();
|
---|
39 | $(this).mouseup( function(e) {
|
---|
40 | e.stopPropagation();
|
---|
41 | var srcElement = $(this);
|
---|
42 | $(this).unbind('mouseup');
|
---|
43 | if( evt.button == 2 ) {
|
---|
44 | // Hide context menus that may be showing
|
---|
45 | $(".contextMenu").hide();
|
---|
46 | // Get this context menu
|
---|
47 | var menu = $('#' + o.menu);
|
---|
48 |
|
---|
49 | if( $(el).hasClass('disabled') ) return false;
|
---|
50 |
|
---|
51 | // Detect mouse position
|
---|
52 | var d = {}, x, y;
|
---|
53 | if( self.innerHeight ) {
|
---|
54 | d.pageYOffset = self.pageYOffset;
|
---|
55 | d.pageXOffset = self.pageXOffset;
|
---|
56 | d.innerHeight = self.innerHeight;
|
---|
57 | d.innerWidth = self.innerWidth;
|
---|
58 | } else if( document.documentElement &&
|
---|
59 | document.documentElement.clientHeight ) {
|
---|
60 | d.pageYOffset = document.documentElement.scrollTop;
|
---|
61 | d.pageXOffset = document.documentElement.scrollLeft;
|
---|
62 | d.innerHeight = document.documentElement.clientHeight;
|
---|
63 | d.innerWidth = document.documentElement.clientWidth;
|
---|
64 | } else if( document.body ) {
|
---|
65 | d.pageYOffset = document.body.scrollTop;
|
---|
66 | d.pageXOffset = document.body.scrollLeft;
|
---|
67 | d.innerHeight = document.body.clientHeight;
|
---|
68 | d.innerWidth = document.body.clientWidth;
|
---|
69 | }
|
---|
70 | (e.pageX) ? x = e.pageX : x = e.clientX + d.scrollLeft;
|
---|
71 | (e.pageY) ? y = e.pageY : y = e.clientY + d.scrollTop;
|
---|
72 |
|
---|
73 | // Show the menu
|
---|
74 | $(document).unbind('click');
|
---|
75 | $(menu).css({ top: y, left: x }).fadeIn(o.inSpeed);
|
---|
76 | // Hover events
|
---|
77 | $(menu).find('A').mouseover( function() {
|
---|
78 | $(menu).find('LI.hover').removeClass('hover');
|
---|
79 | $(this).parent().addClass('hover');
|
---|
80 | }).mouseout( function() {
|
---|
81 | $(menu).find('LI.hover').removeClass('hover');
|
---|
82 | });
|
---|
83 |
|
---|
84 |
|
---|
85 | if (o.disabledItems != null) {
|
---|
86 | for (var i = 0; i < o.disabledItems.length; i ++) {
|
---|
87 | $(menu).find('LI.' + o.disabledItems[i]).addClass('disabled');
|
---|
88 | }
|
---|
89 | }
|
---|
90 | else {
|
---|
91 | $(menu).find('LI').removeClass('disabled');
|
---|
92 | }
|
---|
93 |
|
---|
94 | // Keyboard
|
---|
95 | $(document).keypress( function(e) {
|
---|
96 | switch( e.keyCode ) {
|
---|
97 | case 38: // up
|
---|
98 | if( $(menu).find('LI.hover').size() == 0 ) {
|
---|
99 | $(menu).find('LI:last').addClass('hover');
|
---|
100 | } else {
|
---|
101 | $(menu).find('LI.hover').removeClass('hover').prevAll('LI:not(.disabled)').eq(0).addClass('hover');
|
---|
102 | if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:last').addClass('hover');
|
---|
103 | }
|
---|
104 | break;
|
---|
105 | case 40: // down
|
---|
106 | if( $(menu).find('LI.hover').size() == 0 ) {
|
---|
107 | $(menu).find('LI:first').addClass('hover');
|
---|
108 | } else {
|
---|
109 | $(menu).find('LI.hover').removeClass('hover').nextAll('LI:not(.disabled)').eq(0).addClass('hover');
|
---|
110 | if( $(menu).find('LI.hover').size() == 0 ) $(menu).find('LI:first').addClass('hover');
|
---|
111 | }
|
---|
112 | break;
|
---|
113 | case 13: // enter
|
---|
114 | $(menu).find('LI.hover A').trigger('click');
|
---|
115 | break;
|
---|
116 | case 27: // esc
|
---|
117 | $(document).trigger('click');
|
---|
118 | break
|
---|
119 | }
|
---|
120 | });
|
---|
121 |
|
---|
122 | // When items are selected
|
---|
123 | $('#' + o.menu).find('A').unbind('click');
|
---|
124 | $('#' + o.menu).find('LI:not(.disabled) A').click( function() {
|
---|
125 | $(document).unbind('click').unbind('keypress');
|
---|
126 | $(".contextMenu").hide();
|
---|
127 | // Callback
|
---|
128 | if( callback ) callback( $(this).attr('href').substr(1), $(srcElement), {x: x - offset.left, y: y - offset.top, docX: x, docY: y} );
|
---|
129 | return false;
|
---|
130 | });
|
---|
131 |
|
---|
132 | // Hide bindings
|
---|
133 | setTimeout( function() { // Delay for Mozilla
|
---|
134 | $(document).click( function() {
|
---|
135 | $(document).unbind('click').unbind('keypress');
|
---|
136 | $(menu).fadeOut(o.outSpeed);
|
---|
137 | return false;
|
---|
138 | });
|
---|
139 | }, 0);
|
---|
140 | }
|
---|
141 | });
|
---|
142 | });
|
---|
143 |
|
---|
144 | // Disable text selection
|
---|
145 | /*if( $.browser.mozilla ) {
|
---|
146 | $('#' + o.menu).each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
|
---|
147 | } else if( $.browser.msie ) {
|
---|
148 | $('#' + o.menu).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
|
---|
149 | } else {
|
---|
150 | $('#' + o.menu).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
|
---|
151 | }*/
|
---|
152 | // Disable browser context menu (requires both selectors to work in IE/Safari + FF/Chrome)
|
---|
153 |
|
---|
154 | $('#' + o.menu).each( function() { $(this).css({ 'MozUserSelect' : 'none' }); });
|
---|
155 | $('#' + o.menu).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
|
---|
156 | $('#' + o.menu).each( function() { $(this).bind('selectstart.disableTextSelect', function() { return false; }); });
|
---|
157 |
|
---|
158 | $(el).add($('UL.contextMenu')).bind('contextmenu', function() { return false; });
|
---|
159 |
|
---|
160 | });
|
---|
161 | return $(this);
|
---|
162 | },
|
---|
163 |
|
---|
164 | // Disable context menu items on the fly
|
---|
165 | disableContextMenuItems: function(o) {
|
---|
166 | if( o == undefined ) {
|
---|
167 | // Disable all
|
---|
168 | $(this).find('LI').addClass('disabled');
|
---|
169 | return( $(this) );
|
---|
170 | }
|
---|
171 | $(this).each( function() {
|
---|
172 | if( o != undefined ) {
|
---|
173 | var d = o.split(',');
|
---|
174 | for( var i = 0; i < d.length; i++ ) {
|
---|
175 | $(this).find('A[href="' + d[i] + '"]').parent().addClass('disabled');
|
---|
176 |
|
---|
177 | }
|
---|
178 | }
|
---|
179 | });
|
---|
180 | return( $(this) );
|
---|
181 | },
|
---|
182 |
|
---|
183 | // Enable context menu items on the fly
|
---|
184 | enableContextMenuItems: function(o) {
|
---|
185 | if( o == undefined ) {
|
---|
186 | // Enable all
|
---|
187 | $(this).find('LI.disabled').removeClass('disabled');
|
---|
188 | return( $(this) );
|
---|
189 | }
|
---|
190 | $(this).each( function() {
|
---|
191 | if( o != undefined ) {
|
---|
192 | var d = o.split(',');
|
---|
193 | for( var i = 0; i < d.length; i++ ) {
|
---|
194 | $(this).find('A[href="' + d[i] + '"]').parent().removeClass('disabled');
|
---|
195 |
|
---|
196 | }
|
---|
197 | }
|
---|
198 | });
|
---|
199 | return( $(this) );
|
---|
200 | },
|
---|
201 |
|
---|
202 | // Disable context menu(s)
|
---|
203 | disableContextMenu: function() {
|
---|
204 | $(this).each( function() {
|
---|
205 | $(this).addClass('disabled');
|
---|
206 | });
|
---|
207 | return( $(this) );
|
---|
208 | },
|
---|
209 |
|
---|
210 | // Enable context menu(s)
|
---|
211 | enableContextMenu: function() {
|
---|
212 | $(this).each( function() {
|
---|
213 | $(this).removeClass('disabled');
|
---|
214 | });
|
---|
215 | return( $(this) );
|
---|
216 | },
|
---|
217 |
|
---|
218 | // Destroy context menu(s)
|
---|
219 | destroyContextMenu: function() {
|
---|
220 | // Destroy specified context menus
|
---|
221 | $(this).each( function() {
|
---|
222 | // Disable action
|
---|
223 | $(this).unbind('mousedown').unbind('mouseup');
|
---|
224 | });
|
---|
225 | return( $(this) );
|
---|
226 | }
|
---|
227 |
|
---|
228 | });
|
---|
229 | })(jQuery); |
---|