diff --git a/examples/popups.html b/examples/popups.html index 85b9ecff3c..7ac745cda3 100644 --- a/examples/popups.html +++ b/examples/popups.html @@ -42,6 +42,15 @@ map.addPopup(popup); } + + function addAnchor() { + popup = new OpenLayers.Popup.Anchored("chicken", + new OpenLayers.LonLat(5,40), + new OpenLayers.Size(200,200), + "example popup"); + + map.addPopup(popup); + } function destroy() { popup.destroy(); @@ -60,7 +69,7 @@
click to add popup to map
click to modify popup's attributes
-
click to destroy the popup
+
click to add an anchored popup
click to remove the popup from map
diff --git a/lib/OpenLayers.js b/lib/OpenLayers.js index 4551e17872..62fd4e0086 100644 --- a/lib/OpenLayers.js +++ b/lib/OpenLayers.js @@ -53,6 +53,7 @@ catch(e){ "OpenLayers/Layer/Text.js", "OpenLayers/Layer/WMS.js", "OpenLayers/Layer/WFS.js", + "OpenLayers/Popup/Anchored.js", "OpenLayers/Control.js", "OpenLayers/Control/MouseDefaults.js", "OpenLayers/Control/KeyboardDefaults.js", diff --git a/lib/OpenLayers/Popup/Anchored.js b/lib/OpenLayers/Popup/Anchored.js index aabc2bf825..edd98765d9 100644 --- a/lib/OpenLayers/Popup/Anchored.js +++ b/lib/OpenLayers/Popup/Anchored.js @@ -7,6 +7,13 @@ OpenLayers.Popup.Anchored = Class.create(); OpenLayers.Popup.Anchored.prototype = Object.extend( new OpenLayers.Popup(), { + /** "lr", "ll", "tr", "tl" - relative position of the popup. + * @type String */ + relativePosition: null, + + /** @type OpenLayers.Size */ + anchorSize: null, + /** * @constructor * @@ -15,8 +22,14 @@ OpenLayers.Popup.Anchored.prototype = * @param {OpenLayers.Size} size * @param {String} contentHTML */ - initialize:function(id, lonlat, size, contentHTML) { - OpenLayers.Popup.prototype.initialize.apply(this, arguments); + initialize:function(id, lonlat, size, contentHTML, anchorSize) { + + var newArguments = new Array(id, lonlat, size, contentHTML); + OpenLayers.Popup.prototype.initialize.apply(this, newArguments); + + this.anchorSize = (anchorSize != null) ? anchorSize + : new OpenLayers.Size(0,0); + }, /** @@ -25,6 +38,7 @@ OpenLayers.Popup.Anchored.prototype = OpenLayers.Popup.prototype.destroy.apply(this, arguments); }, + /** * @param {OpenLayers.Pixel} px * @@ -32,14 +46,75 @@ OpenLayers.Popup.Anchored.prototype = * @type DOMElement */ draw: function(px) { - OpenLayers.Popup.prototype.draw.apply(this, arguments); + + //calculate relative position + this.relativePosition = this.calculateRelativePosition(px); + + return OpenLayers.Popup.prototype.draw.apply(this, arguments); }, + + /** + * @param {OpenLayers.Pixel} px + * + * @returns The relative position ("br" "tr" "tl "bl") at which the popup + * should be placed + * @type String + */ + calculateRelativePosition:function(px) { + var lonlat = this.map.getLonLatFromLayerPx(px); + + var extent = this.map.getExtent(); + var quadrant = extent.determineQuadrant(lonlat); + + return this.oppositeQuadrant(quadrant); + }, /** * @param {OpenLayers.Pixel} px */ moveTo: function(px) { - OpenLayers.Popup.prototype.moveTo.apply(this, arguments); + + var newPx = this.calculateNewPx(px); + + var newArguments = new Array(newPx); + OpenLayers.Popup.prototype.moveTo.apply(this, newArguments); + }, + + /** + * @param {OpenLayers.Pixel} px + * + * @returns The the new px position of the popup on the screen + * relative to the passed-in px + * @type OpenLayers.Pixel + */ + calculateNewPx:function(px) { + + var newPx = px.copyOf(); + + var top = (this.relativePosition.charAt(0) == 't'); + newPx.y += (top) ? -this.size.h : this.anchorSize.h; + + var left = (this.relativePosition.charAt(1) == 'l'); + newPx.x += (left) ? -this.size.w : this.anchorSize.w; + + return newPx; + }, + + /** + * @param {String} quadrant + * + * @returns The opposing quadrant ("br" "tr" "tl" "bl"). For Example, if + * you pass in "bl" it returns "tr", if you pass in "br" it + * returns "tl", etc. + * @type String + */ + oppositeQuadrant: function(quadrant) { + var opp = ""; + + opp += (quadrant.charAt(0) == 't') ? 'b' : 't'; + opp += (quadrant.charAt(1) == 'l') ? 'r' : 'l'; + + return opp; }, CLASS_NAME: "OpenLayers.Popup.Anchored"