[Zope-CVS] CVS: ZopeProducts/RotterdamSprint - xmltree.js:1.2

Paul Everitt paul@zope.com
Wed, 4 Dec 2002 09:36:25 -0500


Update of /cvs-repository/ZopeProducts/RotterdamSprint
In directory cvs.zope.org:/tmp/cvs-serv10075

Modified Files:
	xmltree.js 
Log Message:
Recursive loading works


=== ZopeProducts/RotterdamSprint/xmltree.js 1.1 => 1.2 ===
--- ZopeProducts/RotterdamSprint/xmltree.js:1.1	Wed Dec  4 05:23:14 2002
+++ ZopeProducts/RotterdamSprint/xmltree.js	Wed Dec  4 09:36:24 2002
@@ -1,8 +1,23 @@
 
 
-var ELEMENT_NODE = 1;
-var TEXT_NODE = 3;
-var folderdata_url = 'children.xml';
+const ELEMENT_NODE = 1;
+const TEXT_NODE = 3;
+const COLLECTION = 'COLLECTION';
+const FOLDERDATA_URL = 'children.xml';
+var baseurl;
+
+function prettydump(s) {
+  // Put the string "s" in a box on the screen as an log message
+  var logger = document.getElementById('logger');
+  var msg = document.createElement('code');
+  var br1 = document.createElement('br');
+  var br2 = document.createElement('br');
+  var msg_text = document.createTextNode(s);
+  msg.appendChild(msg_text);
+  logger.insertBefore(br1,logger.firstChild);
+  logger.insertBefore(br2,logger.firstChild);
+  logger.insertBefore(msg,logger.firstChild);
+}
 
 function togglenode (n) {
 
@@ -18,9 +33,9 @@
 
 function togglecursor (e) {
   if(e.target)
-    elem=e.target;
+    var elem=e.target;
   else
-    elem=e.srcElement;
+    var elem=e.srcElement;
   if(elem.nodeType == TEXT_NODE)
   {elem.parentNode.style.cursor='pointer';
   /* elem.parentNode.style.textDecoration='underline';*/}
@@ -32,25 +47,43 @@
 function togglenodes (e) {
   if(e.target) {
     // Mozilla uses this
-    elem=e.target.parentNode;
+    var elem=e.target.parentNode;
   }
   else {
     // IE uses this
-    elem=e.srcElement;
+    var elem=e.srcElement;
   }
+  if (elem.tagName.toUpperCase() != COLLECTION) return;
 //  alert(elem.tagName);
-  zid=elem.id;
-  if(zid == 'navtree')
+  if(elem.id == 'navtree')
   {return}
 //  if(elem.nodeType != TEXT_NODE)
 //  {return}
-  collections = elem.getElementsByTagName('collection');
-  num = collections.length;
+
+  // If this collection is empty and should have data, go get it
+  // todo xxx optimize for the case when collection has no length
+   if (elem.getAttribute('isEmpty') == 1) {
+
+     var name = elem.getAttribute('name');
+     var thisbaseurl = baseurl + name + '/';
+     var data = loadtreexml(thisbaseurl);
+
+     createAndAddNodes(data,elem);
+     elem.setAttribute('isEmpty',0);
+
+     // If we don't return here, then the following will immediately 
+     // hide the new data
+     return;
+   }
+
+  // Grab child elements and hide/show them
+  var collections = elem.getElementsByTagName('collection');
+  var num = collections.length;
   for (var i = num - 1; i >=0; i--) {
     togglenode(collections[i]);
   }
-  items = elem.getElementsByTagName('item');
-  num = items.length;
+  var items = elem.getElementsByTagName('item');
+  var num = items.length;
   for (var i = num - 1; i >=0; i--) {
     togglenode(items[i]);
  
@@ -103,15 +136,41 @@
       }
 };
 
-function loadtree () {
+function loadtreexml (baseurl) {
   var xmlHttp = XmlHttp.create(); 
-  xmlHttp.open('GET',folderdata_url,false); 
+  //alert('url'+baseurl + FOLDERDATA_URL);
+
+  xmlHttp.open('GET',baseurl + FOLDERDATA_URL,false); 
   xmlHttp.send(null);
+  prettydump(xmlHttp.responseText);
   var data = xmlHttp.responseXML.documentElement;
+  return data;
+}  
+
+function loadtree (thisbaseurl) {
+  baseurl = thisbaseurl;  // Global baseurl
+  var data = loadtreexml(thisbaseurl);
   var navtree = document.getElementById('navtreecontents');
-  
-  var items = data.childNodes;
+  var newelem = document.createElement('div');
+  newelem.setAttribute('name','');
+  navtree.appendChild(newelem);
+    createAndAddNodes(data,newelem);
+
+
+//  alert(document.getElementsByTagName('collection').length);
+
+}
+
+
+function createAndAddNodes(sourceNode, targetNode)
+{
+//  prettydump(targetNode.nodeType + ' and tag name ' + targetNode.tagName);
+  var items = sourceNode.childNodes;
   var numitems = items.length;
+  var basename = targetNode.getAttribute('name');
+  if (basename != '')
+     basename = basename + '/';
+  var curritem;
   for (var i=0; i< numitems; i++) {
     curritem = items[i];
 
@@ -119,32 +178,19 @@
       continue;
     }
 
-    currtext = curritem.getAttribute('title');
+    var currname = curritem.getAttribute('name');
 //    alert(currtext);
-    newtextnode = document.createTextNode(currtext);
-    newelem = document.createElement(curritem.tagName);
+    var newtextnode = document.createTextNode(currname);
+    var newelem = document.createElement(curritem.tagName);
+    newelem.setAttribute('name',basename+currname);
+    if (curritem.tagName == 'collection')
+       {
+       newelem.setAttribute('isCollapsed', 1);
+       newelem.setAttribute('isEmpty', 1);
+       
+       }
     newelem.appendChild(newtextnode);
-
-  children= curritem.childNodes;
-  numchildren = children.length;
-  for (var j = 0; j < numchildren; j++) {
-    currchild = children[j];
-
-    if (currchild.nodeType != ELEMENT_NODE) {
-      continue;
-    }
-
-    currtext = currchild.getAttribute('title');
-    newtextnode = document.createTextNode(currtext);
-    newchild = document.createElement(currchild.tagName);
-    newchild.appendChild(newtextnode);
-
-    newelem.appendChild(newchild);
-    }
-
-    navtree.appendChild(newelem);
+//   alert (targetNode.nodeType+targetNode.tagName);
+   targetNode.appendChild(newelem);
   }
-
-//  alert(document.getElementsByTagName('collection').length);
-
-}
+}
\ No newline at end of file