OpenLayers OpenLayers

Changeset 6413

Show
Ignore:
Timestamp:
02/29/08 06:50:39 (11 months ago)
Author:
fredj
Message:

Send events when modifying features:

'beforefeaturemodified' triggered when a feature is selected to be modified,
'featuremodified' triggered when a feature has been modified,
'afterfeaturemodified' triggered when a feature is finished being modified

r=tschaub (Closes #1358)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Control/ModifyFeature.js

    r6315 r6413  
    290290        this.dragControl.activate(); 
    291291        this.onModificationStart(this.feature); 
     292        this.layer.events.triggerEvent("beforefeaturemodified",  
     293                                       {feature: this.feature}); 
    292294    }, 
    293295 
     
    316318        this.dragControl.deactivate(); 
    317319        this.onModificationEnd(object.feature); 
     320        this.layer.events.triggerEvent("afterfeaturemodified",  
     321                                       {feature: object.feature}); 
    318322    }, 
    319323 
     
    429433        this.resetVertices(); 
    430434        this.onModification(this.feature); 
     435        this.layer.events.triggerEvent("featuremodified",  
     436                                       {feature: this.feature}); 
    431437    }, 
    432438     
     
    501507                this.resetVertices(); 
    502508                this.onModification(this.feature); 
     509                this.layer.events.triggerEvent("featuremodified",  
     510                                               {feature: this.feature}); 
    503511            } 
    504512        } 
  • trunk/openlayers/lib/OpenLayers/Layer/Vector.js

    r6389 r6413  
    5252     *      Listeners will receive an object with a *feature* property 
    5353     *      referencing the unselected feature. 
     54     *  - *beforefeaturemodified* Triggered when a feature is selected to  
     55     *      be modified.  Listeners will receive an object with a *feature*  
     56     *      property referencing the selected feature. 
     57     *  - *featuremodified* Triggered when a feature has been modified. 
     58     *      Listeners will receive an object with a *feature* property referencing  
     59     *      the modified feature. 
     60     *  - *afterfeaturemodified* Triggered when a feature is finished being modified. 
     61     *      Listeners will receive an object with a *feature* property referencing  
     62     *      the modified feature. 
    5463     */ 
    5564    EVENT_TYPES: ["beforefeatureadded", "featureadded", 
    56                   "featuresadded", "featureselected", "featureunselected"], 
     65                  "featuresadded", "featureselected", "featureunselected",  
     66                  "beforefeaturemodified", "featuremodified", "afterfeaturemodified"], 
    5767 
    5868    /** 
  • trunk/openlayers/tests/Control/test_ModifyFeature.html

    r6240 r6413  
    44    <script type="text/javascript"> 
    55     
    6     function test_ModifyFeature_constructor(t) { 
     6    function test_initalize(t) { 
    77        t.plan(3); 
    88        var layer = { 
    99            styleMap: {createSymbolizer: function(){}}, 
    1010            events: { 
    11                 on: function() {} 
     11                on: function() {}, 
     12                un: function() {} 
    1213            } 
    1314        }; 
     
    2324        t.eq(control.mode, OpenLayers.Control.ModifyFeature.RESHAPE, 
    2425             "constructor initializes modification mode correctly"); 
    25     } 
    26  
    27     function test_ModifyFeature_destroy(t) { 
     26        control.destroy(); 
     27    } 
     28 
     29    function test_destroy(t) { 
    2830        t.plan(2); 
    2931        var map = new OpenLayers.Map("map"); 
     
    4042        } 
    4143        control.destroy(); 
    42     } 
    43      
    44     function test_ModifyFeature_activate(t) { 
     44        map.destroy(); 
     45    } 
     46     
     47    function test_activate(t) { 
    4548        t.plan(2); 
    4649        var map = new OpenLayers.Map("map"); 
     
    5457        t.ok(control.selectControl.active, 
    5558             "select control is active after activating control"); 
    56     } 
    57  
    58     function test_ModifyFeature_initDeleteCodes(t) { 
     59 
     60        map.destroy(); 
     61    } 
     62 
     63    function test_initDeleteCodes(t) { 
    5964        t.plan(3); 
    6065        var layer = new OpenLayers.Layer.Vector(); 
     
    6570        t.eq(control.deleteCodes[1], 100, "Default deleteCodes include 'd'");  
    6671         
    67     } 
    68      
    69     function test_ModifyFeature_handleKeypress(t) { 
    70         t.plan(8); 
     72        control.destroy(); 
     73        layer.destroy(); 
     74    } 
     75     
     76    function test_handleKeypress(t) { 
     77        t.plan(10); 
    7178 
    7279        /** 
     
    7885         */ 
    7986         
    80         var control = new OpenLayers.Control.ModifyFeature({ 
    81             styleMap: {createSymbolizer: function(){}}, 
    82             events: { 
    83                 on: function() {} 
    84             } 
    85         }); 
     87        var layer = new OpenLayers.Layer.Vector(); 
     88        var control = new OpenLayers.Control.ModifyFeature(layer); 
    8689        var delKey = 46; 
    8790        var dKey = 100; 
    8891        control.deleteCodes = [delKey, dKey]; 
    89  
    90         //// test that point is deleted for all delete codes 
    91         //var point = new OpenLayers.Feature.Vector( 
    92         //    new OpenLayers.Geometry.Point() 
    93         //); 
    94         //// mock up deletion before dragging (but after selection) 
    95         //control.dragControl.feature = null; 
    96         //control.feature = point; 
    97         //var oldUnselect = control.unselectFeature; 
    98         //control.unselectFeature = function(feature) { 
    99         //    t.eq(feature.id, point.id, 
    100         //         "point deletion before drag: unselectFeature called with the correct feature"); 
    101         //}; 
    102         //control.layer = { 
    103         //    removeFeatures: function(features) { 
    104         //        t.ok(features.length == 1, 
    105         //             "point deletion before drag: removeFeatures called with a single feature"); 
    106         //        t.eq(features[0].id, point.id, 
    107         //             "point deletion before drag: removeFeatures called with the correct feature"); 
    108         //    } 
    109         //}; 
    110         //control.onDelete = function(feature) { 
    111         //    t.eq(feature.id, point.id, 
    112         //         "point deletion before drag: onDelete called with the correct feature"); 
    113         //}; 
    114         //// run the above four tests twice 
    115         //control.handleKeypress(delKey); 
    116         //control.handleKeypress(dKey); 
    117         //// reset modified methods 
    118         //control.unselectFeatures = oldUnselect; 
    119         //control.onDelete = function() {}; 
    120         // 
    121         //// mock up deletion during dragging - these repeat the above tests 
    122         //control.dragControl.feature = point; 
    123         //control.feature = point; 
    124         //var oldUnselect = control.unselectFeature; 
    125         //control.unselectFeature = function(feature) { 
    126         //    t.eq(feature.id, point.id, 
    127         //         "point deletion during drag: unselectFeature called with the correct feature"); 
    128         //}; 
    129         //control.layer = { 
    130         //    removeFeatures: function(features) { 
    131         //        t.ok(features.length == 1, 
    132         //             "point deletion during drag: removeFeatures called with a single feature"); 
    133         //        t.eq(features[0].id, point.id, 
    134         //             "point deletion during drag: removeFeatures called with the correct feature"); 
    135         //    } 
    136         //}; 
    137         //control.onDelete = function(feature) { 
    138         //    t.eq(feature.id, point.id, 
    139         //         "point deletion during drag: onDelete called with the correct feature"); 
    140         //}; 
    141         //// run the above four tests twice 
    142         //control.handleKeypress(delKey); 
    143         //control.handleKeypress(dKey); 
    144         //// reset modified methods 
    145         //control.unselectFeatures = oldUnselect; 
    146         //control.onDelete = function() {}; 
    14792         
    14893        // test that a polygon vertex is deleted for all delete codes 
     
    164109            } 
    165110        }; 
    166         control.layer =
    167             drawFeature: function(feature) { 
    168                 t.eq(feature.id, poly.id, 
    169                      "vertex deletion: drawFeature called with the proper feature"); 
    170             } 
    171         }
    172         var oldReset = control.resetVertices
     111        layer.events.on({"featuremodified": function(event)
     112            t.eq(event.feature.id, poly.id, "vertex deletion: featuremodifed triggered"); 
     113        }}); 
     114        layer.drawFeature = function(feature) { 
     115            t.eq(feature.id, poly.id, 
     116                 "vertex deletion: drawFeature called with the proper feature")
     117        }
    173118        control.resetVertices = function() { 
    174119            t.ok(true, "vertex deletion: resetVertices called"); 
     
    186131        control.handleKeypress(delKey); 
    187132 
    188         // reset modified methods 
    189         control.onModification = function() {}
    190          
     133        // clean up 
     134        control.destroy()
     135        layer.destroy(); 
    191136    }     
    192137         
    193138 
    194     function test_ModifyFeature_onUnSelect(t) { 
    195         t.plan(5); 
     139    function test_onUnSelect(t) { 
     140        t.plan(6); 
    196141        var layer = new OpenLayers.Layer.Vector(); 
    197142        var control = new OpenLayers.Control.ModifyFeature(layer); 
     
    200145        control.virtualVertices = 'b'; 
    201146        control.features = true; 
     147        layer.events.on({"afterfeaturemodified": function(event) { 
     148            t.eq(event.feature, fakeFeature, "afterfeaturemodified triggered"); 
     149        }}); 
    202150        control.dragControl.deactivate = function() { t.ok(true, "Deactivate called on drag control"); } 
    203151        control.onModificationEnd = function (feature) { t.eq(feature.id, fakeFeature.id, "onModificationEnd got feature.") } 
     
    210158        control.unselectFeature({feature: fakeFeature}); 
    211159        t.eq(control.feature, null, "feature is set to null"); 
     160         
     161        layer.destroyFeatures = function() {};         
     162        control.destroy(); 
     163        layer.destroy(); 
    212164    }     
    213     function test_ModifyFeature_selectFeature(t) { 
    214         t.plan(12); 
     165 
     166    function test_selectFeature(t) { 
     167        t.plan(15); 
    215168        var layer = new OpenLayers.Layer.Vector(); 
    216169        var control = new OpenLayers.Control.ModifyFeature(layer); 
    217170        control.vertices = []; 
    218171        control.virtualVertices = []; 
     172        layer.events.on({"beforefeaturemodified": function(event) { 
     173            t.eq(event.feature, fakeFeature, "beforefeaturemodified triggered"); 
     174        }}); 
    219175        control.dragControl.activate = function() { t.ok(true, "drag Control activated"); } 
    220176        control.onModificationStart = function(feature)  { t.eq(feature.id, fakeFeature.id, "On Modification Start called with correct feature."); }  
     
    257213        control.selectFeature({feature: fakeFeature}); 
    258214         
     215        control.destroy(); 
     216        layer.destroy(); 
    259217    }   
    260218 
    261     function test_ModifyFeature_resetVertices(t) { 
     219    function test_resetVertices(t) { 
    262220        t.plan(18); 
    263221        var layer = new OpenLayers.Layer.Vector(); 
     
    327285    }     
    328286         
    329     function test_ModifyFeature_onDrag(t) { 
     287    function test_onDrag(t) { 
    330288        t.plan(1); 
    331289        t.ok(true, "onDrag not tested yet."); 
    332290    } 
    333291     
    334     function test_ModifyFeature_dragComplete(t) { 
    335         t.plan(6); 
     292    function test_dragComplete(t) { 
     293        t.plan(7); 
    336294        var layer = new OpenLayers.Layer.Vector(); 
    337295        var control = new OpenLayers.Control.ModifyFeature(layer); 
     
    347305            t.ok(verts == 'previous virtual' || verts == 'previous normal', verts + " verts correct"); 
    348306        } 
     307        layer.events.on({"featuremodified": function(event) { 
     308            t.eq(event.feature, fakeFeature, "featuremodified triggered"); 
     309        }}); 
    349310        control.onModification = function(feat) { 
    350311            t.eq(feat.id, fakeFeature.id, "onModification gets correct feat"); 
     
    361322        control.virtualVertices = 'previous virtual'; 
    362323        control.dragComplete(); 
    363     } 
    364      
    365     function test_ModifyFeature_deactivate(t) { 
     324         
     325        control.destroy(); 
     326        layer.destroy(); 
     327    } 
     328     
     329    function test_deactivate(t) { 
    366330        t.plan(2); 
    367331        var map = new OpenLayers.Map("map"); 
     
    381345        control.active = true; 
    382346        control.deactivate(); 
    383     } 
    384  
    385     function test_ModifyFeature_onModificationStart(t) { 
    386         t.plan(1); 
     347         
     348        map.destroy(); 
     349    } 
     350 
     351    function test_onModificationStart(t) { 
     352        t.plan(2); 
    387353        var map = new OpenLayers.Map("map"); 
    388354        var layer = new OpenLayers.Layer.Vector(); 
     
    391357        map.addControl(control); 
    392358        control.activate(); 
     359         
     360        // test that beforefeaturemodified is triggered 
     361        layer.events.on({"beforefeaturemodified": function(event) { 
     362            t.eq(event.feature.id, testFeature.id, 
     363                 "beforefeaturemodified is triggered with correct feature"); 
     364        }}); 
    393365         
    394366        // make sure onModificationStart is called on feature selection 
     
    401373        }; 
    402374        control.selectFeature({feature: testFeature}); 
    403     } 
    404      
    405     function test_ModifyFeature_onModification(t) { 
    406         t.plan(2); 
     375         
     376        map.destroy(); 
     377    } 
     378     
     379    function test_onModification(t) { 
     380        t.plan(3); 
    407381        var map = new OpenLayers.Map("map"); 
    408382        var layer = new OpenLayers.Layer.Vector(); 
     
    433407        control.feature = poly; 
    434408        control.vertices = [point]; 
     409        layer.events.on({"featuremodified": function(event) { 
     410            t.eq(event.feature.id, poly.id, "featuremodified triggered"); 
     411        }}); 
    435412        control.onModification = function(feature) { 
    436413            t.eq(feature.id, poly.id, 
     
    442419        layer.drawFeature = oldDraw; 
    443420         
    444     } 
    445  
    446     function test_ModifyFeature_onModificationEnd(t) { 
    447         t.plan(1); 
     421        map.destroy(); 
     422    } 
     423 
     424    function test_onModificationEnd(t) { 
     425        t.plan(2); 
    448426        var map = new OpenLayers.Map("map"); 
    449427        var layer = new OpenLayers.Layer.Vector(); 
     
    457435            new OpenLayers.Geometry.Point(Math.random(), Math.random()) 
    458436        ); 
     437        layer.events.on({"afterfeaturemodified": function(event) { 
     438            t.eq(event.feature.id, testFeature.id, "afterfeaturemodified triggered"); 
     439        }}); 
    459440        control.onModificationEnd = function(feature) { 
    460441            t.eq(feature.id, testFeature.id, 
     
    462443        }; 
    463444        control.unselectFeature({feature: testFeature}); 
    464     } 
    465  
    466  
    467     //function t//est_ModifyFeature_onDelete(t) { 
    468     //    t.plan(2); 
    469     //    var map = new OpenLayers.Map("map"); 
    470     //    var layer = new OpenLayers.Layer.Vector(); 
    471     //    map.addLayer(layer); 
    472     //    var control = new OpenLayers.Control.ModifyFeature(layer); 
    473     //    map.addControl(control); 
    474     //    control.activate(); 
    475     //     
    476     //    // make sure onDelete is called on point deletion (before dragging) 
    477     //    var point = new OpenLayers.Feature.Vector( 
    478     //        new OpenLayers.Geometry.Point(Math.random(), Math.random()) 
    479     //    ); 
    480     //    control.feature = point; 
    481     //    control.onDelete = function(feature) { 
    482     //        t.eq(feature.id, point.id, 
    483     //            "onDelete called with the right feature before drag"); 
    484     //    }; 
    485     //    control.handleKeypress(46); 
    486     // 
    487     //    // make sure onDelete is called on point deletion (during dragging) 
    488     //    var point = new OpenLayers.Feature.Vector( 
    489     //        new OpenLayers.Geometry.Point(Math.random(), Math.random()) 
    490     //    ); 
    491     //    control.dragControl.feature = point; 
    492     //    control.feature = point; 
    493     //    control.onDelete = function(feature) { 
    494     //        t.eq(feature.id, point.id, 
    495     //            "onDelete called with the right feature during drag"); 
    496     //    }; 
    497     //    control.handleKeypress(46); 
    498     // 
    499     //} 
     445         
     446        map.destroy(); 
     447    } 
     448 
    500449 
    501450    </script>