PageRenderTime 68ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/etherpad/src/static/js/lib/jquery.contextmenu.js

https://github.com/marcmurray/pad
JavaScript | 284 lines | 200 code | 25 blank | 59 comment | 30 complexity | 1bd3e0e055246531b8917536e19cc31d MD5 | raw file
  1. /**
  2. * Copyright (c)2005-2009 Matt Kruse (javascripttoolbox.com)
  3. *
  4. * Dual licensed under the MIT and GPL licenses.
  5. * This basically means you can use this code however you want for
  6. * free, but don't claim to have written it yourself!
  7. * Donations always accepted: http://www.JavascriptToolbox.com/donate/
  8. *
  9. * Please do not link to the .js files on javascripttoolbox.com from
  10. * your site. Copy the files locally to your server instead.
  11. *
  12. */
  13. /**
  14. * jquery.contextmenu.js
  15. * jQuery Plugin for Context Menus
  16. * http://www.JavascriptToolbox.com/lib/contextmenu/
  17. *
  18. * Copyright (c) 2008 Matt Kruse (javascripttoolbox.com)
  19. * Dual licensed under the MIT and GPL licenses.
  20. *
  21. * @version 1.0
  22. * @history 1.0 2008-10-20 Initial Release
  23. * @todo slideUp doesn't work in IE - because of iframe?
  24. * @todo Hide all other menus when contextmenu is shown?
  25. * @todo More themes
  26. * @todo Nested context menus
  27. */
  28. ;(function($){
  29. $.contextMenu = {
  30. shadow:true,
  31. shadowOffset:0,
  32. shadowOffsetX:5,
  33. shadowOffsetY:5,
  34. shadowWidthAdjust:-3,
  35. shadowHeightAdjust:-3,
  36. shadowOpacity:.2,
  37. shadowClass:'context-menu-shadow',
  38. shadowColor:'black',
  39. offsetX:0,
  40. offsetY:0,
  41. appendTo:'body',
  42. direction:'down',
  43. constrainToScreen:true,
  44. showTransition:'show',
  45. hideTransition:'hide',
  46. showSpeed:'',
  47. hideSpeed:'',
  48. showCallback:null,
  49. hideCallback:null,
  50. className:'context-menu',
  51. itemClassName:'context-menu-item',
  52. itemHoverClassName:'context-menu-item-hover',
  53. disabledItemClassName:'context-menu-item-disabled',
  54. disabledItemHoverClassName:'context-menu-item-disabled-hover',
  55. separatorClassName:'context-menu-separator',
  56. innerDivClassName:'context-menu-item-inner',
  57. themePrefix:'context-menu-theme-',
  58. theme:'default',
  59. separator:'context-menu-separator', // A specific key to identify a separator
  60. target:null, // The target of the context click, to be populated when triggered
  61. menu:null, // The jQuery object containing the HTML object that is the menu itself
  62. shadowObj:null, // Shadow object
  63. bgiframe:null, // The iframe object for IE6
  64. shown:false, // Currently being shown?
  65. useIframe:/*@cc_on @*//*@if (@_win32) true, @else @*/false,/*@end @*/ // This is a better check than looking at userAgent!
  66. bindTarget: 'contextmenu',
  67. // Create the menu instance
  68. create: function(menu,opts) {
  69. var cmenu = $.extend({},this,opts); // Clone all default properties to created object
  70. // If a selector has been passed in, then use that as the menu
  71. if (typeof menu=="string") {
  72. cmenu.menu = $(menu);
  73. }
  74. // If a function has been passed in, call it each time the menu is shown to create the menu
  75. else if (typeof menu=="function") {
  76. cmenu.menuFunction = menu;
  77. }
  78. // Otherwise parse the Array passed in
  79. else {
  80. cmenu.menu = cmenu.createMenu(menu,cmenu);
  81. }
  82. if (cmenu.menu) {
  83. cmenu.menu.css({display:'none'});
  84. $(cmenu.appendTo).append(cmenu.menu);
  85. }
  86. // Create the shadow object if shadow is enabled
  87. if (cmenu.shadow) {
  88. cmenu.createShadow(cmenu); // Extracted to method for extensibility
  89. if (cmenu.shadowOffset) { cmenu.shadowOffsetX = cmenu.shadowOffsetY = cmenu.shadowOffset; }
  90. }
  91. // when to hide the menu:
  92. // click anywhere in the body:
  93. $('body').bind('click',function(){cmenu.hide();});
  94. // right-click anywhere in the body:
  95. $('body').bind('contextmenu',function(){cmenu.hide();});
  96. // escape key:
  97. $(document).keyup(function(e) { if (e.keyCode == 27) { cmenu.hide(); } });
  98. return cmenu;
  99. },
  100. // Create an iframe object to go behind the menu
  101. createIframe: function() {
  102. return $('<iframe frameborder="0" tabindex="-1" src="javascript:false" style="display:block;position:absolute;z-index:-1;filter:Alpha(Opacity=0);"/>');
  103. },
  104. // Accept an Array representing a menu structure and turn it into HTML
  105. createMenu: function(menu,cmenu) {
  106. var className = cmenu.className;
  107. $.each(cmenu.theme.split(","),function(i,n){className+=' '+cmenu.themePrefix+n});
  108. var $t = $('<table cellspacing=0 cellpadding=0></table>').click(function(){cmenu.hide(); return false;}); // We wrap a table around it so width can be flexible
  109. var $tr = $('<tr></tr>');
  110. var $td = $('<td></td>');
  111. var $div = $('<div class="'+className+'"></div>');
  112. // Each menu item is specified as either:
  113. // title:function
  114. // or title: { property:value ... }
  115. for (var i=0; i<menu.length; i++) {
  116. var m = menu[i];
  117. if (m==$.contextMenu.separator) {
  118. $div.append(cmenu.createSeparator());
  119. }
  120. else {
  121. for (var opt in menu[i]) {
  122. $div.append(cmenu.createMenuItem(opt,menu[i][opt])); // Extracted to method for extensibility
  123. }
  124. }
  125. }
  126. if ( cmenu.useIframe ) {
  127. $td.append(cmenu.createIframe());
  128. }
  129. $t.append($tr.append($td.append($div)))
  130. return $t;
  131. },
  132. // Create an individual menu item
  133. createMenuItem: function(label,obj) {
  134. var cmenu = this;
  135. if (typeof obj=="function") { obj={onclick:obj}; } // If passed a simple function, turn it into a property of an object
  136. // Default properties, extended in case properties are passed
  137. var o = $.extend({
  138. onclick:function() { },
  139. className:'',
  140. hoverClassName:cmenu.itemHoverClassName,
  141. icon:'',
  142. disabled:false,
  143. title:'',
  144. hoverItem:cmenu.hoverItem,
  145. hoverItemOut:cmenu.hoverItemOut
  146. },obj);
  147. // If an icon is specified, hard-code the background-image style. Themes that don't show images should take this into account in their CSS
  148. var iconStyle = (o.icon)?'background-image:url('+o.icon+');':'';
  149. var $div = $('<div class="'+cmenu.itemClassName+' '+o.className+((o.disabled)?' '+cmenu.disabledItemClassName:'')+'" title="'+o.title+'"></div>')
  150. // If the item is disabled, don't do anything when it is clicked
  151. .click(function(e){if(cmenu.isItemDisabled(this)){return false;}else{return o.onclick.call(cmenu.target,this,cmenu,e)}})
  152. // Change the class of the item when hovered over
  153. .hover( function(){ o.hoverItem.call(this,(cmenu.isItemDisabled(this))?cmenu.disabledItemHoverClassName:o.hoverClassName); }
  154. ,function(){ o.hoverItemOut.call(this,(cmenu.isItemDisabled(this))?cmenu.disabledItemHoverClassName:o.hoverClassName); }
  155. );
  156. var $idiv = $('<div class="'+cmenu.innerDivClassName+'" style="'+iconStyle+'">'+label+'</div>');
  157. $div.append($idiv);
  158. return $div;
  159. },
  160. // Create a separator row
  161. createSeparator: function() {
  162. return $('<div class="'+this.separatorClassName+'"></div>');
  163. },
  164. // Determine if an individual item is currently disabled. This is called each time the item is hovered or clicked because the disabled status may change at any time
  165. isItemDisabled: function(item) { return $(item).is('.'+this.disabledItemClassName); },
  166. // Functions to fire on hover. Extracted to methods for extensibility
  167. hoverItem: function(c) { $(this).addClass(c); },
  168. hoverItemOut: function(c) { $(this).removeClass(c); },
  169. // Create the shadow object
  170. createShadow: function(cmenu) {
  171. cmenu.shadowObj = $('<div class="'+cmenu.shadowClass+'"></div>').css( {display:'none',position:"absolute", zIndex:9998, opacity:cmenu.shadowOpacity, backgroundColor:cmenu.shadowColor } );
  172. $(cmenu.appendTo).append(cmenu.shadowObj);
  173. },
  174. // Display the shadow object, given the position of the menu itself
  175. showShadow: function(x,y,e) {
  176. var cmenu = this;
  177. if (cmenu.shadow) {
  178. cmenu.shadowObj.css( {
  179. width:(cmenu.menu.width()+cmenu.shadowWidthAdjust)+"px",
  180. height:(cmenu.menu.height()+cmenu.shadowHeightAdjust)+"px",
  181. top:(y+cmenu.shadowOffsetY)+"px",
  182. left:(x+cmenu.shadowOffsetX)+"px"
  183. }).addClass(cmenu.shadowClass)[cmenu.showTransition](cmenu.showSpeed);
  184. }
  185. },
  186. // A hook to call before the menu is shown, in case special processing needs to be done.
  187. // Return false to cancel the default show operation
  188. beforeShow: function() { return true; },
  189. // Show the context menu
  190. show: function(t,e) {
  191. var cmenu=this, x=e.pageX, y=e.pageY;
  192. cmenu.target = t; // Preserve the object that triggered this context menu so menu item click methods can see it
  193. if (cmenu.beforeShow()!==false) {
  194. // If the menu content is a function, call it to populate the menu each time it is displayed
  195. if (cmenu.menuFunction) {
  196. if (cmenu.menu) { $(cmenu.menu).remove(); }
  197. cmenu.menu = cmenu.createMenu(cmenu.menuFunction(cmenu,t),cmenu);
  198. cmenu.menu.css({display:'none'});
  199. $(cmenu.appendTo).append(cmenu.menu);
  200. }
  201. var $c = cmenu.menu;
  202. x+=cmenu.offsetX; y+=cmenu.offsetY;
  203. var pos = cmenu.getPosition(x,y,cmenu,e); // Extracted to method for extensibility
  204. cmenu.showShadow(pos.x,pos.y,e);
  205. // Resize the iframe if needed
  206. if (cmenu.useIframe) {
  207. $c.find('iframe').css({width:$c.width()+cmenu.shadowOffsetX+cmenu.shadowWidthAdjust,height:$c.height()+cmenu.shadowOffsetY+cmenu.shadowHeightAdjust});
  208. }
  209. $c.css( {top:pos.y+"px", left:pos.x+"px", position:"absolute",zIndex:9999} )[cmenu.showTransition](cmenu.showSpeed,((cmenu.showCallback)?function(){cmenu.showCallback.call(cmenu);}:null));
  210. cmenu.shown=true;
  211. $(document).one('click',null,function(){cmenu.hide()}); // Handle a single click to the document to hide the menu
  212. }
  213. },
  214. // Find the position where the menu should appear, given an x,y of the click event
  215. getPosition: function(clickX,clickY,cmenu,e) {
  216. var x = clickX+cmenu.offsetX;
  217. var y = clickY+cmenu.offsetY
  218. var h = $(cmenu.menu).height();
  219. var w = $(cmenu.menu).width();
  220. var dir = cmenu.direction;
  221. if (cmenu.constrainToScreen) {
  222. var $w = $(window);
  223. var wh = $w.height();
  224. var ww = $w.width();
  225. if (dir=="down" && (y+h-$w.scrollTop() > wh)) { dir = "up"; }
  226. var maxRight = x+w-$w.scrollLeft();
  227. if (maxRight > ww) { x -= (maxRight-ww); }
  228. }
  229. if (dir=="up") { y -= h; }
  230. return {'x':x,'y':y};
  231. },
  232. // Hide the menu, of course
  233. hide: function() {
  234. var cmenu=this;
  235. if (cmenu.shown) {
  236. if (cmenu.iframe) {
  237. $(cmenu.iframe).hide();
  238. }
  239. if (cmenu.menu) {
  240. cmenu.menu[cmenu.hideTransition](cmenu.hideSpeed,null);
  241. }
  242. if (cmenu.shadow) {
  243. cmenu.shadowObj[cmenu.hideTransition](cmenu.hideSpeed);
  244. }
  245. if (cmenu.hideCallback) {
  246. cmenu.hideCallback.call(cmenu);
  247. }
  248. }
  249. cmenu.shown = false;
  250. }
  251. };
  252. // This actually adds the .contextMenu() function to the jQuery namespace
  253. $.fn.contextMenu = function(menu,options) {
  254. var cmenu = $.contextMenu.create(menu,options);
  255. return this.each(function(){
  256. $(this).bind(cmenu.bindTarget,function(e){cmenu.show(this,e);return false;});
  257. });
  258. };
  259. })(jQuery);