构建权限树
List<Privilege> privileges4Menu;
privileges4Menu = this.privilegeService.findAllPrivilege4Menu(roleList);
session.put("privileges4Menu", privileges4Menu);
// 构造菜单显示的权限树
List<PrivilegeBean> privilegeTree = BuildPrivilegeTree
.buildPrivilegeTree(contextRoot, assignedPrivileges,
assignedPrivileges, false);
session.put("privielgeTree", privilegeTree);
/*
* 工程名:
* 包 名: com.companyName.dhm.XXX.role.web.action
* 文 件名: BuildPrivilegeTree.java
* 版 权: Copyright (c) 2016 companyName All Rights Reserved.
* 描 述: 构造权限树的工具类
* 修 改 人:
* 修改时间:
* 跟踪单号:
* 修改单号:
* 修改内容:
*/
package com.companyName.dhm.cbb.role.web.action;
import java.util.ArrayList;
import java.util.List;
import com.companyName.dhm.cbb.role.entity.Privilege;
import com.companyName.dhm.cbb.role.entity.PrivilegeBean;
/**
*
* 构造权限树的工具类
*
* @author
* @version
* @since
*/
public class BuildPrivilegeTree {
private BuildPrivilegeTree() {}
/**
* 构造权限树。
* 首先先构造一级节点的权限树,然后再构造二级节点的权限树,再构造三级节点的权限,最后构造一颗完整的权限树。
* @param contextRoot 应用名
* @param allPrivileges 系统中所有的权限列表
* @param assignedPrivileges 已分配给用户的权限列表
* @param isFullTree 是否构造一颗完整的树
*
* @return 返回list,代表一棵权限树节点数据。
*
* @return List<PrivilegeBean> 返回list,代表一棵权限树节点数据。
*/
public static List<PrivilegeBean> buildPrivilegeTree( String contextRoot, List<Privilege> allPrivileges, List<Privilege> assignedPrivileges, boolean isFullTree ) {
//获取第一层树节点数据
List<PrivilegeBean> treeRoot = getPrivilegeTreeBy( contextRoot, allPrivileges, assignedPrivileges, 1 );
//获取第二层树节点数据
List<PrivilegeBean> secondTreeLayer = getPrivilegeTreeBy( contextRoot, allPrivileges, assignedPrivileges, 2);
List<PrivilegeBean> threedTreeLayer = getPrivilegeTreeBy( contextRoot, allPrivileges, assignedPrivileges, 3);
List<PrivilegeBean> fourthTreeLayer ;
if ( isFullTree && threedTreeLayer != null) {
//获取第三层树节点数据
fourthTreeLayer = getPrivilegeTreeBy(contextRoot, allPrivileges, assignedPrivileges, 4);
//构造一棵权限树
constructPrivilegeTree(treeRoot, secondTreeLayer, threedTreeLayer,fourthTreeLayer);
} else if(threedTreeLayer != null){
//构造一棵权限树
constructPrivilegeTree(treeRoot, secondTreeLayer, threedTreeLayer,new ArrayList<PrivilegeBean>(0) );
}else{
constructPrivilegeTree(treeRoot, secondTreeLayer, new ArrayList<PrivilegeBean>(0), new ArrayList<PrivilegeBean>(0));
}
return treeRoot;
}
/**
* 建造一颗权限树。
* @param treeRoot 第一层树节点列表
* @param secondTreeLayer 第二层树节点列表
* @param threedTreeLayer 第三层树节点列表
* @return 返回已经构造的一颗权限树。
*
* @return List<PrivilegeBean> 返回已经构造的一颗权限树。
*/
private static List<PrivilegeBean> constructPrivilegeTree( List<PrivilegeBean> treeRoot, List<PrivilegeBean> secondTreeLayer, List<PrivilegeBean> threedTreeLayer,List<PrivilegeBean> fourthTreeLayer ) {
//第二棵子树
List<PrivilegeBean> p1SubTree = null;
//第三棵子树
List<PrivilegeBean> p2SubTree = null;
//第四棵子树
List<PrivilegeBean> p3SubTree = null;
//第一层树遍历
for( PrivilegeBean p1 : treeRoot ) {
int pid1 = p1.getPrivilegeId();
//构造第二棵子树
p1SubTree = new ArrayList<PrivilegeBean>();
p1.setSubPrivileges(p1SubTree);
//第二层树遍历
for( PrivilegeBean p2 : secondTreeLayer ) {
int pid2 = p2.getPrivilegeId();
int p2ParentPid = p2.getParentPrivilegeId();
//构造第三棵子树
p2SubTree = new ArrayList<PrivilegeBean>();
p2.setSubPrivileges(p2SubTree);
if ( p2ParentPid == pid1 ) {
p1SubTree.add( p2 );
p1.setHasSubNode( 1 );//表示父节点有子节点
}
//第三层树遍历
for( PrivilegeBean p3 : threedTreeLayer ) {
int pid3 = p3.getPrivilegeId();
int p3ParentPid = p3.getParentPrivilegeId();
p3SubTree = new ArrayList<PrivilegeBean>();
p3.setSubPrivileges(p3SubTree);
if( p3ParentPid == pid2 ) {
p2SubTree.add( p3 );
p2.setHasSubNode( 1 );//表示父节点有子节点
}
for(PrivilegeBean p4:fourthTreeLayer){
int p4ParentPid = p4.getParentPrivilegeId();
if(p4ParentPid == pid3){
p3SubTree.add(p4);
p3.setHasSubNode(1);
}
}
}
}
}
return treeRoot;
}
/**
* 将系统中查询的权限实体列表,转换成含PrivilegeBean对象的权限列表,然后用该列表,在页面上展现一颗完整的权限树。
* @param contextRoot 应用名
* @param allPrivileges 系统中所有权限列表
* @param assignedPrivileges 已分配的权限列表
* @param privilegeLevel 权限等级
* @return 返回用于页面展现的权限列表
*
* @return List<PrivilegeBean> 返回用于页面展现的权限列表
*/
private static List<PrivilegeBean> getPrivilegeTreeBy( String contextRoot, List<Privilege> allPrivileges, List<Privilege> assignedPrivileges, int privilegeLevel ) {
List<PrivilegeBean> tree = new ArrayList<PrivilegeBean>();
for( Privilege privilege : allPrivileges ) {
if ( privilege.getPrivilegeLevel() == privilegeLevel ) {
int privilegeId = privilege.getPrivilegeId();
String privilegeName = privilege.getPrivilegeName();
String privilegeURL = privilege.getPrivilegeURL();
if ( privilegeURL != null ) {
privilegeURL = contextRoot + privilegeURL;
}
int parentPrivilegeId = privilege.getPrivilegeParentId();
int assigned = 0;
PrivilegeBean privilegeBean = new PrivilegeBean(privilegeId, privilegeName, privilegeURL, parentPrivilegeId);
for( Privilege assignedPrivilege : assignedPrivileges ) {
if( assignedPrivilege.getPrivilegeId() == privilegeId ) {
assigned = 1;
break;
}
}
privilegeBean.setAssigned( assigned );
tree.add( privilegeBean );
}
}
return tree;
}
}
package com.companyName.dhm.cbb.role.entity;
import java.io.Serializable;
public class Privilege implements Serializable {
private static final long serialVersionUID = -8811863695015542982L;
/**权限编号*/
private int privilegeId;
/**权限名称*/
private String privilegeName;
/**http请求URL*/
private String privilegeURL;
/**父编号*/
private int privilegeParentId;
/**权限等级,用于权限树的展现*/
private int privilegeLevel;
public int getPrivilegeId() {
return privilegeId;
}
public void setPrivilegeId(int privilegeId) {
this.privilegeId = privilegeId;
}
public String getPrivilegeName() {
return privilegeName;
}
public void setPrivilegeName(String privilegeName) {
this.privilegeName = privilegeName;
}
public String getPrivilegeURL() {
return privilegeURL;
}
public void setPrivilegeURL(String privilegeURL) {
this.privilegeURL = privilegeURL;
}
public int getPrivilegeParentId() {
return privilegeParentId;
}
public void setPrivilegeParentId(int privilegeParentId) {
this.privilegeParentId = privilegeParentId;
}
public int getPrivilegeLevel() {
return privilegeLevel;
}
public void setPrivilegeLevel(int privilegeLevel) {
this.privilegeLevel = privilegeLevel;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Privilege[privilegeId=");
builder.append(privilegeId);
builder.append(", privilegeLevel=");
builder.append(privilegeLevel);
builder.append(", privilegeName=");
builder.append(privilegeName);
builder.append(", privilegeParentId=");
builder.append(privilegeParentId);
builder.append(", privilegeURL=");
builder.append(privilegeURL);
builder.append("]");
return builder.toString();
}
}
package com.companyName.dhm.cbb.role.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
* 权限树展现的实体Bean。
*
*/
public class PrivilegeBean implements Serializable {
private static final long serialVersionUID = 4049990199216441887L;
private String label;
/**权限编号*/
private int privilegeId;
/**权限URL*/
private String privilegeURL;
/**权限名称*/
private String privilegeName;
private int assigned; //1表示授予角色,0表示未授予角色
/**权限父节点ID*/
private int parentPrivilegeId;
private int hasSubNode;//1表示有子节点,0表示没有子节点
public int getParentPrivilegeId() {
return parentPrivilegeId;
}
public void setParentPrivilegeId(int parentPrivilegeId) {
this.parentPrivilegeId = parentPrivilegeId;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
private List<PrivilegeBean> subPrivileges = new ArrayList<PrivilegeBean>();
public PrivilegeBean() {}
public PrivilegeBean( int privilegeId, String privilegeName, String privilegeURL, int parentPrivilegeId ) {
this.privilegeId = privilegeId;
this.privilegeName = privilegeName;
if ( privilegeURL == null ) {
privilegeURL = "#";
}
this.privilegeURL = privilegeURL ;
this.parentPrivilegeId = parentPrivilegeId;
this.label = this.privilegeURL + "_" +this.privilegeId;
}
public int getPrivilegeId() {
return privilegeId;
}
public void setPrivilegeId(int privilegeId) {
this.privilegeId = privilegeId;
}
public String getPrivilegeURL() {
return privilegeURL;
}
public void setPrivilegeURL(String privilegeURL) {
this.privilegeURL = privilegeURL;
}
public String getPrivilegeName() {
return privilegeName;
}
public void setPrivilegeName(String privilegeName) {
this.privilegeName = privilegeName;
}
public List<PrivilegeBean> getSubPrivileges() {
return subPrivileges;
}
public void setSubPrivileges(List<PrivilegeBean> subPrivileges) {
this.subPrivileges = subPrivileges;
}
public int getAssigned() {
return assigned;
}
public void setAssigned(int assigned) {
this.assigned = assigned;
}
public int getHasSubNode() {
return hasSubNode;
}
public void setHasSubNode(int hasSubNode) {
this.hasSubNode = hasSubNode;
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><s:text name="rolemgr.name" />――<s:text
name="rolemgr.privilege.management" />
</title>
<link href="../style/dtree.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/dtree.js"></script>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#switch_frame").click(function(){
frameSwitch();
});
});
function frameSwitch()
{
if(parent.document.getElementById("switchFrame").cols == "221,*")
{
$("#switch_frame").find("img").attr("src","../images/btn_sopen.gif");
parent.document.getElementById("switchFrame").cols = "8,*";
top.frames["leftFrame"].document.getElementById("switch_frame").style.left = "1px";
}
else
{
$("#switch_frame").find("img").attr("src","../images/btn_sclose.gif");
parent.document.getElementById("switchFrame").cols = "221,*";
top.frames["leftFrame"].document.getElementById("switch_frame").style.left = "200px";
}
}
</script>
<title>menu</title>
<style type="text/css">
body{background:#EFF5FF;margin:0px;padding:0px;}
#menu{width:221px;background:#EFF5FF; position:absolute;left:0px;top:28px;}
.dTreeNode_sys{width:76px;margin:5px 0px 0px 70px;font-size:16px;font-weight:bolder;color: #0E2D5F;position:absolute;left:0px;top:0px;}
</style>
</head>
<body>
<div id="switch_frame" οnclick="" align="right"><img src="../images/btn_sclose.gif" /></div>
<div class="dTreeNode_sys">报表系统</div>
<!--menu edit-->
<div id="menu">
<script type="text/javascript">
d = new dTree('d');
d.add(1000, -1, '', '', '');
<s:iterator value="#session.privileges4Menu" var="privilege">
d.add(<s:property value="#privilege.privilegeId" />,
<s:property value="#privilege.privilegeParentId" />,
'<s:property value="#privilege.privilegeName" />',
"<s:property value="#privilege.privilegeURL" />" == "#" ? ''
: '/biPortal<s:property value="#privilege.privilegeURL" />',
'', 'mainFrame');
</s:iterator>
document.write(d);
</script>
</div>
</body>
</html>
dtree.js
/*--------------------------------------------------|
| dTree 2.05 | www.destroydrop.com/javascript/tree/ |
|---------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landr� |
| |
| This script can be used freely as long as all |
| copyright messages are intact. |
| |
| Updated: 17.04.2003 |
|--------------------------------------------------*/
// Node object
function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
this.id = id;
this.pid = pid;
this.name = name;
this.url = url;
this.title = title;
this.target = target;
this.icon = icon;
this.iconOpen = iconOpen;
this._io = open || false;
this._is = false;
this._ls = false;
this._hc = false;
this._ai = 0;
this._p;
};
// Tree object
function dTree(objName) {
this.config = {
target : null,
folderLinks : true,
useSelection : true,
useCookies : false,
useLines : true,
useIcons : true,
useStatusText : false,
closeSameLevel : false,
inOrder : false
}
this.icon = {
root : '../images/base.gif',
folder : '../images/folder.gif',
folderOpen : '../images/folderopen.gif',
node : '../images/chart.png',
empty : '../images/empty.gif',
line : '../images/line.gif',
join : '../images/join.gif',
joinBottom : '../images/joinbottom.gif',
plus : '../images/plus.gif',
plusBottom : '../images/plusbottom.gif',
minus : '../images/minus.gif',
minusBottom : '../images/minusbottom.gif',
nlPlus : '../images/nolines_plus.gif',
nlMinus : '../images/nolines_minus.gif'
};
this.obj = objName;
this.aNodes = [];
this.aIndent = [];
this.root = new Node(-1);
this.selectedNode = null;
this.selectedFound = false;
this.completed = false;
};
// Adds a new node to the node array
dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
};
// Open/close all nodes
dTree.prototype.openAll = function() {
this.oAll(true);
};
dTree.prototype.closeAll = function() {
this.oAll(false);
};
// Outputs the tree to the page
dTree.prototype.toString = function() {
var str = '<div class="dtree">\n';
if (document.getElementById) {
if (this.config.useCookies) this.selectedNode = this.getSelected();
str += this.addNode(this.root);
} else str += 'Browser not supported.';
str += '</div>';
if (!this.selectedFound) this.selectedNode = null;
this.completed = true;
return str;
};
// Creates the tree structure
dTree.prototype.addNode = function(pNode) {
var str = '';
var n=0;
if (this.config.inOrder) n = pNode._ai;
for (n; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == pNode.id) {
var cn = this.aNodes[n];
cn._p = pNode;
cn._ai = n;
this.setCS(cn);
if (!cn.target && this.config.target) cn.target = this.config.target;
if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
if (!this.config.folderLinks && cn._hc) cn.url = null;
if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
cn._is = true;
this.selectedNode = n;
this.selectedFound = true;
}
str += this.node(cn, n);
if (cn._ls) break;
}
}
return str;
};
// Creates the node icon, url and text
dTree.prototype.node = function(node, nodeId) {
var str = '<div class="dTreeNode '+((node._hc) ? 'dTreeNodeFolder' : '')+'" title="'+node.name+'">' + this.indent(node, nodeId);
if (this.config.useIcons) {
if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
if (this.root.id == node.pid) {
node.icon = this.icon.root;
node.iconOpen = this.icon.root;
}
str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
}
if (node.url) {
str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
if (node.title) str += ' title="' + node.title + '"';
if (node.target) str += ' target="' + node.target + '"';
if (this.config.useStatusText) str += ' οnmοuseοver="window.status=\'' + node.name + '\';return true;" οnmοuseοut="window.status=\'\';return true;" ';
if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
str += ' οnclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
str += '>';
}
else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
str += node.name;
if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
str += '</div>';
if (node._hc) {
str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
str += this.addNode(node);
str += '</div>';
}
this.aIndent.pop();
return str;
};
// Adds the empty and line icons
dTree.prototype.indent = function(node, nodeId) {
var str = '';
if (this.root.id != node.pid) {
for (var n=0; n<this.aIndent.length; n++)
str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
(node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
if (node._hc) {
str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
str += '" alt="" /></a>';
} else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
}
return str;
};
// Checks if a node has any children and if it is the last sibling
dTree.prototype.setCS = function(node) {
var lastId;
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == node.id) node._hc = true;
if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
}
if (lastId==node.id) node._ls = true;
};
// Returns the selected node
dTree.prototype.getSelected = function() {
var sn = this.getCookie('cs' + this.obj);
return (sn) ? sn : null;
};
// Highlights the selected node
dTree.prototype.s = function(id) {
if (!this.config.useSelection) return;
var cn = this.aNodes[id];
if (cn._hc && !this.config.folderLinks) return;
if (this.selectedNode != id) {
if (this.selectedNode || this.selectedNode==0) {
eOld = document.getElementById("s" + this.obj + this.selectedNode);
eOld.className = "node";
}
eNew = document.getElementById("s" + this.obj + id);
eNew.className = "nodeSel";
this.selectedNode = id;
if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
}
};
// Toggle Open or close
dTree.prototype.o = function(id) {
var cn = this.aNodes[id];
this.nodeStatus(!cn._io, id, cn._ls);
cn._io = !cn._io;
if (this.config.closeSameLevel) this.closeLevel(cn);
if (this.config.useCookies) this.updateCookie();
};
// Open or close all nodes
dTree.prototype.oAll = function(status) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
this.nodeStatus(status, n, this.aNodes[n]._ls)
this.aNodes[n]._io = status;
}
}
if (this.config.useCookies) this.updateCookie();
};
// Opens the tree to a specific node
dTree.prototype.openTo = function(nId, bSelect, bFirst) {
if (!bFirst) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].id == nId) {
nId=n;
break;
}
}
}
var cn=this.aNodes[nId];
if (cn.pid==this.root.id || !cn._p) return;
cn._io = true;
cn._is = bSelect;
if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
if (this.completed && bSelect) this.s(cn._ai);
else if (bSelect) this._sn=cn._ai;
this.openTo(cn._p._ai, false, true);
};
// Closes all nodes on the same level as certain node
dTree.prototype.closeLevel = function(node) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
this.nodeStatus(false, n, this.aNodes[n]._ls);
this.aNodes[n]._io = false;
this.closeAllChildren(this.aNodes[n]);
}
}
}
// Closes all children of a node
dTree.prototype.closeAllChildren = function(node) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
this.aNodes[n]._io = false;
this.closeAllChildren(this.aNodes[n]);
}
}
}
// Change the status of a node(open or closed)
dTree.prototype.nodeStatus = function(status, id, bottom) {
eDiv = document.getElementById('d' + this.obj + id);
eJoin = document.getElementById('j' + this.obj + id);
if (this.config.useIcons) {
eIcon = document.getElementById('i' + this.obj + id);
eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
}
eJoin.src = (this.config.useLines)?
((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
((status)?this.icon.nlMinus:this.icon.nlPlus);
eDiv.style.display = (status) ? 'block': 'none';
};
// [Cookie] Clears a cookie
dTree.prototype.clearCookie = function() {
var now = new Date();
var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
this.setCookie('co'+this.obj, 'cookieValue', yesterday);
this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
};
// [Cookie] Sets value in a cookie
dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
document.cookie =
escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
};
// [Cookie] Gets a value from a cookie
dTree.prototype.getCookie = function(cookieName) {
var cookieValue = '';
var posName = document.cookie.indexOf(escape(cookieName) + '=');
if (posName != -1) {
var posValue = posName + (escape(cookieName) + '=').length;
var endPos = document.cookie.indexOf(';', posValue);
if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
else cookieValue = unescape(document.cookie.substring(posValue));
}
return (cookieValue);
};
// [Cookie] Returns ids of open nodes as a string
dTree.prototype.updateCookie = function() {
var str = '';
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
if (str) str += '.';
str += this.aNodes[n].id;
}
}
this.setCookie('co' + this.obj, str);
};
// [Cookie] Checks if a node id is in a cookie
dTree.prototype.isOpen = function(id) {
var aOpen = this.getCookie('co' + this.obj).split('.');
for (var n=0; n<aOpen.length; n++)
if (aOpen[n] == id) return true;
return false;
};
// If Push and pop is not implemented by the browser
if (!Array.prototype.push) {
Array.prototype.push = function array_push() {
for(var i=0;i<arguments.length;i++)
this[this.length]=arguments[i];
return this.length;
}
};
if (!Array.prototype.pop) {
Array.prototype.pop = function array_pop() {
lastElement = this[this.length-1];
this.length = Math.max(this.length-1,0);
return lastElement;
}
};
List<Privilege> privileges4Menu;
privileges4Menu = this.privilegeService.findAllPrivilege4Menu(roleList);
session.put("privileges4Menu", privileges4Menu);
// 构造菜单显示的权限树
List<PrivilegeBean> privilegeTree = BuildPrivilegeTree
.buildPrivilegeTree(contextRoot, assignedPrivileges,
assignedPrivileges, false);
session.put("privielgeTree", privilegeTree);
/*
* 工程名:
* 包 名: com.companyName.dhm.XXX.role.web.action
* 文 件名: BuildPrivilegeTree.java
* 版 权: Copyright (c) 2016 companyName All Rights Reserved.
* 描 述: 构造权限树的工具类
* 修 改 人:
* 修改时间:
* 跟踪单号:
* 修改单号:
* 修改内容:
*/
package com.companyName.dhm.cbb.role.web.action;
import java.util.ArrayList;
import java.util.List;
import com.companyName.dhm.cbb.role.entity.Privilege;
import com.companyName.dhm.cbb.role.entity.PrivilegeBean;
/**
*
* 构造权限树的工具类
*
* @author
* @version
* @since
*/
public class BuildPrivilegeTree {
private BuildPrivilegeTree() {}
/**
* 构造权限树。
* 首先先构造一级节点的权限树,然后再构造二级节点的权限树,再构造三级节点的权限,最后构造一颗完整的权限树。
* @param contextRoot 应用名
* @param allPrivileges 系统中所有的权限列表
* @param assignedPrivileges 已分配给用户的权限列表
* @param isFullTree 是否构造一颗完整的树
*
* @return 返回list,代表一棵权限树节点数据。
*
* @return List<PrivilegeBean> 返回list,代表一棵权限树节点数据。
*/
public static List<PrivilegeBean> buildPrivilegeTree( String contextRoot, List<Privilege> allPrivileges, List<Privilege> assignedPrivileges, boolean isFullTree ) {
//获取第一层树节点数据
List<PrivilegeBean> treeRoot = getPrivilegeTreeBy( contextRoot, allPrivileges, assignedPrivileges, 1 );
//获取第二层树节点数据
List<PrivilegeBean> secondTreeLayer = getPrivilegeTreeBy( contextRoot, allPrivileges, assignedPrivileges, 2);
List<PrivilegeBean> threedTreeLayer = getPrivilegeTreeBy( contextRoot, allPrivileges, assignedPrivileges, 3);
List<PrivilegeBean> fourthTreeLayer ;
if ( isFullTree && threedTreeLayer != null) {
//获取第三层树节点数据
fourthTreeLayer = getPrivilegeTreeBy(contextRoot, allPrivileges, assignedPrivileges, 4);
//构造一棵权限树
constructPrivilegeTree(treeRoot, secondTreeLayer, threedTreeLayer,fourthTreeLayer);
} else if(threedTreeLayer != null){
//构造一棵权限树
constructPrivilegeTree(treeRoot, secondTreeLayer, threedTreeLayer,new ArrayList<PrivilegeBean>(0) );
}else{
constructPrivilegeTree(treeRoot, secondTreeLayer, new ArrayList<PrivilegeBean>(0), new ArrayList<PrivilegeBean>(0));
}
return treeRoot;
}
/**
* 建造一颗权限树。
* @param treeRoot 第一层树节点列表
* @param secondTreeLayer 第二层树节点列表
* @param threedTreeLayer 第三层树节点列表
* @return 返回已经构造的一颗权限树。
*
* @return List<PrivilegeBean> 返回已经构造的一颗权限树。
*/
private static List<PrivilegeBean> constructPrivilegeTree( List<PrivilegeBean> treeRoot, List<PrivilegeBean> secondTreeLayer, List<PrivilegeBean> threedTreeLayer,List<PrivilegeBean> fourthTreeLayer ) {
//第二棵子树
List<PrivilegeBean> p1SubTree = null;
//第三棵子树
List<PrivilegeBean> p2SubTree = null;
//第四棵子树
List<PrivilegeBean> p3SubTree = null;
//第一层树遍历
for( PrivilegeBean p1 : treeRoot ) {
int pid1 = p1.getPrivilegeId();
//构造第二棵子树
p1SubTree = new ArrayList<PrivilegeBean>();
p1.setSubPrivileges(p1SubTree);
//第二层树遍历
for( PrivilegeBean p2 : secondTreeLayer ) {
int pid2 = p2.getPrivilegeId();
int p2ParentPid = p2.getParentPrivilegeId();
//构造第三棵子树
p2SubTree = new ArrayList<PrivilegeBean>();
p2.setSubPrivileges(p2SubTree);
if ( p2ParentPid == pid1 ) {
p1SubTree.add( p2 );
p1.setHasSubNode( 1 );//表示父节点有子节点
}
//第三层树遍历
for( PrivilegeBean p3 : threedTreeLayer ) {
int pid3 = p3.getPrivilegeId();
int p3ParentPid = p3.getParentPrivilegeId();
p3SubTree = new ArrayList<PrivilegeBean>();
p3.setSubPrivileges(p3SubTree);
if( p3ParentPid == pid2 ) {
p2SubTree.add( p3 );
p2.setHasSubNode( 1 );//表示父节点有子节点
}
for(PrivilegeBean p4:fourthTreeLayer){
int p4ParentPid = p4.getParentPrivilegeId();
if(p4ParentPid == pid3){
p3SubTree.add(p4);
p3.setHasSubNode(1);
}
}
}
}
}
return treeRoot;
}
/**
* 将系统中查询的权限实体列表,转换成含PrivilegeBean对象的权限列表,然后用该列表,在页面上展现一颗完整的权限树。
* @param contextRoot 应用名
* @param allPrivileges 系统中所有权限列表
* @param assignedPrivileges 已分配的权限列表
* @param privilegeLevel 权限等级
* @return 返回用于页面展现的权限列表
*
* @return List<PrivilegeBean> 返回用于页面展现的权限列表
*/
private static List<PrivilegeBean> getPrivilegeTreeBy( String contextRoot, List<Privilege> allPrivileges, List<Privilege> assignedPrivileges, int privilegeLevel ) {
List<PrivilegeBean> tree = new ArrayList<PrivilegeBean>();
for( Privilege privilege : allPrivileges ) {
if ( privilege.getPrivilegeLevel() == privilegeLevel ) {
int privilegeId = privilege.getPrivilegeId();
String privilegeName = privilege.getPrivilegeName();
String privilegeURL = privilege.getPrivilegeURL();
if ( privilegeURL != null ) {
privilegeURL = contextRoot + privilegeURL;
}
int parentPrivilegeId = privilege.getPrivilegeParentId();
int assigned = 0;
PrivilegeBean privilegeBean = new PrivilegeBean(privilegeId, privilegeName, privilegeURL, parentPrivilegeId);
for( Privilege assignedPrivilege : assignedPrivileges ) {
if( assignedPrivilege.getPrivilegeId() == privilegeId ) {
assigned = 1;
break;
}
}
privilegeBean.setAssigned( assigned );
tree.add( privilegeBean );
}
}
return tree;
}
}
package com.companyName.dhm.cbb.role.entity;
import java.io.Serializable;
public class Privilege implements Serializable {
private static final long serialVersionUID = -8811863695015542982L;
/**权限编号*/
private int privilegeId;
/**权限名称*/
private String privilegeName;
/**http请求URL*/
private String privilegeURL;
/**父编号*/
private int privilegeParentId;
/**权限等级,用于权限树的展现*/
private int privilegeLevel;
public int getPrivilegeId() {
return privilegeId;
}
public void setPrivilegeId(int privilegeId) {
this.privilegeId = privilegeId;
}
public String getPrivilegeName() {
return privilegeName;
}
public void setPrivilegeName(String privilegeName) {
this.privilegeName = privilegeName;
}
public String getPrivilegeURL() {
return privilegeURL;
}
public void setPrivilegeURL(String privilegeURL) {
this.privilegeURL = privilegeURL;
}
public int getPrivilegeParentId() {
return privilegeParentId;
}
public void setPrivilegeParentId(int privilegeParentId) {
this.privilegeParentId = privilegeParentId;
}
public int getPrivilegeLevel() {
return privilegeLevel;
}
public void setPrivilegeLevel(int privilegeLevel) {
this.privilegeLevel = privilegeLevel;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Privilege[privilegeId=");
builder.append(privilegeId);
builder.append(", privilegeLevel=");
builder.append(privilegeLevel);
builder.append(", privilegeName=");
builder.append(privilegeName);
builder.append(", privilegeParentId=");
builder.append(privilegeParentId);
builder.append(", privilegeURL=");
builder.append(privilegeURL);
builder.append("]");
return builder.toString();
}
}
package com.companyName.dhm.cbb.role.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
/**
*
* 权限树展现的实体Bean。
*
*/
public class PrivilegeBean implements Serializable {
private static final long serialVersionUID = 4049990199216441887L;
private String label;
/**权限编号*/
private int privilegeId;
/**权限URL*/
private String privilegeURL;
/**权限名称*/
private String privilegeName;
private int assigned; //1表示授予角色,0表示未授予角色
/**权限父节点ID*/
private int parentPrivilegeId;
private int hasSubNode;//1表示有子节点,0表示没有子节点
public int getParentPrivilegeId() {
return parentPrivilegeId;
}
public void setParentPrivilegeId(int parentPrivilegeId) {
this.parentPrivilegeId = parentPrivilegeId;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
private List<PrivilegeBean> subPrivileges = new ArrayList<PrivilegeBean>();
public PrivilegeBean() {}
public PrivilegeBean( int privilegeId, String privilegeName, String privilegeURL, int parentPrivilegeId ) {
this.privilegeId = privilegeId;
this.privilegeName = privilegeName;
if ( privilegeURL == null ) {
privilegeURL = "#";
}
this.privilegeURL = privilegeURL ;
this.parentPrivilegeId = parentPrivilegeId;
this.label = this.privilegeURL + "_" +this.privilegeId;
}
public int getPrivilegeId() {
return privilegeId;
}
public void setPrivilegeId(int privilegeId) {
this.privilegeId = privilegeId;
}
public String getPrivilegeURL() {
return privilegeURL;
}
public void setPrivilegeURL(String privilegeURL) {
this.privilegeURL = privilegeURL;
}
public String getPrivilegeName() {
return privilegeName;
}
public void setPrivilegeName(String privilegeName) {
this.privilegeName = privilegeName;
}
public List<PrivilegeBean> getSubPrivileges() {
return subPrivileges;
}
public void setSubPrivileges(List<PrivilegeBean> subPrivileges) {
this.subPrivileges = subPrivileges;
}
public int getAssigned() {
return assigned;
}
public void setAssigned(int assigned) {
this.assigned = assigned;
}
public int getHasSubNode() {
return hasSubNode;
}
public void setHasSubNode(int hasSubNode) {
this.hasSubNode = hasSubNode;
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><s:text name="rolemgr.name" />――<s:text
name="rolemgr.privilege.management" />
</title>
<link href="../style/dtree.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../js/dtree.js"></script>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#switch_frame").click(function(){
frameSwitch();
});
});
function frameSwitch()
{
if(parent.document.getElementById("switchFrame").cols == "221,*")
{
$("#switch_frame").find("img").attr("src","../images/btn_sopen.gif");
parent.document.getElementById("switchFrame").cols = "8,*";
top.frames["leftFrame"].document.getElementById("switch_frame").style.left = "1px";
}
else
{
$("#switch_frame").find("img").attr("src","../images/btn_sclose.gif");
parent.document.getElementById("switchFrame").cols = "221,*";
top.frames["leftFrame"].document.getElementById("switch_frame").style.left = "200px";
}
}
</script>
<title>menu</title>
<style type="text/css">
body{background:#EFF5FF;margin:0px;padding:0px;}
#menu{width:221px;background:#EFF5FF; position:absolute;left:0px;top:28px;}
.dTreeNode_sys{width:76px;margin:5px 0px 0px 70px;font-size:16px;font-weight:bolder;color: #0E2D5F;position:absolute;left:0px;top:0px;}
</style>
</head>
<body>
<div id="switch_frame" οnclick="" align="right"><img src="../images/btn_sclose.gif" /></div>
<div class="dTreeNode_sys">报表系统</div>
<!--menu edit-->
<div id="menu">
<script type="text/javascript">
d = new dTree('d');
d.add(1000, -1, '', '', '');
<s:iterator value="#session.privileges4Menu" var="privilege">
d.add(<s:property value="#privilege.privilegeId" />,
<s:property value="#privilege.privilegeParentId" />,
'<s:property value="#privilege.privilegeName" />',
"<s:property value="#privilege.privilegeURL" />" == "#" ? ''
: '/biPortal<s:property value="#privilege.privilegeURL" />',
'', 'mainFrame');
</s:iterator>
document.write(d);
</script>
</div>
</body>
</html>
dtree.js
/*--------------------------------------------------|
| dTree 2.05 | www.destroydrop.com/javascript/tree/ |
|---------------------------------------------------|
| Copyright (c) 2002-2003 Geir Landr� |
| |
| This script can be used freely as long as all |
| copyright messages are intact. |
| |
| Updated: 17.04.2003 |
|--------------------------------------------------*/
// Node object
function Node(id, pid, name, url, title, target, icon, iconOpen, open) {
this.id = id;
this.pid = pid;
this.name = name;
this.url = url;
this.title = title;
this.target = target;
this.icon = icon;
this.iconOpen = iconOpen;
this._io = open || false;
this._is = false;
this._ls = false;
this._hc = false;
this._ai = 0;
this._p;
};
// Tree object
function dTree(objName) {
this.config = {
target : null,
folderLinks : true,
useSelection : true,
useCookies : false,
useLines : true,
useIcons : true,
useStatusText : false,
closeSameLevel : false,
inOrder : false
}
this.icon = {
root : '../images/base.gif',
folder : '../images/folder.gif',
folderOpen : '../images/folderopen.gif',
node : '../images/chart.png',
empty : '../images/empty.gif',
line : '../images/line.gif',
join : '../images/join.gif',
joinBottom : '../images/joinbottom.gif',
plus : '../images/plus.gif',
plusBottom : '../images/plusbottom.gif',
minus : '../images/minus.gif',
minusBottom : '../images/minusbottom.gif',
nlPlus : '../images/nolines_plus.gif',
nlMinus : '../images/nolines_minus.gif'
};
this.obj = objName;
this.aNodes = [];
this.aIndent = [];
this.root = new Node(-1);
this.selectedNode = null;
this.selectedFound = false;
this.completed = false;
};
// Adds a new node to the node array
dTree.prototype.add = function(id, pid, name, url, title, target, icon, iconOpen, open) {
this.aNodes[this.aNodes.length] = new Node(id, pid, name, url, title, target, icon, iconOpen, open);
};
// Open/close all nodes
dTree.prototype.openAll = function() {
this.oAll(true);
};
dTree.prototype.closeAll = function() {
this.oAll(false);
};
// Outputs the tree to the page
dTree.prototype.toString = function() {
var str = '<div class="dtree">\n';
if (document.getElementById) {
if (this.config.useCookies) this.selectedNode = this.getSelected();
str += this.addNode(this.root);
} else str += 'Browser not supported.';
str += '</div>';
if (!this.selectedFound) this.selectedNode = null;
this.completed = true;
return str;
};
// Creates the tree structure
dTree.prototype.addNode = function(pNode) {
var str = '';
var n=0;
if (this.config.inOrder) n = pNode._ai;
for (n; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == pNode.id) {
var cn = this.aNodes[n];
cn._p = pNode;
cn._ai = n;
this.setCS(cn);
if (!cn.target && this.config.target) cn.target = this.config.target;
if (cn._hc && !cn._io && this.config.useCookies) cn._io = this.isOpen(cn.id);
if (!this.config.folderLinks && cn._hc) cn.url = null;
if (this.config.useSelection && cn.id == this.selectedNode && !this.selectedFound) {
cn._is = true;
this.selectedNode = n;
this.selectedFound = true;
}
str += this.node(cn, n);
if (cn._ls) break;
}
}
return str;
};
// Creates the node icon, url and text
dTree.prototype.node = function(node, nodeId) {
var str = '<div class="dTreeNode '+((node._hc) ? 'dTreeNodeFolder' : '')+'" title="'+node.name+'">' + this.indent(node, nodeId);
if (this.config.useIcons) {
if (!node.icon) node.icon = (this.root.id == node.pid) ? this.icon.root : ((node._hc) ? this.icon.folder : this.icon.node);
if (!node.iconOpen) node.iconOpen = (node._hc) ? this.icon.folderOpen : this.icon.node;
if (this.root.id == node.pid) {
node.icon = this.icon.root;
node.iconOpen = this.icon.root;
}
str += '<img id="i' + this.obj + nodeId + '" src="' + ((node._io) ? node.iconOpen : node.icon) + '" alt="" />';
}
if (node.url) {
str += '<a id="s' + this.obj + nodeId + '" class="' + ((this.config.useSelection) ? ((node._is ? 'nodeSel' : 'node')) : 'node') + '" href="' + node.url + '"';
if (node.title) str += ' title="' + node.title + '"';
if (node.target) str += ' target="' + node.target + '"';
if (this.config.useStatusText) str += ' οnmοuseοver="window.status=\'' + node.name + '\';return true;" οnmοuseοut="window.status=\'\';return true;" ';
if (this.config.useSelection && ((node._hc && this.config.folderLinks) || !node._hc))
str += ' οnclick="javascript: ' + this.obj + '.s(' + nodeId + ');"';
str += '>';
}
else if ((!this.config.folderLinks || !node.url) && node._hc && node.pid != this.root.id)
str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');" class="node">';
str += node.name;
if (node.url || ((!this.config.folderLinks || !node.url) && node._hc)) str += '</a>';
str += '</div>';
if (node._hc) {
str += '<div id="d' + this.obj + nodeId + '" class="clip" style="display:' + ((this.root.id == node.pid || node._io) ? 'block' : 'none') + ';">';
str += this.addNode(node);
str += '</div>';
}
this.aIndent.pop();
return str;
};
// Adds the empty and line icons
dTree.prototype.indent = function(node, nodeId) {
var str = '';
if (this.root.id != node.pid) {
for (var n=0; n<this.aIndent.length; n++)
str += '<img src="' + ( (this.aIndent[n] == 1 && this.config.useLines) ? this.icon.line : this.icon.empty ) + '" alt="" />';
(node._ls) ? this.aIndent.push(0) : this.aIndent.push(1);
if (node._hc) {
str += '<a href="javascript: ' + this.obj + '.o(' + nodeId + ');"><img id="j' + this.obj + nodeId + '" src="';
if (!this.config.useLines) str += (node._io) ? this.icon.nlMinus : this.icon.nlPlus;
else str += ( (node._io) ? ((node._ls && this.config.useLines) ? this.icon.minusBottom : this.icon.minus) : ((node._ls && this.config.useLines) ? this.icon.plusBottom : this.icon.plus ) );
str += '" alt="" /></a>';
} else str += '<img src="' + ( (this.config.useLines) ? ((node._ls) ? this.icon.joinBottom : this.icon.join ) : this.icon.empty) + '" alt="" />';
}
return str;
};
// Checks if a node has any children and if it is the last sibling
dTree.prototype.setCS = function(node) {
var lastId;
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == node.id) node._hc = true;
if (this.aNodes[n].pid == node.pid) lastId = this.aNodes[n].id;
}
if (lastId==node.id) node._ls = true;
};
// Returns the selected node
dTree.prototype.getSelected = function() {
var sn = this.getCookie('cs' + this.obj);
return (sn) ? sn : null;
};
// Highlights the selected node
dTree.prototype.s = function(id) {
if (!this.config.useSelection) return;
var cn = this.aNodes[id];
if (cn._hc && !this.config.folderLinks) return;
if (this.selectedNode != id) {
if (this.selectedNode || this.selectedNode==0) {
eOld = document.getElementById("s" + this.obj + this.selectedNode);
eOld.className = "node";
}
eNew = document.getElementById("s" + this.obj + id);
eNew.className = "nodeSel";
this.selectedNode = id;
if (this.config.useCookies) this.setCookie('cs' + this.obj, cn.id);
}
};
// Toggle Open or close
dTree.prototype.o = function(id) {
var cn = this.aNodes[id];
this.nodeStatus(!cn._io, id, cn._ls);
cn._io = !cn._io;
if (this.config.closeSameLevel) this.closeLevel(cn);
if (this.config.useCookies) this.updateCookie();
};
// Open or close all nodes
dTree.prototype.oAll = function(status) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n]._hc && this.aNodes[n].pid != this.root.id) {
this.nodeStatus(status, n, this.aNodes[n]._ls)
this.aNodes[n]._io = status;
}
}
if (this.config.useCookies) this.updateCookie();
};
// Opens the tree to a specific node
dTree.prototype.openTo = function(nId, bSelect, bFirst) {
if (!bFirst) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].id == nId) {
nId=n;
break;
}
}
}
var cn=this.aNodes[nId];
if (cn.pid==this.root.id || !cn._p) return;
cn._io = true;
cn._is = bSelect;
if (this.completed && cn._hc) this.nodeStatus(true, cn._ai, cn._ls);
if (this.completed && bSelect) this.s(cn._ai);
else if (bSelect) this._sn=cn._ai;
this.openTo(cn._p._ai, false, true);
};
// Closes all nodes on the same level as certain node
dTree.prototype.closeLevel = function(node) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == node.pid && this.aNodes[n].id != node.id && this.aNodes[n]._hc) {
this.nodeStatus(false, n, this.aNodes[n]._ls);
this.aNodes[n]._io = false;
this.closeAllChildren(this.aNodes[n]);
}
}
}
// Closes all children of a node
dTree.prototype.closeAllChildren = function(node) {
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n].pid == node.id && this.aNodes[n]._hc) {
if (this.aNodes[n]._io) this.nodeStatus(false, n, this.aNodes[n]._ls);
this.aNodes[n]._io = false;
this.closeAllChildren(this.aNodes[n]);
}
}
}
// Change the status of a node(open or closed)
dTree.prototype.nodeStatus = function(status, id, bottom) {
eDiv = document.getElementById('d' + this.obj + id);
eJoin = document.getElementById('j' + this.obj + id);
if (this.config.useIcons) {
eIcon = document.getElementById('i' + this.obj + id);
eIcon.src = (status) ? this.aNodes[id].iconOpen : this.aNodes[id].icon;
}
eJoin.src = (this.config.useLines)?
((status)?((bottom)?this.icon.minusBottom:this.icon.minus):((bottom)?this.icon.plusBottom:this.icon.plus)):
((status)?this.icon.nlMinus:this.icon.nlPlus);
eDiv.style.display = (status) ? 'block': 'none';
};
// [Cookie] Clears a cookie
dTree.prototype.clearCookie = function() {
var now = new Date();
var yesterday = new Date(now.getTime() - 1000 * 60 * 60 * 24);
this.setCookie('co'+this.obj, 'cookieValue', yesterday);
this.setCookie('cs'+this.obj, 'cookieValue', yesterday);
};
// [Cookie] Sets value in a cookie
dTree.prototype.setCookie = function(cookieName, cookieValue, expires, path, domain, secure) {
document.cookie =
escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
};
// [Cookie] Gets a value from a cookie
dTree.prototype.getCookie = function(cookieName) {
var cookieValue = '';
var posName = document.cookie.indexOf(escape(cookieName) + '=');
if (posName != -1) {
var posValue = posName + (escape(cookieName) + '=').length;
var endPos = document.cookie.indexOf(';', posValue);
if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
else cookieValue = unescape(document.cookie.substring(posValue));
}
return (cookieValue);
};
// [Cookie] Returns ids of open nodes as a string
dTree.prototype.updateCookie = function() {
var str = '';
for (var n=0; n<this.aNodes.length; n++) {
if (this.aNodes[n]._io && this.aNodes[n].pid != this.root.id) {
if (str) str += '.';
str += this.aNodes[n].id;
}
}
this.setCookie('co' + this.obj, str);
};
// [Cookie] Checks if a node id is in a cookie
dTree.prototype.isOpen = function(id) {
var aOpen = this.getCookie('co' + this.obj).split('.');
for (var n=0; n<aOpen.length; n++)
if (aOpen[n] == id) return true;
return false;
};
// If Push and pop is not implemented by the browser
if (!Array.prototype.push) {
Array.prototype.push = function array_push() {
for(var i=0;i<arguments.length;i++)
this[this.length]=arguments[i];
return this.length;
}
};
if (!Array.prototype.pop) {
Array.prototype.pop = function array_pop() {
lastElement = this[this.length-1];
this.length = Math.max(this.length-1,0);
return lastElement;
}
};