source: other-projects/nz-flag-design/trunk/design-2d/Original editor.method.ac/method-draw/lib/contextmenu/jquery.contextMenu.js@ 29468

Last change on this file since 29468 was 29468, checked in by sjs49, 9 years ago

Initial commit for editor.method.ac for flag design

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