-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUndoRedo.js
252 lines (239 loc) · 7.43 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import {Control} from 'ol/control/Control';
/**
* Class: UndoRedo
* Instance of this class can be used to undo and redo vector edits.
*/
UndoRedo = ol.Contorl(Contorl,{
/**
* APIProperty: currentEditIndex
* {integer} - sequence number for editing the feature[s]
*/
currentEditIndex: 0,
/**
* Property: undoFeatures
* {array} - stack of the edit features
*/
undoFeatures: [],
/**
* Property: redoFeatures
* {array} - stack of the undo features
*/
redoFeatures: [],
/**
* Property: isEditMulty
* {boolean} - true if in one action multiple features are editied
*/
isEditMulty: false,
/**
* Constructor: UndoRedo
* Parameters:
* layers - array of {<OpenLayers.Layers.Vector>}
*/
initialize: function(layers){
if (!(layers instanceof Array)) {
layers = [layers];
}
for(var i=0;i<layers.length; i++){
layers[i].events.register("featureadded",this,this.onInsert);
layers[i].events.register("beforefeatureremoved",this,this.onDelete);
layers[i].events.register("beforefeaturemodified",this,this.onUpdate);
}
},
/**
* Method: onEdit
* on any edit operation performed this has to be triggred
* i.e. on insert, delete, update
* Parameters:
* feature - {<OpenLayers.Feature.Vector>}
* editType - {string} edit type done "Insert","Delete","Update"
* component - {string} layer or any other identifier
* Returns:
*/
onEdit: function(feature,editType,component){
//console.log("Updating undo stack as there is - "+editType);
if (component == undefined){
component = feature.layer.name;
}
if (this.undoFeatures[this.currentEditIndex] == undefined) {
this.undoFeatures[this.currentEditIndex] = {};
this.undoFeatures[this.currentEditIndex][component] = {"Insert":[],"Update":[],"Delete":[]};
}
if(feature.fid == undefined){
feature.fid = feature.id;
}
this.undoFeatures[this.currentEditIndex][component][editType].push(feature);
this.redoFeatures.splice(0,this.redoFeatures.length);
//run increase editIndex outside after this in case of multy feature edit
if(!this.isEditMulty){
this.setEditIndex();
}
},
/**
* Method: onInsert
* event haldler for featureadded
*/
onInsert: function(event){
//onEdit(event.feature,"Insert",event.feature.layer.name);
feature = event.feature.clone();
if(event.feature.fid == undefined) {
event.feature.fid = event.feature.id;
}
feature.fid = event.feature.fid;
feature.state = event.feature.state;
feature.layer = event.feature.layer;
this.onEdit(feature,"Insert",feature.layer.name);
},
/**
* Method: onDelete
* event haldler for beforefeatureremoved
*/
onDelete: function(event){
this.onEdit(event.feature,"Delete",event.feature.layer.name);
},
/**
* Method: onUpdate
* event haldler for beforefeaturemodified
*/
onUpdate: function(event){
//console.log("old feature geometry: " + event.feature.geometry);
feature = event.feature.clone();
feature.fid = event.feature.fid;
feature.state = event.feature.state;
feature.layer = event.feature.layer;
this.onEdit(feature,"Update",feature.layer.name);
},
/**
* Method: setEditIndex
* increase the editIndex
*/
setEditIndex: function(delta){
delta = delta ? delta : 1;
this.currentEditIndex += delta;
},
/**
* Method: getUndoData
* returns the last edited data
*/
getUndoData: function(){
var data = this.undoFeatures.pop();
this.currentEditIndex -= 1;
return data;
},
/* Method: getRedoData
* returns the last redo data
*/
getRedoData: function() {
var data = this.redoFeatures.pop();
this.currentEditIndex += 1;
return data;
},
/**
* APIMethod: reseteditIndex
* reset the editIndex to 0 and empety both undo and redo stack
*/
resetEditIndex: function(){
this.currentEditIndex = 0;
this.undoFeatures.splice(0,this.undoFeatures.length);
this.redoFeatures.splice(0,this.redoFeatures.length);
},
/**
* APIMethod: undo
* perform undo operation
*/
undo: function() {
data = this.getUndoData();
//console.log("data :" + data);
for (component in data){
//console.log("component: "+component);
layer=map.getLayersByName(component)[0];
//console.log("got layer, name: " + layer.name);
for (editType in data[component]){
//console.log("edittype : "+editType);
for(var i=0;i<data[component][editType].length;i++){
feature=data[component][editType][i];
//console.log("features before undo: "+layer.features.length);
switch(editType) {
case "Insert":
//console.log("insert");
//layer.drawFeature(feature,{display : "none"});
insertedFeature = layer.getFeatureByFid(feature.fid);
layer.eraseFeatures(insertedFeature);
OpenLayers.Util.removeItem(layer.features,insertedFeature);
break;
case "Delete":
//console.log("delete");
layer.features.push(feature);
layer.drawFeature(feature);
break;
case "Update":
//console.log("update");
updatedFeature = layer.getFeatureByFid(feature.fid);
//console.log("old feature geometry: " + feature.geometry);
//console.log("updated feature geometry: " + updatedFeature.geometry);
//layer.drawFeature(updatedFeature,{display : "none"});
layer.eraseFeatures(updatedFeature);
OpenLayers.Util.removeItem(layer.features,updatedFeature);
//layer.removeFeatures(updatedFeature);
//console.log("old feature geometry: " + feature.geometry);
layer.features.push(feature);
layer.drawFeature(feature);
data[component][editType][i]= updatedFeature;
break;
default:
//console.log("unkown");
break;
}
//console.log("features after undo: "+layer.features.length);
}
}
}
this.redoFeatures.push(data);
},
/**
* APIMethod: redo
* perform redo operation
*/
redo: function(){
data =this.getRedoData();
for( component in data) {
//console.log("component: "+component);
layer=map.getLayersByName(component)[0];
//console.log("got layer, name: " + layer.name);
for(editType in data[component]){
//console.log("edittype : "+editType);
for (var i=0;i<data[component][editType].length;i++){
feature=data[component][editType][i];
//console.log("features before redo: "+layer.features.length);
switch(editType) {
case "Insert":
//console.log("Insert");
layer.features.push(feature);
layer.drawFeature(feature);
break;
case "Delete":
//console.log("Delete");
deleteFeature = layer.getFeatureByFid(feature.fid)
layer.eraseFeatures(deleteFeature);
OpenLayers.Util.removeItem(layer.features,deleteFeature);
break;
case "Update":
//console.log("Update");
oldFeature = layer.getFeatureByFid(feature.fid);
//console.log("old feature id: " + oldFeature.id);
layer.eraseFeatures(oldFeature);
OpenLayers.Util.removeItem(layer.features,oldFeature);
//console.log("updated feature id: " + oldFeature.id);
layer.features.push(feature);
layer.drawFeature(feature);
data[component][editType][i]= oldFeature;
break;
default:
break;
}
}
}
}
this.undoFeatures.push(data);
},
CLASS_NAME : "UndoRedo"
});