JavaScript实现右键菜单(三)

来源:互联网 发布:淘宝店铺名片设计网站 编辑:程序博客网 时间:2024/05/01 05:22
 

/**
 * <p>标题: BSRightMenu</p>
 * <p>功能描述: BS右键菜单对象。装载所有的右键菜单块</p>
 * <p>作者: BinaryStar</p>
 * <p>版本: 0.1</p>
 * <p>创建日期: 2005-01-18</p>
 */
function BSRightMenu(id){
  this.id = id||"BSRightMenu_1";//ID
  this.itemAreaList = new Array();//右键集合
  this.showItemAreaIndex = -1//当前显示的右键菜单块
  this.maxLevel = 0;//菜单树深度
  var rmlist = null;
  //设置最大深度值
  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;
    var curAreaIndex = 0;
    if (areaIndex != null){
      curAreaIndex = areaIndex;
    }
    this.hiddenAllRM();
    if (areaIndex < 0){
      document.oncontextmenu = null;
      return;
    }
    if(window.event.button == 2){
      document.oncontextmenu = function(){return false;};
      var left = document.body.scrollLeft+window.event.clientX-1;
      var top = document.body.scrollTop+window.event.clientY-1;
      var div = null;
      if (document.getElementById(this.id+"_rm_0") != null){
        div = document.getElementById(this.id+"_rm_0");
      }
      else{
        div = document.createElement("div");
        div.id = this.id+"_rm_0";
        div.className = "bs_rm_div";
        document.body.appendChild(div);
      }
      div.style.display = "block";
      var tempHTML = "<input type=/"hidden/" id=/""+this.id + "_thisItemIndex/" value=/"-1/"/><input type=/"hidden/" id=/""+this.id + "_thisAreaIndex/" value=/"-1/"/>"
      if (document.getElementById("BS_RightMenu_List") == null){
        temprmlist = document.createElement("input");
        temprmlist.id = "BS_RightMenu_List";
        temprmlist.className = "bs_rm_div";
        temprmlist.value = this.id;
        temprmlist.style.display="none";
        temprmlist.type = "hidden";
        document.body.appendChild(temprmlist);
      }
      else {
        this.addBSRMList(this.id);
      }
      div.innerHTML = this.itemAreaList[curAreaIndex].show() + tempHTML;
      if ((left + div.offsetWidth) > document.body.scrollLeft+document.body.clientWidth){
        left -= div.offsetWidth;
      }
      if((top+div.offsetHeight) > document.body.scrollTop+document.body.clientHeight){
        top -= (div.offsetHeight);
      }
      div.style.left = left;
      div.style.top = top;
    }
    else{
      document.oncontextmenu = null;
    }
  }

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

  //隐藏指定深度下所有展现的菜单
  this.hiddenAll = function(inLevel){
    var tlevel = 0;
    if (inLevel != null){
      var tlevel = inLevel
    }
    for (var i=tlevel; i<this.maxLevel+1; i++){
      var divObj = document.getElementById(this.id+"_rm_"+i);
      if (divObj != null){
        divObj.style.display = "none";
      }
    }
  }

  //隐藏所有右键菜单
  this.hiddenAllRM = function(){
    if (document.getElementById("BS_RightMenu_List") == null){
      return;
    }
    var rmlist = document.getElementById("BS_RightMenu_List").value.split(",");
    for (var i=0; i<rmlist.length; i++){
      if (rmlist[i].Trim() != ""){
        var temp_rm = eval(rmlist[i].Trim());
        temp_rm.hiddenAll(0);
      }
    }
  }

  //检查右键菜单
  this.addBSRMList = function(inID){
    var rmlist = document.getElementById("BS_RightMenu_List").value.split(",");
    for (var i=0; i<rmlist.length; i++){
      if (rmlist[0].Trim() == inID.Trim()){
        return;
      }
    }
    document.getElementById("BS_RightMenu_List").value += ("," + inID);
  }
}

原创粉丝点击