[Zope3-checkins] CVS: ZopeProducts/RotterdamSprint - xmltree.js:1.12

Godefroid Chapelle gotcha@swing.be
Wed, 18 Dec 2002 10:29:21 -0500


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

Modified Files:
	xmltree.js 
Log Message:
first step of refactoring to use OO Javascript


=== ZopeProducts/RotterdamSprint/xmltree.js 1.11 => 1.12 ===
--- ZopeProducts/RotterdamSprint/xmltree.js:1.11	Wed Dec 18 05:48:59 2002
+++ ZopeProducts/RotterdamSprint/xmltree.js	Wed Dec 18 10:29:20 2002
@@ -1,3 +1,4 @@
+//constants
 var ELEMENT_NODE = 1;
 var TEXT_NODE = 3;
 var COLLECTION = 'COLLECTION';
@@ -7,7 +8,6 @@
 var XML_CHILDREN_VIEW = 'children.xml';
 var SINGLE_BRANCH_TREE_VIEW = 'singleBranchTree.xml';
 var CONTENT_VIEW = '@@contents.html';
-var baseurl;
 
 
 var LG_DEBUG = 6;
@@ -15,9 +15,68 @@
 var LG_TRACE = 4;
 var LG_INFO = 3;
 var LG_NOLOG = 0;
+
+
+// globals
+var baseurl;
+var navigationTree;
+
 var loglevel = LG_NOLOG;
 
 
+
+//class navigationTreeNode
+function navigationTreeNode (domNode) {
+        this.childNodes = new Array();
+        this.isEmpty = 1;
+        this.isCollapsed = 1;
+        this.domNode = domNode;
+        this.name = '';
+        this.parentNode = null;
+}
+
+navigationTreeNode.prototype.appendChild = function(node) {
+        this.childNodes.push(node);
+        this.domNode.appendChild(node.domNode);
+        node.parentNode = this;
+}
+
+navigationTreeNode.prototype.getName = function() {
+        return this.name;
+}
+
+navigationTreeNode.prototype.setName = function(name) {
+        this.name = name;
+        this.domNode.setAttribute("name", name);
+}
+
+navigationTreeNode.prototype.collapse = function() {
+ 	this.isCollapsed = 1;
+        changeExpandIcon(this.domNode, "pl.gif"); 
+}
+
+navigationTreeNode.prototype.expand = function() {
+ 	this.isCollapsed = 0;
+        changeExpandIcon(this.domNode, "mi.gif"); 
+}
+
+navigationTreeNode.prototype.getNodeByName = function(name) {
+        var numchildren = this.childNodes.length;
+        if (name == this.name) {
+                return this;
+                }
+        else {
+                for (var i=0; i< numchildren; i++) {
+                        foundChild = this.childNodes[i].getNodeByName(name);
+                        if (foundChild) {
+                                return foundChild;
+                                }
+                        }        
+                }
+        return null;
+}
+
+// utilities
 function prettydump(s, locallog) {
         // Put the string "s" in a box on the screen as an log message
         if (locallog <= loglevel) {
@@ -33,16 +92,15 @@
 	        }
         }
 
-function mouseOverTree (e) {
-        prettydump('mouseOverTree', LG_TRACE_EVENTS);
-        var elem = getTreeEventTarget(e);
-        if (elem.id == 'navtree') return;
-        if (isTitle(elem)) {
-	        elem.style.textDecoration = 'underline';
-                window.status = getTargetURL(elem.parentNode.parentNode.parentNode);
-	        }
-        }
 
+function debug(s) {
+        var oldlevel = loglevel;
+        loglevel = LG_DEBUG;
+        prettydump("Debug : " + s, LG_DEBUG);
+        loglevel = oldlevel;
+}
+
+// DOM utilities
 function getTreeEventTarget(e) {
 	var elem;
         if(e.target) {
@@ -61,17 +119,6 @@
         return elem;
         }
 
-function mouseOutTree (e) {
-        prettydump('mouseOutTree', LG_TRACE_EVENTS);
-        var elem = getTreeEventTarget(e);
-        if (elem.id == 'navtree') return;
-        if (isTitle(elem)) {
-	        elem.style.textDecoration = 'none';
-                window.status = '';
-	        }
-        }
-
-
 function isCollection(elem) {
 	return (checkTagName(elem, COLLECTION));
 	}
@@ -92,25 +139,23 @@
 	return (elem.tagName.toUpperCase() == tagName);
 	}
 
