This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
UndoRedo.js
211 lines (180 loc) · 5.51 KB
/
UndoRedo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* @copyright 2011 geOps
* @license https://github.com/geops/ole/blob/master/license.txt
* @link https://github.com/geops/ole
*/
/**
*
* Class: OpenLayers.Editor.Control.UndoRedo
*
* Inherits From:
* - <OpenLayers.Control>
*/
OpenLayers.Editor.Control.UndoRedo = OpenLayers.Class(OpenLayers.Control, {
/**
* Property: layer
* {<OpenLayers.Layer.Vector>}
*/
layer: null,
/**
* Property: handler
* {<OpenLayers.Handler.Keyboard>}
*/
handler: null,
/**
* APIProperty: autoActivate
* {Boolean} Activate the control when it is added to a map. Default is
* true.
*/
autoActivate: true,
/**
* Constant: KEY_Z
* {int}
*/
KEY_Z: 90,
/**
* Constant: KEY_Y
* {int}
*/
KEY_Y: 89,
/**
* APIMethod: onUndo
*
* Called after a successful undo, passing in the feature that was altered.
*/
onUndo: function(){},
/**
* APIMethod: onRedo
*
* Called after a successful redo, passing in the feature that was altered.
*/
onRedo: function(){},
/**
* APIMethod: onRemoveFeature
*
* Called when the Undo/Redo control is about to remove a feature from the layer. This call happens before the feature is removed.
*/
onRemoveFeature: function(){},
/**
* Property: undoStack
* {<Array>}
*
* A stack containing states of a feature that can be undone. Objects on this stack are hashes, of the form {feature: ..., :geometry ...}.
*/
undoStack: null,
/**
* Property: redoStack
* {<Array>}
*
* A stack containing states of a feature that can be redone. Objects on this stack are hashes, of the form {feature: ..., :geometry ...}.
*/
redoStack: null,
/**
* Property: currentState
*/
currentState: null,
/**
* Constructor: OpenLayers.Control.UndoRedo
* Create a new Undo/Redo control.
*
* Parameters:
* layer - {<OpenLayers.Layer.Vector>} The layer from which selected
* features will be deleted.
* options - {Object} An optional object whose properties will be used
* to extend the control.
*/
initialize: function(layer, options) {
this.layer = layer;
OpenLayers.Control.prototype.initialize.apply(this, [options]);
this.layer.events.register('featureadded', this, this.register);
this.layer.events.register('afterfeaturemodified', this, this.register);
this.undoStack = new Array();
this.redoStack = new Array();
},
/**
* Method: draw
* Activates the control.
*/
draw: function() {
this.handler = new OpenLayers.Handler.Keyboard( this, {
"keydown": this.handleKeydown} );
},
/**
* Method: handleKeydown
* Called by the feature handler on keydown.
*
* Parameters:
* {Integer} Key code corresponding to the keypress event.
*/
handleKeydown: function(e) {
if (e.keyCode === this.KEY_Z && e.ctrlKey === true && e.shiftKey === false) {
this.undo();
}
else if (e.ctrlKey === true && ((e.keyCode === this.KEY_Y) || (e.keyCode === this.KEY_Z && e.shiftKey === true))) {
this.redo();
}
},
/**
* APIMethod: undo
* Causes an the Undo/Redo control to process an undo.
*/
undo: function() {
var feature = this.moveBetweenStacks(this.undoStack, this.redoStack, true);
if (feature) this.onUndo(feature);
},
/**
* APIMethod: redo
* Causes an the Undo/Redo control to process an undo.
*/
redo: function() {
var feature = this.moveBetweenStacks(this.redoStack, this.undoStack, false);
if (feature) this.onRedo(feature);
},
/**
* Method: moveBetweenStacks
* The "meat" of the Undo/Redo control -- it actually does the undoing/redoing. Although some idiosyncrasies exist, this function
* handles moving states from the undo stack to the redo stack, and vice versa. It also handles adding and removing features from the map.
*
* Parameters: TODO
*/
moveBetweenStacks: function(fromStack, toStack, undo) {
if (fromStack.length > 0) {
this.map.editor.editLayer.removeAllFeatures();
var state = fromStack.pop();
toStack.push(this.currentState);
if (state) {
var currentFeatures = new Array(len);
var len = state.length;
for(var i=0; i<len; ++i) {
currentFeatures[i] = state[i].clone();
}
this.currentState = currentFeatures;
this.map.editor.editLayer.addFeatures(state, {silent: true});
} else {
this.currentState = null;
}
}
else if (this.currentState && undo) {
toStack.push(this.currentState);
this.map.editor.editLayer.removeAllFeatures();
this.currentState = null;
}
},
/**
*
*/
register: function() {
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.currentState) {
this.undoStack.push(this.currentState);
}
this.currentState = clonedFeatures;
this.redoStack = new Array();
},
CLASS_NAME: "OpenLayers.Editor.Control.UndoRedo"
});