home *** CD-ROM | disk | FTP | other *** search
/ Joystick Magazine 2003 November / CD1_JOY_153.iso / demos / NHL2004Demo.exe / fe / COMMON / js / ContextMenu.js next >
Text File  |  2003-08-20  |  6KB  |  178 lines

  1. function ContextMenu(id, win)
  2. {
  3.     ContextMenu.prototype.uniqueID++;
  4.     this.win        = (win) ? win : window;
  5.     this.el        = this.win.document.createElement("<div id='' hidefocus tabindex=-1 style='font-size:7pt;font-family:verdana;position:absolute;visibility:hidden;overflow:hidden;cursor:hand;z-index:20000; border: outset 2px #696969; background-color:#DCDCDC' nowrap></div>");
  6.     this.el.id        = id + "_contextMenu_" + this.uniqueID;
  7.     this.captureSet    = false;
  8.     this.items        = [];
  9.     this.over        = false;
  10. };
  11. ContextMenu.prototype.uniqueID    = 0;
  12.  
  13. ContextMenu.prototype.write        = function(adjacentEl, whereStr)
  14. {
  15.     try {
  16.         this.doc    = this.win.document;
  17.         if (!whereStr) { whereStr = "beforeEnd"; };
  18.         if (!adjacentEl) {
  19.            if (this.doc.readyState == "complete") {
  20.                 this.doc.body.insertAdajcentHTML("beforeEnd", this.el.outerHTML);
  21.             } else {
  22.                 this.doc.write(this.el.outerHTML);
  23.             }
  24.         } else {
  25.             adjacentEl.appendChild(this.el);
  26.         };
  27.         this.el    = this.win.document.getElementById(this.el.id);
  28.             for (var idx = 0; idx < this.items.length; idx++)
  29.             {
  30.                 this.el.childNodes[idx].onitemselect        = this.items[idx];
  31.             this.el.childNodes[idx].onmousedown        = this._itemDown;
  32.             this.el.childNodes[idx].onmouseenter        = this._itemOver;
  33.             this.el.childNodes[idx].onmouseleave        = this._itemOut;
  34.         };
  35.         this.el                    = this.doc.getElementById(this.el.id);
  36.         this.el.onmousedown        = this._blurMenu;
  37.         this.doc                 = null; delete this.doc;
  38.         this.el.mgr                = this;
  39.     } catch (e) {
  40.     };
  41.  
  42.     win = adjacentEl = whereStr = null;
  43. };
  44. ContextMenu.prototype.addMenuItem        = function(theID, text, fOnClick)
  45. {
  46.     this._img        = this.win.document.createElement("IMG");
  47.     this._itemDiv    = this.win.document.createElement("DIV");
  48.     
  49.     this._img.src        = System.getInstallFolder(true) + "fe/common/images/framework/arrow_right_333333.gif";
  50.     this._img.border    = 0;
  51.     this._img.align        = "absmiddle";
  52.     
  53.     this._itemDiv.innerHTML            = " " + this._img.outerHTML + "  <span>" +text + " </span>";
  54.     this._itemDiv.id                    = theID;
  55.     this._itemDiv.style.fontFamily        = "verdana";
  56.     this._itemDiv.style.color            = "black";
  57.     this._itemDiv.style.backgroundColor     = "#DCDCDC";
  58.     this._itemDiv.style.cursor            = "hand";
  59.     
  60.     this.el.appendChild(this._itemDiv);
  61.     this.items[this.items.length]    = fOnClick;
  62.     
  63.     this._img = this._itemDiv = null;
  64.     delete this._img; delete this._itemDiv;
  65. };
  66. ContextMenu.prototype.show        = function(attachElement, nDeltaX, nDeltaY, bDontAddWindowPos)
  67. {
  68.     nDeltaX = (nDeltaX) ? nDeltaX : 0;
  69.     nDeltaY = (nDeltaY) ? nDeltaY : 0;
  70.         
  71.     this._el_frameEl        = this.el.document.parentWindow.frameElement; 
  72.     this._el_xpos_indoc        = this._getRealLeft(attachElement); 
  73.     this._el_ypos_indoc        = this._getRealTop(attachElement); 
  74.     
  75.     this._el_win_xpos        =((this._el_frameEl) && (!bDontAddWindowPos))  ?  this._getRealLeft(this._el_frameEl) : 0;
  76.     this._el_win_ypos        =((this._el_frameEl) && (!bDontAddWindowPos))  ?  this._getRealTop(this._el_frameEl) : 0; 
  77.  
  78.     this.el.style.pixelLeft        = (this._el_xpos_indoc + this._el_win_xpos + nDeltaX);
  79.     this.el.style.pixelTop        = (this._el_ypos_indoc + this._el_win_ypos + nDeltaY + 12);
  80.     this.el.style.visibility        = "visible";
  81.      this.captureSet = true;
  82.     this.win.document.releaseCapture();
  83.     this.el.setCapture(false);
  84.     
  85.     delete this._el_xpos_indoc; delete this._el_ypos_indoc; delete this._el_win_xpos; delete this._el_win_ypos; delete this._el_frameEl;
  86.  
  87.     var ti = this;
  88.     setTimeout(function() { ti._closeAfterTime(); ti=null;}, 2000);
  89.     
  90.     System.GUI.playSFX(7);
  91. };
  92.  
  93. ContextMenu.prototype._closeAfterTime    = function()
  94. {
  95.     if (this.isOn()) {
  96.         if (!this.over) { 
  97.             this.hide(); 
  98.         } else {
  99.             var ti    = this;
  100.             setTimeout(function() { ti._closeAfterTime(); ti = null; }, 2000);
  101.         };
  102.     };
  103. };        
  104. ContextMenu.prototype.getHeight    = function() { return this.el.offsetHeight; };
  105. ContextMenu.prototype.getWidth    = function() { return this.el.offsetWidth; };
  106. ContextMenu.prototype.hide        = function() 
  107.     this.el.style.visibility='hidden'; 
  108.     this.el.releaseCapture(); 
  109.     this.captureSet = false; 
  110.     this.over    = false;
  111. };
  112. ContextMenu.prototype.isOn        = function() { return (this.el.style.visibility == "visible"); };
  113. ContextMenu.prototype._itemOver    = function()
  114. {
  115.     this.getElementsByTagName("SPAN")[0].style.textDecoration = "underline";
  116.     this.style.color = "black";
  117.     this.style.backgroundColor = "#A9A9A9";
  118.     this.style.cursor="hand";
  119.     System.GUI.playSFX(9);
  120.     this.parentElement.mgr.over = true;
  121. };
  122. ContextMenu.prototype._itemOut    = function()
  123. {
  124.     this.getElementsByTagName("SPAN")[0].style.textDecoration = "none";
  125.     this.style.color="black";
  126.     this.style.backgroundColor="#DCDCDC";
  127.     this.style.cursor="hand";
  128.     this.parentElement.mgr.over = false;
  129. };
  130. ContextMenu.prototype._itemDown    = function()
  131. {
  132.     if (this.onitemselect) { 
  133.         if (typeof(this.onitemselect) == "function") this.onitemselect(); 
  134.     };
  135.     this.parentElement.mgr.hide();
  136. };
  137. ContextMenu.prototype._blurMenu    = function()
  138. {
  139.     this.mgr.hide();
  140. };
  141. ContextMenu.prototype._getRealLeft     = function(el)
  142. {
  143.     if ( !("offsetLeft" in el) ) { return 0; };
  144.     var realLeft        = el.offsetLeft;
  145.     this._tempEl        = el.offsetParent;
  146.     
  147.     while (this._tempEl != null)
  148.     {
  149.         realLeft        += this._tempEl.offsetLeft;
  150.         if (this._tempEl.style) {
  151.             realLeft            += (isNaN(parseInt(this._tempEl.style.borderLeftWidth)) ) ? 0 : parseInt(this._tempEl.style.borderLeftWidth);
  152.         };
  153.         this._tempEl    =  this._tempEl.offsetParent;
  154.     };
  155.     this._tempEl = el = null;
  156.     delete this._tempEl;
  157.     return realLeft;
  158. };
  159. ContextMenu.prototype._getRealTop    = function(el)
  160. {
  161.     if ( !("offsetLeft" in el) ) { return 0; };
  162.     var realTop        = el.offsetTop;
  163.     this._tempEl        = el.offsetParent;
  164.     
  165.     while (this._tempEl != null) 
  166.     {
  167.             realTop         += this._tempEl.offsetTop;
  168.            if (this._tempEl.style) {
  169.             realTop        += (isNaN(parseInt(this._tempEl.style.borderTopWidth)) ) ? 0 : parseInt(this._tempEl.style.borderTopWidth);
  170.         };
  171.             this._tempEl =     this._tempEl.offsetParent;
  172.      };
  173.      this._tempEl = el = null;
  174.      delete this._tempEl;
  175.      return realTop;
  176. };
  177.