<SCRIPT type="text/javascript">
var setting = {
view : {
addHoverDom : addHoverDom,
removeHoverDom : removeHoverDom,
selectedMulti : false
},
edit : {
enable : true,
editNameSelectAll : true,
showRemoveBtn : showRemoveBtn,
showRenameBtn : showRenameBtn
},
data : {
simpleData : {
enable : true
}
},
callback : {
beforeDrag : beforeDrag,
beforeEditName : beforeEditName,
beforeRemove : beforeRemove,
beforeRename : beforeRename,
onRemove : onRemove,
onRename : onRename,
beforeClick : beforeClick,
onAsyncSuccess : zTreeOnAsyncSuccess
}
};

var zNodes = [];
var log, className = "dark";

function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.refresh();
};

function beforeClick(treeId, treeNode) {
//alert(treeId + ' | ' + treeNode.pId + ' | ' + treeNode.tId);
//windows.open("${contextPath}/auth/functionform","窗口名称","height=100,width=100");
//windows.iframe_a.location.href = "${contextPath}/auth/functionform";
/* <a href="${contextPath}/auth/functionform?fid=${v.fid}" target="iframe_a">W3School.com.cn</a> */

alert(treeNode.id);

if (treeNode.level == 0) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.expandNode(treeNode);
return false;
}

//windows.rightFrame.location.href="${contextPath}/auth/functionform";
ajax_get("${contextPath}/auth/removeztree", "&id=" + treeNode.id, function(data) {
//var data=obj;
if (data.errorMessage) {
showmsg(0, data.errorMessage);
} else {
//showmsg(1, "处理成功!");
//$('#id').val=(data.xxx);
}
});

return true;
}

function beforeDrag(treeId, treeNodes) {
return false;
}
function beforeEditName(treeId, treeNode) {
className = (className === "dark" ? "" : "dark");
showLog("[ " + getTime() + " beforeEditName ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.selectNode(treeNode);
//return confirm("进入节点 -- " + treeNode.name + " 的编辑状态吗?");
return true;
}
function beforeRemove(treeId, treeNode) {
className = (className === "dark" ? "" : "dark");
showLog("[ " + getTime() + " beforeRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.selectNode(treeNode);
return confirm("确认删除 节点 -- " + treeNode.name + " 吗?");
}
function onRemove(e, treeId, treeNode) {
ajax_get("${contextPath}/auth/removeztree", "&id=" + treeNode.id, function(data) {
if (data.errorMessage) {
showmsg(0, data.errorMessage);
} else {
showmsg(1, "处理成功!");

}
});
showLog("[ " + getTime() + " onRemove ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name);
}
function beforeRename(treeId, treeNode, newName, isCancel) {
className = (className === "dark" ? "" : "dark");
showLog((isCancel ? "<span style='color:red'>" : "") + "[ " + getTime() + " beforeRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name
+ (isCancel ? "</span>" : ""));
if (newName.length == 0) {
alert("节点名称不能为空.");
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
setTimeout(function() {
zTree.editName(treeNode);
}, 10);
return false;
}

ajax_get("${contextPath}/auth/renameztree", "&id=" + treeNode.id + "&sname=" + newName, function(data) {
if (data.errorMessage) {
showmsg(0, data.errorMessage);
} else {
showmsg(1, "处理成功!");
}
});
return true;
}
function onRename(e, treeId, treeNode, isCancel) {
showLog((isCancel ? "<span style='color:red'>" : "") + "[ " + getTime() + " onRename ]&nbsp;&nbsp;&nbsp;&nbsp; " + treeNode.name
+ (isCancel ? "</span>" : ""));
}
function showRemoveBtn(treeId, treeNode) {
/* return !treeNode.isFirstNode; */
return true;
}
function showRenameBtn(treeId, treeNode) {
/* return !treeNode.isLastNode; */
return true;
}
function showLog(str) {
if (!log)
log = $("#log");
log.append("<li class='"+className+"'>" + str + "</li>");
if (log.children("li").length > 8) {
log.get(0).removeChild(log.children("li")[0]);
}
}
function getTime() {
var now = new Date(), h = now.getHours(), m = now.getMinutes(), s = now.getSeconds(), ms = now.getMilliseconds();
return (h + ":" + m + ":" + s + " " + ms);
}

var newCount = 1;
function addHoverDom(treeId, treeNode) {
var sObj = $("#" + treeNode.tId + "_span");
if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0)
return;
var addStr = "<span class='button add' id='addBtn_" + treeNode.tId + "' title='add node' onfocus='this.blur();'></span>";
sObj.after(addStr);
var btn = $("#addBtn_" + treeNode.tId);
if (btn) {
btn.bind("click", function() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");

var spId = treeNode.id;
var sname = "new node" + (newCount++);
var sid = RndNum(8);
addztree(sid, spId, sname);
zTree.addNodes(treeNode, {
id : sid,
pId : spId,
name : sname
});
return false;
});
}
};

function addztree(id, pId, name) {
ajax_get("${contextPath}/auth/addztree", "&id=" + id + "&pid=" + pId + "&sname=" + name, function(data) {
if (data.errorMessage) {
showmsg(0, data.errorMessage);
} else {
showmsg(1, "处理成功!");
}
});
}

function RndNum(n) {
var rnd = "";
for (var i = 0; i < n; i++)
rnd += Math.floor(Math.random() * 10);
return rnd;
}

/* function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
} */

function removeHoverDom(treeId, treeNode) {
$("#addBtn_" + treeNode.tId).unbind().remove();
};
function selectAll() {
var zTree = $.fn.zTree.getZTreeObj("treeDemo");
zTree.setting.edit.editNameSelectAll = $("#selectAll").attr("checked");
}

$(document).ready(function() {

var s = $('#hinitnodes').val();
if (s != "") {
s = s.replace(/\'/g, "\"");
zNodes = JSON.parse(s);
}

$.fn.zTree.init($("#treeDemo"), setting, zNodes);
$("#selectAll").bind("click", selectAll);
});
</SCRIPT>

04-14 13:21