-function toggleExpansion (elem) {
+function toggleExpansion (navTreeNode) {
 	prettydump('toggleExpansion', LG_TRACE);
         // If this collection is empty, load it from server
         // todo xxx optimize for the case where collection has null length
-        if (elem.getAttribute('isEmpty') == 1) {
-                var name = elem.getAttribute('name');
-                var url = baseurl + name + XML_CHILDREN_VIEW;
+        var elem = navTreeNode.domNode;
+        if (navTreeNode.isEmpty) {
+                var url = baseurl + navTreeNode.name + XML_CHILDREN_VIEW;
                 var data = loadtreexml(url);
-                addNavigationTreeNodes(data, elem, 0);
-                elem.setAttribute('isEmpty', 0);
+                addNavigationTreeNodes(data, navTreeNode, 0);
+                navTreeNode.isEmpty = 0;
                 }
-        if (elem.getAttribute('isCollapsed') == 1) {
-	 	elem.setAttribute('isCollapsed', 0);
-                changeExpandIcon(elem, "mi.gif");
+        if (navTreeNode.isCollapsed) {
+	 	navTreeNode.expand();
                 showChildren(elem);
    		}
         else {
-	 	elem.setAttribute('isCollapsed', 1);
-                changeExpandIcon(elem, "pl.gif");
+                navTreeNode.collapse();
                 hideChildren(elem);
    		}
         } 
@@ -128,12 +173,13 @@
         prettydump('showChildren', LG_TRACE);
         var collections = elem.getElementsByTagName('collection');
         var num = collections.length;
-	for (var i = num - 1; i >=0; i--) {
-                var parentNode = getParentCollection(collections[i]);
-                if (parentNode.getAttribute('isCollapsed') == 0) {
-                	collections[i].style.display = 'block';
+        for (var i = num - 1; i >=0; i--) {
+                var parentColl = getParentCollection(collections[i]);
+                var parentNavTreeNode = navigationTree.getNodeByName(parentColl.getAttribute('name'));
+                if (! (parentNavTreeNode.isCollapsed)) {
+                        collections[i].style.display = 'block';
                         }
-        	}
+                }
         }
 
 function getParentCollection(elem) {
@@ -150,18 +196,31 @@
         expand.style.backgroundImage = 'url("' + baseurl + '@@/' + icon + '")';
         }
 
+//events
+function mouseOverTree (e) {
+        prettydump('mouseOverTree', LG_TRACE_EVENTS);
+        var elem = getTreeEventTarget(e);
+        if (elem.id == 'navtree') return;
+        if (isTitle(elem)) {
+	        elem.style.textDecoration = 'underline';
+                var collectionElem = elem.parentNode.parentNode.parentNode;
+                window.status = getTargetURL(collectionElem);
+	        }
+        }
 
-function getTargetURL(elem) {
-        var location_href = baseurl;
-	location_href = location_href + elem.getAttribute('name');
-	location_href = location_href + CONTENT_VIEW;
-        return location_href;
+function mouseOutTree (e) {
+        prettydump('mouseOutTree', LG_TRACE_EVENTS);
+        var elem = getTreeEventTarget(e);
+        if (elem.id == 'navtree') return;
+        if (isTitle(elem)) {
+	        elem.style.textDecoration = 'none';
+                window.status = '';
+	        }
         }
 
 function treeclicked (e) {
         prettydump('treeclicked', LG_TRACE_EVENTS);
         var elem = getTreeEventTarget(e);
-        prettydump(elem.tagName, LG_DEBUG);
         if (elem.id == 'navtree') return;
         // if node clicked is title elem, change page
         if (isTitle(elem)) {
@@ -172,10 +231,21 @@
         if (isExpand(elem)) {
                 //get collection node
                 elem = elem.parentNode;
-                toggleExpansion(elem);
+                var navTreeNode = navigationTree.getNodeByName(elem.getAttribute('name'));
+                toggleExpansion(navTreeNode);
                 }
         }
 
+// helpers
+
+function getTargetURL(elem) {
+        var location_href = baseurl;
+	location_href = location_href + elem.getAttribute('name');
+	location_href = location_href + CONTENT_VIEW;
+        return location_href;
+        }
+
+
 function getControlPrefix() {
         if (getControlPrefix.prefix)
                 return getControlPrefix.prefix;
@@ -232,7 +302,6 @@
 function loadtreexml (url) {
         var xmlHttp = XmlHttp.create(); 
         if (xmlHttp) {
-                prettydump(url, LG_DEBUG);
                 xmlHttp.open('GET', url, false); 
                 xmlHttp.send(null);
                 prettydump('Response XML ' + xmlHttp.responseText, LG_INFO);
@@ -247,29 +316,30 @@
         baseurl = rooturl;  // Global baseurl
   
 	var url = thisbaseurl + SINGLE_BRANCH_TREE_VIEW;
-	prettydump("load tree " + url, LG_DEBUG);
         var data = loadtreexml(url);
         if (data) {
-                var navtree = document.getElementById('navtreecontents');
-                var newelem = document.createElement('div');
-                newelem.setAttribute('name', '');
-                navtree.appendChild(newelem);
-                addNavigationTreeNodes(data, newelem, 1);
+                var docNavTree = document.getElementById('navtreecontents');
+                addNavigationTreeNodes(data, null, 1);
+                docNavTree.appendChild(navigationTree.domNode);
                 }
         }
 
 
-function addNavigationTreeNodes(sourceNode, targetNode, deep) {
+function addNavigationTreeNodes(sourceNode, targetNavTreeNode, deep) {
         // create tree nodes from XML children nodes of sourceNode         
         // and add them to targetNode
         // if deep, create all descendants of sourceNode
-        var basename = targetNode.getAttribute('name');
+        var basename = "";
+        if (targetNavTreeNode) {
+                basename = targetNavTreeNode.name;
+                }
         var items = getCollectionChildNodes(sourceNode);
         var numitems = items.length;
         for (var i=0; i< numitems; i++) {
-                var newelem = createNavigationTreeNode(items[i], basename, deep);
-                prettydump("add " + newelem.tagName, LG_DEBUG);
-                targetNode.appendChild(newelem);
+                var navTreeChild = createNavigationTreeNode(items[i], basename, deep);
+                if (targetNavTreeNode) {
+                        targetNavTreeNode.appendChild(navTreeChild);
+                        }
                 }
         }       
 
@@ -318,6 +388,7 @@
 function createNavigationTreeNode(source, basename, deep) {
         var newelem = document.createElement(source.tagName);
 
+        var navTreeNode = new navigationTreeNode(newelem);
         var elemName;
         var elemTitle;
         //XXX should not hardcode root folder name string
@@ -325,12 +396,13 @@
                 elemTitle = '[top]';
                 elemName = basename;
                 newelem.style.marginLeft = '0px';
+                navigationTree = navTreeNode;
                 }
         else {
                 elemTitle = source.getAttribute('name');
                 elemName = basename + elemTitle + '/';
                 }
-        newelem.setAttribute('name', elemName);
+        navTreeNode.setName(elemName);
         
         //could show number of child items
         //var length = source.getAttribute('length');
@@ -347,26 +419,24 @@
                 var children = getCollectionChildNodes(source);
                 var numchildren = children.length;
                 for (var i=0; i< numchildren; i++) {
-                        var newchild = createNavigationTreeNode(children[i], elemName, deep);
-                        prettydump("add " + newelem.tagName, LG_DEBUG);
+                        var navTreeNodeChild =  createNavigationTreeNode(children[i], navTreeNode.name, deep); 
+                        var newchild = navTreeNodeChild.domNode;
                         newelem.appendChild(newchild);
+                        navTreeNode.appendChild(navTreeNodeChild);
                         }
                 if (numchildren) {
-	                newelem.setAttribute('isCollapsed', 0);
-	                newelem.setAttribute('isEmpty', 0);
-                        changeExpandIcon(newelem, "mi.gif");
+                        navTreeNode.isEmpty = 0;
+                        navTreeNode.expand();
                         }
                 else {
-	                newelem.setAttribute('isCollapsed', 1);
-	                newelem.setAttribute('isEmpty', 1);
-                        changeExpandIcon(newelem, "pl.gif");
+                        navTreeNode.isEmpty = 1;
+                        navTreeNode.collapse();
                         }
                 }
         else {
-                newelem.setAttribute('isCollapsed', 1);
-                newelem.setAttribute('isEmpty', 1);
-                changeExpandIcon(newelem, "pl.gif");
+                navTreeNode.isEmpty = 1;
+                navTreeNode.collapse();
                 }
-        return newelem;
+        return navTreeNode;
         }