Adding a box property to the SelectFeature control for selecting all features within a box. p=pgiraud, r=me,crschmidt (closes #1071)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@7589 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -59,6 +59,12 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
*/
|
||||
hover: false,
|
||||
|
||||
/**
|
||||
* APIProperty: box
|
||||
* {Boolean} Allow feature selection by drawing a box.
|
||||
*/
|
||||
box: false,
|
||||
|
||||
/**
|
||||
* APIProperty: onSelect
|
||||
* {Function} Optional function to be called when a feature is selected.
|
||||
@@ -88,7 +94,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
|
||||
/**
|
||||
* APIProperty: callbacks
|
||||
* {Object} The functions that are sent to the handler for callback
|
||||
* {Object} The functions that are sent to the handlers.feature for callback
|
||||
*/
|
||||
callbacks: null,
|
||||
|
||||
@@ -106,10 +112,11 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
renderIntent: "select",
|
||||
|
||||
/**
|
||||
* Property: handler
|
||||
* {<OpenLayers.Handler.Feature>}
|
||||
* Property: handlers
|
||||
* {Object} Object with references to multiple <OpenLayers.Handler>
|
||||
* instances.
|
||||
*/
|
||||
handler: null,
|
||||
handlers: null,
|
||||
|
||||
/**
|
||||
* Constructor: <OpenLayers.Control.SelectFeature>
|
||||
@@ -127,10 +134,56 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
over: this.overFeature,
|
||||
out: this.outFeature
|
||||
}, this.callbacks);
|
||||
var handlerOptions = { geometryTypes: this.geometryTypes};
|
||||
this.handler = new OpenLayers.Handler.Feature(this, layer,
|
||||
this.callbacks,
|
||||
handlerOptions);
|
||||
this.handlers = {
|
||||
feature: new OpenLayers.Handler.Feature(
|
||||
this, layer, this.callbacks, {geometryTypes: this.geometryTypes}
|
||||
)
|
||||
};
|
||||
|
||||
if (this.box) {
|
||||
this.handlers.box = new OpenLayers.Handler.Box(
|
||||
this, {done: this.selectBox},
|
||||
{boxDivClassName: "olHandlerBoxSelectFeature"}
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: activate
|
||||
* Activates the control.
|
||||
*
|
||||
* Returns:
|
||||
* {Boolean} The control was effectively activated.
|
||||
*/
|
||||
activate: function () {
|
||||
if (!this.active) {
|
||||
this.handlers.feature.activate();
|
||||
if(this.box && this.handlers.box) {
|
||||
this.handlers.box.activate();
|
||||
}
|
||||
}
|
||||
return OpenLayers.Control.prototype.activate.apply(
|
||||
this, arguments
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: deactivate
|
||||
* Deactivates the control.
|
||||
*
|
||||
* Returns:
|
||||
* {Boolean} The control was effectively deactivated.
|
||||
*/
|
||||
deactivate: function () {
|
||||
if (this.active) {
|
||||
this.handlers.feature.deactivate();
|
||||
if(this.handlers.box) {
|
||||
this.handlers.box.deactivate();
|
||||
}
|
||||
}
|
||||
return OpenLayers.Control.prototype.deactivate.apply(
|
||||
this, arguments
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -188,7 +241,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
* {Boolean} Allow for multiple selected features.
|
||||
*/
|
||||
multipleSelect: function() {
|
||||
return this.multiple || this.handler.evt[this.multipleKey];
|
||||
return this.multiple || this.handlers.feature.evt[this.multipleKey];
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -200,7 +253,7 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
* {Boolean} Toggle the selected state of a feature.
|
||||
*/
|
||||
toggleSelect: function() {
|
||||
return this.toggle || this.handler.evt[this.toggleKey];
|
||||
return this.toggle || this.handlers.feature.evt[this.toggleKey];
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -279,6 +332,49 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
this.layer.events.triggerEvent("featureunselected", {feature: feature});
|
||||
this.onUnselect(feature);
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: selectBox
|
||||
* Callback from the handlers.box set up when <box> selection is true
|
||||
* on.
|
||||
*
|
||||
* Parameters:
|
||||
* position - {<OpenLayers.Bounds> || <OpenLayers.Pixel> }
|
||||
*/
|
||||
selectBox: function(position) {
|
||||
if (position instanceof OpenLayers.Bounds) {
|
||||
var minXY = this.map.getLonLatFromPixel(
|
||||
new OpenLayers.Pixel(position.left, position.bottom)
|
||||
);
|
||||
var maxXY = this.map.getLonLatFromPixel(
|
||||
new OpenLayers.Pixel(position.right, position.top)
|
||||
);
|
||||
var bounds = new OpenLayers.Bounds(
|
||||
minXY.lon, minXY.lat, maxXY.lon, maxXY.lat
|
||||
);
|
||||
|
||||
// if multiple is false, first deselect currently selected features
|
||||
if (!this.multipleSelect()) {
|
||||
this.unselectAll();
|
||||
}
|
||||
|
||||
// because we're using a box, we consider we want multiple selection
|
||||
var prevMultiple = this.multiple;
|
||||
this.multiple = true;
|
||||
for(var i=0, len = this.layer.features.length; i<len; ++i) {
|
||||
var feature = this.layer.features[i];
|
||||
if (this.geometryTypes == null || OpenLayers.Util.indexOf(
|
||||
this.geometryTypes, feature.geometry.CLASS_NAME) > -1) {
|
||||
if (bounds.toGeometry().intersects(feature.geometry)) {
|
||||
if (OpenLayers.Util.indexOf(this.layer.selectedFeatures, feature) == -1) {
|
||||
this.select(feature);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
this.multiple = prevMultiple;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Method: setMap
|
||||
@@ -288,7 +384,10 @@ OpenLayers.Control.SelectFeature = OpenLayers.Class(OpenLayers.Control, {
|
||||
* map - {<OpenLayers.Map>}
|
||||
*/
|
||||
setMap: function(map) {
|
||||
this.handler.setMap(map);
|
||||
this.handlers.feature.setMap(map);
|
||||
if (this.box) {
|
||||
this.handlers.box.setMap(map);
|
||||
}
|
||||
OpenLayers.Control.prototype.setMap.apply(this, arguments);
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user