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