var TreeHandler = { id :0, all :{}, getId :function (obj, key) { var ID = key == null ? this.id :key; this.all[ID] = obj; return key==null ? this.id++ : key; }, setImagePath :function(sPath){ for (i in TreeConfig.TreeIcon) { var tem = new Image(); tem.src = sPath + TreeConfig.TreeIcon[i]; TreeConfig.TreeIcon[i] = tem.src; } } };
//************* // WebCookie //************* var WebCookie = new function () {
this.setValue = function (sName, sValue, sExpire, sPath, sDomain, sSecure) { var cookie = sName + "=" + escape(sValue); if (sExpire) cookie += "; expires=" + sExpire.toGMTString(); if (sPath) cookie += "; path=" + sPath; if (sSecure) cookie += "; secure"; document.cookie = cookie; };
this.getValue = function (sName) { var c = document.cookie.split("; "); for (var i=0; i<c.length; i++) { var cItem = c[i].split("="); if (cItem[0] == sName) return unescape(cItem[1]); } return null; };
this.delCookie = function (sName) { var cVal = this.getValue(sName); if (cVal != null) { var d = new Date();d.setTime(d.getTime()-1); this.setValue(sName, cVal, d); } }; };
//************** // TreeNode //************** Array.prototype.Remove = function(o){ for (var i=0; i<this.length; i++) { if (this[i] == o) break; } if (i != this.length) return this.slice(0,i).concat(this.slice(i+1,this.length)); return this; };
TreeNode.prototype.HTMLtoText = function (s) { return String(s).replace(/&/g, "&").replace(/\"/g, '"').replace(/</g,'<').replace(/>/g, '>'); };
TreeNode.prototype.isLast = function () { var p = this.parentNode; if (p == null) return false; return p.childNodes[p.childNodes.length - 1] == this; };
TreeNode.prototype.indent = function () { for (var i=0; i<this.childNodes.length; i++) { this.childNodes[i].indent(); } var t = this._item, iv = this.level; if (iv) while (--iv) { t.removeChild(t.firstChild); } var node = this.parentNode, v = 0, _root = this.getRoot(); while (node) { v++; if (node == _root) break; var m = document.createElement("img"); m.align = "absmiddle"; m.src = node.isLast() ? TreeConfig.TreeIcon.blank : TreeConfig.TreeIcon.line; t.insertBefore(m, t.firstChild); node = node.parentNode; } this.level = v; };
TreeNode.prototype.recalIndent = function (nLevel, b) { for (var i = 0; i<this.childNodes.length; i++) { this.childNodes[i]._item.childNodes[nLevel-1].src = b ? TreeConfig.TreeIcon.blank : TreeConfig.TreeIcon.line; this.childNodes[i].recalIndent(nLevel, b); } };
TreeNode.prototype.reloadIcon = function () { var l = this.isLast(), o = this.open, m = TreeConfig.TreeIcon; if (this.parentNode) { this._handler.src = this.childNodes.length>0 ? (o ? (l ? m.minusbottom : m.minus) : (l ? m.plusbottom : m.plus)) : (l ? m.joinbottom : m.join); } this._icon.src = this.childNodes.length>0 ? (o ? (this.openIcon ? this.openIcon : (this.icon ? this.icon : m.folderopen)) : (this.icon ? this.icon : m.folder)) : (this.icon ? this.icon : m.file); };
TreeNode.prototype.addXMLNodeLoop = function (doc) { var c = doc.childNodes; for (var i = 0; i < c.length; i++) { var o = c[i]; if (o.nodeType == 1) { var X = this.constructor; var node = new X(o.getAttribute("id"), o.getAttribute("text"), o.getAttribute("href"), o.getAttribute("target"), o.getAttribute("title"), o.getAttribute("icon"), o.getAttribute("openicon"), o.getAttribute('src')); this.add(node); if (!o.getAttribute("src")) { node.addXMLNodeLoop(o); } } } };
TreeNode.prototype.XMLHttpCallBack = function () { if (this._xmlhttp.readyState != 4) return; var oLoad = this.loader; var doc = this._xmlhttp.responseXML; var sXML = String(this._xmlhttp.responseText).replace(/<\?xml[^\?]*\?>/i, ""); if (window.DOMParser) { doc = (new DOMParser()).parseFromString(sXML, 'text/xml'); } else { doc.loadXML(sXML); } if (doc.documentElement) { var oRoot = doc.getElementsByTagName("Tree")[0]; if (oRoot.childNodes.length == 0) { this.setText(TreeConfig.unavaibleText); } else { var s = this._tree.getSelectedNode() == oLoad; this.addXMLNodeLoop(oRoot); oLoad.remove(); this.async(); this.loader = null; } } else { oLoad.setText(TreeConfig.unavaibleText); } };
TreeNode.prototype.getXML = function () { var oLoad = this.loader; var oThis = this; this._xmlhttp = null; try{ if (window.XMLHttpRequest) this._xmlhttp = new XMLHttpRequest(); else if (window.ActiveXObject) this._xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){} if (this._xmlhttp == null) throw new Error('Your browser doesn\'t support!'); this._xmlhttp.onreadystatechange = function () { oThis.XMLHttpCallBack(); }; try { var temp = (/\?/g.test(this.src)?"&":"?") + "temp=" + String(new Date().getTime()) this._xmlhttp.open("get", this.src + temp, true); this._xmlhttp.send(null); }catch(e){ oLoad.setText(TreeConfig.unavaibleText);} };
TreeNode.prototype.resetTree = function (oTree) { for (var i=0; i<this.childNodes.length; i++) this.childNodes[i].resetTree(oTree); this._tree = oTree; };
TreeNode.prototype.setOpen = function (v) { this.open = v; if (TreeConfig.useCookie) { WebCookie.setValue("o" + this.id, v); } };
TreeNode.prototype.getOpen = function () { var o = WebCookie.getValue("o" + this.id); if (o != null) return parseInt(o); return 0; };
TreeNode.prototype.toHTML = function() { var o = this._item; this.indent(); this.reloadIcon(); if (this.parentNode == null) o.removeChild(this._handler); return o; };
TreeNode.prototype.getRoot = function() { var root = this; while (root.parentNode) root = root.parentNode; return root; };
TreeNode.prototype.async = function () { var a = this._tree.context; if (!a.length) return; var id = a[a.length - 1]; var node = TreeHandler.all[id]; if (typeof(node) != 'undefined') { if (node.parentNode == this) { this._.context = a.slice(0, -1); if (node.childNodes.length > 0) node.expand(); else node.select(); } } };
TreeNode.prototype.getPreviousSibling = function() { if (!this.parentNode) return null; for (var i=0;i<this.parentNode.childNodes.length;i++) if (this.parentNode.childNodes[i] == this) break; if (i == 0) return this.parentNode; else return this.parentNode.childNodes[i-1].getLastChild(); };
TreeNode.prototype.getNextSibling = function() { if (!this.parentNode) return null; for (var i=0;i<this.parentNode.childNodes.length;i++) if (this.parentNode.childNodes[i] == this)break; if (i == this.parentNode.childNodes.length-1) return this.parentNode.getNextSibling(); else return this.parentNode.childNodes[i+1]; }
TreeNode.prototype.KeyDown=function(e){ e = e || window.event; var code = e.which || e.keyCode; var o = this; if (code == 37) { if (this.open) this.collapse(); else { if (this.parentNode) this.parentNode.select(false); } return false; } else if (code == 38) { var el = o.getPreviousSibling(); if (el) el.select(false); return false; } else if (code == 39) { if (this.childNodes.length>0) { if (!this.open) this.expand(); else { var el = o.getFirstChild(); if(el) el.select(false); } } return false; } else if (code == 40) { if (this.open && this.childNodes.length>0) this.getFirstChild().select(false); else { var el = o.getNextSibling(); if (el) el.select(false); } return false; } else if (code == 13) { this.toggle(); return true; } return true; };
CheckBoxTreeNode.prototype.check = function () { this.setCheckedChildren(this.getChecked()); this.setCheckedParent(); if (typeof this.oncheck == "function") { this.oncheck(); } else { eval(this.oncheck); } };
CheckBoxTreeNode.prototype.setCheckedChildren = function (b) { for (var i=0,j=0; i<this.childNodes.length; i++) { if (this.childNodes[i] instanceof CheckBoxTreeNode) this.childNodes[i].setCheckedChildren(b); } this._checkbox.checked = b; };
CheckBoxTreeNode.prototype.setCheckedParent = function () { var p = this.parentNode; if (p instanceof CheckBoxTreeNode) { for (var i=0; i<p.childNodes.length; i++) { if (!p.childNodes[i].getChecked()) break; } p._checkbox.checked = i == p.childNodes.length p.setCheckedParent(); } };
CheckBoxTreeNode.prototype.getChecked = function () { return this._checkbox.checked; };
TreeView.prototype.getSelectedNode = function () { if (window.selectedNode) return window.selectedNode; return null; };
TreeView.prototype.setSelectedNode = function (oNode) { window.selectedNode = oNode; };
TreeView.prototype.setContext = function () { this.context = new Array(); for (var i=arguments.length-1,j=0; i>=0; i--,j++) { this.context[j] = arguments[i]; } };
TreeView.prototype.create = function (oTarget) { oTarget.appendChild(this.toHTML()); this._rendered = true; if (this.childNodes.length>0 || this.open || this.src) this.expand(); };
TreeView.prototype.toString = function () { var obj = this.toHTML(); var o = document.createElement("div"); o.appendChild(obj); this._rendered = true; return o.innerHTML; };