update default behaviour of popups. now you can click and drag inside a popup without the events dropping through to the map, yet if you are dragging a zoombox over a popup, it still responds. this is all thanks to a new function i am adding to Util.js which is called OpenLayers.Util.safeStopPropagaition(). Turns out the default Event.stop() from prototype.js is also calling a function called preventDefault() which disallows things like selecting text or clicking a hyperlink. all tests pass.

git-svn-id: http://svn.openlayers.org/trunk/openlayers@1438 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
euzuro
2006-09-13 08:20:02 +00:00
parent 6793507a34
commit 7ce7f2484b
4 changed files with 114 additions and 8 deletions

View File

@@ -4,7 +4,7 @@
<style type="text/css">
#map {
width: 100%;
height: 512px;
height: 100%;
border: 1px solid black;
background-color: blue;
}

View File

@@ -3,8 +3,8 @@
<meta http-equiv="imagetoolbar" content="no"> <!--ie image gizmo OFF!-->
<style type="text/css">
#map {
width: 512px;
height: 512px;
width: 400px;
height: 400px;
border: 1px solid black;
}
</style>
@@ -22,6 +22,15 @@
map.addControl(new OpenLayers.Control.LayerSwitcher());
map.zoomToMaxExtent();
}
function bar(e) {
alert("body");
}
function foo(e) {
alert("yo");
return true;
}
function changer() {
@@ -70,9 +79,10 @@
function mousedown(evt) {
if (popup == null) {
popup = feature.createPopup();
popup.setContentHTML("<a href='http://www.somethingconstructive.net' target='_blank'>click me</a>");
popup.setBackgroundColor("yellow");
popup.setOpacity(0.7);
popup.events.register("mousedown", popup, onPopupMouseDown);
// popup.events.register("mousedown", popup, onPopupMouseDown);
markers.map.addPopup(popup);
} else {
markers.map.removePopup(popup);
@@ -112,12 +122,14 @@
</head>
<body onload="init()">
<div id="map"></div>
<div style="background-color:purple" onclick="add()"> click to add Popup to map</div>
<div id="foo" style="background-color:purple" onclick="add()"> click to add Popup to map</div>
<div style="background-color:green" onclick="addMarker()"> click to add a Marker with an AnchoredBubble popup</div>
<div style="background-color:blue" onclick="changer()"> click to modify popup's attributes</div>
<div style="background-color:red" onclick="remove()"> click to remove the popup from map</div>
<div style="background-color:grey" onclick="removelayer()"> click to remove the markers layer</div>
<div style="background-color:orange" onclick="alert(marker.onScreen())"> marker.onscreen()?</div>
<div style="background-color:yellow" onclick="destroy()"> click to destroy the popup from map</div>
<div id="yar" style="background-color:orange" onclick="alert(marker.onScreen())"> marker.onscreen()?</div>
<div id="ar" style="background-color:yellow">
<a href='http://www.somethingconstructive.net' target='_blank'>click me</a>
</div>
</body>
</html>

View File

@@ -75,7 +75,7 @@ OpenLayers.Popup.prototype = {
this.div = OpenLayers.Util.createDiv(this.id, null, null,
null, null, null, "hidden");
this.events = new OpenLayers.Events(this, this.div, null);
this.registerEvents();
},
/**
@@ -231,5 +231,86 @@ OpenLayers.Popup.prototype = {
}
},
/** Do this in a separate function so that subclasses can
* choose to override it if they wish to deal differently
* with mouse events
*
* Note in the following handler functions that some special
* care is needed to deal correctly with mousing and popups.
*
* Because the user might select the zoom-rectangle option and
* then drag it over a popup, we need a safe way to allow the
* mousemove and mouseup events to pass through the popup when
* they are initiated from outside.
*
* Otherwise, we want to essentially kill the event propagation
* for all other events, though we have to do so carefully,
* without disabling basic html functionality, like clicking on
* hyperlinks or drag-selecting text.
*/
registerEvents:function() {
Event.observe(this.div, "mousedown",
this.onmousedown.bindAsEventListener(this));
Event.observe(this.div, "mousemove",
this.onmousemove.bindAsEventListener(this));
Event.observe(this.div, "mouseup",
this.onmouseup.bindAsEventListener(this));
Event.observe(this.div, "click",
OpenLayers.Util.safeStopPropagation);
Event.observe(this.div, "mouseout",
this.onmouseout.bindAsEventListener(this));
Event.observe(this.div, "dblclick",
OpenLayers.Util.safeStopPropagation);
},
/** When mouse goes down within the popup, make a note of
* it locally, and then do not propagate the mousedown
* (but do so safely so that user can select text inside)
*
* @param {Event} evt
*/
onmousedown: function (evt) {
this.mousedown = true;
OpenLayers.Util.safeStopPropagation(evt);
},
/** If the drag was started within the popup, then
* do not propagate the mousemove (but do so safely
* so that user can select text inside)
*
* @param {Event} evt
*/
onmousemove: function (evt) {
if (this.mousedown) {
OpenLayers.Util.safeStopPropagation(evt);
}
},
/** When mouse comes up within the popup, after going down
* in it, reset the flag, and then (once again) do not
* propagate the event, but do so safely so that user can
* select text inside
*
* @param {Event} evt
*/
onmouseup: function (evt) {
if (this.mousedown) {
this.mousedown = false;
OpenLayers.Util.safeStopPropagation(evt);
}
},
/** When mouse goes out of the popup set the flag to false so that
* if they let go and then drag back in, we won't be confused.
*
* @param {Event} evt
*
* @type Boolean
*/
onmouseout: function (evt) {
this.mousedown = false;
},
CLASS_NAME: "OpenLayers.Popup"
};

View File

@@ -516,3 +516,16 @@ OpenLayers.Util.getResolutionFromScale = function (scale, units) {
* OpenLayers.DOTS_PER_INCH);
return resolution;
};
/** Safely stop the propagation of an event *without* preventing
* the default browser action from occurring.
*
* @param {Event} evt
*/
OpenLayers.Util.safeStopPropagation = function(evt) {
if (evt.stopPropagation) {
evt.stopPropagation();
}
evt.returnValue = false;
evt.cancelBubble = true;
};