diff --git a/examples/popups.html b/examples/popups.html
index 988e3b4660..4a1aa056d7 100644
--- a/examples/popups.html
+++ b/examples/popups.html
@@ -36,6 +36,7 @@
new OpenLayers.Size(200,200),
"example popup",
true);
+ popup.closeOnMove = true;
map.addPopup(popup);
}
diff --git a/lib/OpenLayers/Popup.js b/lib/OpenLayers/Popup.js
index da26f82b9f..a315adfbe1 100644
--- a/lib/OpenLayers/Popup.js
+++ b/lib/OpenLayers/Popup.js
@@ -173,6 +173,13 @@ OpenLayers.Popup = OpenLayers.Class({
* Default is false.
*/
panMapIfOutOfView: false,
+
+ /**
+ * APIProperty: closeOnMove
+ * {Boolean} When map pans, close the popup.
+ * Default is false.
+ */
+ closeOnMove: false,
/**
* Property: map
@@ -253,6 +260,10 @@ OpenLayers.Popup = OpenLayers.Class({
this.opacity = null;
this.border = null;
+ if (this.closeOnMove && this.map) {
+ this.map.events.unregister("movestart", this, this.hide);
+ }
+
this.events.destroy();
this.events = null;
@@ -294,6 +305,12 @@ OpenLayers.Popup = OpenLayers.Class({
px = this.map.getLayerPxFromLonLat(this.lonlat);
}
}
+
+ // this assumes that this.map already exists, which is okay because
+ // this.draw is only called once the popup has been added to the map.
+ if (this.closeOnMove) {
+ this.map.events.register("movestart", this, this.hide);
+ }
//listen to movestart, moveend to disable overflow (FF bug)
if (OpenLayers.Util.getBrowserName() == 'firefox') {
diff --git a/tests/Popup.html b/tests/Popup.html
index 80f07fcdd2..f63e745b3e 100644
--- a/tests/Popup.html
+++ b/tests/Popup.html
@@ -87,7 +87,7 @@
map.destroy();
}
function test_Popup_draw(t) {
- t.plan( 13 );
+ t.plan( 15 );
var id = "chicken";
var x = 50;
@@ -144,6 +144,29 @@
popup.moveTo(new OpenLayers.Pixel(x, y));
t.eq(popup.div.style.left, x + "px", "moveTo updates left position of popup.div correctly");
t.eq(popup.div.style.top, y + "px", "moveTo updates top position of popup.div correctly");
+
+
+ //closeOnMove
+ var checkMapEvent = function(map, popup) {
+ var startListeners = map.events.listeners['movestart'];
+ for(var i=0; i < startListeners.length; i++) {
+ var listener = startListeners[i];
+
+ if ((listener.obj == popup) && (listener.func == popup.hide)) {
+ return true;
+ }
+ }
+ return false;
+ };
+ var registered = checkMapEvent(map1, popup);
+ t.ok(!registered, "when not 'closeOnMove', correctly not registered hide() on map's movestart.")
+
+ var popup2 = new OpenLayers.Popup('test');
+ popup2.closeOnMove = true;
+ map1.addPopup(popup2);
+
+ registered = checkMapEvent(map1, popup2);
+ t.ok(registered, "when 'closeOnMove', correctly registered hide() on map's movestart.")
}