JavaScript实现右键菜单(三)

BSRightMenu

/**
 * <p>标题: BSRightMenu</p>
 * <p>功能描述: BS右键菜单对象。装载所有的右键菜单块</p>
 * <p>作者: BinaryStar原创B/S框架</p>
 * <p>版本: 0.1</p>
 * <p>创建日期: 2005-12-21</p>
 */

 

function  BSRightMenu(id) {
  
this.id = id||"BSRightMenu_1";//ID
  this.itemAreaList = new Array();//右键集合
  this.showItemAreaIndex = -1;//当前显示的右键菜单块
  this.clickItemIndex = -1;//当前鼠标单击所在的子菜单项索引。
  this.maxLevel = 0;//菜单树深度
  var rmlist = null;
  
this.imagePath = "";//缺省的路径
  this.popupList = new Array();//popup窗口列表

  
this.setImagesPath = function(inPath){
    
this.imagePath = inPath;
  }

  
//设置最大深度值
  this.setMaxLevel = function (inLevel){
    
if (inLevel > this.maxLevel){
      
this.maxLevel = inLevel;
    }

  }

  
//添加一个右键菜单块
  this.addItemArea = function (text){
    
var area = new BSRightItemArea(this.id, this.itemAreaList.length, text);
    
this.itemAreaList.length++;
    
this.itemAreaList[this.itemAreaList.length-1= area;
    
return area;
  }


  
//激发右键菜单
  this.doRightMenu = function (areaIndex){
    window.event.cancelBubble
=true;
   
this.rmIni();
    
var curAreaIndex = 0;
    
if (areaIndex != null){
      curAreaIndex 
= areaIndex;
    }

    
this.hiddenAll(0);
    
if (areaIndex < 0){
      event.srcElement.oncontextmenu 
= null;
      
return;
    }

    
if(window.event.button == this.itemAreaList[areaIndex].mouseType){
     
this.setRMIndex(areaIndex, -1);

      event.srcElement.oncontextmenu 
= function(){return false;};
      
//展现前的准备
      if (this.itemAreaList[areaIndex].preShowFun.Trim() != ""){
        
try{
         eval(
this.itemAreaList[areaIndex].preShowFun);
        }

        
catch(e){
         
var errStr = "*^_^*恭喜你中招了!  "+e.name+":"+e.message+"  激发右键前的准备方法 "+this.itemAreaList[areaIndex].preShowFun+" 发生严重错误!";
          
return;
        }

      }

   
//展现第一层子菜单
   this.createPopDiv(curAreaIndex);
    }

    
else{
      event.srcElement.oncontextmenu 
= null;
    }

  }


  
this.rmIni = function(){
    
if (document.getElementById(this.id + "_thisAreaIndex"== null){
      
var temp = document.createElement("input");
      temp.id 
= this.id + "_thisAreaIndex";
      temp.value 
= -1;
      temp.style.display
="none";
      temp.type 
= "hidden";
      document.body.appendChild(temp);
    }

    
if (document.getElementById(this.id + "_thisItemIndex"== null){
      
var temp = document.createElement("input");
      temp.id 
= this.id + "_thisItemIndex";
      temp.value 
= -1;
      temp.style.display
="none";
      temp.type 
= "hidden";
      document.body.appendChild(temp);
    }

  }


  
this.setRMIndex = function (areaIndex, itemIndex){
    document.getElementById(
this.id+"_thisAreaIndex").value = areaIndex;
    
this.showItemAreaIndex = areaIndex;
    document.getElementById(
this.id+"_thisItemIndex").value = itemIndex;
    
this.clickItemIndex = itemIndex;
  }


 
this.createPopDiv = function(curAreaIndex){
  
var div = null;
   
//创建popup
  if (this.popupList[0== null){
   
this.popupList[0= window.createPopup();
   
if (this.popupList[0].document.charset != "GB2312"){
    
try{
     
this.popupList[0].document.charset="GB2312";
    }

    
catch(ex){
    }

   }


   
this.popupList[0].document.oncontextmenu = function(){return false;};
  }

  
this.popupList[0].document.body.innerHTML = "";
  
this.popupList[0].show(0,0,1,1);
   
if (this.popupList[0].document.getElementById(this.id+"_ItemDiv"== null){
      div 
= this.popupList[0].document.createElement("div");
      div.style.cssText 
= bs_rm_div;
     div.id 
= this.id+"_ItemDiv";
     div.innerHTML 
= this.itemAreaList[curAreaIndex].show();
     
this.popupList[0].document.body.appendChild(div);
     div.innerHTML 
+= "<input type="hidden" id=""+this.id+"_selectItem" name=""+this.id+"_selectItem" value="-1"/>";
   }

   
else{
     div 
= this.popupList[0].document.getElementById(this.id+"_ItemDiv");
     div.innerHTML 
= this.itemAreaList[curAreaIndex].show();
     div.innerHTML 
+= "<input type="hidden" id=""+this.id+"_selectItem" name=""+this.id+"_selectItem" value="-1"/>";
   }


    
var left = window.event.clientX-1;
    
var top = window.event.clientY-1;
    
//window.status = (left+window.screenLeft);
    if (((left+window.screenLeft) + div.offsetWidth) >= window.screen.availWidth){
      left 
-= (div.offsetWidth-1);
    }

    
if(((top+window.screenTop)+div.offsetHeight) >= window.screen.availHeight){
      top 
-= (div.offsetHeight-1);
    }

  
var width = div.offsetWidth;
  
var Height = div.offsetHeight;
  
this.popupList[0].show(left, top, width, Height, document.body);
 }


  
//隐藏指定深度下所有展现的菜单
  this.hiddenAll = function(inLevel){
    
var tlevel = 0;
    
if (inLevel != null){
      
var tlevel = inLevel
    }

   
for (var i=this.popupList.length-1; i>=inLevel; i--){
    
if (this.popupList[i] !=null && this.popupList[i].isOpen){
     
this.popupList[i].hide();
     
//this.popupList[i] = null;
    }

   }

  }


  
//得到当前显示的菜单块
  this.getThisRMArea = function(){
    
return this.itemAreaList[this.showItemAreaIndex];
  }


  
//得到指定显示的菜单块
  this.getThisRMAreaByIndex = function(inIndex){
   
if (inIndex >=0 && inIndex<this.itemAreaList.length){
     
return this.itemAreaList[inIndex];
   }

   alert(
"对不起,没有找到指定的菜单块!");
   
return null;
  }


  
//得到当前显示的菜单项
  this.getThisRMItem = function(){
    
return this.getThisRMArea().itemList[this.clickItemIndex];
  }


  
//得到指定Key的菜单项
  this.getThisRMItemByKey = function(inKey, inAreaIndex){
   
var tArea = this.getThisRMArea();
   
if (tArea == null && inAreaIndex != null){
    tArea 
= this.getThisRMAreaByIndex(inAreaIndex);
   }

   
if (tArea != null){
    
for (var i=0; i<tArea.itemList.length; i++){
     
if (tArea.itemList[i].key == inKey){
      
return tArea.itemList[i];
     }

    }

   }

   alert(
"对不起,没有找到指定的菜单项!");
    
return null;
  }


  
//得到父亲名称
  this.getRMName = function(level){
   
var fname = "";
   
for (var i=0; i<level+1; i++){
    fname 
+= "parent."
   }

   fname 
+= this.id;
   
return fname;
  }

}

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mynickel2000

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值