[OpenLayers-Commits] r4756 - in sandbox/tschaub/wfsv: examples lib/OpenLayers/Control tests/Control

commits at openlayers.org commits at openlayers.org
Tue Oct 2 13:59:53 EDT 2007


Author: tcoulter
Date: 2007-10-02 13:59:52 -0400 (Tue, 02 Oct 2007)
New Revision: 4756

Modified:
   sandbox/tschaub/wfsv/examples/undo-redo-control.htm
   sandbox/tschaub/wfsv/lib/OpenLayers/Control/UndoRedo.js
   sandbox/tschaub/wfsv/tests/Control/test_UndoRedo.html
Log:
Finally have a working version of the Undo/Redo control. Example updated to get rid of odd behavior (more can be done on this), and all tests are passing.

Modified: sandbox/tschaub/wfsv/examples/undo-redo-control.htm
===================================================================
--- sandbox/tschaub/wfsv/examples/undo-redo-control.htm	2007-10-02 17:24:45 UTC (rev 4755)
+++ sandbox/tschaub/wfsv/examples/undo-redo-control.htm	2007-10-02 17:59:52 UTC (rev 4756)
@@ -58,17 +58,22 @@
 			
 			undoControl.onUndo = function(feature) {
 				drawControls.select.resetVertices();
+//				if (OpenLayers.Util.indexOf(polygonLayer.features, feature) != -1) {
+//					drawControls.select.selectControl.select(feature);
+//					//drawControls.select.selectFeature(feature);
+//				}
 			};
 			
 			undoControl.onRedo = function(feature) {
                 drawControls.select.resetVertices();
+//				if (OpenLayers.Util.indexOf(polygonLayer.features, feature) != -1) drawControls.select.selectFeature(feature);
             };
 			
 			undoControl.onRemoveFeature = function(feature) {
 				
-//				if (drawControls.select.deactivate()) {
-//					drawControls.select.activate();
-//				}
+				if (drawControls.select.deactivate()) {
+					drawControls.select.activate();
+				}
 				//drawControls.select.selectFeature(feature);
 			};
 				

Modified: sandbox/tschaub/wfsv/lib/OpenLayers/Control/UndoRedo.js
===================================================================
--- sandbox/tschaub/wfsv/lib/OpenLayers/Control/UndoRedo.js	2007-10-02 17:24:45 UTC (rev 4755)
+++ sandbox/tschaub/wfsv/lib/OpenLayers/Control/UndoRedo.js	2007-10-02 17:59:52 UTC (rev 4756)
@@ -158,6 +158,12 @@
 		}
 		else
 		{
+			// If we're here, we're removing a layer; if nextGeometry == null (there's nothing new to draw),
+			// we don't plan on re-adding it. Let's let everyone else know that we're removing it for good.
+			if (nextGeometry == null) {
+				this.onRemoveFeature(feature);
+			}
+			
 			// So, we're either undoing or redoing a feature to a previous state.
 			// Remove the feature from the layer.
 		    feature.layer.removeFeatures(feature);
@@ -167,11 +173,7 @@
 			if (nextGeometry != null) {             
 	            feature.geometry = nextGeometry.clone();
 	            feature.layer.addFeatures(feature);
-	        } else {
-				// If we're here, we're removing a layer (in fact, we already removed it,
-				// and we don't plan on re-adding it). Let's let everyone else know. 
-				this.onRemoveFeature(feature);
-			}
+	        }
 		}
 		
 		return feature;

Modified: sandbox/tschaub/wfsv/tests/Control/test_UndoRedo.html
===================================================================
--- sandbox/tschaub/wfsv/tests/Control/test_UndoRedo.html	2007-10-02 17:24:45 UTC (rev 4755)
+++ sandbox/tschaub/wfsv/tests/Control/test_UndoRedo.html	2007-10-02 17:59:52 UTC (rev 4756)
@@ -10,6 +10,9 @@
     // reducing code duplication, and 2) be able to call test functions from other
     // test functions to extend new tests with already tested units of code. 
     
