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
This commit is contained in:
euzuro
2008-11-19 15:51:18 +00:00
parent d5950d4b29
commit 05cd192ba3
3 changed files with 42 additions and 1 deletions

View File

@@ -36,6 +36,7 @@
new OpenLayers.Size(200,200),
"example popup",
true);
popup.closeOnMove = true;
map.addPopup(popup);
}

View File

@@ -174,6 +174,13 @@ OpenLayers.Popup = OpenLayers.Class({
*/
panMapIfOutOfView: false,
/**
* APIProperty: closeOnMove
* {Boolean} When map pans, close the popup.
* Default is false.
*/
closeOnMove: false,
/**
* Property: map
* {<OpenLayers.Map>} this gets set in Map.js when the popup is added to the 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;
@@ -295,6 +306,12 @@ OpenLayers.Popup = OpenLayers.Class({
}
}
// 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') {
this.map.events.register("movestart", this, function() {

View File

@@ -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.")
}
</script>