contextmenu.js
上传用户:yfdli66
上传日期:2010-02-20
资源大小:47k
文件大小:4k
源码类别:

JavaScript

开发平台:

JavaScript

  1. ContextMenu.intializeContextMenu=function()
  2. {
  3. document.body.insertAdjacentHTML("BeforeEnd", '<iframe scrolling="no" class="WebFX-ContextMenu" marginwidth="0" marginheight="0" frameborder="0" style="position:absolute;display:none;z-index:50000000;" id="WebFX_PopUp"></iframe>');
  4. WebFX_PopUp    = self.frames["WebFX_PopUp"]
  5. WebFX_PopUpcss = document.getElementById("WebFX_PopUp")
  6. document.body.attachEvent("onmousedown",function(){WebFX_PopUpcss.style.display="none"})
  7. WebFX_PopUpcss.onfocus  = function(){WebFX_PopUpcss.style.display="inline"};
  8. WebFX_PopUpcss.onblur  = function(){WebFX_PopUpcss.style.display="none"};
  9. self.attachEvent("onblur",function(){WebFX_PopUpcss.style.display="none"})
  10. }
  11. function ContextSeperator(){}
  12. function ContextMenu(){}
  13. ContextMenu.showPopup=function(x,y)
  14. {
  15. WebFX_PopUpcss.style.display = "block"
  16. }
  17. ContextMenu.display=function(popupoptions)
  18. {
  19.     var eobj,x,y;
  20. eobj = window.event;
  21. x    = eobj.x;
  22. y    = eobj.y
  23. /*
  24. not really sure why I had to pass window here
  25. it appears that an iframe inside a frames page
  26. will think that its parent is the frameset as
  27. opposed to the page it was created in...
  28. */
  29. ContextMenu.populatePopup(popupoptions,window)
  30. ContextMenu.showPopup(x,y)
  31. ContextMenu.fixSize()
  32. ContextMenu.fixPos(x,y)
  33.     eobj.cancelBubble = true;
  34.     eobj.returnValue  = false;
  35. }
  36. //TODO
  37.  ContextMenu.getScrollTop=function()
  38.  {
  39.   return document.body.scrollTop;
  40. //window.pageXOffset and window.pageYOffset for moz
  41.  }
  42.  ContextMenu.getScrollLeft=function()
  43.  {
  44.   return document.body.scrollLeft;
  45.  }
  46. ContextMenu.fixPos=function(x,y)
  47. {
  48. var docheight,docwidth,dh,dw;
  49. docheight = document.body.clientHeight;
  50. docwidth  = document.body.clientWidth;
  51. dh = (WebFX_PopUpcss.offsetHeight+y) - docheight;
  52. dw = (WebFX_PopUpcss.offsetWidth+x)  - docwidth;
  53. if(dw>0)
  54. {
  55. WebFX_PopUpcss.style.left = (x - dw) + ContextMenu.getScrollLeft() + "px";
  56. }
  57. else
  58. {
  59. WebFX_PopUpcss.style.left = x + ContextMenu.getScrollLeft();
  60. }
  61. if(dh>0)
  62. {
  63. WebFX_PopUpcss.style.top = (y - dh) + ContextMenu.getScrollTop() + "px"
  64. }
  65. else
  66. {
  67. WebFX_PopUpcss.style.top  = y + ContextMenu.getScrollTop();
  68. }
  69. }
  70. ContextMenu.fixSize=function()
  71. {
  72. var body,h,w;
  73. WebFX_PopUpcss.style.width = "10px";
  74. WebFX_PopUpcss.style.height = "100000px";
  75. body = WebFX_PopUp.document.body;
  76. // check offsetHeight twice... fixes a bug where scrollHeight is not valid because the visual state is undefined
  77. var dummy = WebFX_PopUpcss.offsetHeight + " dummy";
  78. h = body.scrollHeight + WebFX_PopUpcss.offsetHeight - body.clientHeight;
  79. w = body.scrollWidth + WebFX_PopUpcss.offsetWidth - body.clientWidth;
  80. WebFX_PopUpcss.style.height = h + "px";
  81. WebFX_PopUpcss.style.width = w + "px";
  82. //use document.height for moz
  83. }
  84. ContextMenu.populatePopup=function(arr,win)
  85. {
  86. var alen,i,tmpobj,doc,height,htmstr;
  87. alen = arr.length;
  88. doc  = WebFX_PopUp.document;
  89. doc.body.innerHTML  = ""
  90. if (doc.getElementsByTagName("LINK").length == 0) {
  91. doc.open();
  92. doc.write('<html><head><link rel="StyleSheet" type="text/css" href="css/workflow/contextmenu.css"></head><body></body></html>');
  93. doc.close();
  94. }
  95. for(i=0;i<alen;i++)
  96. {
  97. if(arr[i].constructor==ContextItem)
  98. {
  99. tmpobj=doc.createElement("DIV");
  100. tmpobj.noWrap = true;
  101. tmpobj.className = "WebFX-ContextMenu-Item";
  102. if(arr[i].disabled)
  103. {
  104. htmstr  = '<span class="WebFX-ContextMenu-DisabledContainer"><span class="WebFX-ContextMenu-DisabledContainer">'
  105. htmstr += arr[i].text+'</span></span>'
  106. tmpobj.innerHTML = htmstr
  107. tmpobj.className = "WebFX-ContextMenu-Disabled";
  108. tmpobj.onmouseover = function(){this.className="WebFX-ContextMenu-Disabled-Over"}
  109. tmpobj.onmouseout  = function(){this.className="WebFX-ContextMenu-Disabled"}
  110. }
  111. else
  112. {
  113. tmpobj.innerHTML = arr[i].text;
  114. tmpobj.onclick = (function (f)
  115. {
  116.      return function () {
  117.            win.WebFX_PopUpcss.style.display='none'
  118.          if (typeof(f)=="function"){ f(); }
  119.                   };
  120. })(arr[i].action);
  121. tmpobj.onmouseover = function(){this.className="WebFX-ContextMenu-Over"}
  122. tmpobj.onmouseout  = function(){this.className="WebFX-ContextMenu-Item"}
  123. }
  124. doc.body.appendChild(tmpobj);
  125. }
  126. else
  127. {
  128. doc.body.appendChild(doc.createElement("DIV")).className = "WebFX-ContextMenu-Separator";
  129. }
  130. }
  131. doc.body.className  = "WebFX-ContextMenu-Body" ;
  132. doc.body.onselectstart = function(){return false;}
  133. }
  134. function ContextItem(str,fnc,disabled)
  135. {
  136. this.text     = str;
  137. this.action   = fnc;
  138. this.disabled = disabled || false;
  139. }