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