JavaScript实现右键菜单(二)

BSRightItem

/**
 * <p>标题: BSRightItem</p>
 * <p>功能描述: 右键菜单项。</p>
 * <p>作者: BinaryStar原创B/S框架</p>
 * <p>版本: 0.1</p>
 * <p>创建日期: 2005-12-21</p>
 */

 

function  BSRightItem(pid, areaIndex, areaId,  pIndex, index, text, jsfun, img, disabled) {
  
this.pid = pid||"BSRightMenu_1";//BS右键菜单对象ID
  this.areaIndex = areaIndex;//右键块索引
  this.areaId = areaId||"";//BS右键菜单块ID
  this.pIndex = pIndex;//父菜单项索引

  
this.level = 0;//树的深度
  this.index = index;//本菜单项索引
  this.id = this.areaId + "_item_" + this.index;//ID
  this.text = text || "BS菜单项";//菜单项文字
  this.jsfun = jsfun;//菜单项js函数
  this.img = img||"";//菜单项图片
  this.childList = new Array();//该菜单项包含的子菜单项
  this.isSperator = false;//是否是分隔符
  this.thisItemIndex = -1;//当前鼠标所在的子菜单项索引。
  this.disabled = disabled || false;//该菜单项是否可用
  this.childIsShow = false;//孩子菜单正在展现
  this.key = "";//右键菜单项的key;
  this.body = null;//该菜单项的对象;
  this.isError = false;//该节点点击出错


  
this.setDisabled = function (flag){
    
if (flag){
      
this.disabled = true;
    }

    
else{
      
this.disabled = false;
    }

  }


  
//添加子菜单项
  this.addItem = function (text, jsfun, img, disabled){
   
var rmObj = eval(this.pid);
 
return rmObj.itemAreaList[this.areaIndex].addItem(this.index, text, jsfun, img, disabled);
  }


  
//菜单项的展现
  this.show = function(){
   
var rmObj = eval(this.pid);
    
var htmlStr = "";
   
var pName = rmObj.getRMName(this.level);
    htmlStr 
+= "<tr id=""+this.id+"_tr" style=""+bs_rm_out+""";
    htmlStr 
+= " οnmοuseοver=""+pName+".itemAreaList["+this.areaIndex+"].itemList["+this.index+"].doOnmouseover(event, this)"";
    htmlStr 
+= " οnmοuseοut=""+pName+".itemAreaList["+this.areaIndex+"].itemList["+this.index+"].doOnmouseout(event, this)"";
    
if (!this.isSperator && !this.disabled){
      htmlStr 
+= " οnmοuseup=""+pName+".itemAreaList["+this.areaIndex+"].itemList["+this.index+"].doOnmouseup(event, this)"";
      htmlStr 
+= " οnmοusedοwn=""+pName+".itemAreaList["+this.areaIndex+"].itemList["+this.index+"].doOnmousedown(event, this)"";
    }

    
else{
      htmlStr 
+= " οnmοuseup="event.cancelBubble=true;return false;"";
      htmlStr 
+= " οnmοusedοwn="event.cancelBubble=true;return false;"";
    }

    htmlStr 
+= " οnclick=window.event.cancelBubble=true;return false;";
    htmlStr 
+= ">";
    
//图片
    if (!this.isSperator){
      htmlStr 
+= "<td align="right" ";
      
if (this.img.Trim() != ""){
        htmlStr 
+= "style="width:22px;height:22px;";
        htmlStr 
+= "background: url("+rmObj.imagePath+this.img+");";
        htmlStr 
+= "background-repeat: no-repeat;";
        htmlStr 
+= "background-attachment: no-fixed;";
        htmlStr 
+= "background-position: right;"";
        htmlStr 
+= " valign="middle">&nbsp;&nbsp;&nbsp;</td>";
      }

      
else{
        htmlStr 
+= "style="width:2px;height:22px;";
        htmlStr 
+= " valign="middle">&nbsp;</td>";
      }

      
//文字
      var tempText = this.text;
      
var isLong = false;
      
var cn = 0;
      
if (tempText.match(/[^ -~]/g) != null){
       cn 
= tempText.match(/[^ -~]/g).length;
      }


      
if ((tempText.length-cn)+cn*2 >= 18){
       
if (cn <= 3){
        tempText 
= tempText.substring(018)+"...";
       }

       
else {
        tempText 
= tempText.substring(010)+"...";
       }

       isLong 
= true;
      }

      htmlStr 
+= "<td valign="middle" οnmοuseοver="function(){return false;}"";
      
if (isLong){
       htmlStr 
+= " title=""+this.text+""";
      }

      
if (this.disabled){
        htmlStr 
+= " style="color:Gray;white-space:nowrap;word-break:word-break ;"";
      }

      htmlStr 
+= "><nobr οnmοuseοver="function(){return false;}">"+tempText+"&nbsp;&nbsp;</nobr></td>";
      
//存在孩子时显示箭头。
      htmlStr += "<td style='font-family: webdings;'>";
      
if (this.childList.length > 0){
        htmlStr 
+= "4";
      }

        htmlStr 
+= "</td>";
    }

    
else{
      
//分隔符
      htmlStr += "<td colspan="3" height="5"><hr style=""+bs_rm_sperator+""/></td>";
    }

    htmlStr 
+= "</tr>";
    
return htmlStr;
  }


  
//设置图片的背景色
  this.setImgSelect = function(elmObj, flag){
   
//文字背景
   if (!flag){
   elmObj.style.cssText 
= bs_rm_out;
   }

   
else if (this.isError){
   elmObj.style.cssText 
= bs_rm_error;
   }

   
else {
   elmObj.style.cssText 
= bs_rm_over;
   }

   
//图片背景
    if (this.img.Trim() != ""){
      
if (flag){
        elmObj.cells[
0].style.backgroundColor = "#8989bc";
      }

      
else{
        elmObj.cells[
0].style.backgroundColor = "";
      }

    }

  }


  
//展现下一级菜单
 this.showChildren = function(event){
    
var pobj = eval(this.pid);
   
var the_obj = event.srcElement.parentElement;
   
if (the_obj.tagName == "TD"){
    the_obj 
= the_obj.parentElement;
   }

   
if (the_obj.id == this.id+"_tr_str"){
    the_obj 
= the_obj.parentElement;
   }

    
if (this.childList.length > 0 && !this.disabled){
      
//创建popup
   if (pobj.popupList[this.level+1== null){
    pobj.popupList[
this.level+1= pobj.popupList[this.level].document.parentWindow.createPopup();
    pobj.popupList[
this.level+1].document.oncontextmenu = function(){return false;};
    
if (pobj.popupList[this.level+1].document.charset != "GB2312"){
     
try{
      pobj.popupList[
this.level+1].document.charset="GB2312";
     }

     
catch(ex){
     }

    }

   }

   pobj.popupList[
this.level+1].document.body.innerHTML = "";
   pobj.popupList[
this.level+1].show(0,0,1,1);

      
var htmlStr = "<table border='0' cellspacing='0'>";
      
for (var i=0; i<this.childList.length; i++){
        htmlStr 
+= pobj.itemAreaList[this.areaIndex].itemList[this.childList[i]].show();
      }

      htmlStr 
+= "</table>";
      
var levelObj = null;
      levelObj 
= pobj.popupList[this.level+1].document.getElementById(this.pid+"_rm_"+(this.level+1));
      
//level+1层没有创建
      if (levelObj == null){
        levelObj 
= pobj.popupList[this.level+1].document.createElement("div");
        levelObj.id 
= this.pid+"_rm_"+(this.level+1);
        levelObj.style.cssText 
= bs_rm_div;
        pobj.popupList[
this.level+1].document.body.appendChild(levelObj);
      }

      htmlStr 
+= "<input type="hidden" id=""+pobj.id+"_selectItem" name=""+pobj.id+"_selectItem" value="-1"/>";
      
//alert(htmlStr);
      levelObj.innerHTML = htmlStr;
      levelObj.style.display 
= "block";
      
this.childIsShow = true;
   
var width = levelObj.offsetWidth;
   
var Height = levelObj.offsetHeight;
      
//定位
   var left = GetDefineX(the_obj)+the_obj.offsetWidth;
   
var top = GetDefineY(the_obj);
      
if(((left+pobj.popupList[this.level].document.parentWindow.screenLeft)+levelObj.offsetWidth) >= window.screen.availWidth){
        left 
-= (the_obj.offsetWidth + levelObj.offsetWidth-4);
      }

      
if(((top+pobj.popupList[this.level].document.parentWindow.screenTop)+levelObj.offsetHeight) > window.screen.availHeight){
        top 
-= (levelObj.offsetHeight-the_obj.offsetHeight);
      }

   pobj.popupList[
this.level+1].show(left, top, width, Height, pobj.popupList[this.level].document.body);
    }

   
else{
    pobj.hiddenAll(
this.level+1);
  }

 }


  
//鼠标按下动作
  this.doOnmousedown = function(event, elmObj){
    event.cancelBubble
=true;
    
if (this.childList.length <= 0){
      elmObj.setCapture();
    }

    
return false;
  }

  
//鼠标弹起动作
  this.doOnmouseup = function(event, elmObj){
    event.cancelBubble
=true;
    
if (this.childList.length <= 0){
      
var pobj = eval(this.pid);
      pobj.setRMIndex(
this.areaIndex, this.index);
      elmObj.releaseCapture();
      
if (this.jsfun.Trim() != ""){
        
try{
         eval(
this.jsfun);
       
this.isError = false;
        }

        
catch(e){
         
var errStr = "*^_^*恭喜你中招了!  "+e.name+":"+e.message+"  节点操作方法 "+this.jsfun+" 发生严重错误!";
       elmObj.style.cssText 
= bs_rm_error;
       
this.isError = true;
       elmObj.title 
= errStr;
          
return;
        }

      }

      pobj.hiddenAll(
0);
    }

    
return false;
  }

  
//鼠标移入动作
  this.doOnmouseover = function(event, elmObj){
    event.cancelBubble
=true;
    
if (!this.isSperator){
      
this.setImgSelect(elmObj, true);
    }

    
//改变其他的项的高选
    var pobj = eval(this.pid);
    
if (!pobj.itemAreaList[this.areaIndex].setIndexItem(this.level, this.index)){
     
this.showChildren(event);
    }

   
else if (this.childList.length <= 0){
     pobj.hiddenAll(
this.level+1);
  }

  pobj.setRMIndex(
this.areaIndex, this.index);
    
return false;
  }


  
//鼠标移出动作
  this.doOnmouseout = function(event, elmObj){
    event.cancelBubble
=true;
    
if (!this.isSperator && !this.childIsShow){
      
this.setImgSelect(elmObj, false);
    }

    
return false;
  }

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mynickel2000

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

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

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

打赏作者

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

抵扣说明:

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

余额充值