[OpenLayers-Commits] r12013 - in sandbox/tschaub/editing/lib/OpenLayers: Control Handler

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Wed May 25 17:13:42 EDT 2011


Author: tschaub
Date: 2011-05-25 14:13:41 -0700 (Wed, 25 May 2011)
New Revision: 12013

Modified:
   sandbox/tschaub/editing/lib/OpenLayers/Control/DrawFeature.js
   sandbox/tschaub/editing/lib/OpenLayers/Handler/Path.js
Log:
Add undo/redo for draw control and sketch handlers.

Modified: sandbox/tschaub/editing/lib/OpenLayers/Control/DrawFeature.js
===================================================================
--- sandbox/tschaub/editing/lib/OpenLayers/Control/DrawFeature.js	2011-05-25 20:51:53 UTC (rev 12012)
+++ sandbox/tschaub/editing/lib/OpenLayers/Control/DrawFeature.js	2011-05-25 21:13:41 UTC (rev 12013)
@@ -119,6 +119,13 @@
             this.layer.addFeatures([feature]);
             this.featureAdded(feature);
             this.events.triggerEvent("featureadded",{feature : feature});
+            if (!this.undoStack) {
+                this.undoStack = [];
+            }
+            this.undoStack.push(this.layer.features.length-1);
+            if (!this._redoing) {
+                delete this.redoStack;
+            }
         }
     },
     
@@ -179,8 +186,54 @@
             this.handler.insertDeflectionLength(deflection, length);
         }
     },
-
+    
     /**
+     * Method: undo
+     * Remove the most recently added point in the sketch geometry.
+     *
+     * Returns: 
+     * {Boolean} An edit was undone.
+     */
+    undo: function() {
+        var undone = this.handler.undo && this.handler.undo();
+        if (!undone) {
+            var index = this.undoStack && this.undoStack.pop();
+            if (index != null) {
+                var feature = this.layer.features[index];
+                this.layer.removeFeatures([feature]);
+                if (!this.redoStack) {
+                    this.redoStack = [];
+                }
+                this.redoStack.push(feature.geometry);
+                undone = true;
+            }
+        }
+        return !!undone;
+    },
+    
+    /**
+     * Method: redo
+     * Reinsert the most recently removed point resulting from an <undo> call.
+     *     The undo stack is deleted whenever a point is added by other means.
+     *
+     * Returns: 
+     * {Boolean} An edit was redone.
+     */
+    redo: function() {
+        var redone = false;
+        var geometry = this.redoStack && this.redoStack.pop();
+        if (geometry) {
+            this._redoing = true;
+            this.drawFeature(geometry);
+            delete this._redoing;
+            redone = true;
+        } else {
+            redone = this.handler.redo && this.handler.redo();
+        }
+        return !!redone;
+    },
+    
+    /**
      * APIMethod: finishSketch
      * Finishes the sketch without including the currently drawn point.
      *     This method can be called to terminate drawing programmatically

Modified: sandbox/tschaub/editing/lib/OpenLayers/Handler/Path.js
===================================================================
--- sandbox/tschaub/editing/lib/OpenLayers/Handler/Path.js	2011-05-25 20:51:53 UTC (rev 12012)
+++ sandbox/tschaub/editing/lib/OpenLayers/Handler/Path.js	2011-05-25 21:13:41 UTC (rev 12013)
@@ -68,6 +68,12 @@
     timerId: null,
 
     /**
+     * Property: redoStack
+     * {Array} Stack containing points removed with <undo>.
+     */
+    redoStack: null,
+
+    /**
      * Constructor: OpenLayers.Handler.Path
      * Create a new path hander
      *
@@ -169,6 +175,7 @@
         this.callback("point", [this.point.geometry, this.getGeometry()]);
         this.callback("modify", [this.point.geometry, this.getSketch()]);
         this.drawFeature();
+        delete this.redoStack;
     },
     
     /**
@@ -186,6 +193,7 @@
             this.getCurrentPointIndex()
         );
         this.drawFeature();
+        delete this.redoStack;
     },
 
     /**
@@ -249,7 +257,51 @@
         return this.line.geometry.components.length - 1;
     },
     
+    
     /**
+     * Method: undo
+     * Remove the most recently added point in the sketch geometry.
+     *
+     * Returns: 
+     * {Boolean} A point was removed.
+     */
+    undo: function() {
+        var undone = false;
+        var geometry = this.line.geometry;
+        var index = this.getCurrentPointIndex() - 1;
+        var target = geometry.components[index];
+        var count = geometry.components.length;
+        if (count > 1) {
+            undone = true;
+            OpenLayers.Util.removeItem(geometry.components, target);
+            geometry.clearBounds();
+            if (!this.redoStack) {
+                this.redoStack = [];
+            }
+            this.redoStack.push(target);
+            this.drawFeature();
+        }
+        return undone;
+    },
+    
+    /**
+     * Method: redo
+     * Reinsert the most recently removed point resulting from an <undo> call.
+     *     The undo stack is deleted whenever a point is added by other means.
+     *
+     * Returns: 
+     * {Boolean} A point was added.
+     */
+    redo: function() {
+        var target = this.redoStack && this.redoStack.pop();
+        if (target) {
+            this.line.geometry.addComponent(target, this.getCurrentPointIndex());
+            this.drawFeature();
+        }
+        return !!target;
+    },
+    
+    /**
      * Method: freehandMode
      * Determine whether to behave in freehand mode or not.
      *



More information about the Commits mailing list