home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 January / com! 2002.01.iso / geld / bund / file_tree.js next >
Encoding:
JavaScript  |  2001-08-31  |  11.8 KB  |  479 lines

  1. //**************************************************************** 
  2. // You are free to copy the "Folder-Tree" script as long as you  
  3. // keep this copyright notice: 
  4. // Script found in: http://www.geocities.com/Paris/LeftBank/2178/ 
  5. // Author: Marcelino Alves Martins (martins@hks.com) December '97. 
  6. //**************************************************************** 
  7.  
  8. //Log of changes: 
  9. //       17 Feb 98 - Fix initialization flashing problem with Netscape
  10. //       
  11. //       27 Jan 98 - Root folder starts open; support for USETEXTLINKS; 
  12. //                   make the ftien4 a js file 
  13. //
  14. // Definition of class Folder 
  15. // ***************************************************************** 
  16.  
  17. function Folder(folderDescription, hreference) //constructor 
  18.   //constant data 
  19.   this.desc = folderDescription 
  20.   this.hreference = hreference 
  21.   this.id = -1   
  22.   this.navObj = 0  
  23.   this.iconImg = 0  
  24.   this.nodeImg = 0  
  25.   this.isLastNode = 0 
  26.  
  27.   //dynamic data 
  28.   this.isOpen = true 
  29.   this.iconSrc = "images/16ptfilled.gif"   
  30.   this.children = new Array 
  31.   this.nChildren = 0 
  32.  
  33.   //methods 
  34.   this.initialize = initializeFolder 
  35.   this.setState = setStateFolder 
  36.   this.addChild = addChild 
  37.   this.createIndex = createEntryIndex 
  38.   this.hide = hideFolder 
  39.   this.display = display 
  40.   this.renderOb = drawFolder 
  41.   this.totalHeight = totalHeight 
  42.   this.subEntries = folderSubEntries 
  43.   this.outputLink = outputFolderLink 
  44.  
  45. function setStateFolder(isOpen) 
  46.   var subEntries 
  47.   var totalHeight 
  48.   var fIt = 0 
  49.   var i=0 
  50.  
  51.   if (isOpen == this.isOpen) 
  52.     return 
  53.  
  54.   if (browserVersion == 2)  
  55.   { 
  56.     totalHeight = 0 
  57.     for (i=0; i < this.nChildren; i++) 
  58.       totalHeight = totalHeight + this.children[i].navObj.clip.height 
  59.       subEntries = this.subEntries() 
  60.     if (this.isOpen) 
  61.       totalHeight = 0 - totalHeight 
  62.     for (fIt = this.id + subEntries + 1; fIt < nEntries; fIt++) 
  63.       indexOfEntries[fIt].navObj.moveBy(0, totalHeight) 
  64.   }  
  65.   this.isOpen = isOpen 
  66.   propagateChangesInState(this) 
  67.  
  68. function propagateChangesInState(folder) 
  69. {   
  70.   var i=0 
  71.  
  72.   if (folder.isOpen) 
  73.   { 
  74.     if (folder.nodeImg) 
  75.       if (folder.isLastNode) 
  76.         folder.nodeImg.src = "images/ftv2mnode.gif" 
  77.       else 
  78.       folder.nodeImg.src = "images/ftv2mnode.gif" 
  79.     folder.iconImg.src = "images/16ptfilled.gif" 
  80.     for (i=0; i<folder.nChildren; i++) 
  81.       folder.children[i].display() 
  82.   } 
  83.   else 
  84.   { 
  85.     if (folder.nodeImg) 
  86.       if (folder.isLastNode) 
  87.         folder.nodeImg.src = "images/ftv2pnode.gif" 
  88.       else 
  89.       folder.nodeImg.src = "images/ftv2pnode.gif" 
  90.     folder.iconImg.src = "images/16ptfilled.gif" 
  91.     for (i=0; i<folder.nChildren; i++) 
  92.       folder.children[i].hide() 
  93.   }  
  94.  
  95. function hideFolder() 
  96.   if (browserVersion == 1) { 
  97.     if (this.navObj.style.display == "none") 
  98.       return 
  99.     this.navObj.style.display = "none" 
  100.   } else { 
  101.     if (this.navObj.visibility == "hidden") 
  102.       return 
  103.     this.navObj.visibility = "hidden" 
  104.   } 
  105.    
  106.   this.setState(0) 
  107.  
  108. function initializeFolder(level, lastNode, leftSide) 
  109. var j=0 
  110. var i=0 
  111. var numberOfFolders 
  112. var numberOfDocs 
  113. var nc 
  114.       
  115.   nc = this.nChildren 
  116.    
  117.   this.createIndex() 
  118.  
  119.   var auxEv = "" 
  120.  
  121.   if (browserVersion > 0) 
  122.     auxEv = "<a href='javascript:clickOnNode("+this.id+")' class='menulink'>" 
  123.   else 
  124.     auxEv = "<a>" 
  125.  
  126.   if (level>0) 
  127.     if (lastNode) //the last 'brother' in the children array 
  128.     { 
  129.       this.renderOb(leftSide + auxEv) 
  130.       leftSide = leftSide + "<img src='images/ftv2blank.gif' width=20 height=20>"  
  131.       this.isLastNode = 1 
  132.     } 
  133.     else 
  134.     { 
  135.       this.renderOb(leftSide + auxEv ) 
  136.       leftSide = leftSide + "<img src='images/ftv2blank.gif' width=20 height=20>" 
  137.       this.isLastNode = 0 
  138.     } 
  139.   else 
  140.     this.renderOb("") 
  141.    
  142.   if (nc > 0) 
  143.   { 
  144.     level = level + 1 
  145.     for (i=0 ; i < this.nChildren; i++)  
  146.     { 
  147.       if (i == this.nChildren-1) 
  148.         this.children[i].initialize(level, 1, leftSide) 
  149.       else 
  150.         this.children[i].initialize(level, 0, leftSide) 
  151.       } 
  152.   } 
  153.  
  154. function drawFolder(leftSide) 
  155.   if (browserVersion == 2) { 
  156.     if (!doc.yPos) 
  157.       doc.yPos=0
  158.     doc.write("<layer id='folder" + this.id + "' top=" + doc.yPos + " visibility=hiden>") 
  159.   } 
  160.    
  161.   doc.write("<table ") 
  162.   if (browserVersion == 1) 
  163.     doc.write(" id='folder" + this.id + "' style='position:block;' ") 
  164.   doc.write(" border=0 cellspacing=0 cellpadding=0>") 
  165.   doc.write("<tr><td>") 
  166.   doc.write(leftSide) 
  167.   this.outputLink() 
  168.   doc.write("<img name='folderIcon" + this.id + "' ") 
  169.   doc.write("src='" + this.iconSrc+"' border=0></a>") 
  170.   doc.write("</td><td valign=middle>") 
  171.   if (USETEXTLINKS) 
  172.   { 
  173.     this.outputLink() 
  174.     doc.write(this.desc + "</a>") 
  175.   } 
  176.   else 
  177.     doc.write(this.desc) 
  178.   doc.write("</td>")  
  179.   doc.write("</table>") 
  180.    
  181.   if (browserVersion == 2) { 
  182.     doc.write("</layer>") 
  183.   } 
  184.  
  185.   if (browserVersion == 1) { 
  186.     this.navObj = doc.all["folder"+this.id] 
  187.     this.iconImg = doc.all["folderIcon"+this.id] 
  188.     this.nodeImg = doc.all["nodeIcon"+this.id] 
  189.   } else if (browserVersion == 2) { 
  190.     this.navObj = doc.layers["folder"+this.id] 
  191.     this.iconImg = this.navObj.document.images["folderIcon"+this.id] 
  192.     this.nodeImg = this.navObj.document.images["nodeIcon"+this.id] 
  193.     doc.yPos=doc.yPos+this.navObj.clip.height 
  194.   } 
  195. // This is changed *********** 
  196. function outputFolderLink() 
  197. if (this.id > 0){
  198.   if (this.hreference) 
  199.   { 
  200.     doc.write("<a href='" + this.hreference + "' class='menulink'") 
  201.     if (browserVersion > 0) 
  202.       doc.write("onClick='javascript:clickOnNode("+this.id+")'") 
  203.     doc.write(">") 
  204.   } 
  205.   else 
  206.     doc.write("<a>") 
  207.     doc.write("<a href='javascript:clickOnNode("+this.id+")' class='menulink'>")   
  208. }
  209. else{
  210.   if (this.hreference) 
  211.   { 
  212.     doc.write("<a href='" + this.hreference + "' target='frm_mid_rig' class='menulink'") 
  213.     if (browserVersion > 0) 
  214.       doc.write("onClick='javascript:clickOnFolder("+this.id+")'") 
  215.     doc.write(">") 
  216.   } 
  217.   else 
  218.     //doc.write("<a>") 
  219.     doc.write("<a href='javascript:clickOnFolder("+this.id+")'>")   
  220. }
  221.  
  222. function addChild(childNode) 
  223.   this.children[this.nChildren] = childNode 
  224.   this.nChildren++ 
  225.   return childNode 
  226.  
  227. function folderSubEntries() 
  228.   var i = 0 
  229.   var se = this.nChildren 
  230.  
  231.   for (i=0; i < this.nChildren; i++){ 
  232.     if (this.children[i].children) //is a folder 
  233.       se = se + this.children[i].subEntries() 
  234.   } 
  235.  
  236.   return se 
  237.  
  238.  
  239. // Definition of class Item (a document or link inside a Folder) 
  240. // ************************************************************* 
  241.  
  242. function Item(itemDescription, itemLink) // Constructor 
  243.   // constant data 
  244.   this.desc = itemDescription 
  245.   this.link = itemLink 
  246.   this.id = -1 //initialized in initalize() 
  247.   this.navObj = 0 //initialized in render() 
  248.   this.iconImg = 0 //initialized in render() 
  249.   this.iconSrc = "images/16ptfilled.gif" 
  250.  
  251.   // methods 
  252.   this.initialize = initializeItem 
  253.   this.createIndex = createEntryIndex 
  254.   this.hide = hideItem 
  255.   this.display = display 
  256.   this.renderOb = drawItem 
  257.   this.totalHeight = totalHeight 
  258.  
  259. function hideItem() 
  260.   if (browserVersion == 1) { 
  261.     if (this.navObj.style.display == "none") 
  262.       return 
  263.     this.navObj.style.display = "none" 
  264.   } else { 
  265.     if (this.navObj.visibility == "hidden") 
  266.       return 
  267.     this.navObj.visibility = "hidden" 
  268.   }     
  269.  
  270. function initializeItem(level, lastNode, leftSide) 
  271. {  
  272.   this.createIndex() 
  273.  
  274.   if (level>0) 
  275.     if (lastNode) //the last 'brother' in the children array 
  276.     { 
  277.         this.renderOb(leftSide) 
  278.       leftSide = leftSide + "<img src='images/ftv2blank.gif' width=20 height=20>"  
  279.     } 
  280.     else 
  281.     { 
  282.       this.renderOb(leftSide) 
  283. leftSide = leftSide + "<img src='images/ftv2blank.gif' width=20 height=20>" 
  284.     } 
  285.   else 
  286.     this.renderOb("")   
  287.  
  288. function drawItem(leftSide) 
  289.   if (browserVersion == 2) 
  290.     doc.write("<layer id='item" + this.id + "' top=" + doc.yPos + " visibility=hiden>") 
  291.      
  292.   doc.write("<table") 
  293.   if (browserVersion == 1) 
  294.     doc.write(" id='item" + this.id + "' style='position:block;' ") 
  295.   doc.write(" border=0 cellspacing=0 cellpadding=0>") 
  296.   doc.write("<tr><td>") 
  297.   doc.write(leftSide) 
  298.   doc.write("<a href=" + this.link + ">") 
  299.   doc.write("<img id='itemIcon"+this.id+"' ") 
  300.   doc.write("src='"+this.iconSrc+"' border=0>") 
  301.   doc.write("</a>") 
  302.   doc.write("</td><td valign=middle>") 
  303.   if (USETEXTLINKS) 
  304.     doc.write("<a href=" + this.link + ">" + this.desc + "</a>") 
  305.   else 
  306.     doc.write(this.desc) 
  307.   doc.write("</table>") 
  308.    
  309.   if (browserVersion == 2) 
  310.     doc.write("</layer>") 
  311.  
  312.   if (browserVersion == 1) { 
  313.     this.navObj = doc.all["item"+this.id] 
  314.     this.iconImg = doc.all["itemIcon"+this.id] 
  315.   } else if (browserVersion == 2) { 
  316.     this.navObj = doc.layers["item"+this.id] 
  317.     this.iconImg = this.navObj.document.images["itemIcon"+this.id] 
  318.     doc.yPos=doc.yPos+this.navObj.clip.height 
  319.   } 
  320.  
  321.  
  322. // Methods common to both objects (pseudo-inheritance) 
  323. // ******************************************************** 
  324.  
  325. function display() 
  326.   if (browserVersion == 1) 
  327.     this.navObj.style.display = "block" 
  328.   else 
  329.     this.navObj.visibility = "show" 
  330.  
  331. function createEntryIndex() 
  332.   this.id = nEntries 
  333.   indexOfEntries[nEntries] = this 
  334.   nEntries++ 
  335.  
  336. // total height of subEntries open 
  337. function totalHeight() //used with browserVersion == 2 
  338.   var h = this.navObj.clip.height 
  339.   var i = 0 
  340.    
  341.   if (this.isOpen) //is a folder and _is_ open 
  342.     for (i=0 ; i < this.nChildren; i++)  
  343.       h = h + this.children[i].totalHeight() 
  344.  
  345.   return h 
  346.  
  347.  
  348. // Events 
  349. // ********************************************************* 
  350.  
  351. function clickOnFolder(folderId) 
  352.   var clicked = indexOfEntries[folderId] 
  353.  
  354.   if (!clicked.isOpen) 
  355.     clickOnNode(folderId) 
  356.  
  357.   return  
  358.  
  359.   if (clicked.isSelected) 
  360.     return 
  361.  
  362. function clickOnNode(folderId) 
  363.   
  364.   var clickedFolder = 0 
  365.   var state = 0 
  366.  
  367.   clickedFolder = indexOfEntries[folderId] 
  368.   state = clickedFolder.isOpen 
  369.  
  370.   clickedFolder.setState(!state) //open<->close  
  371.   
  372.  
  373. function initializeDocument() 
  374.   if (doc.all) 
  375.     browserVersion = 1 //IE4   
  376.   else 
  377.     if (doc.layers) 
  378.       browserVersion = 2 //NS4 
  379.     else 
  380.       browserVersion = 0 //other 
  381.  
  382.   foldersTree.initialize(0, 1, "") 
  383.   foldersTree.display()
  384.   
  385.   if (browserVersion > 0) 
  386.   { 
  387.     doc.write("<layer top="+indexOfEntries[nEntries-1].navObj.top+"> </layer>") 
  388.  
  389.     // close the whole tree 
  390.     clickOnNode(0) 
  391.     // open the root folder 
  392.     clickOnNode(0) 
  393.   } 
  394.  
  395. // Auxiliary Functions for Folder-Treee backward compatibility 
  396. // ********************************************************* 
  397.  
  398. function gFld(description, hreference) 
  399.   folder = new Folder(description, hreference) 
  400.   return folder 
  401.  
  402. function gLnk(target, description, linkData) 
  403.   fullLink = "" 
  404.  
  405.   if (target==0) 
  406.   { 
  407.     fullLink = "'"+linkData+"'" 
  408.   } 
  409.   else
  410.   { 
  411.     if (target==1) 
  412.        fullLink = "'"+linkData+"' class='menulink' target='frm_mid_rig'" 
  413.     else 
  414.        fullLink = "'http://"+linkData+"' class='menulink' target='frm_mid_rig'" 
  415.   } 
  416.  
  417.   linkItem = new Item(description, fullLink)   
  418.   return linkItem 
  419.  
  420. function insFld(parentFolder, childFolder) 
  421.   return parentFolder.addChild(childFolder) 
  422.  
  423. function insDoc(parentFolder, document) 
  424.   parentFolder.addChild(document) 
  425.  
  426. // Global variables 
  427. // **************** 
  428.  
  429. USETEXTLINKS = 1 
  430. indexOfEntries = new Array 
  431. nEntries = 0 
  432. doc = document 
  433. browserVersion = 0 
  434. selectedFolder=1
  435.