+	// NOTE: Because test results are in HTML, all spaces in the debug output get truncated (visually). 
+	// To have prettery (and easier to read) debug output, uncomment the line in the this.log() function below.
+	
     function TestUndoRedoControl() {
         
         // Setup function like those present in other xUnit frameworks.
@@ -81,7 +84,7 @@
             this.assert_undo_stack_length(t, 0, 
 			     "The undo should have undone the addition, since all registrations after the addition were equal to what was already on the layer.");
        
-	        this.log("Stacks:");
+	        this.logStacks(t, "Stacks:");
 	    }
 		
 		this.test_redo_after_removing_feature_with_undo = function(t) {
@@ -101,8 +104,6 @@
 			//  - Point(1,1)
 			//  - Point(2,2)
 			
-			//alert("adsfasd");
-			
 			this.assert_undo_stack_length(t, 0, "Nothing should be on the undo stack.");
 			this.assert_redo_stack_length(t, 3); 
 			
@@ -126,11 +127,12 @@
             this.do_and_assert_addition(t, feature);
             this.do_and_assert_modification(t, feature);
             this.do_and_assert_modification(t, feature);
+			
+			// Save the current state for later. Remember, the next state registered will be ignored.
+            var currentState = feature.geometry.clone();
+			
             this.do_and_assert_modification(t, feature); // This is the latest. Will be ignored by undo.
-            
-            // Save the current state for later.
-            var currentState = feature.geometry.clone();
-            
+
             this.do_and_assert_undo(t, feature);
             
             this.assert_undo_stack_length(t, 2);
@@ -141,11 +143,9 @@
             // undo.
             this.do_and_assert_modification(t, feature); 
             
-            this.assert_undo_stack_length(t, 3);
+            this.assert_undo_stack_length(t, 4, "The last modification should have pushed one state from the redo stack onto the undo stack.");
             this.assert_redo_stack_length(t, 0, "Redo stack should have been cleared because a new modification was made."); 
 			
-			OpenLayers.Console.log(feature);
-			
 			this.do_and_assert_undo(t, feature);
 			
 			t.ok(feature.geometry.equals(currentState), "State lost in undoing; undo reverted back to the wrong state.")
@@ -170,13 +170,20 @@
 	        return string;
 		}
 		
-		this.log = function(message, feature) {
-			OpenLayers.Console.log("   " + message);
-            OpenLayers.Console.log(this.stringifyStack(this.control.undoStack));
-            OpenLayers.Console.log(this.stringifyStack(this.control.redoStack));
-			//OpenLayers.Console.log("ON LAYER: " + feature.geometry + " (" + feature.geometry.id + ")" );
+		this.logStacks = function(t, message, feature) {
+			this.log(t, "   " + message);
+            this.log(t, this.stringifyStack(this.control.undoStack));
+            this.log(t, this.stringifyStack(this.control.redoStack));
+			//this.log(t, "ON LAYER: " + feature.geometry + " (" + feature.geometry.id + ")" );
 		}
 		
+		// A function encapsulating the logging functionality. This makes it easy to switch between
+		// console logging and Test.AnotherWay debug_print() logging.
+		this.log = function(t, message) {
+			// OpenLayers.Console.log(message);
+			t.debug_print(message);
+		}
+		
         // Custom assertions.
         
         this.assert_undo_stack_length = function(t, expectedLength, message) {
@@ -211,14 +218,23 @@
             afterY = feature.geometry.y;
             
             undoStackLength = this.control.undoStack.length;
+			redoStackLength = this.control.redoStack.length;
         
             this.control.registerState(feature, cloned_geometry);
         
-            this.assert_undo_stack_length(t, undoStackLength + 1);
+			if (redoStackLength == 0) {
+				var amountExpectedOnUndoStack = undoStackLength + 1;
+			} else {
+				// If there's anything on the redo stack, the above registration should have taken one state
+				// from the redo stack and pushed it onto the undo stack. This is in addition to new registration.
+				var amountExpectedOnUndoStack = undoStackLength + 2;
+			}
+		 
+            this.assert_undo_stack_length(t,amountExpectedOnUndoStack);
 			
-			OpenLayers.Console.log("MODIFYING: (" + beforeX + "," + beforeY + ") -> (" + afterX + "," + afterY + ")");
+			this.log(t, "MODIFYING: (" + beforeX + "," + beforeY + ") -> (" + afterX + "," + afterY + ")");
         
-		    this.log("After modification:", feature);
+		    this.logStacks(t, "After modification:", feature);
 		}
         
         this.do_and_assert_undo = function(t, feature_to_be_undone) {
@@ -230,12 +246,12 @@
             t.ok(top.feature.id == feature_to_be_undone.id, 
                 "Unexpected feature next to be undone: " + feature_to_be_undone.id)
             
-			OpenLayers.Console.log("UNDOING ----------");
-            this.log("Before:", feature_to_be_undone);
+			this.log(t, "UNDOING ----------");
+            this.logStacks(t, "Before:", feature_to_be_undone);
 			
             this.control.undo();
 			
-			this.log("After:", feature_to_be_undone);
+			this.logStacks(t, "After:", feature_to_be_undone);
         }
 		
 		this.do_and_assert_redo = function(t, feature_to_be_redone) {
@@ -247,12 +263,12 @@
             t.ok(top.feature.id == feature_to_be_redone.id, 
                 "Unexpected feature next to be redone: " + feature_to_be_redone.id)
             
-			OpenLayers.Console.log("REDOING ----------");
-            this.log("Before:", feature_to_be_redone);
+			this.log(t, "REDOING ----------");
+            this.logStacks(t, "Before:", feature_to_be_redone);
 
             this.control.redo();
 			
-			this.log("After:", feature_to_be_redone);
+			this.logStacks(t, "After:", feature_to_be_redone);
         }
         
         this.assert_feature_not_on_map = function(t, feature) {
@@ -271,7 +287,7 @@
         test_class = new TestUndoRedoControl();
         test_class.setup();
         func = test_class[function_name];
-        OpenLayers.Console.log("Calling: " + function_name);
+        test_class.log(t, "Calling: " + function_name);
         func.apply(test_class, [t]);
     }
     



More information about the Commits mailing list