OpenLayers OpenLayers

Changeset 6394

Show
Ignore:
Timestamp:
02/28/08 11:40:08 (11 months ago)
Author:
sbenthall
Message:

partial merge to euzoro's pop sandbox popup code, in anticipation of eventual OL trunk merge.

Main functionality is the switch from shiftMap to panTo based map panning code.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/topp/vespucci/lib/OpenLayers/Popup.js

    r6262 r6394  
    11/* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD 
    22 * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the 
    3  * full text of the license.  */ 
     3 * full text of the license. */ 
     4 
    45 
    56/** 
     
    8384     
    8485    /**  
     86     * Property: backgroundColor  
     87     * {String} the background color used by the popup. 
     88     */ 
     89    backgroundColor: "", 
     90     
     91    /**  
    8592     * Property: opacity  
    8693     * {float} the opacity of this popup (between 0.0 and 1.0) 
     
    8996 
    9097    /**  
     98     * Property: border  
     99     * {String} the border size of the popup.  (eg 2px) 
     100     */ 
     101    border: "", 
     102     
     103    /**  
     104     * Property: contentDiv  
     105     * {DOMElement} a reference to the element that holds the content of 
     106     *              the div. 
     107     */ 
     108    contentDiv: null, 
     109     
     110    /**  
     111     * Property: groupDiv  
     112     * {DOMElement} First and only child of 'div'. The group Div contains the 
     113     *     'contentDiv' and the 'closeDiv'. 
     114     */ 
     115    groupDiv: null, 
     116 
     117    /**  
     118     * Property: closeDiv 
     119     * {DOMElement} the optional closer image 
     120     */ 
     121    closeDiv: null, 
     122 
     123    /**  
     124     * APIProperty: autoSize 
     125     * {Boolean} Resize the popup to auto-fit the contents. 
     126     *     Default is false. 
     127     */ 
     128    autoSize: false, 
     129 
     130    /** 
     131     * APIProperty: minSize 
     132     * {<OpenLayers.Size>} Minimum size allowed for the popup's contents. 
     133     */ 
     134    minSize: null, 
     135 
     136    /** 
     137     * APIProperty: maxSize 
     138     * {<OpenLayers.Size>} Maximum size allowed for the popup's contents. 
     139     */ 
     140    maxSize: null, 
     141 
     142    /** 
     143     * APIProperty: margin 
     144     * {<OpenLayers.Bounds>} Outside margin of the popup. Used to prevent  
     145     *     the popup from getting too close to the map border. 
     146     */ 
     147    margin: null, 
     148 
     149    /** 
     150     * APIProperty: panMapIfOutOfView 
     151     * {Boolean} When drawn, pan map such that the entire popup is visible in 
     152     *     the current viewport (if necessary). 
     153     *     Default is false. 
     154     */ 
     155    panMapIfOutOfView: false, 
     156 
     157    /**  
    91158     * Property: map  
    92      * {<OpenLayers.Map>} Reference to the map. 
     159     * {<OpenLayers.Map>} this gets set in Map.js when the popup is added to the map 
    93160     */ 
    94161    map: null, 
     
    143210        this.id = id; 
    144211        this.lonlat = lonlat; 
     212 
     213        //set default margin -- REDUNDAN WITH OLD MARGIN STUFF 
     214        this.margin = new OpenLayers.Bounds(5, 5, 5, 5); 
     215 
    145216         
    146217        if (closeButton != null) { 
     
    389460    * 
    390461    * Parameters: 
    391     * px - {<OpenLayers.Pixel>} The top and left position of 
    392     * the popup in pixels.  
     462    * px - {<OpenLayers.Pixel>} the position the popup in pixels. 
    393463    *  
    394464    * Returns: 
     
    396466    */ 
    397467    draw: function(px) { 
     468        if (px == null) { 
     469            if ((this.lonlat != null) && (this.map != null)) { 
     470                px = this.map.getLayerPxFromLonLat(this.lonlat); 
     471            } 
     472        } 
     473 
    398474        if (this.size == null) { 
    399475            this.setSize(this.getPackedSize()); 
     
    409485        this.onLoad(); 
    410486         
    411         this.shiftMapIfNotViewable(); 
    412            
     487        this.moveTo(px); 
     488 
    413489        return this.div; 
    414490    }, 
    415      
     491 
    416492    /**  
    417493     * Method: updatePosition 
     
    419495     * then have it move itself to its proper position 
    420496     */ 
    421     updatePosition: function(px) { 
    422         if (px == null && this.map) 
    423         { 
    424             px = this.map.getLayerPxFromLonLat(this.lonlat); 
    425         } 
    426         this.moveTo(px);            
     497    updatePosition: function() { 
     498        if ((this.lonlat) && (this.map)) { 
     499                var px = this.map.getLayerPxFromLonLat(this.lonlat); 
     500                if (px) { 
     501                    this.moveTo(px);            
     502                }     
     503        } 
    427504    }, 
    428505     
     
    457534        return px;  
    458535    }, 
    459  
     536     
    460537    /** 
    461538     * Method: moveTo 
     
    465542     */ 
    466543    moveTo: function(px) { 
    467         px = this.calculateRelativePosition(px, this.size); 
    468          
    469         this.div.style.left = px.x + "px"; 
    470         this.div.style.top = px.y + "px"; 
     544        if ((px != null) && (this.div != null)) { 
     545            px = this.calculateRelativePosition(px, this.size); 
     546            this.div.style.left = px.x + "px"; 
     547            this.div.style.top = px.y + "px"; 
     548        } 
     549         
     550        if (this.panMapIfOutOfView) { 
     551            this.panIntoView(); 
     552        }     
     553         
    471554    }, 
    472555 
     
    508591     
    509592    /** 
     593    * Method: setBackgroundColor 
     594    * Sets the background color of the popup. 
     595    * Parameters: 
     596    * color - {String} the background color.  eg "#FFBBBB" 
     597    */ 
     598    setBackgroundColor:function(color) {  
     599        if (color != undefined) { 
     600            this.backgroundColor = color;  
     601        } 
     602         
     603        if (this.div != null) { 
     604            this.div.style.backgroundColor = this.backgroundColor; 
     605        } 
     606    },   
     607     
     608    /** 
    510609     * Method: setOpacity 
    511610     * Sets the opacity of the popup. 
     
    527626        } 
    528627    },   
     628    /** 
     629     * Method: addCloseBox 
     630     *  
     631     * Parameters: 
     632     * callback - {Function} The callback to be called when the close button 
     633     *     is clicked. 
     634     */ 
     635    addCloseBox: function(callback) { 
     636 
     637       // close icon 
     638        var closeSize = new OpenLayers.Size(17,17); 
     639        var img = OpenLayers.Util.getImagesLocation() + "close.gif"; 
     640        this.closeDiv = OpenLayers.Util.createAlphaImageDiv( 
     641            this.id + "_close", null, closeSize, img 
     642        ); 
     643         
     644        //this is a workaround since API respect won't let us mandate  
     645        // padding be a Bounds object. This needs to be removed in 3.0 
     646        // 
     647        this.fixPadding(); 
     648         
     649        this.closeDiv.style.right = this.padding.right + "px"; 
     650        this.closeDiv.style.top = this.padding.top + "px"; 
     651        this.groupDiv.appendChild(this.closeDiv); 
     652 
     653        var closePopup = callback || function(e) { 
     654            this.hide(); 
     655            OpenLayers.Event.stop(e); 
     656        }; 
     657        OpenLayers.Event.observe(this.closeDiv, "click",  
     658                OpenLayers.Function.bindAsEventListener(closePopup, this)); 
     659    }, 
     660 
     661    /** 
     662     * Method: panIntoView 
     663     * Pans the map such that the popup is totaly viewable (if necessary) 
     664     */ 
     665    panIntoView: function() { 
     666         
     667        var mapSize = this.map.getSize(); 
     668     
     669        //start with the top left corner of the popup, in px,  
     670        // relative to the viewport 
     671        var origTL = this.map.getViewPortPxFromLayerPx( new OpenLayers.Pixel( 
     672            parseInt(this.div.style.left), 
     673            parseInt(this.div.style.top) 
     674        )); 
     675        var newTL = origTL.clone(); 
     676     
     677        //new left (compare to margins, using this.size to calculate right) 
     678        if (origTL.x < this.margin.left) { 
     679            newTL.x = this.margin.left; 
     680        } else  
     681        if ( (origTL.x + this.size.w) > (mapSize.w - this.margin.right)) { 
     682            newTL.x = mapSize.w - this.margin.right - this.size.w; 
     683        } 
     684         
     685        //new top (compare to margins, using this.size to calculate bottom) 
     686        if (origTL.y < this.margin.top) { 
     687            newTL.y = this.margin.top; 
     688        } else  
     689        if ( (origTL.y + this.size.h) > (mapSize.h - this.margin.bottom)) { 
     690            newTL.y = mapSize.h - this.margin.bottom - this.size.h; 
     691        } 
     692         
     693        var dx = origTL.x - newTL.x; 
     694        var dy = origTL.y - newTL.y; 
     695         
     696        this.map.pan(dx, dy); 
     697    }, 
    529698 
    530699    /**  
     
    549718     *    hyperlinks or drag-selecting text. 
    550719     */ 
    551     registerEvents:function() { 
     720    registerEvents:function() { 
    552721        this.events = new OpenLayers.Events(this, this.div, null, true); 
    553         this.events.register("mousedown", this, this.onmousedown); 
    554         this.events.register("mousemove", this, this.onmousemove); 
    555         this.events.register("mouseup", this, this.onmouseup); 
    556         this.events.register("click", this, this.onclick); 
    557         this.events.register("mouseout", this, this.onmouseout); 
    558         this.events.register("dblclick", this, this.ondblclick); 
    559         this.events.register("mouseover", this, this.onmouseover); 
    560     }, 
     722        this.events.on({ 
     723            "mousedown": this.onmousedown, 
     724            "mousemove": this.onmousemove, 
     725            "mouseup": this.onmouseup, 
     726            "onmouseover": this.onmouseover, 
     727            "click": this.onclick, 
     728            "mouseout": this.onmouseout, 
     729            "dblclick": this.ondblclick, 
     730            scope: this 
     731        }); 
     732         
     733     }, 
    561734 
    562735    /**