[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * @requires OpenLayers/Popup.js |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | /** |
---|
| 12 | * Class: OpenLayers.Popup.Anchored |
---|
| 13 | * |
---|
| 14 | * Inherits from: |
---|
| 15 | * - <OpenLayers.Popup> |
---|
| 16 | */ |
---|
| 17 | OpenLayers.Popup.Anchored = |
---|
| 18 | OpenLayers.Class(OpenLayers.Popup, { |
---|
| 19 | |
---|
| 20 | /** |
---|
| 21 | * Parameter: relativePosition |
---|
| 22 | * {String} Relative position of the popup ("br", "tr", "tl" or "bl"). |
---|
| 23 | */ |
---|
| 24 | relativePosition: null, |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * APIProperty: keepInMap |
---|
| 28 | * {Boolean} If panMapIfOutOfView is false, and this property is true, |
---|
| 29 | * contrain the popup such that it always fits in the available map |
---|
| 30 | * space. By default, this is set. If you are creating popups that are |
---|
| 31 | * near map edges and not allowing pannning, and especially if you have |
---|
| 32 | * a popup which has a fixedRelativePosition, setting this to false may |
---|
| 33 | * be a smart thing to do. |
---|
| 34 | * |
---|
| 35 | * For anchored popups, default is true, since subclasses will |
---|
| 36 | * usually want this functionality. |
---|
| 37 | */ |
---|
| 38 | keepInMap: true, |
---|
| 39 | |
---|
| 40 | /** |
---|
| 41 | * Parameter: anchor |
---|
| 42 | * {Object} Object to which we'll anchor the popup. Must expose a |
---|
| 43 | * 'size' (<OpenLayers.Size>) and 'offset' (<OpenLayers.Pixel>). |
---|
| 44 | */ |
---|
| 45 | anchor: null, |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * Constructor: OpenLayers.Popup.Anchored |
---|
| 49 | * |
---|
| 50 | * Parameters: |
---|
| 51 | * id - {String} |
---|
| 52 | * lonlat - {<OpenLayers.LonLat>} |
---|
| 53 | * contentSize - {<OpenLayers.Size>} |
---|
| 54 | * contentHTML - {String} |
---|
| 55 | * anchor - {Object} Object which must expose a 'size' <OpenLayers.Size> |
---|
| 56 | * and 'offset' <OpenLayers.Pixel> (generally an <OpenLayers.Icon>). |
---|
| 57 | * closeBox - {Boolean} |
---|
| 58 | * closeBoxCallback - {Function} Function to be called on closeBox click. |
---|
| 59 | */ |
---|
| 60 | initialize:function(id, lonlat, contentSize, contentHTML, anchor, closeBox, |
---|
| 61 | closeBoxCallback) { |
---|
| 62 | var newArguments = [ |
---|
| 63 | id, lonlat, contentSize, contentHTML, closeBox, closeBoxCallback |
---|
| 64 | ]; |
---|
| 65 | OpenLayers.Popup.prototype.initialize.apply(this, newArguments); |
---|
| 66 | |
---|
| 67 | this.anchor = (anchor != null) ? anchor |
---|
| 68 | : { size: new OpenLayers.Size(0,0), |
---|
| 69 | offset: new OpenLayers.Pixel(0,0)}; |
---|
| 70 | }, |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * APIMethod: destroy |
---|
| 74 | */ |
---|
| 75 | destroy: function() { |
---|
| 76 | this.anchor = null; |
---|
| 77 | this.relativePosition = null; |
---|
| 78 | |
---|
| 79 | OpenLayers.Popup.prototype.destroy.apply(this, arguments); |
---|
| 80 | }, |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * APIMethod: show |
---|
| 84 | * Overridden from Popup since user might hide popup and then show() it |
---|
| 85 | * in a new location (meaning we might want to update the relative |
---|
| 86 | * position on the show) |
---|
| 87 | */ |
---|
| 88 | show: function() { |
---|
| 89 | this.updatePosition(); |
---|
| 90 | OpenLayers.Popup.prototype.show.apply(this, arguments); |
---|
| 91 | }, |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Method: moveTo |
---|
| 95 | * Since the popup is moving to a new px, it might need also to be moved |
---|
| 96 | * relative to where the marker is. We first calculate the new |
---|
| 97 | * relativePosition, and then we calculate the new px where we will |
---|
| 98 | * put the popup, based on the new relative position. |
---|
| 99 | * |
---|
| 100 | * If the relativePosition has changed, we must also call |
---|
| 101 | * updateRelativePosition() to make any visual changes to the popup |
---|
| 102 | * which are associated with putting it in a new relativePosition. |
---|
| 103 | * |
---|
| 104 | * Parameters: |
---|
| 105 | * px - {<OpenLayers.Pixel>} |
---|
| 106 | */ |
---|
| 107 | moveTo: function(px) { |
---|
| 108 | var oldRelativePosition = this.relativePosition; |
---|
| 109 | this.relativePosition = this.calculateRelativePosition(px); |
---|
| 110 | |
---|
| 111 | var newPx = this.calculateNewPx(px); |
---|
| 112 | |
---|
| 113 | var newArguments = new Array(newPx); |
---|
| 114 | OpenLayers.Popup.prototype.moveTo.apply(this, newArguments); |
---|
| 115 | |
---|
| 116 | //if this move has caused the popup to change its relative position, |
---|
| 117 | // we need to make the appropriate cosmetic changes. |
---|
| 118 | if (this.relativePosition != oldRelativePosition) { |
---|
| 119 | this.updateRelativePosition(); |
---|
| 120 | } |
---|
| 121 | }, |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * APIMethod: setSize |
---|
| 125 | * |
---|
| 126 | * Parameters: |
---|
| 127 | * contentSize - {<OpenLayers.Size>} the new size for the popup's |
---|
| 128 | * contents div (in pixels). |
---|
| 129 | */ |
---|
| 130 | setSize:function(contentSize) { |
---|
| 131 | OpenLayers.Popup.prototype.setSize.apply(this, arguments); |
---|
| 132 | |
---|
| 133 | if ((this.lonlat) && (this.map)) { |
---|
| 134 | var px = this.map.getLayerPxFromLonLat(this.lonlat); |
---|
| 135 | this.moveTo(px); |
---|
| 136 | } |
---|
| 137 | }, |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Method: calculateRelativePosition |
---|
| 141 | * |
---|
| 142 | * Parameters: |
---|
| 143 | * px - {<OpenLayers.Pixel>} |
---|
| 144 | * |
---|
| 145 | * Returns: |
---|
| 146 | * {String} The relative position ("br" "tr" "tl" "bl") at which the popup |
---|
| 147 | * should be placed. |
---|
| 148 | */ |
---|
| 149 | calculateRelativePosition:function(px) { |
---|
| 150 | var lonlat = this.map.getLonLatFromLayerPx(px); |
---|
| 151 | |
---|
| 152 | var extent = this.map.getExtent(); |
---|
| 153 | var quadrant = extent.determineQuadrant(lonlat); |
---|
| 154 | |
---|
| 155 | return OpenLayers.Bounds.oppositeQuadrant(quadrant); |
---|
| 156 | }, |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * Method: updateRelativePosition |
---|
| 160 | * The popup has been moved to a new relative location, so we may want to |
---|
| 161 | * make some cosmetic adjustments to it. |
---|
| 162 | * |
---|
| 163 | * Note that in the classic Anchored popup, there is nothing to do |
---|
| 164 | * here, since the popup looks exactly the same in all four positions. |
---|
| 165 | * Subclasses such as the AnchoredBubble and Framed, however, will |
---|
| 166 | * want to do something special here. |
---|
| 167 | */ |
---|
| 168 | updateRelativePosition: function() { |
---|
| 169 | //to be overridden by subclasses |
---|
| 170 | }, |
---|
| 171 | |
---|
| 172 | /** |
---|
| 173 | * Method: calculateNewPx |
---|
| 174 | * |
---|
| 175 | * Parameters: |
---|
| 176 | * px - {<OpenLayers.Pixel>} |
---|
| 177 | * |
---|
| 178 | * Returns: |
---|
| 179 | * {<OpenLayers.Pixel>} The the new px position of the popup on the screen |
---|
| 180 | * relative to the passed-in px. |
---|
| 181 | */ |
---|
| 182 | calculateNewPx:function(px) { |
---|
| 183 | var newPx = px.offset(this.anchor.offset); |
---|
| 184 | |
---|
| 185 | //use contentSize if size is not already set |
---|
| 186 | var size = this.size || this.contentSize; |
---|
| 187 | |
---|
| 188 | var top = (this.relativePosition.charAt(0) == 't'); |
---|
| 189 | newPx.y += (top) ? -(size.h + this.anchor.size.h) : this.anchor.size.h; |
---|
| 190 | |
---|
| 191 | var left = (this.relativePosition.charAt(1) == 'l'); |
---|
| 192 | newPx.x += (left) ? -(size.w + this.anchor.size.w) : this.anchor.size.w; |
---|
| 193 | |
---|
| 194 | return newPx; |
---|
| 195 | }, |
---|
| 196 | |
---|
| 197 | CLASS_NAME: "OpenLayers.Popup.Anchored" |
---|
| 198 | }); |
---|