﻿
var Tree = function(obj, Parent) { var self = this; this.dataObj = obj; this.rootNode = null; var refDoc = new $doc(Parent.document); var prevSelNode = null; var selectedNode = null; function _src(pathname) {         var src = "http://"+window.location.hostname;
src += pathname; return src; } var imgEmpty = _src("/img/empty.gif"); var imgPlus = _src("/img/plus.gif"); var imgMinus = _src("/img/minus.gif"); var imgExpand = _src("/img/folder_c.ico"); var imgCollapse = _src("/img/folder_o.ico"); var imgItem = _src("/img/item.gif"); this.Init = function() { function loadTree(response) { var root = Request.Validate(response); if(root) { var trees = root.getElementsByTagName("tree"); if(trees) { var node = loadTreeNode(Parent, trees[0], self.dataObj.exec); if(node && node.previousSibling) { self.rootNode = node.previousSibling; onClickItemText(self.rootNode); } } } } Request.GET(obj.url, loadTree); if(obj.callback) { obj.callback('tree', self); } }; function loadTreeNode(parent, node, exec) { for(var i=0; i<node.childNodes.length; i++) { var name = node.childNodes[i].getAttribute("name"); var url = node.childNodes[i].getAttribute("url"); var target = node.childNodes[i].getAttribute("target"); var pname = node.childNodes[i].getAttribute("pname"); var fontWeight = node.childNodes[i].getAttribute("font-weight"); var fontStyle = node.childNodes[i].getAttribute("font-style"); var fontSize = node.childNodes[i].getAttribute("font-size"); var color = node.childNodes[i].getAttribute("color"); var icon0 = node.childNodes[i].getAttribute("icon0"); var icon1 = node.childNodes[i].getAttribute("icon1"); var nodeTagName = node.childNodes[i].tagName.toLowerCase(); if(nodeTagName == "group") { if(!icon0) icon0 = imgExpand; if(!icon1) icon1 = imgCollapse; if(!target) target = "children"; var groupNode = {fontSize:fontSize, fontStyle:fontStyle, fontWeight:fontWeight, color:color, icon0:icon0, icon1:icon1, pname:pname, exec:exec }; var group = insertTreeNode(parent, name, url, target, groupNode); loadTreeNode(group, node.childNodes[i], exec); if(node.childNodes.length == 1) { return group; } } else { if(!icon0) icon0 = imgItem; if(!icon1) icon1 = imgItem; var itemNode = {fontSize:fontSize, fontStyle:fontStyle, fontWeight:fontWeight, color:color, icon0:icon0, icon1:icon1, pname:pname, exec:exec }; var node = insertTreeNode(parent, name, url, target, itemNode); if(node.childNodes.length == 1) { return node; } } } } function collapseNode(node) { var table = node; var div = node.nextSibling; var imgBtn = table.rows[0].cells[0].firstChild; if(imgBtn) { var src = imgBtn.src.toLowerCase(); if(src.indexOf(imgEmpty) == -1) { if(src.indexOf(imgPlus) == -1) { imgBtn.setAttribute("src", imgPlus); if(div.data && div.data.icon0) { var imgIcon = table.rows[0].cells[1].firstChild; imgIcon.setAttribute("src", div.data.icon0); } } } } if(_isTag(div, "div")) { if(div.style.display == "block") div.style.display = "none"; }	} function expandNode(node) { var table = node; var div = node.nextSibling; var imgBtn = table.rows[0].cells[0].firstChild; if(imgBtn) { var src = imgBtn.src.toLowerCase(); if(src.indexOf(imgEmpty) == -1) { if(src.indexOf(imgMinus) == -1) { imgBtn.setAttribute("src", imgMinus); if(div.data && div.data.icon1) { var imgIcon = table.rows[0].cells[1].firstChild; imgIcon.setAttribute("src", div.data.icon1); } }	} } if(_isTag(div, "div")) { if(div.style.display == "none") div.style.display = "block"; }	} function collapseDivNode(node) { var table = node.previousSibling; if(table && _isTag(table, "table")) collapseNode(table); } function expandDivNode(node) { var table = node.previousSibling; if(table && _isTag(table, "table")) expandNode(table); } function setTreeNodeText(node, text) { var table = node.previousSibling; if(table && _isTag(table, "table")) { var cellText = table.rows[0].cells[2]; var spanText = cellText.firstChild; spanText.innerHTML = text; } } function getTreeNodeText(node) { var table = node.previousSibling; if(table && _isTag(table, "table")) { var cellText = table.rows[0].cells[2]; var spanText = cellText.firstChild; return spanText.innerHTML; } return null; } function getTreeNodeSpanText(node) { var table = node.previousSibling; if(table && _isTag(table, "table")) { var cellText = table.rows[0].cells[2]; var spanText = cellText.firstChild; return spanText; } return null; } function getTreeNodeCellText(node) { var table = node.previousSibling; if(table && _isTag(table, "table")) { var cellText = table.rows[0].cells[2]; return cellText; } return null; } function removeTreeNode(node) { var table = node.previousSibling; if(table && _isTag(table, "table")) { var parent = node.parentNode; parent.removeChild(table); parent.removeChild(node); } } function toggleChildren(item, bCollapseSibling) { var table = item; var div = item.nextSibling; var imgBtn = table.rows[0].cells[0].firstChild; if(imgBtn) { var src = imgBtn.src.toLowerCase(); if(src.indexOf(imgEmpty) == -1) { if(src.indexOf(imgPlus) != -1) { imgBtn.setAttribute("src", imgMinus); if(div.data && div.data.icon1) { var imgIcon = table.rows[0].cells[1].firstChild; imgIcon.setAttribute("src", div.data.icon1); } } else { imgBtn.setAttribute("src", imgPlus); if(div.data && div.data.icon0) { var imgIcon = table.rows[0].cells[1].firstChild; imgIcon.setAttribute("src", div.data.icon0); } } } }	if(div.style.display == "none") div.style.display = "block"; else div.style.display = "none"; var bCallNext = false; if(!div.hasChildNodes()) { if(div.target == "children" && div.url) { div.appendChild(refDoc.newText("Loading...")); Request.GET(div.url, div.loadChildren); } else if(div.target == "load_more") { Request.GET(div.url, div.loadMore); } else if(div.target == "load_page") { Request.GET(div.url, div.loadPage); } else bCallNext = true; } else if(div.target == "select") { bCallNext = true; } if(bCollapseSibling && prevSelNode != null && item != prevSelNode && div.style.display == "block") { if(item.parentNode == prevSelNode.parentNode) { collapseNode(prevSelNode); } } return bCallNext; } function setItemOpen(item, bOpen) { if(item) { var table = item; var div = item.nextSibling; if(!div.hasChildNodes() && div.data) { var imgIcon = table.rows[0].cells[1].firstChild; if(bOpen && div.data.icon1) { imgIcon.setAttribute("src", div.data.icon1); } else if(div.data.icon0) { imgIcon.setAttribute("src", div.data.icon0); } } } } function onClickItemButton(item) { toggleChildren(item); onSelected(item); } function onClickItemIcon(item) { toggleChildren(item); onSelected(item); } function onClickItemText(item) { var bCallNext = toggleChildren(item, true); if(bCallNext) { var table = item; var div = item.nextSibling; if(div.data && div.data.exec) { if(div.data.exec.onclick) div.data.exec.onclick(div); } } onSelected(item); } function onSelected(item) { setItemOpen(prevSelNode, false); prevSelNode = item; selectedNode = item; setItemOpen(prevSelNode, true); if(self.dataObj && self.dataObj.callback) { var url = getUrl(item); self.dataObj.callback('url', url); var path = getPath(item); self.dataObj.callback('path', path); } } function insertTreeNode(parent, text, url, target, data) { var table = refDoc.newElement("TABLE"); table.setAttribute("border", 0); table.setAttribute("width", "100%"); table.setAttribute("cellpadding", 1); table.setAttribute("cellspacing", 1); var currentNode = table; var tablebody = refDoc.newElement("TBODY"); table.appendChild(tablebody); var row = refDoc.newElement("TR"); tablebody.appendChild(row); var cellBtn = refDoc.newElement("TD"); cellBtn.setAttribute("width", 16); row.appendChild(cellBtn); var imgBtn = refDoc.newElement("IMG"); imgBtn.setAttribute("src", imgEmpty); imgBtn.setAttribute("width", 16); imgBtn.setAttribute("height", 16); imgBtn.setAttribute("hspace", 0); imgBtn.setAttribute("vspace", 0); imgBtn.setAttribute("border", 0); imgBtn.onclick = function() { onClickItemButton(currentNode); }; cellBtn.appendChild(imgBtn); var cellIcon = refDoc.newElement("TD"); if(data && data.icon0 != null) { var imgIcon = refDoc.newElement("IMG"); imgIcon.setAttribute("src", data.icon0); imgIcon.setAttribute("width", 16); imgIcon.setAttribute("height", 16); imgIcon.setAttribute("hspace", 0); imgIcon.setAttribute("vspace", 0); imgIcon.setAttribute("border", 0); imgIcon.style.cursor = "pointer"; imgIcon.onclick = function() { onClickItemIcon(currentNode); }; cellIcon.setAttribute("width", 16); cellIcon.appendChild(imgIcon); } row.appendChild(cellIcon); var cellText = refDoc.newElement("TD"); cellText.setAttribute("height", "16px"); cellText.style.padding = "2px"; cellText.noWrap = true; var spanText = refDoc.newElement("span"); spanText.appendChild(refDoc.newText(text)); spanText.style.cursor = "pointer"; spanText.onmouseover = function() { if(spanText.className != "spanTextFocus") spanText.className = "spanTextOver"; }; spanText.onmouseout = function() { if(spanText.className != "spanTextFocus") spanText.className = "spanText"; }; spanText.onclick = function() { onClickItemText(currentNode); }; if(target == "disabled") cellText.className = "disabled"; else cellText.className = "TreeItem"; cellText.appendChild(spanText); if(data) { if(data.fontWeight) cellText.style.fontWeight = data.fontWeight; if(data.fontStyle) cellText.style.fontStyle = data.fontStyle; if(data.fontSize) cellText.style.fontSize = data.fontSize; if(data.color) cellText.style.color = data.color; } row.appendChild(cellText); var cellTool = refDoc.newElement("TD"); cellTool.setAttribute("width", "50px"); cellTool.style.padding = "2px"; cellTool.noWrap = true; row.appendChild(cellTool); var div = refDoc.newElement("DIV"); div.style.display = "none"; div.style.marginLeft = "1em"; div.url = url; div.target = target; div.data = data; if(div.target == "children") { imgBtn.setAttribute("src", imgPlus); div.loadChildren = function(response) { if(response == null) return; div.target = null; div.removeChild(div.firstChild); var rootNode = response.responseXML.documentElement; var root = rootNode.getElementsByTagName("tree")[0]; loadTreeNode(div, root, data.exec); if(data.exec && data.exec.onloaded) { data.exec.onloaded(currentNode); } }; } else if(div.target == "load_more") { div.loadMore = function(response) { if(response == null) return; parent.removeChild(table); parent.removeChild(div); var rootNode = response.responseXML.documentElement; var root = rootNode.getElementsByTagName("tree")[0]; loadTreeNode(parent, root, data.exec); }; } else if(div.target == "load_page") { div.loadPage = function(response) { if(response == null) return; while(parent.children.length) { parent.removeChild(parent.lastChild); } var rootNode = response.responseXML.documentElement; var root = rootNode.getElementsByTagName("tree")[0]; loadTreeNode(parent, root, data.exec); }; } div.onLoadedChild = function() { if(this.style.display == "none") { imgBtn.setAttribute("src", imgPlus); if(div.data && div.data.icon0) { var imgIcon = table.rows[0].cells[1].firstChild; imgIcon.setAttribute("src", div.data.icon0); } } else { imgBtn.setAttribute("src", imgMinus); if(div.data && div.data.icon1) { var imgIcon = table.rows[0].cells[1].firstChild; imgIcon.setAttribute("src", div.data.icon1); } } }; if(data && data.exec) { if(data.exec.onmouseover) row.onmouseover = function() { data.exec.onmouseover(div); }; if(data.exec.onmouseout) row.onmouseout = function() { data.exec.onmouseout(div); }; }	if(parent != null) { parent.appendChild(table); parent.appendChild(div); if(parent.onLoadedChild) parent.onLoadedChild(); } return div;	} function getParentNode(node) { if(node) { var parentDiv = node.parentNode; if(parentDiv && _isTag(parentDiv, "div")) { if(parentDiv.data && parentDiv.previousSibling) { var parentTable = parentDiv.previousSibling; if(_isTag(parentTable, "table")) return parentTable; } } } } function getPath(item) { if(item) { var path = ''; var parent = getParentNode(item); if(parent) { path += getPath(parent); } var table = item; var div = item.nextSibling; var text = getTreeNodeText(div); path += text + '/'; return path; } } function getPName(node) { if(node.data && node.data.pname) return node.data.pname; else return getTreeNodeText(node); } function getUrl(item) { if(item) { var path = ''; var parent = getParentNode(item); if(parent) { path += getUrl(parent); } var table = item; var div = item.nextSibling; var text = getPName(div); path += text + '/'; return path; } } this.delSelected = function() { if(selectedNode != null) { var parent = getParentNode(selectedNode); var div = selectedNode.nextSibling; removeTreeNode(div); selectedNode = null; prevSelNode = null; onSelected(parent); } }; this.setSelected = function(name) { if(selectedNode != null) { var div = selectedNode.nextSibling; setTreeNodeText(div, name); } }; this.refSelected = function(bReload) { function reload(response) { if(response != null) { var table = selectedNode; var div = table.nextSibling; _removeChildNodes(div); var rootNode = response.responseXML.documentElement; var root = rootNode.getElementsByTagName("tree")[0]; loadTreeNode(div, root, div.data.exec); } } if(selectedNode != null) { if(bReload) { var div = selectedNode.nextSibling; Request.GET(div.url, reload); } else { onSelected(selectedNode); } } }; this.Init(); }; 
