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