From 05cd192ba343bfbfca7dc7e8c8c26adbcb8c68d9 Mon Sep 17 00:00:00 2001 From: euzuro Date: Wed, 19 Nov 2008 15:51:18 +0000 Subject: [PATCH] give popups a 'closeOnMove' option. if set to true, the popup will close itself as soon as the map is pan/zoom (move)ed. patch by jrf, euzuro review by elemoine, cr5. (Closes #1726) git-svn-id: http://svn.openlayers.org/trunk/openlayers@8397 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- examples/popups.html | 1 + lib/OpenLayers/Popup.js | 17 +++++++++++++++++ tests/Popup.html | 25 ++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 1 deletion(-) 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.") }