make the drawing handlers create the sketch feature at an appropriate time, when we can actually derive geographic coordinates from a pixel, r=ahocevar (References #3327)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@12046 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2011-06-06 07:12:52 +00:00
parent 9dcdbac238
commit 9b361ef35d
9 changed files with 116 additions and 208 deletions

View File

@@ -25,7 +25,7 @@
}
function test_Handler_Path_activation(t) {
t.plan(12);
t.plan(5);
var log = [];
var map = new OpenLayers.Map("map", {
resolutions: [1]
@@ -36,11 +36,7 @@
});
map.addLayer(layer);
var control = new OpenLayers.Control();
var handler = new OpenLayers.Handler.Path(control, {
"create": function(g, f) {
log.push({geometry: g, feature: f});
}
});
var handler = new OpenLayers.Handler.Path(control, {});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
@@ -57,20 +53,6 @@
"activate creates a vector layer");
t.ok(handler.layer.map == map,
"activate adds the vector layer to the map");
t.ok(handler.point instanceof OpenLayers.Feature.Vector,
"activate creates a point feature");
t.ok(handler.point.layer == handler.layer,
"activate adds the point feature to the layer");
t.ok(handler.line instanceof OpenLayers.Feature.Vector,
"acttivates creates a line feature");
t.ok(handler.line.layer == handler.layer,
"activate adds the line feature to the layer");
t.eq(log.length, 1,
"activate calls \"create\" once");
t.ok(log[0].geometry == handler.point.geometry,
"\"create\" called with expected geometry");
t.ok(log[0].feature == handler.line,
"\"create\" called with expected feature");
activated = handler.deactivate();
t.ok(activated,
"deactivate returns true if the handler was active already");
@@ -105,7 +87,7 @@
}
function test_bounds(t) {
t.plan(5);
t.plan(4);
var geometry;
var map = new OpenLayers.Map('map');
map.addLayer(new OpenLayers.Layer.WMS("", "", {}));
@@ -115,8 +97,6 @@
var handler = new OpenLayers.Handler.Path(control, {},
{stopDown: true, stopUp: true});
var activated = handler.activate();
t.eq(handler.layer.features.length, 2,
"There are two features in the layer after activation.");
// click on (150, 75)
var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1};
handler.mousemove(evt);
@@ -150,7 +130,7 @@
}
function test_callbacks(t) {
t.plan(45);
t.plan(39);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -185,19 +165,18 @@
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
// create line
handler.activate();
t.eq(logs.length, 1, "[activate] called back");
log = logs.shift();
t.eq(log.type, "create", "[activate] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[activate] initial point");
t.ok(log.args[1] == handler.line,
"[activate] correct feature");
// mouse move
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
t.eq(logs.length, 1, "[mousemove] called back");
t.eq(logs.length, 2, "[mousemove] called back twice");
log = logs.shift();
t.eq(log.type, "create", "[mousemove] create called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75),
"[mousemove] correct point");
t.ok(log.args[1] === handler.line,
"[mousemove] correct feature");
log = logs.shift();
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75),
@@ -279,7 +258,7 @@
// double click
handler.dblclick({type: "dblclick",
xy: new OpenLayers.Pixel(10, 10)});
t.eq(logs.length, 2, "[dblclick] called back twice");
t.eq(logs.length, 1, "[dblclick] called back");
log = logs.shift();
t.eq(log.type, "done", "[dblclick] done called");
t.geom_eq(log.args[0],
@@ -289,24 +268,13 @@
]),
"[dblclick] correct linestring"
);
log = logs.shift();
t.eq(log.type, "create", "[dblclick] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[dblclick] initial point");
t.ok(log.args[1] == handler.line,
"[dblclick] correct feature");
// cancel
handler.cancel();
t.eq(logs.length, 2, "[cancel] called back");
t.eq(logs.length, 1, "[cancel] called back");
log = logs.shift();
t.eq(log.type, "cancel", "[cancel] canced called");
t.ok(isNaN(log.args[0].components[0].x) && isNaN(log.args[0].components[0].y),
"[cancel] initial linestring"
t.eq(log.args[0], null, "[cancel] got null"
);
log = logs.shift();
t.eq(log.type, "create", "[cancel] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[cancel] initial point");
map.destroy();
}
@@ -379,9 +347,9 @@
handler.activate();
handler.persist = false;
var feature1 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature1 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
@@ -393,9 +361,9 @@
t.ok(feature1.layer == null, "a) feature1 destroyed");
handler.persist = true;
var feature2 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature2 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
@@ -406,9 +374,9 @@
{type: "dblclick", xy: new OpenLayers.Pixel(1, 1)});
t.ok(feature2.layer != null, "b) feature2 not destroyed");
var feature3 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature3 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
@@ -442,9 +410,9 @@
handler.activate();
handler.persist = false;
var feature1 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature1 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
handler.mousemove(
@@ -456,9 +424,9 @@
t.ok(feature1.layer == null, "a) feature1 destroyed");
handler.persist = true;
feature2 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
feature2 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
handler.mousemove(
@@ -469,9 +437,9 @@
{type: "mouseup", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
t.ok(feature2.layer != null, "b) feature2 not destroyed");
feature3 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
feature3 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
handler.mousemove(
@@ -483,9 +451,9 @@
t.ok(feature3.layer != null, "c) feature3 not destroyed");
t.ok(feature2.layer == null, "c) feature2 destroyed");
feature4 = handler.line;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
feature4 = handler.line;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: false});
handler.mousemove(

View File

@@ -25,8 +25,7 @@
}
function test_Handler_Point_activation(t) {
t.plan(11);
var log = [];
t.plan(6);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -36,11 +35,7 @@
});
map.addLayer(layer);
var control = new OpenLayers.Control();
var handler = new OpenLayers.Handler.Point(control, {
"create": function(g, f) {
log.push({geometry: g, feature: f});
}
});
var handler = new OpenLayers.Handler.Point(control, {});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
@@ -57,16 +52,6 @@
"activate creates a vector layer");
t.ok(handler.layer.map == map,
"activate adds the vector layer to the map");
t.ok(handler.point instanceof OpenLayers.Feature.Vector,
"activate creates a feature");
t.ok(handler.point.layer == handler.layer,
"activate adds the feature to the layer");
t.eq(log.length, 1,
"activate calls \"create\" once");
t.ok(log[0].geometry == handler.point.geometry,
"\"create\" called with expected geometry");
t.ok(log[0].feature == handler.point,
"\"create\" called with expected feature");
activated = handler.deactivate();
t.ok(activated,
"deactivate returns true if the handler was active already");
@@ -176,7 +161,7 @@
}
function test_callbacks(t) {
t.plan(28);
t.plan(24);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -209,17 +194,19 @@
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
// create point
handler.activate();
t.eq(logs.length, 1, "[activate] called back");
log = logs.shift();
t.eq(log.type, "create", "[activate] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[activate] initial point");
// mouse down
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
t.eq(logs.length, 1, "[mousedown] called back");
t.eq(logs.length, 2, "[mousedown] called back twice");
log = logs.shift();
t.eq(log.type, "create", "[mousedown] create 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 feature");
log = logs.shift();
t.eq(log.type, "modify", "[mousedown] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75),
@@ -258,29 +245,20 @@
"[mousedown] correct feature");
// mouse up
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(2, 0)});
t.eq(logs.length, 2, "[mouseup] called back twice");
t.eq(logs.length, 1, "[mouseup] called back");
log = logs.shift();
t.eq(log.type, "done", "[mouseup] done called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-148, 75),
"[mouseup] correct point");
log = logs.shift();
t.eq(log.type, "create", "[mouseup] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[mouseup] initial point");
// mouse up on same pixel
handler.mouseup({type: "mouseup", xy: new OpenLayers.Pixel(2, 0)});
t.eq(logs.length, 0, "[mouseup] not called back");
// cancel
handler.cancel();
t.eq(logs.length, 2, "[cancel] called back");
t.eq(logs.length, 1, "[cancel] called back");
log = logs.shift();
t.eq(log.type, "cancel", "[cancel] canced called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[cancel] initial point");
log = logs.shift();
t.eq(log.type, "create", "[cancel] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[] initial point");
t.eq(log.type, "cancel", "[cancel] cancel called");
t.eq(log.args[0], null, "[cancel] got null");
map.destroy();
}
@@ -308,7 +286,7 @@
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
{type: "mouseup", xy: new OpenLayers.Pixel(0, 0)});
t.eq(handler.layer.features.length, 1,
t.eq(handler.layer.features.length, 0,
"feature destroyed on mouseup when persist is false");
handler.persist = true;
@@ -316,7 +294,7 @@
{type: "mousedown", xy: new OpenLayers.Pixel(1, 0)});
handler.mouseup(
{type: "mouseup", xy: new OpenLayers.Pixel(1, 0)});
t.eq(handler.layer.features.length, 2,
t.eq(handler.layer.features.length, 1,
"feature not destroyed on mouseup when persist is true");
var feature = handler.layer.features[0];
handler.mousedown(
@@ -352,6 +330,7 @@
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
handler.activate();
handler.mousemove({xy: new OpenLayers.Pixel(0, 0)});
var _layer = handler.layer;
var _geometry = handler.point.geometry;
handler.deactivate();
@@ -405,6 +384,7 @@
var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'});
handler.activate();
handler.mousemove({xy: new OpenLayers.Pixel(150, 75)});
t.ok(handler.layer,
"handler has a layer prior to destroy");
@@ -544,13 +524,11 @@
ret = handler.touchstart({xy: new OpenLayers.Pixel(0, 0)});
t.ok(ret, '[touchstart] event propagates');
t.eq(log, null, '[touchstart] no finalization');
t.ok(isNaN(handler.point.geometry.x) && isNaN(handler.point.geometry.y),
'[touchstart] feature not modified');
t.eq(handler.point, null, '[touchstart] feature not modified');
ret = handler.touchmove({xy: new OpenLayers.Pixel(1, 0)});
t.ok(ret, '[touchmove] event propagates');
t.eq(log, null, '[touchmove] no finalization');
t.ok(isNaN(handler.point.geometry.x) && isNaN(handler.point.geometry.y),
'[touchmove] feature not modified');
t.eq(handler.point, null, '[touchmove] feature not modified');
ret = handler.touchend({});
t.ok(ret, '[touchend] event propagates');
t.geom_eq(log.geometry, new OpenLayers.Geometry.Point(-149, 75),
@@ -595,17 +573,17 @@
ret = handler.touchstart({xy: new OpenLayers.Pixel(0, 0)});
t.ok(ret, '[touchstart] event propagates');
t.eq(log, null, '[touchstart] no finalization');
t.ok(isNaN(handler.point.geometry.x) && isNaN(handler.point.geometry.y),
t.eq(handler.point, null, null,
'[touchstart] feature not modified');
ret = handler.touchmove({xy: new OpenLayers.Pixel(9, 0)});
t.ok(ret, '[touchmove] event propagates');
t.eq(log, null, '[touchmove] no finalization');
t.ok(isNaN(handler.point.geometry.x) && isNaN(handler.point.geometry.y),
t.eq(handler.point, null,
'[touchmove] feature not modified');
ret = handler.touchend({});
t.ok(ret, '[touchend] event propagates');
t.eq(log, null, '[touchend] no finalization');
t.ok(isNaN(handler.point.geometry.x) && isNaN(handler.point.geometry.y),
t.eq(handler.point, null,
'[touchend] feature not modified');
// tear down

View File

@@ -25,7 +25,7 @@
}
function test_Handler_Polygon_activation(t) {
t.plan(13);
t.plan(5);
var log = [];
var map = new OpenLayers.Map("map", {
resolutions: [1]
@@ -36,11 +36,7 @@
});
map.addLayer(layer);
var control = new OpenLayers.Control();
var handler = new OpenLayers.Handler.Polygon(control, {
"create": function(g, f) {
log.push({geometry: g, feature: f});
}
});
var handler = new OpenLayers.Handler.Polygon(control, {});
control.handler = handler;
map.addControl(control);
map.setCenter(new OpenLayers.LonLat(0, 0), 0);
@@ -57,22 +53,6 @@
"activate creates a vector layer");
t.ok(handler.layer.map == map,
"activate adds the vector layer to the map");
t.ok(handler.point instanceof OpenLayers.Feature.Vector,
"activate creates a point feature");
t.ok(handler.point.layer == handler.layer,
"activate adds the point feature to the layer");
t.ok(handler.line instanceof OpenLayers.Feature.Vector,
"activates creates a line feature");
t.ok(handler.polygon instanceof OpenLayers.Feature.Vector,
"acttivates creates a polygon feature");
t.ok(handler.polygon.layer == handler.layer,
"activate adds the polygin feature to the layer");
t.eq(log.length, 1,
"activate calls \"create\" once");
t.ok(log[0].geometry == handler.point.geometry,
"\"create\" called with expected geometry");
t.ok(log[0].feature == handler.polygon,
"\"create\" called with expected feature");
activated = handler.deactivate();
t.ok(activated,
"deactivate returns true if the handler was active already");
@@ -140,7 +120,7 @@
}
function test_callbacks(t) {
t.plan(45);
t.plan(39);
var map = new OpenLayers.Map("map", {
resolutions: [1]
});
@@ -178,17 +158,16 @@
// create polygon
handler.activate();
handler.activate();
t.eq(logs.length, 1, "[activate] called back");
log = logs.shift();
t.eq(log.type, "create", "[activate] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[activate] initial point");
t.ok(log.args[1] == handler.polygon,
"[activate] correct feature");
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
t.eq(logs.length, 1, "[mousemove] called back");
t.eq(logs.length, 2, "[mousemove] called back");
log = logs.shift();
t.eq(log.type, "create", "[activate] create called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75),
"[mousemove] correct point");
t.ok(log.args[1] == handler.polygon,
"[mousemove] correct feature");
log = logs.shift();
t.eq(log.type, "modify", "[mousemove] modify called");
t.geom_eq(log.args[0], new OpenLayers.Geometry.Point(-150, 75),
@@ -287,7 +266,7 @@
// dblclick
handler.dblclick(
{type: "dblclick", xy: new OpenLayers.Pixel(0, 10)});
t.eq(logs.length, 2, "[dblclick] called back twice");
t.eq(logs.length, 1, "[dblclick] called back");
log = logs.shift();
t.eq(log.type, "done", "[dblclick] done called");
t.geom_eq(
@@ -302,21 +281,11 @@
]),
"[dblclick] correct polygon"
);
log = logs.shift();
t.eq(log.type, "create", "[dblclick] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[dblclick] initial point");
t.ok(log.args[1] == handler.polygon,
"[dblclick] correct feature");
// cancel
handler.cancel();
t.eq(logs.length, 2, "[cancel] called back");
t.eq(logs.length, 1, "[cancel] called back");
log = logs.shift();
t.eq(log.type, "cancel", "[cancel] canced called");
log = logs.shift();
t.eq(log.type, "create", "[cancel] create called");
t.ok(isNaN(log.args[0].x) && isNaN(log.args[0].y),
"[cancel] initial point");
map.destroy();
}
@@ -389,9 +358,9 @@
handler.activate();
handler.persist = false;
var feature1 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature1 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
@@ -409,9 +378,9 @@
t.ok(feature1.layer == null, "a) feature1 destroyed");
handler.persist = true;
var feature2 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature2 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
@@ -428,9 +397,9 @@
{type: "dblclick", xy: new OpenLayers.Pixel(2, 2)});
t.ok(feature2.layer != null, "b) feature2 not destroyed");
var feature3 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature3 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0)});
handler.mouseup(
@@ -470,9 +439,9 @@
handler.activate();
handler.persist = false;
var feature1 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature1 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
handler.mousemove(
@@ -484,9 +453,9 @@
t.ok(feature1.layer == null, "a) feature1 destroyed");
handler.persist = true;
var feature2 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature2 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
handler.mousemove(
@@ -497,9 +466,9 @@
{type: "mouseup", xy: new OpenLayers.Pixel(2, 2), shiftKey: true});
t.ok(feature2.layer != null, "b) feature2 not destroyed");
var feature3 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
var feature3 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: true});
handler.mousemove(
@@ -511,9 +480,9 @@
t.ok(feature3.layer != null, "c) feature3 not destroyed");
t.ok(feature2.layer == null, "c) feature2 destroyed");
feature4 = handler.polygon;
handler.mousemove(
{type: "mousemove", xy: new OpenLayers.Pixel(0, 0)});
feature4 = handler.polygon;
handler.mousedown(
{type: "mousedown", xy: new OpenLayers.Pixel(0, 0), shiftKey: false});
handler.mousemove(