OpenLayers OpenLayers

Ticket #1489: permalink.2.patch

File permalink.2.patch, 7.6 kB (added by euzuro, 4 months ago)

tiny mod to previous patch -- removing the double-return

  • tests/Control/Permalink.html

    old new  
    108108        OpenLayers.Util.getElement('edit_permalink').href = './edit.html?zoom=2&lat=0&lon=1.75781&layers=B'; 
    109109        t.eq(OpenLayers.Util.getElement('permalink').href, OpenLayers.Util.getElement('edit_permalink').href, "Panning sets permalink with existing zoom in base"); 
    110110  } 
     111   
     112    function test_Control_Permalink_customized(t) { 
     113        t.plan(2); 
     114       
     115        var argParserClass = OpenLayers.Class(OpenLayers.Control.ArgParser, { 
     116            CLASS_NAME: "CustomArgParser" 
     117        }); 
     118       
     119        control = new OpenLayers.Control.Permalink(null, "./edit.html", { 
     120            argParserClass: argParserClass, 
     121            createParams: function(center, zoom, layers) { 
     122                var params = OpenLayers.Control.Permalink.prototype.createParams.apply(control, arguments); 
     123                params.customParam = "foo"; 
     124                return params; 
     125            } 
     126        }); 
     127        map = new OpenLayers.Map('map'); 
     128        layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'}); 
     129        map.addLayer(layer); 
     130        if (!map.getCenter())  map.zoomToMaxExtent(); 
     131        map.addControl(control); 
     132        map.pan(5, 0, {animate:false}); 
     133       
     134        t.eq(this.map.controls[this.map.controls.length-1].CLASS_NAME, "CustomArgParser", "Custom ArgParser added correctly."); 
     135        t.eq(control.div.firstChild.getAttribute("href"), "./edit.html?zoom=2&lat=0&lon=1.75781&layers=B&customParam=foo", "Custom parameter encoded correctly."); 
     136    } 
    111137  </script> 
    112138</head> 
    113139<body> 
  • lib/OpenLayers/Control/Permalink.js

    old new  
    1515 *  - <OpenLayers.Control> 
    1616 */ 
    1717OpenLayers.Control.Permalink = OpenLayers.Class(OpenLayers.Control, { 
     18     
     19    /** 
     20     * APIProperty: argParserClass 
     21     * {Class} The ArgParser control class (not instance) to use with this 
     22     *     control. 
     23     */ 
     24    argParserClass: OpenLayers.Control.ArgParser, 
    1825 
    1926    /**  
    2027     * Property: element  
     
    8188        //make sure we have an arg parser attached 
    8289        for(var i=0, len=this.map.controls.length; i<len; i++) { 
    8390            var control = this.map.controls[i]; 
    84             if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") { 
     91            if (control.CLASS_NAME == this.argParserClass.CLASS_NAME) { 
    8592                 
    8693                // If a permalink is added to the map, and an ArgParser already 
    8794                // exists, we override the displayProjection to be the one 
     
    94101            } 
    95102        } 
    96103        if (i == this.map.controls.length) { 
    97             this.map.addControl(new OpenLayers.Control.ArgParser
     104            this.map.addControl(new this.argParserClass
    98105                { 'displayProjection': this.displayProjection }));        
    99106        } 
    100107 
     
    110117        OpenLayers.Control.prototype.draw.apply(this, arguments); 
    111118           
    112119        if (!this.element) { 
     120            this.div.className = this.displayClass; 
    113121            this.element = document.createElement("a"); 
    114122            this.element.innerHTML = OpenLayers.i18n("permalink"); 
    115123            this.element.href=""; 
     
    121129            'changebaselayer': this.updateLink, 
    122130            scope: this 
    123131        }); 
     132         
     133        // Make it so there is at least a link even though the map may not have 
     134        // moved yet. 
     135        this.updateLink(); 
     136         
    124137        return this.div; 
    125138    }, 
    126139    
     
    128141     * Method: updateLink  
    129142     */ 
    130143    updateLink: function() { 
    131         var center = this.map.getCenter(); 
    132          
    133         // Map not initialized yet. Break out of this function. 
    134         if (!center) {  
    135             return;  
     144        var href = this.base; 
     145        if (href.indexOf('?') != -1) { 
     146            href = href.substring( 0, href.indexOf('?') ); 
    136147        } 
    137148 
     149        href += '?' + OpenLayers.Util.getParameterString(this.createParams()); 
     150        this.element.href = href; 
     151    },  
     152     
     153    /** 
     154     * APIMethod: createParams 
     155     * Creates the parameters that need to be encoded into the permalink url. 
     156     *  
     157     * Parameters: 
     158     * center - {<OpenLayers.LonLat>} center to encode in the permalink. 
     159     *     Defaults to the current map center. 
     160     * zoom - {Integer} zoom level to encode in the permalink. Defaults to the 
     161     *     current map zoom level. 
     162     * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink. 
     163     *     Defaults to the current map layers. 
     164     *  
     165     * Returns: 
     166     * {Object} Hash of parameters that will be url-encoded into the 
     167     * permalink. 
     168     */ 
     169    createParams: function(center, zoom, layers) { 
     170        center = center || this.map.getCenter(); 
     171        zoom = zoom || this.map.getZoom(); 
     172        layers = layers || this.map.layers;   
     173           
    138174        var params = OpenLayers.Util.getParameters(this.base); 
    139175         
    140         params.zoom = this.map.getZoom();  
    141         var lat = center.lat; 
    142         var lon = center.lon; 
    143          
    144         if (this.displayProjection) { 
    145             var mapPosition = OpenLayers.Projection.transform( 
    146               { x: lon, y: lat },  
    147               this.map.getProjectionObject(),  
    148               this.displayProjection ); 
    149             lon = mapPosition.x;   
    150             lat = mapPosition.y;   
    151         }        
    152         params.lat = Math.round(lat*100000)/100000; 
    153         params.lon = Math.round(lon*100000)/100000; 
    154          
    155         params.layers = ''; 
    156         for (var i=0, len=this.map.layers.length; i<len; i++) { 
    157             var layer = this.map.layers[i]; 
    158  
    159             if (layer.isBaseLayer) { 
    160                 params.layers += (layer == this.map.baseLayer) ? "B" : "0"; 
    161             } else { 
    162                 params.layers += (layer.getVisibility()) ? "T" : "F";            
     176        // If there's still no center, map is not initialized yet.  
     177        // Break out of this function, and simply return the params from the 
     178        // base link. 
     179        if (center) {  
     180     
     181            params.zoom = this.map.getZoom();  
     182            var lat = center.lat; 
     183            var lon = center.lon; 
     184             
     185            if (this.displayProjection) { 
     186                var mapPosition = OpenLayers.Projection.transform( 
     187                  { x: lon, y: lat },  
     188                  this.map.getProjectionObject(),  
     189                  this.displayProjection ); 
     190                lon = mapPosition.x;   
     191                lat = mapPosition.y;   
     192            }        
     193            params.lat = Math.round(lat*100000)/100000; 
     194            params.lon = Math.round(lon*100000)/100000; 
     195             
     196            params.layers = ''; 
     197            for (var i=0, len=this.map.layers.length; i<len; i++) { 
     198                var layer = this.map.layers[i]; 
     199     
     200                if (layer.isBaseLayer) { 
     201                    params.layers += (layer == this.map.baseLayer) ? "B" : "0"; 
     202                } else { 
     203                    params.layers += (layer.getVisibility()) ? "T" : "F";            
     204                } 
    163205            } 
    164206        } 
    165207 
    166         var href = this.base; 
    167         if (href.indexOf('?') != -1) { 
    168             href = href.substring( 0, href.indexOf('?') ); 
    169         } 
    170  
    171         href += '?' + OpenLayers.Util.getParameterString(params); 
    172         this.element.href = href; 
     208        return params; 
    173209    },  
    174210 
    175211    CLASS_NAME: "OpenLayers.Control.Permalink"