[求助]用Javascript脚本
*/ --------------------------------------------------------------------------------------<BR>*/ 出自: 编程中国 http://www.bc-cn.net<BR>*/ 作者: a2332579 QQ:240179387<BR>*/ 时间: 2007-8-2 编程论坛首发<BR>*/ 声明: 尊重作者劳动,转载请保留本段文字<BR>*/ --------------------------------------------------------------------------------------<BR><BR><BR><P>function Folder(folderDescription, hreference) {<BR> //constant data;<BR> this.desc = folderDescription;<BR> this.hreference = hreference;<BR> this.id = -1;<BR> this.navObj = 0;<BR> this.iconImg = 0;<BR> this.nodeImg = 0;<BR> this.isLastNode = 0;</P>
<P> // dynamic data<BR> this.isRoot = true;<BR> this.isOpen = true;<BR> this.iconSrc = "ftv2folderopen.gif";<BR> this.iconRoot = "ftv2folderopen.gif"<BR> this.children = new Array;<BR> this.nChildren = 0;<BR> <BR> // methods<BR> this.initialize = initializeFolder;<BR> this.setState = setStateFolder;<BR> this.addChild = addChild;<BR> this.createIndex = createEntryIndex;<BR> this.hide = hideFolder;<BR> this.display = display;<BR> this.renderOb = drawFolder;<BR> this.totalHeight = totalHeight;<BR> this.subEntries = folderSubEntries;<BR> this.outputLink = outputFolderLink;<BR>}</P>
<P><BR>function setStateFolder(isOpen) { <BR> var subEntries; <BR> var totalHeight; <BR> var fIt = 0; <BR> var i = 0; <BR> <BR> if(isOpen == this.isOpen)<BR> return;<BR> <BR> if(browserVersion == 2) {<BR> totalHeight = 0 <BR> for(i = 0; i < this.nChildren; i++) <BR> totalHeight = totalHeight + this.children[i].navObj.clip.height;<BR> subEntries = this.subEntries();<BR> <BR> if(this.isOpen)<BR> totalHeight = 0 - totalHeight;</P>
<P> for(fIt = this.id + subEntries + 1; fIt < nEntries; fIt++)<BR> indexOfEntries[fIt].navObj.moveBy(0, totalHeight);<BR> }</P>
<P> this.isOpen = isOpen;<BR> propagateChangesInState(this);<BR>} </P>
<P><BR>function propagateChangesInState(folder) { <BR> var i = 0;<BR> if(folder.isOpen) { <BR> if(folder.nodeImg) {<BR> if(folder.isLastNode)<BR> folder.nodeImg.src = "images/ftv2mlastnode.gif";<BR> else<BR> folder.nodeImg.src = "images/ftv2mnode.gif";<BR> }</P>
<P> if(this.isRoot)<BR> folder.iconImg.src = "images/ftv2mail.gif";<BR> else <BR> folder.iconImg.src = "images/ftv2folderopen.gif";<BR> <BR> for(i = 0; i < folder.nChildren; i++) <BR> folder.children[i].display();<BR> } <BR> else { <BR> if(folder.nodeImg) {<BR> if(folder.isLastNode)<BR> folder.nodeImg.src = "images/ftv2plastnode.gif";<BR> else<BR> folder.nodeImg.src = "images/ftv2pnode.gif";<BR> }</P>
<P> if(this.isRoot)<BR> folder.iconImg.src = "images/ftv2mail.gif";<BR> else <BR> folder.iconImg.src = "images/ftv2folderclosed.gif";<BR> <BR> for(i = 0; i < folder.nChildren; i++) <BR> folder.children[i].hide();<BR> }<BR>} </P>
<P>function hideFolder() { <BR> if(browserVersion == 1) { <BR> if(this.navObj.style.display == "none")<BR> return;</P>
<P> this.navObj.style.display = "none";<BR> }<BR> else { <BR> if(this.navObj.visibility == "hiden")<BR> return;</P>
<P> this.navObj.visibility = "hiden";<BR> } </P>
<P> this.setState(0);<BR>} </P>
<P><BR>function initializeFolder(level, lastNode, leftSide) { <BR> var i = 0;<BR> var j = 0;</P>
<P> var numberOfFolders;<BR> var numberOfDocs;</P>
<P> nc = this.nChildren;<BR> this.createIndex();</P>
<P> var nc;<BR> var auxEv = "";</P>
<P> if(browserVersion > 0)<BR> auxEv = "<A HREF='JavaScript: clickOnNode("+this.id+")'>";//+<BR> else<BR> auxEv = "<A>";</P>
<P> if(level > 0) {<BR> if(lastNode) { //the last 'brother' in the children array<BR> this.renderOb(leftSide + auxEv + "<IMG NAME='nodeIcon" + this.id + "' SRC='images/ftv2mlastnode.gif' WIDTH=16 HEIGHT=22 BORDER=0></A>");<BR> leftSide = leftSide + "<IMG SRC='images/ftv2blank.gif' WIDTH=16 HEIGHT=22>";<BR> this.isLastNode = 1;<BR> }<BR> else {<BR> this.renderOb(leftSide + auxEv + "<IMG NAME='nodeIcon" + this.id + "' SRC='images/ftv2mnode.gif' WIDTH=16 HEIGHT=22 BORDER=0></A>");<BR> leftSide = leftSide + "<IMG SRC='images/ftv2vertline.gif' WIDTH=16 HEIGHT=22>";<BR> this.isLastNode = 0;<BR> }<BR> }<BR> else {<BR> this.renderOb("");<BR> }</P>
<P> if(nc > 0) {<BR> level = level + 1;<BR> for(i = 0; i < this.nChildren; i++) {<BR> if(i == this.nChildren-1)<BR> this.children[i].initialize(level, 1, leftSide);<BR> else<BR> this.children[i].initialize(level, 0, leftSide);<BR> }<BR> }<BR>}<BR> </P>
<P>function drawFolder(leftSide) { <BR> //alert(leftSide);<BR> if(browserVersion == 2) {<BR> if(!doc.yPos) <BR> doc.yPos = 8;</P>
<P> doc.write("<LAYER ID='folder" + this.id + "' TOP=" + doc.yPos + " VISIBILITY=hiden>");<BR> } <BR> doc.write("<TABLE") </P>
<P> if(browserVersion == 1) <BR> doc.write(" ID='folder" + this.id + "' STYLE='position:block;' ");</P>
<P> doc.write(" BORDER=0 CELLSPACING=0 CELLPADDING=0>");<BR> doc.write("<TR><TD>");<BR> doc.write(leftSide);<BR> this.outputLink();<BR> <BR> if(leftSide != '') { <BR> doc.write("<IMG NAME='folderIcon" + this.id + "' ");<BR> <BR> if(leftSide == '') {<BR> doc.write("SRC='images/" + this.iconRoot+"' BORDER=0></A>");<BR> }<BR> else {<BR> doc.write("SRC='images/" + this.iconSrc+"' BORDER=0></A>");<BR> } <BR> }<BR> else<BR> { <BR> doc.write("<span id='folderIcon" + this.id + "' NAME='folderIcon" + this.id + "' >");</P>
<P> }<BR> doc.write("</TD><TD NOWRAP>");<BR> <BR> doc.write("<DIV CLASS=\"fldrroot\">");</P>
<P> if(leftSide == '') { <BR> if(USETEXTLINKS) {<BR> //this.outputLink();<BR> //doc.write(this.desc + "</A>");<BR> }<BR> else <BR> doc.write(this.desc);<BR> }<BR> else {<BR> doc.write("<A HREF='JavaScript: clickOnNode("+this.id+")'>");<BR> doc.write(this.desc + "</A>");<BR> }</P>
<P> doc.write("</DIV>");<BR> doc.write("</TD>");<BR> doc.write("</TABLE>");</P>
<P> if(browserVersion == 2) { <BR> doc.write("</LAYER>");<BR> } </P>
<P> if(browserVersion == 1) { <BR> this.navObj = doc.all["folder"+this.id];<BR> this.iconImg = doc.all["folderIcon"+this.id] <BR> this.nodeImg = doc.all["nodeIcon"+this.id] <BR> }<BR> else if(browserVersion == 2) { <BR> this.navObj = doc.layers["folder"+this.id]; <BR> this.iconImg = this.navObj.document.images["folderIcon"+this.id];<BR> this.nodeImg = this.navObj.document.images["nodeIcon"+this.id]; <BR> doc.yPos = doc.yPos + this.navObj.clip.height;<BR> }<BR>} <BR> </P>
<P>function outputFolderLink() {<BR> if(this.hreference) { <BR> doc.write("<A HREF='" + this.hreference + "' TARGET=\"main\" ") </P>
<P> if(browserVersion > 0) {<BR> doc.write("onClick='JavaScript: clickOnFolder("+this.id+")'")//folder <BR> }<BR> doc.write(">") <BR> } <BR> else <BR> doc.write("<A>")<BR>} </P>
<P> <BR>function addChild(childNode) {<BR> this.children[this.nChildren] = childNode;<BR> this.nChildren++;</P>
<P> return(childNode);<BR>} </P>
<P><BR>function folderSubEntries() { <BR> var i = 0;<BR> var se = this.nChildren;<BR> <BR> for(i = 0; i < this.nChildren; i++) {<BR> if(this.children[i].children) //is a folder <BR> se = se + this.children[i].subEntries();<BR> }<BR> <BR> return(se)<BR>} <BR> <BR> <BR>// Definition of class Item (a document or link inside a Folder) </P>
<P>function Item(itemDescription, hreference, itemLink, itemImg) // Constructor <BR>{ <BR> // constant data <BR> this.desc = itemDescription <BR> this.link = itemLink <BR> this.hreference = hreference;<BR> this.id = -1; //initialized in initalize() <BR> this.navObj = 0; //initialized in render() <BR> this.iconImg = 0; //initialized in render() </P>
<P> this.iconSrc = itemImg;</P>
<P> // methods<BR> this.initialize = initializeItem ;<BR> this.createIndex = createEntryIndex;<BR> this.hide = hideItem;<BR> this.display = display;<BR> this.renderOb = drawItem;<BR> this.totalHeight = totalHeight;<BR>} </P>
<P><BR>function hideItem() {<BR> if(browserVersion == 1) {<BR> if(this.navObj.style.display == "none") <BR> return;<BR> this.navObj.style.display = "none" <BR> }<BR> else {<BR> if(this.navObj.visibility == "hiden")<BR> return;<BR> this.navObj.visibility = "hiden";<BR> }<BR>} <BR> </P>
<P>function initializeItem(level, lastNode, leftSide) {<BR> this.createIndex();</P>
<P> if(level > 0) {<BR> if(lastNode) { //the last 'brother' in the children array <BR> this.renderOb(leftSide + "<IMG SRC='images/ftv2lastnode.gif' WIDTH=16 HEIGHT=22>") <BR> leftSide = leftSide + "<IMG SRC='images/ftv2blank.gif' WIDTH=16 HEIGHT=22>" <BR> }<BR> else {<BR> this.renderOb(leftSide + "<IMG SRC='images/ftv2node.gif' WIDTH=16 HEIGHT=22>") <BR> leftSide = leftSide + "<IMG SRC='images/ftv2vertline.gif' WIDTH=16 HEIGHT=22>" <BR> }<BR> }<BR> else <BR> this.renderOb("") <BR>}</P>
<P><BR>function drawItem(leftSide) { <BR> if(browserVersion == 2)<BR> doc.write("<LAYER ID='item" + this.id + "' TOP=" + doc.yPos + " VISIBILITY=hiden>");//link<BR> <BR> doc.write("<TABLE ");<BR> if(browserVersion == 1) <BR> doc.write(" ID='item" + this.id + "' STYLE='position:block;' ");<BR> doc.write(" BORDER=0 CELLSPACING=0 CELLPADDING=0>");<BR> doc.write("<TR><TD>");<BR> doc.write(leftSide);</P>
<P> if(this.link != "")<BR> doc.write("<A HREF=" + this.link + ">");<BR> <BR> doc.write("<IMG ID='itemIcon"+this.id+"' ") <BR> doc.write("SRC='images/"+this.iconSrc+"' BORDER=0>") </P>
<P> if(this.link != "")<BR> doc.write("</A>");<BR> <BR> doc.write("</TD><TD NOWRAP>"); <BR> doc.write("<DIV CLASS=\"fldritem\">");<BR> if(USETEXTLINKS) {<BR> if(this.link != "")<BR> doc.write("<A HREF=" + this.link + ">" + this.desc + "</A>");<BR> else<BR> doc.write(this.desc);<BR> }<BR> else {<BR> doc.write(this.desc);<BR> }</P>
<P> doc.write("</DIV>");<BR> doc.write("</TABLE>") <BR> <BR> if(browserVersion == 2)<BR> doc.write("</layer>");</P>
<P> if(browserVersion == 1) {<BR> this.navObj = doc.all["item"+this.id] <BR> this.iconImg = doc.all["itemIcon"+this.id] <BR> }<BR> else if(browserVersion == 2) {<BR> this.navObj = doc.layers["item"+this.id];<BR> this.iconImg = this.navObj.document.images["itemIcon"+this.id];<BR> doc.yPos = doc.yPos+this.navObj.clip.height;<BR> }<BR>} <BR> <BR> <BR>// Methods common to both objects (pseudo-inheritance) <BR>function display() { <BR> if(browserVersion == 1) <BR> this.navObj.style.display = "block";<BR> else<BR> this.navObj.visibility = "show";<BR>}</P>
<P><BR>function createEntryIndex() { <BR> this.id = nEntries;<BR> indexOfEntries[nEntries] = this;<BR> nEntries++;<BR>}</P>
<P><BR>// total height of subEntries open <BR>function totalHeight() { //used with browserVersion == 2 <BR> var h = this.navObj.clip.height;<BR> var i = 0;</P>
<P> if(this.isOpen) { // is a folder and _is_ open <BR> for(i = 0 ; i < this.nChildren; i++) <BR> h = h + this.children[i].totalHeight();<BR> }</P>
<P> return h <BR>} <BR> <BR> <BR>// Events <BR> <BR>function clickOnFolder(folderId) { <BR> var clicked = indexOfEntries[folderId];</P>
<P> if(!clicked.isOpen)<BR> clickOnNode(folderId);</P>
<P> if(clicked.isSelected) <BR> return;<BR>} <BR> </P>
<P>function clickOnNode(folderId) { <BR> var clickedFolder = 0;<BR> var state = 0;</P>
<P> clickedFolder = indexOfEntries[folderId];<BR> state = clickedFolder.isOpen;<BR> <BR> clickedFolder.setState(!state); //open<->close<BR>}</P>
<P><BR>function initializeDocument() { <BR> if(doc.all)<BR> browserVersion = 1; //IE4<BR> else if(doc.layers)<BR> browserVersion = 2; //NS4<BR> else <BR> browserVersion = 0; //other</P>
<P> ax0.initialize(0, 1, "");<BR> ax0.display();</P>
<P> if(browserVersion > 0) {<BR> doc.write("<LAYER TOP="+indexOfEntries[nEntries-1].navObj.top+">&nbsp;</LAYER>");</P>
<P> // close the whole tree<BR> clickOnNode(0);<BR> <BR> // open the root folder <BR> clickOnNode(0);<BR> } <BR>} <BR> </P>
<P>// Auxiliary Functions for Folder-Treee backward compatibility <BR>function gFld(description, hreference) { <BR> folder = new Folder(description, hreference);<BR> return(folder);<BR>}</P>
<P><BR>function gLnk(target, description, linkData, itemImg) { <BR> fullLink = "" <BR> <BR> if(target == 0) {<BR> if(linkData != "")<BR> fullLink = "'"+linkData+"' TARGET=\"main\"";<BR> else<BR> fullLink = "";<BR> }<BR> else if(target == 1) {<BR> if(linkData != "")<BR> fullLink = "'"+linkData+"' TARGET=\"_top\"";<BR> else<BR> fullLink = "";<BR> }</P>
<P> linkItem = new Item(description, linkData, fullLink, itemImg) <BR> return linkItem <BR>} </P>
<P><BR>function insFld(parentFolder, childFolder) { <BR> return(parentFolder.addChild(childFolder));<BR>}</P>
<P><BR>function insDoc(parentFolder, document) { <BR> parentFolder.addChild(document);<BR>}</P>
<P><BR>// Global variables <BR> <BR>USETEXTLINKS = 1;<BR>indexOfEntries = new Array;<BR>nEntries = 0;<BR>doc = document;<BR>browserVersion = 0;<BR>selectedFolder = 0;</P>
页:
[1]
