Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / jquery / plugins / jquery.contextMenu.js
blobc93defce50b4ec4e0df3a8e80ced725eb074d339
1 // jQuery Context Menu Plugin
2 //
3 // Version 1.00
4 //
5 // Cory S.N. LaViska
6 // A Beautiful Site (http://abeautifulsite.net/)
7 //
8 // Visit http://abeautifulsite.net/notebook/80 for usage and more information
9 //
10 // Terms of Use
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/
17 (function($){
18         $.extend({
19                 
20                 contextMenu: function(o, callback) {
21                         // Defaults
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() {
30                                 var el = $(this);
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) {
36                                         var evt = 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);
45                                                         
46                                                         if( $(el).hasClass('disabled') ) return false;
47                                                         
48                                                         // Detect mouse position
49                                                         var d = {}, x, y;
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;
66                                                         }
67                                                         (e.pageX) ? x = e.pageX : x = e.clientX + d.scrollLeft;
68                                                         (e.pageY) ? y = e.pageY : x = e.clientY + d.scrollTop;
69                                                         
70                                                         // Show the menu
71                                                         $(document).unbind('click');
72                                                         $(menu).css({ top: y, left: x }).fadeIn(o.inSpeed);
73                                                         // Hover events
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');
79                                                         });
80                                                         
81                                                         // Keyboard
82                                                         $(document).keypress( function(e) {
83                                                                 switch( e.keyCode ) {
84                                                                         case 38: // up
85                                                                                 if( $(menu).find('LI.hover').size() == 0 ) {
86                                                                                         $(menu).find('LI:last').addClass('hover');
87                                                                                 } else {
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');
90                                                                                 }
91                                                                         break;
92                                                                         case 40: // down
93                                                                                 if( $(menu).find('LI.hover').size() == 0 ) {
94                                                                                         $(menu).find('LI:first').addClass('hover');
95                                                                                 } else {
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');
98                                                                                 }
99                                                                         break;
100                                                                         case 13: // enter
101                                                                                 $(menu).find('LI.hover A').trigger('click');
102                                                                         break;
103                                                                         case 27: // esc
104                                                                                 $(document).trigger('click');
105                                                                         break
106                                                                 }
107                                                         });
108                                                         
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();
114                                                                 // Callback
115                                                                 if( callback ) callback( $(this).attr('href').substr(1), $(srcElement), {x: x - offset.left, y: y - offset.top, docX: x, docY: y} );
116                                                                 return false;
117                                                         });
118                                                         
119                                                         // Hide bindings
120                                                         setTimeout( function() { // Delay for Mozilla
121                                                                 $(document).click( function() {
122                                                                         $(document).unbind('click').unbind('keypress');
123                                                                         $(menu).fadeOut(o.outSpeed);
124                                                                         return false;
125                                                                 });
126                                                         }, 0);
127                                                 }
128                                         });
129                                 });
130                                 
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; }); });
136                                 } else {
137                                         $('#' + o.menu).each(function() { $(this).bind('mousedown.disableTextSelect', function() { return false; }); });
138                                 }
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; });
141                                 
142                         });
143                         return $(this);
144                 },
145                 
146                 // Disable context menu items on the fly
147                 disableContextMenuItems: function(o) {
148                         if( o == undefined ) {
149                                 // Disable all
150                                 $(this).find('LI').addClass('disabled');
151                                 return( $(this) );
152                         }
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');
158                                                 
159                                         }
160                                 }
161                         });
162                         return( $(this) );
163                 },
164                 
165                 // Enable context menu items on the fly
166                 enableContextMenuItems: function(o) {
167                         if( o == undefined ) {
168                                 // Enable all
169                                 $(this).find('LI.disabled').removeClass('disabled');
170                                 return( $(this) );
171                         }
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');
177                                                 
178                                         }
179                                 }
180                         });
181                         return( $(this) );
182                 },
183                 
184                 // Disable context menu(s)
185                 disableContextMenu: function() {
186                         $(this).each( function() {
187                                 $(this).addClass('disabled');
188                         });
189                         return( $(this) );
190                 },
191                 
192                 // Enable context menu(s)
193                 enableContextMenu: function() {
194                         $(this).each( function() {
195                                 $(this).removeClass('disabled');
196                         });
197                         return( $(this) );
198                 },
199                 
200                 // Destroy context menu(s)
201                 destroyContextMenu: function() {
202                         // Destroy specified context menus
203                         $(this).each( function() {
204                                 // Disable action
205                                 $(this).unbind('mousedown').unbind('mouseup');
206                         });
207                         return( $(this) );
208                 }
209                 
210         });
211 })(jQuery);