PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/js/context.js

https://gitlab.com/rstechnosoft/nest
JavaScript | 284 lines | 201 code | 23 blank | 60 comment | 29 complexity | 0d61b2009c2cb5987292e1cad6a0be51 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.1
  22. * @history 1.1 2010-01-25 Fixed a problem with 1.4 which caused undesired show/hide animations
  23. * @history 1.0 2008-10-20 Initial Release
  24. * @todo slideUp doesn't work in IE - because of iframe?
  25. * @todo Hide all other menus when contextmenu is shown?
  26. * @todo More themes
  27. * @todo Nested context menus
  28. */
  29. var _contextMenu = null;
  30. ;(function($){
  31. $.contextMenu = {
  32. shadow:false,
  33. shadowOffset:0,
  34. shadowOffsetX:5,
  35. shadowOffsetY:5,
  36. shadowWidthAdjust:-3,
  37. shadowHeightAdjust:-3,
  38. shadowOpacity:.2,
  39. shadowClass:'context-menu-shadow',
  40. shadowColor:'black',
  41. offsetX:0,
  42. offsetY:0,
  43. appendTo:'body',
  44. direction:'down',
  45. constrainToScreen:true,
  46. showTransition:'show',
  47. hideTransition:'hide',
  48. showSpeed:null,
  49. hideSpeed:null,
  50. showCallback:null,
  51. hideCallback:null,
  52. className:'context-menu',
  53. itemClassName:'context-menu-item',
  54. itemHoverClassName:'context-menu-item-hover',
  55. disabledItemClassName:'context-menu-item-disabled',
  56. disabledItemHoverClassName:'context-menu-item-disabled-hover',
  57. separatorClassName:'context-menu-separator',
  58. innerDivClassName:'context-menu-item-inner',
  59. themePrefix:'context-menu-theme-',
  60. theme:'default',
  61. separator:'context-menu-separator', // A specific key to identify a separator
  62. target:null, // The target of the context click, to be populated when triggered
  63. menu:null, // The jQuery object containing the HTML object that is the menu itself
  64. shadowObj:null, // Shadow object
  65. bgiframe:null, // The iframe object for IE6
  66. shown:false, // Currently being shown?
  67. useIframe:/*@cc_on @*//*@if (@_win32) true, @else @*/false,/*@end @*/ // This is a better check than looking at userAgent!
  68. // Create the menu instance
  69. create: function(menu,opts) {
  70. var cmenu = $.extend({},this,opts); // Clone all default properties to created object
  71. // If a selector has been passed in, then use that as the menu
  72. if (typeof menu=="string") {
  73. cmenu.menu = $(menu);
  74. //cmenu.menu.find("li:has(ul)").find("a:first").append("   "); // causes problem with objectlist refresh
  75. }
  76. // If a function has been passed in, call it each time the menu is shown to create the menu
  77. else if (typeof menu=="function") {
  78. cmenu.menuFunction = menu;
  79. }
  80. // Otherwise parse the Array passed in
  81. else {
  82. cmenu.menu = cmenu.createMenu(menu,cmenu);
  83. }
  84. if (cmenu.menu) {
  85. cmenu.menu.css({display:'none'});
  86. $(cmenu.appendTo).append(cmenu.menu);
  87. }
  88. // Create the shadow object if shadow is enabled
  89. if (cmenu.shadow) {
  90. cmenu.createShadow(cmenu); // Extracted to method for extensibility
  91. if (cmenu.shadowOffset) { cmenu.shadowOffsetX = cmenu.shadowOffsetY = cmenu.shadowOffset; }
  92. }
  93. $('body').bind('contextmenu',function(){cmenu.hide();}); // If right-clicked somewhere else in the document, hide this menu
  94. return cmenu;
  95. },
  96. // Create an iframe object to go behind the menu
  97. createIframe: function() {
  98. return $('<iframe frameborder="0" tabindex="-1" src="javascript:false" style="display:block;position:absolute;z-index:-1;filter:Alpha(Opacity=0);"/>');
  99. },
  100. // Accept an Array representing a menu structure and turn it into HTML
  101. createMenu: function(menu,cmenu) {
  102. var className = cmenu.className;
  103. $.each(cmenu.theme.split(","),function(i,n){className+=' '+cmenu.themePrefix+n});
  104. 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
  105. var $tr = $('<tr></tr>');
  106. var $td = $('<td></td>');
  107. var $div = $('<div class="'+className+'"></div>');
  108. // Each menu item is specified as either:
  109. // title:function
  110. // or title: { property:value ... }
  111. for (var i=0; i<menu.length; i++) {
  112. var m = menu[i];
  113. if (m==$.contextMenu.separator) {
  114. $div.append(cmenu.createSeparator());
  115. }
  116. else {
  117. for (var opt in menu[i]) {
  118. $div.append(cmenu.createMenuItem(opt,menu[i][opt])); // Extracted to method for extensibility
  119. }
  120. }
  121. }
  122. if ( cmenu.useIframe ) {
  123. $td.append(cmenu.createIframe());
  124. }
  125. $t.append($tr.append($td.append($div)))
  126. return $t;
  127. },
  128. // Create an individual menu item
  129. createMenuItem: function(label,obj) {
  130. var cmenu = this;
  131. if (typeof obj=="function") { obj={onclick:obj}; } // If passed a simple function, turn it into a property of an object
  132. // Default properties, extended in case properties are passed
  133. var o = $.extend({
  134. onclick:function() { },
  135. className:'',
  136. hoverClassName:cmenu.itemHoverClassName,
  137. icon:'',
  138. disabled:false,
  139. title:'',
  140. hoverItem:cmenu.hoverItem,
  141. hoverItemOut:cmenu.hoverItemOut
  142. },obj);
  143. // 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
  144. var iconStyle = (o.icon)?'background-image:url('+o.icon+');':'';
  145. var $div = $('<div class="'+cmenu.itemClassName+' '+o.className+((o.disabled)?' '+cmenu.disabledItemClassName:'')+'" title="'+o.title+'"></div>')
  146. // If the item is disabled, don't do anything when it is clicked
  147. .click(function(e){if(cmenu.isItemDisabled(this)){return false;}else{return o.onclick.call(cmenu.target,this,cmenu,e)}})
  148. // Change the class of the item when hovered over
  149. .hover( function(){ o.hoverItem.call(this,(cmenu.isItemDisabled(this))?cmenu.disabledItemHoverClassName:o.hoverClassName); }
  150. ,function(){ o.hoverItemOut.call(this,(cmenu.isItemDisabled(this))?cmenu.disabledItemHoverClassName:o.hoverClassName); }
  151. );
  152. var $idiv = $('<div class="'+cmenu.innerDivClassName+'" style="'+iconStyle+'">'+label+'</div>');
  153. $div.append($idiv);
  154. return $div;
  155. },
  156. // Create a separator row
  157. createSeparator: function() {
  158. return $('<div class="'+this.separatorClassName+'"></div>');
  159. },
  160. // 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
  161. isItemDisabled: function(item) { return $(item).is('.'+this.disabledItemClassName); },
  162. // Functions to fire on hover. Extracted to methods for extensibility
  163. hoverItem: function(c) { $(this).addClass(c); },
  164. hoverItemOut: function(c) { $(this).removeClass(c); },
  165. // Create the shadow object
  166. createShadow: function(cmenu) {
  167. cmenu.shadowObj = $('<div class="'+cmenu.shadowClass+'"></div>').css( {display:'none',position:"absolute", zIndex:9998, opacity:cmenu.shadowOpacity, backgroundColor:cmenu.shadowColor } );
  168. $(cmenu.appendTo).append(cmenu.shadowObj);
  169. },
  170. // Display the shadow object, given the position of the menu itself
  171. showShadow: function(x,y,e) {
  172. var cmenu = this;
  173. if (cmenu.shadow) {
  174. cmenu.shadowObj.css( {
  175. width:(cmenu.menu.width()+cmenu.shadowWidthAdjust)+"px",
  176. height:(cmenu.menu.height()+cmenu.shadowHeightAdjust)+"px",
  177. top:(y+cmenu.shadowOffsetY)+"px",
  178. left:(x+cmenu.shadowOffsetX)+"px"
  179. }).addClass(cmenu.shadowClass)[cmenu.showTransition](cmenu.showSpeed);
  180. }
  181. },
  182. // A hook to call before the menu is shown, in case special processing needs to be done.
  183. // Return false to cancel the default show operation
  184. beforeShow: function() { return true; },
  185. // Show the context menu
  186. show: function(t,e) {
  187. var cmenu=this, x=e.pageX, y=e.pageY;
  188. _contextMenu = this;
  189. cmenu.target = t; // Preserve the object that triggered this context menu so menu item click methods can see it
  190. if (cmenu.beforeShow()!==false) {
  191. // If the menu content is a function, call it to populate the menu each time it is displayed
  192. if (cmenu.menuFunction) {
  193. if (cmenu.menu) { $(cmenu.menu).remove(); }
  194. //cmenu.menu = cmenu.createMenu(cmenu.menuFunction(cmenu,t,e),cmenu);
  195. cmenu.menu = cmenu.menuFunction(cmenu,t,e);
  196. if (! cmenu.menu)
  197. return false; // ok we don't need a menu for this target
  198. cmenu.menu.css({display:'none'});
  199. $(cmenu.appendTo).append(cmenu.menu);
  200. }
  201. var $c = cmenu.menu;
  202. /* custom changes to make it work for div based menus */
  203. cmenu.menu.find('a').unbind('click').click(function(e2) {
  204. otarget = e.originalTarget || e.target; // for firefox originalTarget works, for some other just 'target'
  205. e2.preventDefault();
  206. p = $(otarget).attr("data-parent");
  207. name = p ? p + "." + $(otarget).text().replace('"', '&qout;') : $(otarget).text().replace('"', '&qout;');
  208. tempFn = $(this).attr('href').replace('[name]', '"' + name + '"');
  209. eval(tempFn);
  210. });
  211. /* custom changes end */
  212. x+=cmenu.offsetX; y+=cmenu.offsetY;
  213. var pos = cmenu.getPosition(x,y,cmenu,e); // Extracted to method for extensibility
  214. cmenu.showShadow(pos.x,pos.y,e);
  215. // Resize the iframe if needed
  216. if (cmenu.useIframe) {
  217. $c.find('iframe').css({width:$c.width()+cmenu.shadowOffsetX+cmenu.shadowWidthAdjust,height:$c.height()+cmenu.shadowOffsetY+cmenu.shadowHeightAdjust});
  218. }
  219. $c.css( {top:pos.y+"px", left:pos.x+"px", position:"absolute",zIndex:99999} )[cmenu.showTransition](cmenu.showSpeed,((cmenu.showCallback)?function(){cmenu.showCallback.call(cmenu);}:null));
  220. cmenu.shown=true;
  221. $(document).one('click',null,function(){cmenu.hide()}); // Handle a single click to the document to hide the menu
  222. }
  223. },
  224. // Find the position where the menu should appear, given an x,y of the click event
  225. getPosition: function(clickX,clickY,cmenu,e) {
  226. var x = clickX+cmenu.offsetX;
  227. var y = clickY+cmenu.offsetY
  228. var h = $(cmenu.menu).height();
  229. var w = $(cmenu.menu).width();
  230. var dir = cmenu.direction;
  231. if (cmenu.constrainToScreen) {
  232. var $w = $(window);
  233. var wh = $w.height();
  234. var ww = $w.width();
  235. if (dir=="down" && (y+h-$w.scrollTop() > wh)) { dir = "up"; }
  236. var maxRight = x+w-$w.scrollLeft();
  237. if (maxRight > ww) { x -= (maxRight-ww); }
  238. }
  239. if (dir=="up") { y -= h; }
  240. return {'x':x,'y':y};
  241. },
  242. // Hide the menu, of course
  243. hide: function() {
  244. var cmenu=this;
  245. if (cmenu.shown) {
  246. if (cmenu.iframe) { $(cmenu.iframe).hide(); }
  247. if (cmenu.menu) { cmenu.menu[cmenu.hideTransition](cmenu.hideSpeed,((cmenu.hideCallback)?function(){cmenu.hideCallback.call(cmenu);}:null)); }
  248. if (cmenu.shadow) { cmenu.shadowObj[cmenu.hideTransition](cmenu.hideSpeed); }
  249. }
  250. cmenu.shown = false;
  251. _contextMenu = null;
  252. }
  253. };
  254. // This actually adds the .contextMenu() function to the jQuery namespace
  255. $.fn.contextMenu = function(menu,options) {
  256. var cmenu = $.contextMenu.create(menu,options);
  257. return this.each(function(){
  258. $(this).bind('contextmenu',function(e){cmenu.show(this,e);return false;});
  259. });
  260. };
  261. })(jQuery);