<html xmlns:th="http://www.thymeleaf.org">
  <head>
<title>私塾家</title>
<script th:include="sys/common/metaCSS" th:remove="tag"></script> 
<script th:include="sys/common/metaJS" th:remove="tag"></script> 
<link href="sys/css/bootstrap.css" th:href="@{/static/sys/css/bootstrap.css}" rel="stylesheet"/>
<link href="sys/css/zTree/zTreeStyle.css" th:href="@{/static/sys/css/zTree/zTreeStyle.css}" rel="stylesheet"/>
<script src="js/jquery-2.2.4.min.js" th:src="@{/static/js/jquery-2.2.4.min.js}"></script>
<script src="sys/js/zTree/jquery.ztree.all-3.5.min.js" th:src="@{/static/sys/js/zTree/jquery.ztree.all-3.5.min.js}"></script>
<style type="text/css">
    	span{margin:0;padding:0;display: inline-block;}
    	@media (min-width: 1200px){
			.row-fluid [class*="span"] {
			    margin-left: 0px;
			}
		}
    </style>
</head>
<body>
<div class="place">
	<span>位置：</span>
	<ul class="placeul">
		<li><a href="#">组织架构管理</a></li>
		<li><a th:href="@{/sys/org/main.html}">组织架构</a></li>
	</ul>
</div>
<div class="rightinfo container-fluid">
	<div class="row-fluid">
		<div class="span2" style="border-right: #999999 1px dashed;">
			<ul id="columnsTree" class="ztree" style="min-height:560px; overflow: auto;"></ul>
		</div>
		<div class="span10">
			<iframe id="listIframe" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>
		</div>
	</div>
</div>
<script type="text/javascript">
function addTab(url) {
	$("#listIframe").attr("src",url);
	startInit("listIframe", 560);
}

function refreshList(){
	$("#listIframe").attr("src", $("#listIframe").attr("src"));
}

var setting = {
	async : {
		enable : true,
		autoParam : [ "id" ],
		url : "[[${sysUrl}]]/sys/org/maintree",
		dataFilter : filter
	},
	data : {
		key : {
			name : "name"
		},
		simpleData : {
			enable : true,
			pIdKey : "pId"
		}
	},
	callback : {
		onClick : clickNode,
		onAsyncSuccess : renderIcon
	}
};

function filter(treeId, parentNode, childNodes) {
	if (!childNodes)
		return null;
	for (var i = 0, l = childNodes.length; i < l; i++) {
		childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
	}
	return childNodes;
}

function clickNode(event, treeId, treeNode, clickFlag) {
	var url = "[[${sysUrl}]]/sys/org/list.html?EQ_pId=" + treeNode.id;
	addTab(url);
	event.preventDefault();
}

function renderIcon(event, treeId, treeNode, msg){
	var ztree = $.fn.zTree.getZTreeObj(treeId);
	var nodes = null;
	if(treeNode){
		nodes = treeNode.children;
	}
	if(nodes){
		for(var i = 0; i < nodes.length; i++){
			nodes[i].icon="[[${imgUrl}]]/resources/images/menu_1.png";
			ztree.updateNode(nodes[i]);
		}
	}
	zTreeOnAsyncSuccess(event, treeId, treeNode, msg, ztree);
}

var firstAsyncSuccessFlag = 0;
function zTreeOnAsyncSuccess(event, treeId, treeNode, msg, zTree) {
	if (firstAsyncSuccessFlag == 0) {  
	      try {  
	             //调用默认展开第一个结点  
	             var selectedNode = zTree.getSelectedNodes();  
	             var nodes = zTree.getNodes();  
	             zTree.expandNode(nodes[0], true);
	             
	             var childNodes = zTree.transformToArray(nodes[0]);  
                 zTree.selectNode(childNodes[0]);  
	          	 
	             clickNode(event, treeId, treeNode, true);
	             firstAsyncSuccessFlag = 1;
	       } catch (err) {  
	          
	       }  
	   }  
}

// 增删改节点之后，刷新局部
function refreshNode(id, pid, name, op) {
	var ztree = $.fn.zTree.getZTreeObj("columnsTree");
	var pNode = ztree.getNodeByParam("id", pid, null);
	// 添加一级节点
	if (pNode == null) {
		ztree.refresh();
	}
	// 父节点是叶子节点，只能是新增
	if (!pNode.isParent) {
		pNode.isParent = true;
		ztree.reAsyncChildNodes(pNode, "refresh");
	}
	// 父节点未展开，直接返回
	if (!pNode.open)
		return;
	// 新增节点，重新展开父节点
	if (id == "") {
		pNode.open = false;
		ztree.reAsyncChildNodes(pNode, "refresh");
		return;
	}
	// 删除
	var treeNode = ztree.getNodeByParam("id", id, null);
	if (op == "delete") {
		ztree.removeNode(treeNode);
	} else {
		// 修改
		treeNode.name = name;
		ztree.updateNode(treeNode);
	}
}

$(document).ready(function() {
	$.fn.zTree.init($("#columnsTree"), setting);
});

var browserVersion = window.navigator.userAgent.toUpperCase();
var isOpera = false;
var isFireFox = false;
var isChrome = false;
var isSafari = false;
var isIE = false;
var iframeTime;
function reinitIframe(iframeId, minHeight) {
    try {
    	if (typeof(reValue) == "undefined") {
    		browserVersion = window.navigator.userAgent.toUpperCase();
    	}
        var iframe = document.getElementById(iframeId);
        var bHeight = 0;
        if (isChrome == false && isSafari == false)
            bHeight = iframe.contentWindow.document.body.scrollHeight;

        var dHeight = 0;
        if (isFireFox == true)
            dHeight = iframe.contentWindow.document.documentElement.offsetHeight + 2;
        else if (isIE == false && isOpera == false)
            dHeight = iframe.contentWindow.document.documentElement.scrollHeight;
        else
            bHeight += 3;
        var height = Math.max(bHeight, dHeight);
        if (height < minHeight) height = minHeight;
        iframe.style.height = height + "px";
    } catch (ex) { }
}
function startInit(iframeId, minHeight) {
	if (typeof(reValue) == "undefined") {
		browserVersion = window.navigator.userAgent.toUpperCase();
	}
    isOpera = browserVersion.indexOf("OPERA") > -1 ? true : false;
    isFireFox = browserVersion.indexOf("FIREFOX") > -1 ? true : false;
    isChrome = browserVersion.indexOf("CHROME") > -1 ? true : false;
    isSafari = browserVersion.indexOf("SAFARI") > -1 ? true : false;
    if (!!window.ActiveXObject || "ActiveXObject" in window)
        isIE = true;
    reinitIframe(iframeId, minHeight);
    if (iframeTime != null)
        clearInterval(iframeTime)
    iframeTime = window.setInterval("reinitIframe('" + iframeId + "'," + minHeight + ")", 100);
}
</script>
</body>
</html>