[OpenLayers-Dev] DeleteFeature control

Alexandre Dube adube at mapgears.com
Tue Jul 21 08:32:05 EDT 2009


Hi,

  I'm trying to reproduce what we discuss.  I'm not really familiar with 
this method and I'm having difficulties with 'this'.  Here's what I have 
so far :

/*** START ***/
var delCtrl = new OpenLayers.Control({
    layer: olWFSRoads,
      handlers: {"keyboard": new OpenLayers.Handler.Keyboard(this,{ // 
<------- 'this' is not the control
            keydown: function(evt) {
            var code = evt.keyCode;
            //var features = this.layer.selectedFeatures; // <------- 
this is not the control, and so on...
            var features = olWFSRoads.selectedFeatures; // <----- can we 
avoid the use of olWFSRoads ?

            if(code == OpenLayers.Event.KEY_DELETE && features.length > 0 &&
               this.confirmDelete() ) {
                this.deleteFeatures(features);
                return;
            }
        }
      })},
    controls: [
        new OpenLayers.Control.SelectFeature(
            olWFSRoads, {
                onSelect: function(f) {
                    f.state = OpenLayers.State.DELETE;
                    this.layer.drawFeature(f);
                }
        })
    ],
    confirmDelete: function() {
        return (confirm("Delete selected features ?"))
    },
    deleteFeatures: function(features) {
        this.layer.destroyFeatures(features);
    }
});
/*** END ***/

I made the discussed changes to the OL.Control to be able to automatize 
activation/deactivation/setMap.

If you look at the <---------- arrow, you'll see a 'this', which is 
supposed to be the control, owner of the handler.  Well, that 'this' is 
not the control, it's the html page (figures).  I tried to put 'delCtrl' 
instead but it didn't work.  I really wonder how I am supposed to create 
controls that way (without creating a class) and use the 'this' (or 
something else).

Any hint would be greatly appreciated,

Alexandre

P.S. Also, I believe it's ok to have my 'user' functions, like 
confirmDelete directly inside the control since the control is 
completely custom-made (i.e. not using events).

Alexandre Dube wrote:
> Eric Lemoine wrote:
>   
>> so, to get to the delete control you've been working on, I see more
>> value, in terms of flexibility, in adding "control composition" to the
>> control base class.
>>
>>
>>   
>>     
>
> I also agree.  Originally, the control had a Feature handler instead of 
> a SelectFeature control.  Looking at it now, it's too simple to be 
> justified as a new control.
>
>   
>>>> About the 'controls' property and the act./dea. automatism : that's a good idea, but wouldn't it be good to have a 'handlers' property also and activate/deactivate all handlers as well ?  Currently, control has a 'handler' property only.  Would it make sense to have 'handlers' added a do the same kind of automatism as the new 'controls' property ?
>>>>       
>>>>         
>> Agreed.
>>
>>   
>>     
>
> Great.
>
>
>   


-- 
Alexandre Dubé
Mapgears
www.mapgears.com




More information about the Dev mailing list