[OpenLayers-Users] DeleteFeature.js --> on click and ctrl+click

Alexandre Dube adube at mapgears.com
Mon Nov 24 14:47:46 EST 2008


Hi list,

  I asked on Friday about keyMask and got it working but unfortunately 
it couldn't help in the end.  I wanted my control to have those behaviors :
  - on single click : "unselect" all features, then only select the 
clicked feature, or only "unselect" the clicked feature if that was to 
one selected.
  - on ctrl+click : select/unselect multiple features at the same time.

  I couldn't figure how to make that work with keyMask. It seemed that I 
would not be able to make both ways working ( click with an without ctrl 
key ) at the same time. 

  So, instead I look at control.handler.evt.ctrlKey directly.  See the 
code below if you're interested in comments about it.

Alexandre


/**
 * Class: DeleteFeature
 */
OpenLayers.Control.DeleteFeature = OpenLayers.Class(OpenLayers.Control, {
    features: null,
    initialize: function(layer, options) {
        OpenLayers.Control.prototype.initialize.apply(this, [options]);
        this.features = [];
        this.layer = layer;
        this.handler = new OpenLayers.Handler.Feature(
            this, layer,
            { click: this.clickFeature }//,
            //{ keyMask:OpenLayers.Handler.MOD_CTRL }
        );
    },
    clickFeature: function(feature) {
        if(this.handler.evt.ctrlKey) { // multiple features selection
            // if feature doesn't have a fid, destroy it
            if(feature.fid == undefined) {
                removeRoadInfoPopup(oLastHoverFeature); // HARDCODED adube
                oLastHoverFeature = null; // HARDCODED adube
                this.layer.destroyFeatures([feature]);
            } else if (feature.state != OpenLayers.State.DELETE){
                this.features.push(feature);
                feature.state = OpenLayers.State.DELETE;
                this.layer.events.triggerEvent("afterfeaturemodified",
                                               {feature: feature});
                feature.renderIntent = "select";
                this.layer.drawFeature(feature);
            } else {
                this.features =
                    OpenLayers.Util.removeItem(this.features, feature);
                feature.state = null;
                feature.renderIntent = "default";
                this.layer.drawFeature(feature);
            }
        } else { // single feature selection
            // unselect all features but current if already selected
            if (this.features.length == 1 &&
                this.features[0].fid == feature.fid){
                // do nothing, this feature will be removed below
            } else {
                for(i=0; i<this.features.length; i++){
                    this.features[i].state = null;
                    this.features[i].renderIntent = "default";
                    this.layer.drawFeature(this.features[i]);
                }
                this.features = [];
            }

            if(feature.fid == undefined) {
                removeRoadInfoPopup(oLastHoverFeature); // HARDCODED adube
                oLastHoverFeature = null; // HARDCODED adube
                this.layer.destroyFeatures([feature]);
            } else if (feature.state != OpenLayers.State.DELETE){
                this.features.push(feature);
                feature.state = OpenLayers.State.DELETE;
                this.layer.events.triggerEvent("afterfeaturemodified",
                                               {feature: feature});
                feature.renderIntent = "select";
                this.layer.drawFeature(feature);
            } else {
                this.features =
                    OpenLayers.Util.removeItem(this.features, feature);
                feature.state = null;
                feature.renderIntent = "default";
                this.layer.drawFeature(feature);
            }
        }
    },
    setMap: function(map) {
        this.handler.setMap(map);
        OpenLayers.Control.prototype.setMap.apply(this, arguments);
    },
    CLASS_NAME: "OpenLayers.Control.DeleteFeature"
});


-- 
Alexandre Dubé
Mapgears
www.mapgears.com




More information about the Users mailing list