Adding support for a modify callback in the sketch handlers. This lets controls know about each sketch modification as a sketch is being drawn. Also a bit of a refactor of the sketch handlers to share sketch geometry parts. r=ahocevar (closes #1903)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8831 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-02-04 22:45:16 +00:00
parent 112debe5f5
commit f4bae0a011
6 changed files with 352 additions and 103 deletions
+71 -48
View File
@@ -48,17 +48,20 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* Create a new path hander * Create a new path hander
* *
* Parameters: * Parameters:
* control - {<OpenLayers.Control>} * control - {<OpenLayers.Control>} The control that owns this handler
* callbacks - {Object} An object with a 'done' property whos value is a * callbacks - {Object} An object with a properties whose values are
* function to be called when the path drawing is finished. The * functions. Various callbacks described below.
* callback should expect to recieve a single argument, the line
* string geometry. If the callbacks object contains a 'point'
* property, this function will be sent each point as they are added.
* If the callbacks object contains a 'cancel' property, this function
* will be called when the handler is deactivated while drawing. The
* cancel should expect to receive a geometry.
* options - {Object} An optional object with properties to be set on the * options - {Object} An optional object with properties to be set on the
* handler * handler
*
* Named callbacks:
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the linestring geometry.
* point - Called as each point is added. Receives the new point geometry.
* modify - Called with each move of a vertex with the vertex (point)
* geometry and the sketch feature.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/ */
initialize: function(control, callbacks, options) { initialize: function(control, callbacks, options) {
OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments); OpenLayers.Handler.Point.prototype.initialize.apply(this, arguments);
@@ -67,12 +70,19 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
/** /**
* Method: createFeature * Method: createFeature
* Add temporary geometries * Add temporary geometries
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
* feature.
*/ */
createFeature: function() { createFeature: function(pixel) {
this.line = new OpenLayers.Feature.Vector( var lonlat = this.control.map.getLonLatFromPixel(pixel);
new OpenLayers.Geometry.LineString());
this.point = new OpenLayers.Feature.Vector( this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()); new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LineString([this.point.geometry])
);
this.layer.addFeatures([this.line, this.point], {silent: true}); this.layer.addFeatures([this.line, this.point], {silent: true});
}, },
@@ -86,12 +96,12 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
}, },
/** /**
* Method: destroyPoint * Method: removePoint
* Destroy the temporary point. * Destroy the temporary point.
*/ */
destroyPoint: function() { removePoint: function() {
if(this.point) { if(this.point) {
this.layer.destroyFeatures([this.point]); this.layer.removeFeatures([this.point]);
} }
}, },
@@ -99,11 +109,22 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
* Method: addPoint * Method: addPoint
* Add point to geometry. Send the point index to override * Add point to geometry. Send the point index to override
* the behavior of LinearRing that disregards adding duplicate points. * the behavior of LinearRing that disregards adding duplicate points.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The pixel location for the new point.
*/ */
addPoint: function() { addPoint: function(pixel) {
this.line.geometry.addComponent(this.point.geometry.clone(), this.layer.removeFeatures([this.point]);
this.line.geometry.components.length); var lonlat = this.control.map.getLonLatFromPixel(pixel);
this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line.geometry.addComponent(
this.point.geometry, this.line.geometry.components.length
);
this.callback("point", [this.point.geometry, this.getGeometry()]); this.callback("point", [this.point.geometry, this.getGeometry()]);
this.callback("modify", [this.point.geometry, this.getSketch()]);
this.drawFeature();
}, },
/** /**
@@ -121,14 +142,20 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
/** /**
* Method: modifyFeature * Method: modifyFeature
* Modify the existing geometry given the new point * Modify the existing geometry given the new point
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The updated pixel location for the latest
* point.
*/ */
modifyFeature: function() { modifyFeature: function(pixel) {
var index = this.line.geometry.components.length - 1; var lonlat = this.control.map.getLonLatFromPixel(pixel);
this.line.geometry.components[index].x = this.point.geometry.x; this.point.geometry.x = lonlat.lon;
this.line.geometry.components[index].y = this.point.geometry.y; this.point.geometry.y = lonlat.lat;
this.line.geometry.components[index].clearBounds(); this.callback("modify", [this.point.geometry, this.getSketch()]);
this.point.geometry.clearBounds();
this.drawFeature();
}, },
/** /**
* Method: drawFeature * Method: drawFeature
* Render geometries on the temporary layer. * Render geometries on the temporary layer.
@@ -138,6 +165,17 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.layer.drawFeature(this.point, this.style); this.layer.drawFeature(this.point, this.style);
}, },
/**
* Method: getSketch
* Return the sketch feature.
*
* Returns:
* {<OpenLayers.Feature.Vector>}
*/
getSketch: function() {
return this.line;
},
/** /**
* Method: getGeometry * Method: getGeometry
* Return the sketch geometry. If <multi> is true, this will return * Return the sketch geometry. If <multi> is true, this will return
@@ -174,18 +212,12 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
if(this.persist) { if(this.persist) {
this.destroyFeature(); this.destroyFeature();
} }
this.createFeature(); this.createFeature(evt.xy);
} else if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
this.addPoint(evt.xy);
} }
this.mouseDown = true; this.mouseDown = true;
this.lastDown = evt.xy; this.lastDown = evt.xy;
var lonlat = this.control.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
if((this.lastUp == null) || !this.lastUp.equals(evt.xy)) {
this.addPoint();
}
this.drawFeature();
this.drawing = true; this.drawing = true;
return false; return false;
}, },
@@ -203,16 +235,11 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
*/ */
mousemove: function (evt) { mousemove: function (evt) {
if(this.drawing) { if(this.drawing) {
var lonlat = this.map.getLonLatFromPixel(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
if(this.mouseDown && this.freehandMode(evt)) { if(this.mouseDown && this.freehandMode(evt)) {
this.addPoint(); this.addPoint(evt.xy);
} else { } else {
this.modifyFeature(); this.modifyFeature(evt.xy);
} }
this.drawFeature();
} }
return true; return true;
}, },
@@ -232,13 +259,11 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
this.mouseDown = false; this.mouseDown = false;
if(this.drawing) { if(this.drawing) {
if(this.freehandMode(evt)) { if(this.freehandMode(evt)) {
if(this.persist) { this.removePoint();
this.destroyPoint();
}
this.finalize(); this.finalize();
} else { } else {
if(this.lastUp == null) { if(this.lastUp == null) {
this.addPoint(); this.addPoint(evt.xy);
} }
this.lastUp = evt.xy; this.lastUp = evt.xy;
} }
@@ -262,9 +287,7 @@ OpenLayers.Handler.Path = OpenLayers.Class(OpenLayers.Handler.Point, {
if(!this.freehandMode(evt)) { if(!this.freehandMode(evt)) {
var index = this.line.geometry.components.length - 1; var index = this.line.geometry.components.length - 1;
this.line.geometry.removeComponent(this.line.geometry.components[index]); this.line.geometry.removeComponent(this.line.geometry.components[index]);
if(this.persist) { this.removePoint();
this.destroyPoint();
}
this.finalize(); this.finalize();
} }
return false; return false;
+35 -20
View File
@@ -11,9 +11,11 @@
/** /**
* Class: OpenLayers.Handler.Point * Class: OpenLayers.Handler.Point
* Handler to draw a point on the map. Point is displayed on mouse down, * Handler to draw a point on the map. Point is displayed on mouse down,
* moves on mouse move, and is finished on mouse up. The handler triggers * moves on mouse move, and is finished on mouse up. The handler triggers
* callbacks for 'done' and 'cancel'. Create a new instance with the * callbacks for 'done', 'cancel', and 'modify'. The modify callback is
* <OpenLayers.Handler.Point> constructor. * called with each change in the sketc and will receive the latest point
* drawn. Create a new instance with the <OpenLayers.Handler.Point>
* constructor.
* *
* Inherits from: * Inherits from:
* - <OpenLayers.Handler> * - <OpenLayers.Handler>
@@ -84,19 +86,23 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
* *
* Parameters: * Parameters:
* control - {<OpenLayers.Control>} The control that owns this handler * control - {<OpenLayers.Control>} The control that owns this handler
* callbacks - {Object} An object with a 'done' property whose value is a * callbacks - {Object} An object with a properties whose values are
* function to be called when the point drawing is finished. * functions. Various callbacks described below.
* The callback should expect to recieve a single argument,
* the point geometry. If the callbacks object contains a
* 'cancel' property, this function will be called when the
* handler is deactivated while drawing. The cancel should
* expect to receive a geometry.
* options - {Object} An optional object with properties to be set on the * options - {Object} An optional object with properties to be set on the
* handler * handler
*
* Named callbacks:
* done - Called when the point drawing is finished. The callback will
* recieve a single argument, the point geometry.
* modify - Called with each move of a vertex with the vertex (point)
* geometry and the sketch feature.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/ */
initialize: function(control, callbacks, options) { initialize: function(control, callbacks, options) {
// TBD: deal with style if(!(options && options.layerOptions && options.layerOptions.styleMap)) {
this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {}); this.style = OpenLayers.Util.extend(OpenLayers.Feature.Vector.style['default'], {});
}
OpenLayers.Handler.prototype.initialize.apply(this, arguments); OpenLayers.Handler.prototype.initialize.apply(this, arguments);
}, },
@@ -231,6 +237,21 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
return false; return false;
}, },
/**
* Method: modifyFeature
* Modify the existing geometry given a pixel location.
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} A pixel location on the map.
*/
modifyFeature: function(pixel) {
var lonlat = this.map.getLonLatFromPixel(pixel);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.callback("modify", [this.point.geometry, this.point]);
this.point.geometry.clearBounds();
},
/** /**
* Method: drawFeature * Method: drawFeature
* Render features on the temporary layer. * Render features on the temporary layer.
@@ -294,10 +315,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
} }
this.lastDown = evt.xy; this.lastDown = evt.xy;
this.drawing = true; this.drawing = true;
var lonlat = this.map.getLonLatFromPixel(evt.xy); this.modifyFeature(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
this.drawFeature(); this.drawFeature();
return false; return false;
}, },
@@ -315,10 +333,7 @@ OpenLayers.Handler.Point = OpenLayers.Class(OpenLayers.Handler, {
*/ */
mousemove: function (evt) { mousemove: function (evt) {
if(this.drawing) { if(this.drawing) {
var lonlat = this.map.getLonLatFromPixel(evt.xy); this.modifyFeature(evt.xy);
this.point.geometry.x = lonlat.lon;
this.point.geometry.y = lonlat.lat;
this.point.geometry.clearBounds();
this.drawFeature(); this.drawFeature();
} }
return true; return true;
+40 -34
View File
@@ -30,18 +30,20 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
* Create a Polygon Handler. * Create a Polygon Handler.
* *
* Parameters: * Parameters:
* control - {<OpenLayers.Control>} * control - {<OpenLayers.Control>} The control that owns this handler
* callbacks - {Object} An object with a 'done' property whos value is * callbacks - {Object} An object with a properties whose values are
* a function to be called when the path drawing is * functions. Various callbacks described below.
* finished. The callback should expect to recieve a * options - {Object} An optional object with properties to be set on the
* single argument, the polygon geometry. * handler
* If the callbacks object contains a 'point' *
* property, this function will be sent each point * Named callbacks:
* as they are added. If the callbacks object contains * done - Called when the point drawing is finished. The callback will
* a 'cancel' property, this function will be called when * recieve a single argument, the polygon geometry.
* the handler is deactivated while drawing. The cancel * point - Called as each point is added. Receives the new point geometry.
* should expect to receive a geometry. * modify - Called with each move of a vertex with the vertex (point)
* options - {Object} * geometry and the sketch feature.
* cancel - Called when the handler is deactivated while drawing. The
* cancel callback will receive a geometry.
*/ */
initialize: function(control, callbacks, options) { initialize: function(control, callbacks, options) {
OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments); OpenLayers.Handler.Path.prototype.initialize.apply(this, arguments);
@@ -50,15 +52,22 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
/** /**
* Method: createFeature * Method: createFeature
* Add temporary geometries * Add temporary geometries
*
* Parameters:
* pixel - {<OpenLayers.Pixel>} The initial pixel location for the new
* feature.
*/ */
createFeature: function() { createFeature: function(pixel) {
this.polygon = new OpenLayers.Feature.Vector( var lonlat = this.control.map.getLonLatFromPixel(pixel);
new OpenLayers.Geometry.Polygon());
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LinearRing());
this.polygon.geometry.addComponent(this.line.geometry);
this.point = new OpenLayers.Feature.Vector( this.point = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point()); new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat)
);
this.line = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.LinearRing([this.point.geometry])
);
this.polygon = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Polygon([this.line.geometry])
);
this.layer.addFeatures([this.polygon, this.point], {silent: true}); this.layer.addFeatures([this.polygon, this.point], {silent: true});
}, },
@@ -71,18 +80,6 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
this.polygon = null; this.polygon = null;
}, },
/**
* Method: modifyFeature
* Modify the existing geometry given the new point
*
*/
modifyFeature: function() {
var index = this.line.geometry.components.length - 2;
this.line.geometry.components[index].x = this.point.geometry.x;
this.line.geometry.components[index].y = this.point.geometry.y;
this.line.geometry.components[index].clearBounds();
},
/** /**
* Method: drawFeature * Method: drawFeature
* Render geometries on the temporary layer. * Render geometries on the temporary layer.
@@ -92,6 +89,17 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
this.layer.drawFeature(this.point, this.style); this.layer.drawFeature(this.point, this.style);
}, },
/**
* Method: getSketch
* Return the sketch feature.
*
* Returns:
* {<OpenLayers.Feature.Vector>}
*/
getSketch: function() {
return this.polygon;
},
/** /**
* Method: getGeometry * Method: getGeometry
* Return the sketch geometry. If <multi> is true, this will return * Return the sketch geometry. If <multi> is true, this will return
@@ -121,9 +129,7 @@ OpenLayers.Handler.Polygon = OpenLayers.Class(OpenLayers.Handler.Path, {
// remove the penultimate point // remove the penultimate point
var index = this.line.geometry.components.length - 2; var index = this.line.geometry.components.length - 2;
this.line.geometry.removeComponent(this.line.geometry.components[index]); this.line.geometry.removeComponent(this.line.geometry.components[index]);
if(this.persist) { this.removePoint();
this.destroyPoint();
}
this.finalize(); this.finalize();
} }
return false; return false;
+71
View File
@@ -68,7 +68,78 @@
t.ok(!handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds after dragging without letting go. (Came out as "+handler.line.geometry.getBounds().toBBOX() + ".)"); t.ok(!handler.line.geometry.getBounds().equals(new OpenLayers.Bounds(0,-35.15625,35.15625,0)), "Correct bounds after dragging without letting go. (Came out as "+handler.line.geometry.getBounds().toBBOX() + ".)");
map.destroy(); map.destroy();
} }
function test_callbacks(t) {
t.plan(12);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
var layer = new OpenLayers.Layer.Vector("foo", {
maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
isBaseLayer: true
});
map.addLayer(layer);
var control = new OpenLayers.Control({
});
var log = {};
var handler = new OpenLayers.Handler.Path(control, {
modify: function() {
log.type = "modify",
log.args = arguments
},
done: function() {
log.type = "done",
log.args = arguments
},
cancel: function() {
log.type = "cancel",
log.args = arguments
}
});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
// mock up feature drawing
handler.activate();
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "modify", "[mouseup] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");
t.ok(log.args[1] === handler.line, "[mouseup] correct sketch feature");
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 74), "[mousemove] correct vertex");
t.ok(log.args[1] === handler.line, "[mousemove] correct sketch feature");
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(10, 10)});
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), "[mousemove] correct vertex");
t.ok(log.args[1] === handler.line, "[mousemove] correct sketch feature");
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
handler.dblclick({type: "dblclick", xy: new OpenLayers.Pixel(10, 10)});
t.eq(log.type, "done", "[dblclick] done called");
t.geom_eq(
log.args[0],
new OpenLayers.Geometry.LineString([
new OpenLayers.Geometry.Point(-150, 75),
new OpenLayers.Geometry.Point(-140, 65)
]),
"[dblclick] correct linestring"
);
// mock up sketch cancel
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
handler.deactivate();
t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
map.destroy();
}
function test_Handler_Path_destroy(t) { function test_Handler_Path_destroy(t) {
t.plan(6); t.plan(6);
var map = new OpenLayers.Map('map'); var map = new OpenLayers.Map('map');
+55 -1
View File
@@ -97,6 +97,60 @@
OpenLayers.Event.stop = oldStop; OpenLayers.Event.stop = oldStop;
} }
function test_callbacks(t) {
t.plan(10);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
var layer = new OpenLayers.Layer.Vector("foo", {
maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
isBaseLayer: true
});
map.addLayer(layer);
var control = new OpenLayers.Control({
});
var log = {};
var handler = new OpenLayers.Handler.Point(control, {
modify: function() {
log.type = "modify",
log.args = arguments
},
done: function() {
log.type = "done",
log.args = arguments
},
cancel: function() {
log.type = "cancel",
log.args = arguments
}
});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
// mock up feature drawing
handler.activate();
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "modify", "[mousedown] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct point");
t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-150, 75), "[mousedown] correct sketch feature");
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 0)});
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 75), "[mousemove] correct point");
t.geom_eq(log.args[1].geometry, new OpenLayers.Geometry.Point(-149, 75), "[mousemove] correct sketch feature");
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(1, 0)});
t.eq(log.type, "done", "[mouseup] done called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-149, 75), "[mouseup] correct point");
// mock up feature drawing with a cancel
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.deactivate();
t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[deactivate while drawing] correct point");
map.destroy();
}
function test_Handler_Point_deactivation(t) { function test_Handler_Point_deactivation(t) {
@@ -121,7 +175,7 @@
map.zoomToMaxExtent(); map.zoomToMaxExtent();
var control = new OpenLayers.Control(); var control = new OpenLayers.Control();
map.addControl(control); map.addControl(control);
var handler = new OpenLayers.Handler.Point(control); var handler = new OpenLayers.Handler.Point(control, {});
var activated = handler.activate(); var activated = handler.activate();
var px = new OpenLayers.Pixel(150, 75); var px = new OpenLayers.Pixel(150, 75);
var evt = {xy: px, which: 1}; var evt = {xy: px, which: 1};
+80
View File
@@ -71,6 +71,86 @@
map.destroy(); map.destroy();
} }
function test_callbacks(t) {
t.plan(12);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
var layer = new OpenLayers.Layer.Vector("foo", {
maxExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
isBaseLayer: true
});
map.addLayer(layer);
var control = new OpenLayers.Control({
});
var log = {};
var handler = new OpenLayers.Handler.Polygon(control, {
modify: function() {
log.type = "modify",
log.args = arguments
},
done: function() {
log.type = "done",
log.args = arguments
},
cancel: function() {
log.type = "cancel",
log.args = arguments
}
});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
// mock up feature drawing
handler.activate();
// click at 0, 0
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
t.eq(log.type, "modify", "[mouseup] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75), "[mouseup] correct vertex");
t.ok(log.args[1] === handler.polygon, "[mouseup] correct sketch feature");
// move to 10, 10 and click
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(10, 10)});
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-140, 65), "[mousemove] correct vertex");
t.ok(log.args[1] === handler.polygon, "[mouseup] correct sketch feature");
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(10, 10)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(10, 10)});
// move to 0, 10 and double click
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(0, 10)});
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 65), "[mousemove] correct vertex");
t.ok(log.args[1] === handler.polygon, "[mouseup] correct sketch feature");
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 10)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 10)});
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 10)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 10)});
handler.dblclick({type: "dblclick", xy: new OpenLayers.Pixel(0, 10)});
t.eq(log.type, "done", "[dblclick] done called");
t.geom_eq(
log.args[0],
new OpenLayers.Geometry.Polygon([
new OpenLayers.Geometry.LinearRing([
new OpenLayers.Geometry.Point(-150, 75),
new OpenLayers.Geometry.Point(-140, 65),
new OpenLayers.Geometry.Point(-150, 65),
new OpenLayers.Geometry.Point(-150, 75)
])
]),
"[dblclick] correct polygon"
);
// mock up sketch cancel
handler.mousedown({type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
handler.mousemove({type: "mousemove", xy: new OpenLayers.Pixel(1, 1)});
handler.deactivate();
t.eq(log.type, "cancel", "[deactivate while drawing] cancel called");
map.destroy();
}
function test_Handler_Polygon_destroy(t) { function test_Handler_Polygon_destroy(t) {
t.plan(8); t.plan(8);
var map = new OpenLayers.Map('map'); var map = new OpenLayers.Map('map');