[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Control/DragFeature.js |
---|
| 8 | * @requires OpenLayers/Control/SelectFeature.js |
---|
| 9 | * @requires OpenLayers/Handler/Keyboard.js |
---|
| 10 | */ |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * Class: OpenLayers.Control.ModifyFeature |
---|
| 14 | * Control to modify features. When activated, a click renders the vertices |
---|
| 15 | * of a feature - these vertices can then be dragged. By default, the |
---|
| 16 | * delete key will delete the vertex under the mouse. New features are |
---|
| 17 | * added by dragging "virtual vertices" between vertices. Create a new |
---|
| 18 | * control with the <OpenLayers.Control.ModifyFeature> constructor. |
---|
| 19 | * |
---|
| 20 | * Inherits From: |
---|
| 21 | * - <OpenLayers.Control> |
---|
| 22 | */ |
---|
| 23 | OpenLayers.Control.ModifyFeature = OpenLayers.Class(OpenLayers.Control, { |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * APIProperty: geometryTypes |
---|
| 27 | * {Array(String)} To restrict modification to a limited set of geometry |
---|
| 28 | * types, send a list of strings corresponding to the geometry class |
---|
| 29 | * names. |
---|
| 30 | */ |
---|
| 31 | geometryTypes: null, |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * APIProperty: clickout |
---|
| 35 | * {Boolean} Unselect features when clicking outside any feature. |
---|
| 36 | * Default is true. |
---|
| 37 | */ |
---|
| 38 | clickout: true, |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * APIProperty: toggle |
---|
| 42 | * {Boolean} Unselect a selected feature on click. |
---|
| 43 | * Default is true. |
---|
| 44 | */ |
---|
| 45 | toggle: true, |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * APIProperty: standalone |
---|
| 49 | * {Boolean} Set to true to create a control without SelectFeature |
---|
| 50 | * capabilities. Default is false. If standalone is true, to modify |
---|
| 51 | * a feature, call the <selectFeature> method with the target feature. |
---|
| 52 | * Note that you must call the <unselectFeature> method to finish |
---|
| 53 | * feature modification in standalone mode (before starting to modify |
---|
| 54 | * another feature). |
---|
| 55 | */ |
---|
| 56 | standalone: false, |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Property: layer |
---|
| 60 | * {<OpenLayers.Layer.Vector>} |
---|
| 61 | */ |
---|
| 62 | layer: null, |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Property: feature |
---|
| 66 | * {<OpenLayers.Feature.Vector>} Feature currently available for modification. |
---|
| 67 | */ |
---|
| 68 | feature: null, |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Property: vertices |
---|
| 72 | * {Array(<OpenLayers.Feature.Vector>)} Verticies currently available |
---|
| 73 | * for dragging. |
---|
| 74 | */ |
---|
| 75 | vertices: null, |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Property: virtualVertices |
---|
| 79 | * {Array(<OpenLayers.Feature.Vector>)} Virtual vertices in the middle |
---|
| 80 | * of each edge. |
---|
| 81 | */ |
---|
| 82 | virtualVertices: null, |
---|
| 83 | |
---|
| 84 | /** |
---|
| 85 | * Property: selectControl |
---|
| 86 | * {<OpenLayers.Control.SelectFeature>} |
---|
| 87 | */ |
---|
| 88 | selectControl: null, |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Property: dragControl |
---|
| 92 | * {<OpenLayers.Control.DragFeature>} |
---|
| 93 | */ |
---|
| 94 | dragControl: null, |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * Property: handlers |
---|
| 98 | * {Object} |
---|
| 99 | */ |
---|
| 100 | handlers: null, |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * APIProperty: deleteCodes |
---|
| 104 | * {Array(Integer)} Keycodes for deleting verticies. Set to null to disable |
---|
| 105 | * vertex deltion by keypress. If non-null, keypresses with codes |
---|
| 106 | * in this array will delete vertices under the mouse. Default |
---|
| 107 | * is 46 and 68, the 'delete' and lowercase 'd' keys. |
---|
| 108 | */ |
---|
| 109 | deleteCodes: null, |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * APIProperty: virtualStyle |
---|
| 113 | * {Object} A symbolizer to be used for virtual vertices. |
---|
| 114 | */ |
---|
| 115 | virtualStyle: null, |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * APIProperty: mode |
---|
| 119 | * {Integer} Bitfields specifying the modification mode. Defaults to |
---|
| 120 | * OpenLayers.Control.ModifyFeature.RESHAPE. To set the mode to a |
---|
| 121 | * combination of options, use the | operator. For example, to allow |
---|
| 122 | * the control to both resize and rotate features, use the following |
---|
| 123 | * syntax |
---|
| 124 | * (code) |
---|
| 125 | * control.mode = OpenLayers.Control.ModifyFeature.RESIZE | |
---|
| 126 | * OpenLayers.Control.ModifyFeature.ROTATE; |
---|
| 127 | * (end) |
---|
| 128 | */ |
---|
| 129 | mode: null, |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Property: modified |
---|
| 133 | * {Boolean} The currently selected feature has been modified. |
---|
| 134 | */ |
---|
| 135 | modified: false, |
---|
| 136 | |
---|
| 137 | /** |
---|
| 138 | * Property: radiusHandle |
---|
| 139 | * {<OpenLayers.Feature.Vector>} A handle for rotating/resizing a feature. |
---|
| 140 | */ |
---|
| 141 | radiusHandle: null, |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Property: dragHandle |
---|
| 145 | * {<OpenLayers.Feature.Vector>} A handle for dragging a feature. |
---|
| 146 | */ |
---|
| 147 | dragHandle: null, |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * APIProperty: onModificationStart |
---|
| 151 | * {Function} *Deprecated*. Register for "beforefeaturemodified" instead. |
---|
| 152 | * The "beforefeaturemodified" event is triggered on the layer before |
---|
| 153 | * any modification begins. |
---|
| 154 | * |
---|
| 155 | * Optional function to be called when a feature is selected |
---|
| 156 | * to be modified. The function should expect to be called with a |
---|
| 157 | * feature. This could be used for example to allow to lock the |
---|
| 158 | * feature on server-side. |
---|
| 159 | */ |
---|
| 160 | onModificationStart: function() {}, |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * APIProperty: onModification |
---|
| 164 | * {Function} *Deprecated*. Register for "featuremodified" instead. |
---|
| 165 | * The "featuremodified" event is triggered on the layer with each |
---|
| 166 | * feature modification. |
---|
| 167 | * |
---|
| 168 | * Optional function to be called when a feature has been |
---|
| 169 | * modified. The function should expect to be called with a feature. |
---|
| 170 | */ |
---|
| 171 | onModification: function() {}, |
---|
| 172 | |
---|
| 173 | /** |
---|
| 174 | * APIProperty: onModificationEnd |
---|
| 175 | * {Function} *Deprecated*. Register for "afterfeaturemodified" instead. |
---|
| 176 | * The "afterfeaturemodified" event is triggered on the layer after |
---|
| 177 | * a feature has been modified. |
---|
| 178 | * |
---|
| 179 | * Optional function to be called when a feature is finished |
---|
| 180 | * being modified. The function should expect to be called with a |
---|
| 181 | * feature. |
---|
| 182 | */ |
---|
| 183 | onModificationEnd: function() {}, |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * Constructor: OpenLayers.Control.ModifyFeature |
---|
| 187 | * Create a new modify feature control. |
---|
| 188 | * |
---|
| 189 | * Parameters: |
---|
| 190 | * layer - {<OpenLayers.Layer.Vector>} Layer that contains features that |
---|
| 191 | * will be modified. |
---|
| 192 | * options - {Object} Optional object whose properties will be set on the |
---|
| 193 | * control. |
---|
| 194 | */ |
---|
| 195 | initialize: function(layer, options) { |
---|
| 196 | this.layer = layer; |
---|
| 197 | this.vertices = []; |
---|
| 198 | this.virtualVertices = []; |
---|
| 199 | this.virtualStyle = OpenLayers.Util.extend({}, |
---|
| 200 | this.layer.style || this.layer.styleMap.createSymbolizer()); |
---|
| 201 | this.virtualStyle.fillOpacity = 0.3; |
---|
| 202 | this.virtualStyle.strokeOpacity = 0.3; |
---|
| 203 | this.deleteCodes = [46, 68]; |
---|
| 204 | this.mode = OpenLayers.Control.ModifyFeature.RESHAPE; |
---|
| 205 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
| 206 | if(!(this.deleteCodes instanceof Array)) { |
---|
| 207 | this.deleteCodes = [this.deleteCodes]; |
---|
| 208 | } |
---|
| 209 | var control = this; |
---|
| 210 | |
---|
| 211 | // configure the select control |
---|
| 212 | var selectOptions = { |
---|
| 213 | geometryTypes: this.geometryTypes, |
---|
| 214 | clickout: this.clickout, |
---|
| 215 | toggle: this.toggle, |
---|
| 216 | onBeforeSelect: this.beforeSelectFeature, |
---|
| 217 | onSelect: this.selectFeature, |
---|
| 218 | onUnselect: this.unselectFeature, |
---|
| 219 | scope: this |
---|
| 220 | }; |
---|
| 221 | if(this.standalone === false) { |
---|
| 222 | this.selectControl = new OpenLayers.Control.SelectFeature( |
---|
| 223 | layer, selectOptions |
---|
| 224 | ); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | // configure the drag control |
---|
| 228 | var dragOptions = { |
---|
| 229 | geometryTypes: ["OpenLayers.Geometry.Point"], |
---|
| 230 | snappingOptions: this.snappingOptions, |
---|
| 231 | onStart: function(feature, pixel) { |
---|
| 232 | control.dragStart.apply(control, [feature, pixel]); |
---|
| 233 | }, |
---|
| 234 | onDrag: function(feature, pixel) { |
---|
| 235 | control.dragVertex.apply(control, [feature, pixel]); |
---|
| 236 | }, |
---|
| 237 | onComplete: function(feature) { |
---|
| 238 | control.dragComplete.apply(control, [feature]); |
---|
| 239 | }, |
---|
| 240 | featureCallbacks: { |
---|
| 241 | over: function(feature) { |
---|
| 242 | /** |
---|
| 243 | * In normal mode, the feature handler is set up to allow |
---|
| 244 | * dragging of all points. In standalone mode, we only |
---|
| 245 | * want to allow dragging of sketch vertices and virtual |
---|
| 246 | * vertices - or, in the case of a modifiable point, the |
---|
| 247 | * point itself. |
---|
| 248 | */ |
---|
| 249 | if(control.standalone !== true || feature._sketch || |
---|
| 250 | control.feature === feature) { |
---|
| 251 | control.dragControl.overFeature.apply( |
---|
| 252 | control.dragControl, [feature]); |
---|
| 253 | } |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | }; |
---|
| 257 | this.dragControl = new OpenLayers.Control.DragFeature( |
---|
| 258 | layer, dragOptions |
---|
| 259 | ); |
---|
| 260 | |
---|
| 261 | // configure the keyboard handler |
---|
| 262 | var keyboardOptions = { |
---|
| 263 | keydown: this.handleKeypress |
---|
| 264 | }; |
---|
| 265 | this.handlers = { |
---|
| 266 | keyboard: new OpenLayers.Handler.Keyboard(this, keyboardOptions) |
---|
| 267 | }; |
---|
| 268 | }, |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | * APIMethod: destroy |
---|
| 272 | * Take care of things that are not handled in superclass. |
---|
| 273 | */ |
---|
| 274 | destroy: function() { |
---|
| 275 | this.layer = null; |
---|
| 276 | this.standalone || this.selectControl.destroy(); |
---|
| 277 | this.dragControl.destroy(); |
---|
| 278 | OpenLayers.Control.prototype.destroy.apply(this, []); |
---|
| 279 | }, |
---|
| 280 | |
---|
| 281 | /** |
---|
| 282 | * APIMethod: activate |
---|
| 283 | * Activate the control. |
---|
| 284 | * |
---|
| 285 | * Returns: |
---|
| 286 | * {Boolean} Successfully activated the control. |
---|
| 287 | */ |
---|
| 288 | activate: function() { |
---|
| 289 | return ((this.standalone || this.selectControl.activate()) && |
---|
| 290 | this.handlers.keyboard.activate() && |
---|
| 291 | OpenLayers.Control.prototype.activate.apply(this, arguments)); |
---|
| 292 | }, |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * APIMethod: deactivate |
---|
| 296 | * Deactivate the control. |
---|
| 297 | * |
---|
| 298 | * Returns: |
---|
| 299 | * {Boolean} Successfully deactivated the control. |
---|
| 300 | */ |
---|
| 301 | deactivate: function() { |
---|
| 302 | var deactivated = false; |
---|
| 303 | // the return from the controls is unimportant in this case |
---|
| 304 | if(OpenLayers.Control.prototype.deactivate.apply(this, arguments)) { |
---|
| 305 | this.layer.removeFeatures(this.vertices, {silent: true}); |
---|
| 306 | this.layer.removeFeatures(this.virtualVertices, {silent: true}); |
---|
| 307 | this.vertices = []; |
---|
| 308 | this.dragControl.deactivate(); |
---|
| 309 | var feature = this.feature; |
---|
| 310 | var valid = feature && feature.geometry && feature.layer; |
---|
| 311 | if(this.standalone === false) { |
---|
| 312 | if(valid) { |
---|
| 313 | this.selectControl.unselect.apply(this.selectControl, |
---|
| 314 | [feature]); |
---|
| 315 | } |
---|
| 316 | this.selectControl.deactivate(); |
---|
| 317 | } else { |
---|
| 318 | if(valid) { |
---|
| 319 | this.unselectFeature(feature); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | this.handlers.keyboard.deactivate(); |
---|
| 323 | deactivated = true; |
---|
| 324 | } |
---|
| 325 | return deactivated; |
---|
| 326 | }, |
---|
| 327 | |
---|
| 328 | /** |
---|
| 329 | * Method: beforeSelectFeature |
---|
| 330 | * Called before a feature is selected. |
---|
| 331 | * |
---|
| 332 | * Parameters: |
---|
| 333 | * feature - {<OpenLayers.Feature.Vector>} The feature about to be selected. |
---|
| 334 | */ |
---|
| 335 | beforeSelectFeature: function(feature) { |
---|
| 336 | return this.layer.events.triggerEvent( |
---|
| 337 | "beforefeaturemodified", {feature: feature} |
---|
| 338 | ); |
---|
| 339 | }, |
---|
| 340 | |
---|
| 341 | /** |
---|
| 342 | * Method: selectFeature |
---|
| 343 | * Called when the select feature control selects a feature. |
---|
| 344 | * |
---|
| 345 | * Parameters: |
---|
| 346 | * feature - {<OpenLayers.Feature.Vector>} the selected feature. |
---|
| 347 | */ |
---|
| 348 | selectFeature: function(feature) { |
---|
| 349 | this.feature = feature; |
---|
| 350 | this.modified = false; |
---|
| 351 | this.resetVertices(); |
---|
| 352 | this.dragControl.activate(); |
---|
| 353 | this.onModificationStart(this.feature); |
---|
| 354 | }, |
---|
| 355 | |
---|
| 356 | /** |
---|
| 357 | * Method: unselectFeature |
---|
| 358 | * Called when the select feature control unselects a feature. |
---|
| 359 | * |
---|
| 360 | * Parameters: |
---|
| 361 | * feature - {<OpenLayers.Feature.Vector>} The unselected feature. |
---|
| 362 | */ |
---|
| 363 | unselectFeature: function(feature) { |
---|
| 364 | this.layer.removeFeatures(this.vertices, {silent: true}); |
---|
| 365 | this.vertices = []; |
---|
| 366 | this.layer.destroyFeatures(this.virtualVertices, {silent: true}); |
---|
| 367 | this.virtualVertices = []; |
---|
| 368 | if(this.dragHandle) { |
---|
| 369 | this.layer.destroyFeatures([this.dragHandle], {silent: true}); |
---|
| 370 | delete this.dragHandle; |
---|
| 371 | } |
---|
| 372 | if(this.radiusHandle) { |
---|
| 373 | this.layer.destroyFeatures([this.radiusHandle], {silent: true}); |
---|
| 374 | delete this.radiusHandle; |
---|
| 375 | } |
---|
| 376 | this.feature = null; |
---|
| 377 | this.dragControl.deactivate(); |
---|
| 378 | this.onModificationEnd(feature); |
---|
| 379 | this.layer.events.triggerEvent("afterfeaturemodified", { |
---|
| 380 | feature: feature, |
---|
| 381 | modified: this.modified |
---|
| 382 | }); |
---|
| 383 | this.modified = false; |
---|
| 384 | }, |
---|
| 385 | |
---|
| 386 | /** |
---|
| 387 | * Method: dragStart |
---|
| 388 | * Called by the drag feature control with before a feature is dragged. |
---|
| 389 | * This method is used to differentiate between points and vertices |
---|
| 390 | * of higher order geometries. This respects the <geometryTypes> |
---|
| 391 | * property and forces a select of points when the drag control is |
---|
| 392 | * already active (and stops events from propagating to the select |
---|
| 393 | * control). |
---|
| 394 | * |
---|
| 395 | * Parameters: |
---|
| 396 | * feature - {<OpenLayers.Feature.Vector>} The point or vertex about to be |
---|
| 397 | * dragged. |
---|
| 398 | * pixel - {<OpenLayers.Pixel>} Pixel location of the mouse event. |
---|
| 399 | */ |
---|
| 400 | dragStart: function(feature, pixel) { |
---|
| 401 | // only change behavior if the feature is not in the vertices array |
---|
| 402 | if(feature != this.feature && !feature.geometry.parent && |
---|
| 403 | feature != this.dragHandle && feature != this.radiusHandle) { |
---|
| 404 | if(this.standalone === false && this.feature) { |
---|
| 405 | // unselect the currently selected feature |
---|
| 406 | this.selectControl.clickFeature.apply(this.selectControl, |
---|
| 407 | [this.feature]); |
---|
| 408 | } |
---|
| 409 | // check any constraints on the geometry type |
---|
| 410 | if(this.geometryTypes == null || |
---|
| 411 | OpenLayers.Util.indexOf(this.geometryTypes, |
---|
| 412 | feature.geometry.CLASS_NAME) != -1) { |
---|
| 413 | // select the point |
---|
| 414 | this.standalone || this.selectControl.clickFeature.apply( |
---|
| 415 | this.selectControl, [feature]); |
---|
| 416 | /** |
---|
| 417 | * TBD: These lines improve workflow by letting the user |
---|
| 418 | * immediately start dragging after the mouse down. |
---|
| 419 | * However, it is very ugly to be messing with controls |
---|
| 420 | * and their handlers in this way. I'd like a better |
---|
| 421 | * solution if the workflow change is necessary. |
---|
| 422 | */ |
---|
| 423 | // prepare the point for dragging |
---|
| 424 | this.dragControl.overFeature.apply(this.dragControl, |
---|
| 425 | [feature]); |
---|
| 426 | this.dragControl.lastPixel = pixel; |
---|
| 427 | this.dragControl.handlers.drag.started = true; |
---|
| 428 | this.dragControl.handlers.drag.start = pixel; |
---|
| 429 | this.dragControl.handlers.drag.last = pixel; |
---|
| 430 | } |
---|
| 431 | } |
---|
| 432 | }, |
---|
| 433 | |
---|
| 434 | /** |
---|
| 435 | * Method: dragVertex |
---|
| 436 | * Called by the drag feature control with each drag move of a vertex. |
---|
| 437 | * |
---|
| 438 | * Parameters: |
---|
| 439 | * vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged. |
---|
| 440 | * pixel - {<OpenLayers.Pixel>} Pixel location of the mouse event. |
---|
| 441 | */ |
---|
| 442 | dragVertex: function(vertex, pixel) { |
---|
| 443 | this.modified = true; |
---|
| 444 | /** |
---|
| 445 | * Five cases: |
---|
| 446 | * 1) dragging a simple point |
---|
| 447 | * 2) dragging a virtual vertex |
---|
| 448 | * 3) dragging a drag handle |
---|
| 449 | * 4) dragging a real vertex |
---|
| 450 | * 5) dragging a radius handle |
---|
| 451 | */ |
---|
| 452 | if(this.feature.geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { |
---|
| 453 | // dragging a simple point |
---|
| 454 | if(this.feature != vertex) { |
---|
| 455 | this.feature = vertex; |
---|
| 456 | } |
---|
| 457 | this.layer.events.triggerEvent("vertexmodified", { |
---|
| 458 | vertex: vertex.geometry, |
---|
| 459 | feature: this.feature, |
---|
| 460 | pixel: pixel |
---|
| 461 | }); |
---|
| 462 | } else { |
---|
| 463 | if(vertex._index) { |
---|
| 464 | // dragging a virtual vertex |
---|
| 465 | vertex.geometry.parent.addComponent(vertex.geometry, |
---|
| 466 | vertex._index); |
---|
| 467 | // move from virtual to real vertex |
---|
| 468 | delete vertex._index; |
---|
| 469 | OpenLayers.Util.removeItem(this.virtualVertices, vertex); |
---|
| 470 | this.vertices.push(vertex); |
---|
| 471 | } else if(vertex == this.dragHandle) { |
---|
| 472 | // dragging a drag handle |
---|
| 473 | this.layer.removeFeatures(this.vertices, {silent: true}); |
---|
| 474 | this.vertices = []; |
---|
| 475 | if(this.radiusHandle) { |
---|
| 476 | this.layer.destroyFeatures([this.radiusHandle], {silent: true}); |
---|
| 477 | this.radiusHandle = null; |
---|
| 478 | } |
---|
| 479 | } else if(vertex !== this.radiusHandle) { |
---|
| 480 | // dragging a real vertex |
---|
| 481 | this.layer.events.triggerEvent("vertexmodified", { |
---|
| 482 | vertex: vertex.geometry, |
---|
| 483 | feature: this.feature, |
---|
| 484 | pixel: pixel |
---|
| 485 | }); |
---|
| 486 | } |
---|
| 487 | // dragging a radius handle - no special treatment |
---|
| 488 | if(this.virtualVertices.length > 0) { |
---|
| 489 | this.layer.destroyFeatures(this.virtualVertices, {silent: true}); |
---|
| 490 | this.virtualVertices = []; |
---|
| 491 | } |
---|
| 492 | this.layer.drawFeature(this.feature, this.standalone ? undefined : |
---|
| 493 | this.selectControl.renderIntent); |
---|
| 494 | } |
---|
| 495 | // keep the vertex on top so it gets the mouseout after dragging |
---|
| 496 | // this should be removed in favor of an option to draw under or |
---|
| 497 | // maintain node z-index |
---|
| 498 | this.layer.drawFeature(vertex); |
---|
| 499 | }, |
---|
| 500 | |
---|
| 501 | /** |
---|
| 502 | * Method: dragComplete |
---|
| 503 | * Called by the drag feature control when the feature dragging is complete. |
---|
| 504 | * |
---|
| 505 | * Parameters: |
---|
| 506 | * vertex - {<OpenLayers.Feature.Vector>} The vertex being dragged. |
---|
| 507 | */ |
---|
| 508 | dragComplete: function(vertex) { |
---|
| 509 | this.resetVertices(); |
---|
| 510 | this.setFeatureState(); |
---|
| 511 | this.onModification(this.feature); |
---|
| 512 | this.layer.events.triggerEvent("featuremodified", |
---|
| 513 | {feature: this.feature}); |
---|
| 514 | }, |
---|
| 515 | |
---|
| 516 | /** |
---|
| 517 | * Method: setFeatureState |
---|
| 518 | * Called when the feature is modified. If the current state is not |
---|
| 519 | * INSERT or DELETE, the state is set to UPDATE. |
---|
| 520 | */ |
---|
| 521 | setFeatureState: function() { |
---|
| 522 | if(this.feature.state != OpenLayers.State.INSERT && |
---|
| 523 | this.feature.state != OpenLayers.State.DELETE) { |
---|
| 524 | this.feature.state = OpenLayers.State.UPDATE; |
---|
| 525 | } |
---|
| 526 | }, |
---|
| 527 | |
---|
| 528 | /** |
---|
| 529 | * Method: resetVertices |
---|
| 530 | */ |
---|
| 531 | resetVertices: function() { |
---|
| 532 | // if coming from a drag complete we're about to destroy the vertex |
---|
| 533 | // that was just dragged. For that reason, the drag feature control |
---|
| 534 | // will never detect a mouse-out on that vertex, meaning that the drag |
---|
| 535 | // handler won't be deactivated. This can cause errors because the drag |
---|
| 536 | // feature control still has a feature to drag but that feature is |
---|
| 537 | // destroyed. To prevent this, we call outFeature on the drag feature |
---|
| 538 | // control if the control actually has a feature to drag. |
---|
| 539 | if(this.dragControl.feature) { |
---|
| 540 | this.dragControl.outFeature(this.dragControl.feature); |
---|
| 541 | } |
---|
| 542 | if(this.vertices.length > 0) { |
---|
| 543 | this.layer.removeFeatures(this.vertices, {silent: true}); |
---|
| 544 | this.vertices = []; |
---|
| 545 | } |
---|
| 546 | if(this.virtualVertices.length > 0) { |
---|
| 547 | this.layer.removeFeatures(this.virtualVertices, {silent: true}); |
---|
| 548 | this.virtualVertices = []; |
---|
| 549 | } |
---|
| 550 | if(this.dragHandle) { |
---|
| 551 | this.layer.destroyFeatures([this.dragHandle], {silent: true}); |
---|
| 552 | this.dragHandle = null; |
---|
| 553 | } |
---|
| 554 | if(this.radiusHandle) { |
---|
| 555 | this.layer.destroyFeatures([this.radiusHandle], {silent: true}); |
---|
| 556 | this.radiusHandle = null; |
---|
| 557 | } |
---|
| 558 | if(this.feature && |
---|
| 559 | this.feature.geometry.CLASS_NAME != "OpenLayers.Geometry.Point") { |
---|
| 560 | if((this.mode & OpenLayers.Control.ModifyFeature.DRAG)) { |
---|
| 561 | this.collectDragHandle(); |
---|
| 562 | } |
---|
| 563 | if((this.mode & (OpenLayers.Control.ModifyFeature.ROTATE | |
---|
| 564 | OpenLayers.Control.ModifyFeature.RESIZE))) { |
---|
| 565 | this.collectRadiusHandle(); |
---|
| 566 | } |
---|
| 567 | if(this.mode & OpenLayers.Control.ModifyFeature.RESHAPE){ |
---|
| 568 | // Don't collect vertices when we're resizing |
---|
| 569 | if (!(this.mode & OpenLayers.Control.ModifyFeature.RESIZE)){ |
---|
| 570 | this.collectVertices(); |
---|
| 571 | } |
---|
| 572 | } |
---|
| 573 | } |
---|
| 574 | }, |
---|
| 575 | |
---|
| 576 | /** |
---|
| 577 | * Method: handleKeypress |
---|
| 578 | * Called by the feature handler on keypress. This is used to delete |
---|
| 579 | * vertices. If the <deleteCode> property is set, vertices will |
---|
| 580 | * be deleted when a feature is selected for modification and |
---|
| 581 | * the mouse is over a vertex. |
---|
| 582 | * |
---|
| 583 | * Parameters: |
---|
| 584 | * {Integer} Key code corresponding to the keypress event. |
---|
| 585 | */ |
---|
| 586 | handleKeypress: function(evt) { |
---|
| 587 | var code = evt.keyCode; |
---|
| 588 | |
---|
| 589 | // check for delete key |
---|
| 590 | if(this.feature && |
---|
| 591 | OpenLayers.Util.indexOf(this.deleteCodes, code) != -1) { |
---|
| 592 | var vertex = this.dragControl.feature; |
---|
| 593 | if(vertex && |
---|
| 594 | OpenLayers.Util.indexOf(this.vertices, vertex) != -1 && |
---|
| 595 | !this.dragControl.handlers.drag.dragging && |
---|
| 596 | vertex.geometry.parent) { |
---|
| 597 | // remove the vertex |
---|
| 598 | vertex.geometry.parent.removeComponent(vertex.geometry); |
---|
| 599 | this.layer.drawFeature(this.feature, this.standalone ? |
---|
| 600 | undefined : |
---|
| 601 | this.selectControl.renderIntent); |
---|
| 602 | this.resetVertices(); |
---|
| 603 | this.setFeatureState(); |
---|
| 604 | this.onModification(this.feature); |
---|
| 605 | this.layer.events.triggerEvent("featuremodified", |
---|
| 606 | {feature: this.feature}); |
---|
| 607 | } |
---|
| 608 | } |
---|
| 609 | }, |
---|
| 610 | |
---|
| 611 | /** |
---|
| 612 | * Method: collectVertices |
---|
| 613 | * Collect the vertices from the modifiable feature's geometry and push |
---|
| 614 | * them on to the control's vertices array. |
---|
| 615 | */ |
---|
| 616 | collectVertices: function() { |
---|
| 617 | this.vertices = []; |
---|
| 618 | this.virtualVertices = []; |
---|
| 619 | var control = this; |
---|
| 620 | function collectComponentVertices(geometry) { |
---|
| 621 | var i, vertex, component, len; |
---|
| 622 | if(geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { |
---|
| 623 | vertex = new OpenLayers.Feature.Vector(geometry); |
---|
| 624 | vertex._sketch = true; |
---|
| 625 | control.vertices.push(vertex); |
---|
| 626 | } else { |
---|
| 627 | var numVert = geometry.components.length; |
---|
| 628 | if(geometry.CLASS_NAME == "OpenLayers.Geometry.LinearRing") { |
---|
| 629 | numVert -= 1; |
---|
| 630 | } |
---|
| 631 | for(i=0; i<numVert; ++i) { |
---|
| 632 | component = geometry.components[i]; |
---|
| 633 | if(component.CLASS_NAME == "OpenLayers.Geometry.Point") { |
---|
| 634 | vertex = new OpenLayers.Feature.Vector(component); |
---|
| 635 | vertex._sketch = true; |
---|
| 636 | control.vertices.push(vertex); |
---|
| 637 | } else { |
---|
| 638 | collectComponentVertices(component); |
---|
| 639 | } |
---|
| 640 | } |
---|
| 641 | |
---|
| 642 | // add virtual vertices in the middle of each edge |
---|
| 643 | if(geometry.CLASS_NAME != "OpenLayers.Geometry.MultiPoint") { |
---|
| 644 | for(i=0, len=geometry.components.length; i<len-1; ++i) { |
---|
| 645 | var prevVertex = geometry.components[i]; |
---|
| 646 | var nextVertex = geometry.components[i + 1]; |
---|
| 647 | if(prevVertex.CLASS_NAME == "OpenLayers.Geometry.Point" && |
---|
| 648 | nextVertex.CLASS_NAME == "OpenLayers.Geometry.Point") { |
---|
| 649 | var x = (prevVertex.x + nextVertex.x) / 2; |
---|
| 650 | var y = (prevVertex.y + nextVertex.y) / 2; |
---|
| 651 | var point = new OpenLayers.Feature.Vector( |
---|
| 652 | new OpenLayers.Geometry.Point(x, y), |
---|
| 653 | null, control.virtualStyle |
---|
| 654 | ); |
---|
| 655 | // set the virtual parent and intended index |
---|
| 656 | point.geometry.parent = geometry; |
---|
| 657 | point._index = i + 1; |
---|
| 658 | point._sketch = true; |
---|
| 659 | control.virtualVertices.push(point); |
---|
| 660 | } |
---|
| 661 | } |
---|
| 662 | } |
---|
| 663 | } |
---|
| 664 | } |
---|
| 665 | collectComponentVertices.call(this, this.feature.geometry); |
---|
| 666 | this.layer.addFeatures(this.virtualVertices, {silent: true}); |
---|
| 667 | this.layer.addFeatures(this.vertices, {silent: true}); |
---|
| 668 | }, |
---|
| 669 | |
---|
| 670 | /** |
---|
| 671 | * Method: collectDragHandle |
---|
| 672 | * Collect the drag handle for the selected geometry. |
---|
| 673 | */ |
---|
| 674 | collectDragHandle: function() { |
---|
| 675 | var geometry = this.feature.geometry; |
---|
| 676 | var center = geometry.getBounds().getCenterLonLat(); |
---|
| 677 | var originGeometry = new OpenLayers.Geometry.Point( |
---|
| 678 | center.lon, center.lat |
---|
| 679 | ); |
---|
| 680 | var origin = new OpenLayers.Feature.Vector(originGeometry); |
---|
| 681 | originGeometry.move = function(x, y) { |
---|
| 682 | OpenLayers.Geometry.Point.prototype.move.call(this, x, y); |
---|
| 683 | geometry.move(x, y); |
---|
| 684 | }; |
---|
| 685 | origin._sketch = true; |
---|
| 686 | this.dragHandle = origin; |
---|
| 687 | this.layer.addFeatures([this.dragHandle], {silent: true}); |
---|
| 688 | }, |
---|
| 689 | |
---|
| 690 | /** |
---|
| 691 | * Method: collectRadiusHandle |
---|
| 692 | * Collect the radius handle for the selected geometry. |
---|
| 693 | */ |
---|
| 694 | collectRadiusHandle: function() { |
---|
| 695 | var geometry = this.feature.geometry; |
---|
| 696 | var bounds = geometry.getBounds(); |
---|
| 697 | var center = bounds.getCenterLonLat(); |
---|
| 698 | var originGeometry = new OpenLayers.Geometry.Point( |
---|
| 699 | center.lon, center.lat |
---|
| 700 | ); |
---|
| 701 | var radiusGeometry = new OpenLayers.Geometry.Point( |
---|
| 702 | bounds.right, bounds.bottom |
---|
| 703 | ); |
---|
| 704 | var radius = new OpenLayers.Feature.Vector(radiusGeometry); |
---|
| 705 | var resize = (this.mode & OpenLayers.Control.ModifyFeature.RESIZE); |
---|
| 706 | var reshape = (this.mode & OpenLayers.Control.ModifyFeature.RESHAPE); |
---|
| 707 | var rotate = (this.mode & OpenLayers.Control.ModifyFeature.ROTATE); |
---|
| 708 | |
---|
| 709 | radiusGeometry.move = function(x, y) { |
---|
| 710 | OpenLayers.Geometry.Point.prototype.move.call(this, x, y); |
---|
| 711 | var dx1 = this.x - originGeometry.x; |
---|
| 712 | var dy1 = this.y - originGeometry.y; |
---|
| 713 | var dx0 = dx1 - x; |
---|
| 714 | var dy0 = dy1 - y; |
---|
| 715 | if(rotate) { |
---|
| 716 | var a0 = Math.atan2(dy0, dx0); |
---|
| 717 | var a1 = Math.atan2(dy1, dx1); |
---|
| 718 | var angle = a1 - a0; |
---|
| 719 | angle *= 180 / Math.PI; |
---|
| 720 | geometry.rotate(angle, originGeometry); |
---|
| 721 | } |
---|
| 722 | if(resize) { |
---|
| 723 | var scale, ratio; |
---|
| 724 | // 'resize' together with 'reshape' implies that the aspect |
---|
| 725 | // ratio of the geometry will not be preserved whilst resizing |
---|
| 726 | if (reshape) { |
---|
| 727 | scale = dy1 / dy0; |
---|
| 728 | ratio = (dx1 / dx0) / scale; |
---|
| 729 | } else { |
---|
| 730 | var l0 = Math.sqrt((dx0 * dx0) + (dy0 * dy0)); |
---|
| 731 | var l1 = Math.sqrt((dx1 * dx1) + (dy1 * dy1)); |
---|
| 732 | scale = l1 / l0; |
---|
| 733 | } |
---|
| 734 | geometry.resize(scale, originGeometry, ratio); |
---|
| 735 | } |
---|
| 736 | }; |
---|
| 737 | radius._sketch = true; |
---|
| 738 | this.radiusHandle = radius; |
---|
| 739 | this.layer.addFeatures([this.radiusHandle], {silent: true}); |
---|
| 740 | }, |
---|
| 741 | |
---|
| 742 | /** |
---|
| 743 | * Method: setMap |
---|
| 744 | * Set the map property for the control and all handlers. |
---|
| 745 | * |
---|
| 746 | * Parameters: |
---|
| 747 | * map - {<OpenLayers.Map>} The control's map. |
---|
| 748 | */ |
---|
| 749 | setMap: function(map) { |
---|
| 750 | this.standalone || this.selectControl.setMap(map); |
---|
| 751 | this.dragControl.setMap(map); |
---|
| 752 | OpenLayers.Control.prototype.setMap.apply(this, arguments); |
---|
| 753 | }, |
---|
| 754 | |
---|
| 755 | CLASS_NAME: "OpenLayers.Control.ModifyFeature" |
---|
| 756 | }); |
---|
| 757 | |
---|
| 758 | /** |
---|
| 759 | * Constant: RESHAPE |
---|
| 760 | * {Integer} Constant used to make the control work in reshape mode |
---|
| 761 | */ |
---|
| 762 | OpenLayers.Control.ModifyFeature.RESHAPE = 1; |
---|
| 763 | /** |
---|
| 764 | * Constant: RESIZE |
---|
| 765 | * {Integer} Constant used to make the control work in resize mode |
---|
| 766 | */ |
---|
| 767 | OpenLayers.Control.ModifyFeature.RESIZE = 2; |
---|
| 768 | /** |
---|
| 769 | * Constant: ROTATE |
---|
| 770 | * {Integer} Constant used to make the control work in rotate mode |
---|
| 771 | */ |
---|
| 772 | OpenLayers.Control.ModifyFeature.ROTATE = 4; |
---|
| 773 | /** |
---|
| 774 | * Constant: DRAG |
---|
| 775 | * {Integer} Constant used to make the control work in drag mode |
---|
| 776 | */ |
---|
| 777 | OpenLayers.Control.ModifyFeature.DRAG = 8; |
---|