[Mapbender-commits] r2947 - branches/tree_dev/http/x_test

svn_mapbender at osgeo.org svn_mapbender at osgeo.org
Wed Sep 17 05:24:53 EDT 2008


Author: christoph
Date: 2008-09-17 05:24:52 -0400 (Wed, 17 Sep 2008)
New Revision: 2947

Modified:
   branches/tree_dev/http/x_test/customTreeModel.js
Log:
nested sets import

Modified: branches/tree_dev/http/x_test/customTreeModel.js
===================================================================
--- branches/tree_dev/http/x_test/customTreeModel.js	2008-09-17 09:22:20 UTC (rev 2946)
+++ branches/tree_dev/http/x_test/customTreeModel.js	2008-09-17 09:24:52 UTC (rev 2947)
@@ -131,7 +131,52 @@
 		var nodeArray = toStringNode.apply(this, [[], this.root, 1]);
 		return $.toJSON(nodeArray);
 	};
-	
+
+	/**
+	 * Create this tree from nested sets
+	 */
+	this.fromString = function(str){
+		var nodeArray = eval(str);
+
+		if(nodeArray===undefined || nodeArray.length<1)
+			return false;
+		
+		//numeric sort function
+		var nodeSort = function(a,b){
+			return a.left-b.left;
+		}
+
+		//sort nodes by their left
+		nodeArray.sort(nodeSort);
+		
+		//apply root node
+		var currentNode = this.root;
+		currentNode.applyKeys(nodeArray[0]);
+
+		var rights = [nodeArray[0].right];
+		for(var i=1;i<nodeArray.length;i++){
+			//finish all nodes that have no further childs
+			while(rights[rights.length-1]<nodeArray[i].left){
+				rights.pop();
+				currentNode = currentNode.parentNode;
+				
+				//check if there is an errror in data or we have muliple roots				
+				if(currentNode == null)
+					return false;
+			}
+
+			//insert new node
+			var newNode = new CustomTreeNode(currentNode);
+			rights.push(nodeArray[i].right);
+			newNode.applyKeys(nodeArray[i]);
+			currentNode.append(newNode);
+			
+			//new node is the one that take next childs
+			currentNode = newNode;
+		}
+		return true;
+	}
+
 	var toStringNode = function (nodeArray, node, nextLeft) {
 		var left = nextLeft;
 		var right;
@@ -185,6 +230,10 @@
 
 	this.$domNode = null;
 	
+	this.applyKeys = function(obj){
+		this.name = obj.name;
+	}
+
 	this.afterMove = function () {
 	};
 	
@@ -194,7 +243,7 @@
 	this.afterAddChild = function () {
 		
 	};
-	
+
 	this.addChild = function () {
 		this.childNodeList.add(new CustomTreeNode(this));
 		var newChild = this.childNodeList.get(-1);



More information about the Mapbender_commits mailing list