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