home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 45 / cda45.iso / html / scarbol.js < prev    next >
Encoding:
JavaScript  |  2000-02-24  |  12.0 KB  |  478 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.  
  15.  
  16. // Definition of class Folder 
  17. // ***************************************************************** 
  18.  
  19. function Folder(folderDescription, hreference) //constructor 
  20.   //constant data 
  21.   this.desc = "<div id='folderEstilo'>"+folderDescription+"</div> "
  22.   this.hreference = hreference 
  23.   this.id = -1   
  24.   this.navObj = 0  
  25.   this.iconImg = 0  
  26.   this.nodeImg = 0  
  27.   this.isLastNode = 0 
  28.  
  29.   //dynamic data 
  30.   this.isOpen = true 
  31.   this.iconSrc = "imagenes/folderopen.gif"   
  32.   this.children = new Array 
  33.   this.nChildren = 0 
  34.  
  35.   //methods 
  36.   this.initialize = initializeFolder 
  37.   this.setState = setStateFolder 
  38.   this.addChild = addChild 
  39.   this.createIndex = createEntryIndex 
  40.   this.hide = hideFolder 
  41.   this.display = display 
  42.   this.renderOb = drawFolder 
  43.   this.totalHeight = totalHeight 
  44.   this.subEntries = folderSubEntries 
  45.   this.outputLink = outputFolderLink 
  46.  
  47. function setStateFolder(isOpen) 
  48.   var subEntries 
  49.   var totalHeight 
  50.   var fIt = 0 
  51.   var i=0 
  52.  
  53.   if (isOpen == this.isOpen) 
  54.     return 
  55.  
  56.   if (browserVersion == 2)  
  57.   { 
  58.     totalHeight = 0 
  59.     for (i=0; i < this.nChildren; i++) 
  60.       totalHeight = totalHeight + this.children[i].navObj.clip.height 
  61.       subEntries = this.subEntries() 
  62.     if (this.isOpen) 
  63.       totalHeight = 0 - totalHeight 
  64.     for (fIt = this.id + subEntries + 1; fIt < nEntries; fIt++) 
  65.       indexOfEntries[fIt].navObj.moveBy(0, totalHeight) 
  66.   }  
  67.   this.isOpen = isOpen 
  68.   propagateChangesInState(this) 
  69.  
  70. function propagateChangesInState(folder) 
  71. {   
  72.   var i=0 
  73.  
  74.   if (folder.isOpen) 
  75.   { 
  76.     if (folder.nodeImg) 
  77.       if (folder.isLastNode) 
  78.         folder.nodeImg.src = "imagenes/alastnode.gif" 
  79.       else 
  80.       folder.nodeImg.src = "imagenes/anode.gif" 
  81.  
  82.     if (folder.id == 0)
  83.        folder.iconImg.src = "imagenes/blank.gif" 
  84.     else
  85.        folder.iconImg.src = "imagenes/folderopen.gif" 
  86.  
  87.     for (i=0; i<folder.nChildren; i++) 
  88.       folder.children[i].display() 
  89.   } 
  90.   else 
  91.   { 
  92.     if (folder.nodeImg) 
  93.       if (folder.isLastNode) 
  94.         folder.nodeImg.src = "imagenes/clastnode.gif" 
  95.       else 
  96.       folder.nodeImg.src = "imagenes/cnode.gif" 
  97.  
  98.     if (folder.id == 0)
  99.        folder.iconImg.src = "imagenes/blank.gif" 
  100.     else
  101.        folder.iconImg.src = "imagenes/folderclosed.gif" 
  102.  
  103.     for (i=0; i<folder.nChildren; i++) 
  104.       folder.children[i].hide() 
  105.   }  
  106.  
  107. function hideFolder() 
  108.   if (browserVersion == 1) { 
  109.     if (this.navObj.style.display == "none") 
  110.       return 
  111.     this.navObj.style.display = "none" 
  112.   } else { 
  113.     if (this.navObj.visibility == "hiden") 
  114.       return 
  115.     this.navObj.visibility = "hiden" 
  116.   } 
  117.    
  118.   this.setState(0) 
  119.  
  120. function initializeFolder(level, lastNode, leftSide) 
  121. var j=0 
  122. var i=0 
  123. var numberOfFolders 
  124. var numberOfDocs 
  125. var nc 
  126.       
  127.   nc = this.nChildren 
  128.    
  129.   this.createIndex() 
  130.  
  131.   var auxEv = "" 
  132.  
  133.   if (browserVersion > 0) 
  134.     auxEv = "<a href='javascript:clickOnNode("+this.id+")'>" 
  135.   else 
  136.     auxEv = "<a>" 
  137.  
  138.   if (level>0) 
  139.     if (lastNode) //the last 'brother' in the children array 
  140.     { 
  141.       this.renderOb(leftSide + auxEv + "<img name='nodeIcon" + this.id + "' src='imagenes/alastnode.gif' width=10 height=22 border=0></a>") 
  142.       leftSide = leftSide + "<img src='imagenes/blank.gif' width=10 height=22>"  
  143.       this.isLastNode = 1 
  144.     } 
  145.     else 
  146.     { 
  147.       this.renderOb(leftSide + auxEv + "<img name='nodeIcon" + this.id + "' src='imagenes/anode.gif' width=10 height=22 border=0></a>") 
  148.       leftSide = leftSide + "<img src='imagenes/vertline.gif' width=10 height=22>" 
  149.       this.isLastNode = 0 
  150.     } 
  151.   else 
  152.     this.renderOb("") 
  153.    
  154.   if (nc > 0) 
  155.   { 
  156.     level = level + 1 
  157.     for (i=0 ; i < this.nChildren; i++)  
  158.     { 
  159.       if (i == this.nChildren-1) 
  160.         this.children[i].initialize(level, 1, leftSide) 
  161.       else 
  162.         this.children[i].initialize(level, 0, leftSide) 
  163.       } 
  164.   } 
  165.  
  166. function drawFolder(leftSide) 
  167.   if (browserVersion == 2) { 
  168.     if (!doc.yPos) 
  169.       doc.yPos=8 
  170.     doc.write("<layer id='folder" + this.id + "' top=" + doc.yPos + " visibility=hiden>") 
  171.   } 
  172.    
  173.   doc.write("<table ") 
  174.   if (browserVersion == 1) 
  175.     doc.write(" id='folder" + this.id + "' style='position:block;' ") 
  176.   doc.write(" border=0 cellspacing=0 cellpadding=0>") 
  177.   doc.write("<tr><td>") 
  178.   doc.write(leftSide) 
  179.   this.outputLink() 
  180.   doc.write("<img name='folderIcon" + this.id + "' ") 
  181.   doc.write("src='" + this.iconSrc+"' border=0></a>") 
  182.   doc.write("</td><td valign=middle nowrap>") 
  183.   if (USETEXTLINKS) 
  184.   { 
  185.     this.outputLink() 
  186.     doc.write(this.desc + "</a>") 
  187.   } 
  188.   else 
  189.     doc.write(this.desc) 
  190.   doc.write("</td>")  
  191.   doc.write("</table>") 
  192.    
  193.   if (browserVersion == 2) { 
  194.     doc.write("</layer>") 
  195.   } 
  196.  
  197.   if (browserVersion == 1) { 
  198.     this.navObj = doc.all["folder"+this.id] 
  199.     this.iconImg = doc.all["folderIcon"+this.id] 
  200.     this.nodeImg = doc.all["nodeIcon"+this.id] 
  201.   } else if (browserVersion == 2) { 
  202.     this.navObj = doc.layers["folder"+this.id] 
  203.     this.iconImg = this.navObj.document.images["folderIcon"+this.id] 
  204.     this.nodeImg = this.navObj.document.images["nodeIcon"+this.id] 
  205.     doc.yPos=doc.yPos+this.navObj.clip.height 
  206.   } 
  207.  
  208. function outputFolderLink() 
  209.   if (this.hreference) 
  210.   { 
  211.     doc.write("<a href='" + this.hreference + "' TARGET=\"inferior\" ") 
  212.     if (browserVersion > 0) 
  213.       doc.write("onClick='javascript:clickOnFolder("+this.id+")'") 
  214.     doc.write(">") 
  215.   } 
  216.   else 
  217.   {
  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 = "<div id='itemEstilo'>"+itemDescription+"</div>"
  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 = "imagenes/doc.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 == "hiden") 
  266.       return 
  267.     this.navObj.visibility = "hiden" 
  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 + "<img src='imagenes/lastnode.gif' width=10 height=22>") 
  278.       leftSide = leftSide + "<img src='imagenes/blank.gif'>"  
  279.     } 
  280.     else 
  281.     { 
  282.       this.renderOb(leftSide + "<img src='imagenes/node.gif' width=10 height=22>") 
  283.       leftSide = leftSide + "<img src='imagenes/vertline.gif'>" 
  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 nowrap>") 
  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.   var clickedFolder = 0 
  364.   var state = 0 
  365.  
  366.   clickedFolder = indexOfEntries[folderId] 
  367.   state = clickedFolder.isOpen 
  368.  
  369.   clickedFolder.setState(!state) //open<->close  
  370.  
  371. function initializeDocument() 
  372.   if (doc.all) 
  373.     browserVersion = 1 //IE4   
  374.   else 
  375.     if (doc.layers) 
  376.       browserVersion = 2 //NS4 
  377.     else 
  378.       browserVersion = 0 //other 
  379.  
  380.   cabecera.initialize(0, 1, "") 
  381.   cabecera.display()
  382.  
  383.     doc.write("<layer top="+indexOfEntries[nEntries-1].navObj.top+"> </layer>") 
  384.  
  385.   if (browserVersion > 0) 
  386.   { 
  387.     doc.write("<layer top="+indexOfEntries[nEntries-1].navObj.top+"> </layer>")  
  388.     // close the whole tree 
  389.     clickOnNode(0) 
  390.     clickOnNode(0) 
  391.   } 
  392.  
  393.  
  394. // Auxiliary Functions for Folder-Treee backward compatibility 
  395. // ********************************************************* 
  396.  
  397. function gFld(description, hreference) 
  398.   folder = new Folder(description, hreference) 
  399.   return folder 
  400.  
  401. function gLnk(target, description, linkData) 
  402.   fullLink = "" 
  403.  
  404.   if (target==0) 
  405.   { 
  406.     fullLink = "'"+linkData+"' target=\"inferior\"" 
  407.   } 
  408.   else 
  409.   { 
  410.     if (target==1) 
  411.        fullLink = "'http://"+linkData+"' target=_blank" 
  412.     else 
  413.        fullLink = "'http://"+linkData+"' target=\"inferior\"" 
  414.   } 
  415.  
  416.   linkItem = new Item(description, fullLink)   
  417.   return linkItem 
  418.  
  419. function insFld(parentFolder, childFolder) 
  420.   return parentFolder.addChild(childFolder) 
  421.  
  422. function insDoc(parentFolder, document) 
  423.   parentFolder.addChild(document) 
  424.  
  425. // Global variables 
  426. // **************** 
  427.  
  428. USETEXTLINKS = 1 
  429. indexOfEntries = new Array 
  430. nEntries = 0 
  431. doc = document 
  432. browserVersion = 0 
  433. selectedFolder=0
  434.