replacing the select handler with a feature handler - with support for up, down, over, out, and move callbacks associated with mouse events on a feature - the select-feature.html example shows the use of a control that activates the feature handler - see #544

git-svn-id: http://svn.openlayers.org/trunk/openlayers@2843 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2007-03-21 23:30:20 +00:00
parent 5bb0a3087e
commit 0c8dcabce8
4 changed files with 138 additions and 58 deletions

View File

@@ -4,14 +4,15 @@
/**
* Handler to draw a path on the map. Polygon is displayed on mouse down,
* moves on mouse move, and is finished on mouse up.
* Handler to respond to mouse events related to a drawn feature.
* Callbacks will be called for over, move, out, up, and down (corresponding
* to the equivalent mouse events).
*
* @class
* @requires OpenLayers/Handler.js
*/
OpenLayers.Handler.Select = OpenLayers.Class.create();
OpenLayers.Handler.Select.prototype =
OpenLayers.Handler.Feature = OpenLayers.Class.create();
OpenLayers.Handler.Feature.prototype =
OpenLayers.Class.inherit(OpenLayers.Handler, {
/**
@@ -19,6 +20,11 @@ OpenLayers.Handler.Select.prototype =
*/
layerIndex: null,
/**
* @type {OpenLayers.Geometry}
*/
geometry: null,
/**
* @constructor
*
@@ -36,16 +42,19 @@ OpenLayers.Handler.Select.prototype =
},
/**
* Handle mouse down. Call the "up" callback if down on a feature.
* Handle mouse down. Call the "down" callback if down on a feature.
*
* @param {Event} evt
*/
mousedown: function(evt) {
return this.select('down', evt);
var selected = this.select('down', evt);
return !selected; // stop event propagation if selected
},
/**
* Handle mouse moves. Call the "over" callback if over a feature.
* Handle mouse moves. Call the "move" callback if moving over a feature.
* Call the "over" callback if moving over a feature for the first time.
* Call the "out" callback if moving off of a feature.
*
* @param {Event} evt
*/
@@ -55,37 +64,57 @@ OpenLayers.Handler.Select.prototype =
},
/**
* Handle mouse moves. Call the "down" callback if up on a feature.
* Handle mouse moves. Call the "up" callback if up on a feature.
*
* @param {Event} evt
*/
mouseup: function(evt) {
return this.select('up', evt);
var selected = this.select('up', evt);
return !selected; // stop event propagation if selected
},
/**
* Capture double-clicks.
* Capture double-clicks. Let the event continue propagating if the
* double-click doesn't hit a geometry. Otherwise call the dblclick
* callback.
*
* @param {Event} evt
*/
dblclick: function(evt) {
return false;
var selected = this.select('dblclick', evt);
return !selected; // stop event propagation if selected
},
/**
* Trigger the appropriate callback if a feature is under the mouse.
*
* @param {String} type Callback key
* @type {Boolean} A feature was selected
*/
select: function(type, evt) {
var geometry = this.layer.renderer.getGeometryFromEvent(evt);
if(geometry) {
if (geometry.parent) {
geometry = geometry.parent;
}
// three cases:
// over a new, out of the last and over a new, or still on the last
if(!this.geometry) {
// over a new geometry
this.callback('over', [geometry]);
} else if(this.geometry != geometry) {
// out of the last and over a new
this.callback('out', [this.geometry]);
this.callback('over', [geometry]);
}
this.geometry = geometry;
this.callback(type, [geometry]);
return false; // stop event propagation
return true;
} else {
if(this.geometry) {
// out of the last
this.callback('out', [this.geometry]);
this.geometry = null;
}
return false;
}
return true;
},
/**
@@ -118,5 +147,5 @@ OpenLayers.Handler.Select.prototype =
},
/** @final @type String */
CLASS_NAME: "OpenLayers.Handler.Select"
CLASS_NAME: "OpenLayers.Handler.Feature"
});