Skip to content

Commit

Permalink
implement undo/redo control
Browse files Browse the repository at this point in the history
  • Loading branch information
friedjoff committed Jan 7, 2011
1 parent 65ab284 commit 8bb20b4
Showing 1 changed file with 33 additions and 49 deletions.
82 changes: 33 additions & 49 deletions lib/Editor/Control/UndoRedo.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ OpenLayers.Editor.Control.UndoRedo = OpenLayers.Class(OpenLayers.Control, {
*/
redoStack: null,

currentState: null,

/**
* Constructor: OpenLayers.Control.UndoRedo
* Create a new Undo/Redo control. Does not take any parameters.
Expand Down Expand Up @@ -124,7 +126,7 @@ OpenLayers.Editor.Control.UndoRedo = OpenLayers.Class(OpenLayers.Control, {
* Causes an the Undo/Redo control to process an undo.
*/
undo: function() {
var feature = this.moveBetweenStacks(this.undoStack, this.redoStack);
var feature = this.moveBetweenStacks(this.undoStack, this.redoStack, true);
if (feature) this.onUndo(feature);
},

Expand All @@ -133,7 +135,7 @@ OpenLayers.Editor.Control.UndoRedo = OpenLayers.Class(OpenLayers.Control, {
* Causes an the Undo/Redo control to process an undo.
*/
redo: function() {
var feature = this.moveBetweenStacks(this.redoStack, this.undoStack);
var feature = this.moveBetweenStacks(this.redoStack, this.undoStack, false);
if (feature) this.onRedo(feature);
},

Expand All @@ -144,66 +146,48 @@ OpenLayers.Editor.Control.UndoRedo = OpenLayers.Class(OpenLayers.Control, {
*
* Parameters: TODO
*/
moveBetweenStacks: function(fromStack, toStack) {
moveBetweenStacks: function(fromStack, toStack, undo) {

if (fromStack.length > 0) {

this.map.editor.editLayer.removeAllFeatures();
var state = fromStack.pop();
toStack.push(state);

var layer = this.map.getLayersBy('id', state.layerId)[0];
var feature = layer.getFeatureById(state.id);

// If the feature is not on the layer we must be redoing a previous addition.
// Add the feature on the map.
if (feature == null) {
console.log("redo previous addition");
feature = new OpenLayers.Feature.Vector(state.geometry);
feature.id = state.id;
layer.addFeatures(feature);
}
toStack.push(this.currentState);

// So, we're either undoing or redoing a feature to a previous state.
// Remove the feature from the layer.
else {
console.log("goto previous state");
layer.removeFeatures(feature);
if (fromStack.length > 0) {
var prevState = fromStack[fromStack.length - 1];
if (prevState.id == state.id) {
feature.geometry = prevState.geometry;
feature.id = prevState.id;
layer.drawFeature(feature);
return state;
}
if (state) {
var currentFeatures = new Array(len);
var len = state.length;
for(var i=0; i<len; ++i) {
currentFeatures[i] = state[i].clone();
}
layer.removeFeatures(feature);
this.currentState = currentFeatures;
this.map.editor.editLayer.addFeatures(state, {silent: true});
} else {
this.currentState = null;
}
return state;
}
else if (this.currentState && undo) {
toStack.push(this.currentState);
this.map.editor.editLayer.removeAllFeatures();
this.currentState = null;
}
},

register: function(event) {
register: function() {

if (this.undoStack.length > 0) {
var prev = this.undoStack[this.undoStack.length - 1];
if (prev.geometry.equals(event.feature.geometry) && prev.id === event.feature.id) {
// console.log("already registered: "+prev.geometry.equals(event.feature.geometry));
return;
}
var features = this.map.editor.editLayer.features;
var len = features.length;
var clonedFeatures = new Array(len);
for(var i=0; i<len; ++i) {
clonedFeatures[i] = features[i].clone();
}

// if (this.undoStack[this.undoStack.length - 1].geometry != event.feature.geometry) {
// console.log("registered");
// console.log(event.feature);
this.redoStack = new Array();
this.undoStack.push({
id: event.feature.id,
layerId: event.feature.layer.id,
geometry: event.feature.geometry.clone()
});
// }

if (this.currentState) {
this.undoStack.push(this.currentState);
}

this.currentState = clonedFeatures;
this.redoStack = new Array();

},

Expand Down

0 comments on commit 8bb20b4

Please sign in to comment.