Fire measurepartial synchronously if in freehand mode. Cancel delayed messages when measure completes, when control is deactivated, or when measure is canceled. p=jorix,me r=me (closes #2820)
git-svn-id: http://svn.openlayers.org/trunk/openlayers@10799 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
@@ -130,12 +130,21 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
this.handler = new handler(this, this.callbacks, this.handlerOptions);
|
this.handler = new handler(this, this.callbacks, this.handlerOptions);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* APIMethod: deactivate
|
||||||
|
*/
|
||||||
|
deactivate: function() {
|
||||||
|
this.cancelDelay();
|
||||||
|
return OpenLayers.Control.prototype.deactivate.apply(this, arguments);
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* APIMethod: cancel
|
* APIMethod: cancel
|
||||||
* Stop the control from measuring. If <persist> is true, the temporary
|
* Stop the control from measuring. If <persist> is true, the temporary
|
||||||
* sketch will be erased.
|
* sketch will be erased.
|
||||||
*/
|
*/
|
||||||
cancel: function() {
|
cancel: function() {
|
||||||
|
this.cancelDelay();
|
||||||
this.handler.cancel();
|
this.handler.cancel();
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -165,9 +174,7 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* geometry - {<OpenLayers.Geometry>}
|
* geometry - {<OpenLayers.Geometry>}
|
||||||
*/
|
*/
|
||||||
measureComplete: function(geometry) {
|
measureComplete: function(geometry) {
|
||||||
if(this.delayedTrigger) {
|
this.cancelDelay();
|
||||||
window.clearTimeout(this.delayedTrigger);
|
|
||||||
}
|
|
||||||
this.measure(geometry, "measure");
|
this.measure(geometry, "measure");
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -180,10 +187,17 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
* geometry - {<OpenLayers.Geometry>} The sketch geometry.
|
* geometry - {<OpenLayers.Geometry>} The sketch geometry.
|
||||||
*/
|
*/
|
||||||
measurePartial: function(point, geometry) {
|
measurePartial: function(point, geometry) {
|
||||||
if (geometry.getLength() > 0) {
|
this.cancelDelay();
|
||||||
geometry = geometry.clone();
|
geometry = geometry.clone();
|
||||||
|
// when we're wating for a dblclick, we have to trigger measurepartial
|
||||||
|
// after some delay to deal with reflow issues in IE
|
||||||
|
if (this.handler.freehandMode(this.handler.evt)) {
|
||||||
|
// no dblclick in freehand mode
|
||||||
|
this.measure(geometry, "measurepartial");
|
||||||
|
} else {
|
||||||
this.delayedTrigger = window.setTimeout(
|
this.delayedTrigger = window.setTimeout(
|
||||||
OpenLayers.Function.bind(function() {
|
OpenLayers.Function.bind(function() {
|
||||||
|
this.delayedTrigger = null;
|
||||||
this.measure(geometry, "measurepartial");
|
this.measure(geometry, "measurepartial");
|
||||||
}, this),
|
}, this),
|
||||||
this.partialDelay
|
this.partialDelay
|
||||||
@@ -191,6 +205,17 @@ OpenLayers.Control.Measure = OpenLayers.Class(OpenLayers.Control, {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method: cancelDelay
|
||||||
|
* Cancels the delay measurement that measurePartial began.
|
||||||
|
*/
|
||||||
|
cancelDelay: function() {
|
||||||
|
if (this.delayedTrigger !== null) {
|
||||||
|
window.clearTimeout(this.delayedTrigger);
|
||||||
|
this.delayedTrigger = null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method: measure
|
* Method: measure
|
||||||
*
|
*
|
||||||
|
|||||||
+144
-35
@@ -69,50 +69,159 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function test_partial(t) {
|
function test_partial(t) {
|
||||||
t.plan(1);
|
|
||||||
|
|
||||||
// set up
|
t.plan(28);
|
||||||
|
|
||||||
var map, layer, control, geometry, log;
|
var map = new OpenLayers.Map({
|
||||||
|
div: "map",
|
||||||
map = new OpenLayers.Map("map", {units: "m"});
|
units: "m",
|
||||||
|
resolutions: [1],
|
||||||
layer = new OpenLayers.Layer(null, {isBaseLayer: true});
|
layers: [
|
||||||
map.addLayer(layer);
|
new OpenLayers.Layer(null, {
|
||||||
|
isBaseLayer: true
|
||||||
map.zoomToMaxExtent();
|
})
|
||||||
|
],
|
||||||
control = new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
|
center: new OpenLayers.LonLat(0, 0)
|
||||||
partialDelay: null
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var log = [];
|
||||||
|
var control = new OpenLayers.Control.Measure(
|
||||||
|
OpenLayers.Handler.Path, {persist: true,
|
||||||
|
eventListeners: {
|
||||||
|
measurepartial: function(evt) {
|
||||||
|
log.push(evt);
|
||||||
|
},
|
||||||
|
measure: function(evt){
|
||||||
|
log.push(evt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
map.addControl(control);
|
map.addControl(control);
|
||||||
control.activate();
|
control.activate();
|
||||||
|
|
||||||
control.events.on({
|
|
||||||
"measurepartial": function(e) {
|
// convenience function to trigger mouse events
|
||||||
log.measure = e.measure;
|
function trigger(type, x, y) {
|
||||||
|
map.events.triggerEvent(type, {
|
||||||
|
xy: new OpenLayers.Pixel(x, y)
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
// delay in seconds
|
||||||
|
var delay = control.partialDelay / 1000;
|
||||||
|
|
||||||
|
// establish first point
|
||||||
|
trigger("mousedown", 0, 0);
|
||||||
|
trigger("mouseup", 0, 0);
|
||||||
|
|
||||||
|
|
||||||
|
// a) move 10 pixels and click
|
||||||
|
trigger("mousemove", 0, 10);
|
||||||
|
trigger("mousedown", 0, 10);
|
||||||
|
trigger("mouseup", 0, 10);
|
||||||
|
|
||||||
|
// confirm measurepartial is not fired before delay
|
||||||
|
t.eq(log.length, 0, "a) no event fired yet")
|
||||||
|
|
||||||
|
t.delay_call(
|
||||||
|
// wait for delay then confirm event was logged
|
||||||
|
delay, function() {
|
||||||
|
t.eq(log.length, 1, "a) event logged")
|
||||||
|
t.eq(log[0].type, "measurepartial", "a) event logged");
|
||||||
|
t.eq(log[0].measure, 10, "a) correct measure");
|
||||||
|
|
||||||
|
// b) move 10 pixels and click
|
||||||
|
trigger("mousemove", 0, 20);
|
||||||
|
trigger("mousedown", 0, 20);
|
||||||
|
trigger("mouseup", 0, 20);
|
||||||
|
|
||||||
|
// confirm measurepartial is not fired before delay
|
||||||
|
t.eq(log.length, 1, "b) no event fired yet")
|
||||||
|
|
||||||
|
},
|
||||||
|
delay, function() {
|
||||||
|
t.eq(log.length, 2, "b) event logged");
|
||||||
|
t.eq(log[1].type, "measurepartial", "b) correct type");
|
||||||
|
t.eq(log[1].measure, 20, "b) correct measure");
|
||||||
|
|
||||||
|
// c) move 10 pixels and click
|
||||||
|
trigger("mousemove", 0, 30);
|
||||||
|
trigger("mousedown", 0, 30);
|
||||||
|
trigger("mouseup", 0, 30);
|
||||||
|
},
|
||||||
|
// wait for half delay and confirm event not logged
|
||||||
|
delay / 2, function() {
|
||||||
|
// confirm measurepartial is not fired before delay
|
||||||
|
t.eq(log.length, 2, "c) no event fired yet")
|
||||||
|
},
|
||||||
|
// wait for rest of delay and confirm event logged
|
||||||
|
delay / 2, function() {
|
||||||
|
t.eq(log.length, 3, "c) event logged");
|
||||||
|
t.eq(log[2].type, "measurepartial", "c) correct type");
|
||||||
|
t.eq(log[2].measure, 30, "c) correct measure");
|
||||||
|
|
||||||
|
// d) move 10 pixels and click
|
||||||
|
trigger("mousemove", 0, 40);
|
||||||
|
trigger("mousedown", 0, 40);
|
||||||
|
trigger("mouseup", 0, 40);
|
||||||
|
|
||||||
|
// confirm measurepartial is not fired before delay
|
||||||
|
t.eq(log.length, 3, "d) no event fired yet")
|
||||||
|
|
||||||
|
// e) double click to finish
|
||||||
|
trigger("dblclick", 0, 40);
|
||||||
|
|
||||||
|
t.eq(log.length, 4, "e) event logged");
|
||||||
|
t.eq(log[3].type, "measure", "e) correct type");
|
||||||
|
t.eq(log[3].measure, 40, "e) correct measure");
|
||||||
|
},
|
||||||
|
// wait for rest of delay and confirm no measurepartial logged
|
||||||
|
delay, function() {
|
||||||
|
// confirm measurepartial is not fired after dblclick
|
||||||
|
t.eq(log.length, 4, "e) no additional event fired");
|
||||||
|
|
||||||
|
// change to freehand mode and confirm synchronous event dispatch
|
||||||
|
control.handler.freehand = true;
|
||||||
|
// clear log
|
||||||
|
log = [];
|
||||||
|
|
||||||
|
// f) establish first freehand point
|
||||||
|
trigger("mousedown", 0, 0);
|
||||||
|
t.eq(log.length, 0, "f) no event fired yet")
|
||||||
|
|
||||||
|
// g) move 10 pixels
|
||||||
|
trigger("mousemove", 10, 0);
|
||||||
|
|
||||||
|
t.eq(log.length, 1, "g) event logged");
|
||||||
|
t.eq(log[0].type, "measurepartial", "g) correct type");
|
||||||
|
t.eq(log[0].measure, 10, "g) correct measure");
|
||||||
|
|
||||||
|
// h) move 10 pixels
|
||||||
|
trigger("mousemove", 20, 0);
|
||||||
|
|
||||||
|
t.eq(log.length, 2, "h) event logged");
|
||||||
|
t.eq(log[1].type, "measurepartial", "h) correct type");
|
||||||
|
t.eq(log[1].measure, 20, "h) correct measure");
|
||||||
|
|
||||||
|
// i) mouse up to finish
|
||||||
|
trigger("mouseup", 20, 0);
|
||||||
|
|
||||||
|
t.eq(log.length, 3, "i) event logged");
|
||||||
|
t.eq(log[2].type, "measure", "i) correct type");
|
||||||
|
t.eq(log[2].measure, 20, "i) correct measure");
|
||||||
|
|
||||||
|
// j) clean up
|
||||||
|
log = [];
|
||||||
|
map.destroy();
|
||||||
|
},
|
||||||
|
// wait for delay and confirm event not logged
|
||||||
|
delay, function() {
|
||||||
|
t.eq(log.length, 0, "j) no event fired after destroy");
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
// test
|
}
|
||||||
|
|
||||||
geometry = new OpenLayers.Geometry.LineString([
|
|
||||||
new OpenLayers.Geometry.Point(1, 1),
|
|
||||||
new OpenLayers.Geometry.Point(2, 1)
|
|
||||||
]);
|
|
||||||
|
|
||||||
log = {};
|
|
||||||
control.measurePartial(null, geometry);
|
|
||||||
geometry.components[1].x = 3;
|
|
||||||
t.delay_call(0.2, function() {
|
|
||||||
|
|
||||||
t.eq(log.measure, 1, "partial measure is correct");
|
|
||||||
|
|
||||||
// tear down
|
|
||||||
map.destroy
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
Reference in New Issue
Block a user