?? tree.js
字號:
/***************************************************************************************
* XMLTree2.1
* 此代碼版權(quán)歸海洋工作室ocean所有,您可以非商業(yè)目的使用、復(fù)制、修改此代碼,但需要
* 保留本工作室的版權(quán)信息。如果您使用、修改此代碼為商業(yè)目的,請聯(lián)系本工作室取得使用許可。
* 此腳本的商業(yè)許可為RMB30,被許可方除不能分發(fā)和轉(zhuǎn)售之外,可以用于被許可方的任何項目和
* 產(chǎn)品當(dāng)中。
* 如果您對本程序有什么建議,請email to:ocean@forever.net.cn。
*
* 海洋工作室
* http://www.oceanstudio.net
* ocean(ocean@forever.net.cn) 制作
*****************************************************************************************/
//XML樹類
function XMLTree(treeName) {
this.id = treeName;
this.xmlDoc = null;
this.dblClick = false; // "true"為允許有雙擊事件,"false"為不允許有雙擊事件,默認(rèn)為"false"
this.openAction = false; //展開節(jié)點時的行為,"true"為已展開的節(jié)點全部關(guān)閉,"false"為已展開的節(jié)點全部保持不變.
this.isOpened = false; //節(jié)點是否是展開的,false-沒有展開
this.isLoaded = false; //節(jié)點是否是已經(jīng)加載的。(針對ref型節(jié)點)
this.isSelected = false; //節(jié)點是否被選中
this.ref = ""; //引用外部的xml路徑
this.autoRefresh = false; //是否總是重新加載外部xml(false-不重新加載)
this.text = ""; //節(jié)點的文字
this.title = ""; //節(jié)點的提示
this.textColor = "#000000"; //節(jié)點文字的顏色
this.overTextColor = "#cccccc"; //節(jié)點鼠標(biāo)放上去時候文字的顏色
this.selectedTextColor = "red"; //節(jié)點被選中后的顏色
this.backgroundColor = ""; //節(jié)點的背景色
this.overBackgroundColor = ""; //節(jié)點鼠標(biāo)放上去是的背景色
this.selectedBackgroundColor = "#cccccc"; //節(jié)點被選中時候的背景色
this.underLine = false; //節(jié)點文字是否有下劃線,false-沒有下劃線
this.overUnderLine = true; //節(jié)點鼠標(biāo)放上去的時候文字是否有下劃線,false-沒有下劃線
this.selectedUnderLine = false; //節(jié)點選中的時候文字是否有下劃線
this.fontSize = "9pt"; //文字大小
this.paddingLeft = "18px";//左邊縮進
this.paddingTop = "2px"; //上邊縮進
this.spaceWidth = "2px"; //圖片和圖片以及文字與圖片之間間距
this.leafPaddingLeft = "18px"; //2.01:葉子左邊多余縮進
this.cursor = "hand"; //鼠標(biāo)的默認(rèn)形狀
this.openFlag = "images/openflag.gif"; //節(jié)點打開時的第一個圖片(一般是-號)
this.closeFlag = "images/closeflag.gif"; //節(jié)點關(guān)閉時的第一個圖片(一般是+號)
this.openFolder = "images/openfolder.gif"; //節(jié)點打開時的第二個圖片(一般是一個文件夾的圖片)
this.closeFolder = "images/closedfolder.gif"; //節(jié)點關(guān)閉時的第二個圖片(一般是一個文件夾的圖片)
this.leafImage = "images/url.gif"; //葉子節(jié)點的圖片
this.href = ""; //節(jié)點的url鏈接
this.selectedNode = null; //被選中的節(jié)點
this.doc = null; //節(jié)點的文檔對象
this.menuNode = new Array(); //節(jié)點數(shù)組:此節(jié)點的子菜單中的每一個節(jié)點
this.parent = null; //節(jié)點的父親(為null的話表明這是一個樹根)
this.root = this; //節(jié)點的根(為null的話表明這是一個樹根)
this.currentNode = null; //當(dāng)前正在使用的節(jié)點
}
//外部方法:異步xml讀取數(shù)據(jù),數(shù)據(jù)來自一個xml文件
XMLTree.prototype.load = function (url,container) {
if (container != null)
this.doc = container;
this.xmlDoc = new ActiveXObject("Msxml.DOMDocument");
this.root.currentNode = this;
var f = new Function("event",this.root.id+".currentNode.parseTree()");
this.xmlDoc.onreadystatechange = f;
this.xmlDoc.load(url);
}
//展開一個節(jié)點的下一級子節(jié)點
XMLTree.prototype.open = function (nodeId) {
var node = this.getMenuItem(nodeId);
if (node == null) node = this; //如果沒有找到相應(yīng)的節(jié)點,則以自身節(jié)點
if (node.parent == null) return; //如果節(jié)點是根結(jié)點,則返回
var nodeType = node.getType();
if (nodeType != "leaf") { //葉子節(jié)點不去理會
if (nodeType == "node") { //如果是一個包含字節(jié)點的節(jié)點
node.doc.children[1].style.display = "block";
}
else { //如果是引用節(jié)點
if (node.isLoaded) { //如果節(jié)點已經(jīng)加載,則直接展開
if (node.doc.children.length >= 2) {
node.doc.children[1].style.display = "block";
}
}
else {
node.load(node.ref); //加載子節(jié)點的樹
node.isLoaded = true;
}
}
//標(biāo)識節(jié)點為打開狀態(tài)
node.isOpened = true;
node.doc.children[0].rows[0].cells[0].children[0].src = node.openFlag;
node.doc.children[0].rows[0].cells[1].children[0].src = node.openFolder;
}
}
//關(guān)閉一個節(jié)點的下一級子節(jié)點
XMLTree.prototype.close = function (nodeId) {
var node = this.getMenuItem(nodeId);
if (node == null) node = this; //如果沒有找到相應(yīng)的節(jié)點,則以自身節(jié)點
if (node.parent == null) return; //如果節(jié)點是根結(jié)點,則返回
var nodeType = node.getType();
if (nodeType != "leaf") { //葉子節(jié)點不去理會
if (nodeType == "node") { //如果是一個包含字節(jié)點的節(jié)點
if (node.doc.children.length >= 2) {
node.doc.children[1].style.display = "none";
}
}
else { //如果是引用節(jié)點
if (node.autoRefresh) { //如果節(jié)點屬于總是加載情況,則刪除掉此節(jié)點的子節(jié)點
node.doc.children[1].removeNode(true);
node.menuNode = new Array();
node.isLoaded = false;
}
else {
if (node.doc.children.length >= 2) {
node.doc.children[1].style.display = "none";
}
}
}
node.isOpened = false;
node.doc.children[0].rows[0].cells[0].children[0].src = node.closeFlag;
node.doc.children[0].rows[0].cells[1].children[0].src = node.closeFolder;
}
}
//選中一個節(jié)點
XMLTree.prototype.selected = function (nodeId) {
var node = this.getMenuItem(nodeId);
if (node == null) node = this; //如果沒有找到相應(yīng)的節(jié)點,則以自身節(jié)點
if (node.parent == null) return; //如果節(jié)點是根結(jié)點,則返回
var oSpan = node.doc.children[0].cells[2].children[0]; //找到此節(jié)點相應(yīng)的文字對象
with (oSpan.style) {
color = node.selectedTextColor;
backgroundColor = node.selectedBackgroundColor;
textDecoration = node.selectedUnderLine ? "underline" : "none";
}
node.isSelected = true; //標(biāo)識節(jié)點被選中
}
//取消選中的一個節(jié)點
XMLTree.prototype.unSelected = function (nodeId) {
var node = this.getMenuItem(nodeId);
if (node == null) node = this; //如果沒有找到相應(yīng)的節(jié)點,則以自身節(jié)點
if (node.parent == null) return; //如果節(jié)點是根結(jié)點,則返回
var oSpan = node.doc.children[0].cells[2].children[0]; //找到此節(jié)點相應(yīng)的文字對象
with (oSpan.style) {
color = node.textColor;
backgroundColor = node.backgroundColor;
textDecoration = node.underLine ? "underline" : "none";
}
node.isSelected = false; //標(biāo)識節(jié)點被選中
}
//得到當(dāng)前節(jié)點的類型,返回"leaf"、"node"、"ref"三種情況
XMLTree.prototype.getType = function () {
if (this.ref != "") return "ref";
if (this.menuNode.length > 0) return "node";
return "leaf";
}
//根據(jù)菜單項id查找菜單項,返回?fù)碛写薸d的菜單項
XMLTree.prototype.getMenuItem = function (id) {
if (this.id == id) { //如果此節(jié)點就是要尋找的節(jié)點
return this;
}
else {
for (var i=0;i<this.menuNode.length;i++) {
var result = this.menuNode[i].getMenuItem(id); //遞歸搜索子節(jié)點
if (result != null) //如果搜索到則返回此節(jié)點
return result;
}
return null; //如果搜索不到則返回null
}
}
//2.01:根據(jù)菜單項href查找菜單項,返回?fù)碛写薶ref的菜單項
XMLTree.prototype.getMenuItemByHref = function (href) {
if (this.href == href) { //如果此節(jié)點就是要尋找的節(jié)點
return this;
}
else {
for (var i=0;i<this.menuNode.length;i++) {
var result = this.menuNode[i].getMenuItemByHref(href); //遞歸搜索子節(jié)點
if (result != null) //如果搜索到則返回此節(jié)點
return result;
}
return null; //如果搜索不到則返回null
}
}
//解析xml樹
XMLTree.prototype.parseTree = function () {
var state = this.xmlDoc.readyState;
if (state == 4)
{
var err = this.xmlDoc.parseError;
if (err.errorCode != 0) {
alert("菜單樹加載失敗");
return false;
}
else {
this.xmlDoc = this.xmlDoc.childNodes[1];
this.initTree();
this.attachTree();
if (this.parent == null) { //如果這是根的話,則附加事件
var f = new Function("event",this.id+".doClick()");
this.doc.attachEvent("onclick",f);
f = new Function("event",this.id+".doDblClick()");
this.doc.attachEvent("ondblclick",f);
f = new Function("event",this.id+".doMouseOver()");
this.doc.attachEvent("onmouseover",f);
f = new Function("event",this.id+".doMouseOut()");
this.doc.attachEvent("onmouseout",f);
}
}
}
}
//初始化xml樹
XMLTree.prototype.initTree = function () {
if (this.xmlDoc == null) return;
if (this.parent == null) { //如果這是樹根
this.dblClick = (this.xmlDoc.getAttribute("dbl-click") == "true") ? true : false;
this.openAction = (this.xmlDoc.getAttribute("open-action") == "true") ? true : false;
//指定節(jié)點是否已經(jīng)打開(此節(jié)點為根,這是取默認(rèn)值)
this.isOpened = false;
//節(jié)點的樣式和內(nèi)容賦值(此節(jié)點為根,這是取默認(rèn)值)
this.autoRefresh = (this.xmlDoc.getAttribute("auto-refresh") == "true") ? true : false;
this.textColor = (this.xmlDoc.getAttribute("text-color") == null) ? this.textColor : this.xmlDoc.getAttribute("text-color");
this.overTextColor = (this.xmlDoc.getAttribute("over-text-color") == null) ? this.overTextColor : this.xmlDoc.getAttribute("over-text-color");
this.selectedTextColor = (this.xmlDoc.getAttribute("selected-text-color") == null) ? this.selectedTextColor : this.xmlDoc.getAttribute("selected-text-color");
this.backgroundColor = (this.xmlDoc.getAttribute("background-color") == null) ? this.backgroundColor : this.xmlDoc.getAttribute("background-color");
this.overBackgroundColor = (this.xmlDoc.getAttribute("over-background-color") == null) ? this.overBackgroundColor : this.xmlDoc.getAttribute("over-background-color");
this.selectedBackgroundColor = (this.xmlDoc.getAttribute("selected-background-color") == null) ? this.selectedBackgroundColor : this.xmlDoc.getAttribute("selected-background-color");
this.underLine = (this.xmlDoc.getAttribute("under-line") == "true") ? true : false;
this.overUnderLine = (this.xmlDoc.getAttribute("over-under-line") == "false") ? false : true;
this.selectedUnderLine = (this.xmlDoc.getAttribute("selected-under-line") == "true") ? true : false;
this.fontSize = (this.xmlDoc.getAttribute("font-size") == null) ? this.fontSize : this.xmlDoc.getAttribute("font-size");
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -