OpenLayers OpenLayers

Ticket #1489: 1489-r7869-A0.patch

File 1489-r7869-A0.patch, 5.9 kB (added by ahocevar, 4 months ago)
  • 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 argParser = OpenLayers.Class(OpenLayers.Control.ArgParser, { 
     116            CLASS_NAME: "CustomArgParser" 
     117        }); 
     118       
     119        control = new OpenLayers.Control.Permalink(null, "./edit.html", { 
     120            argParser: argParser, 
     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: argParser 
     21     * {Class} The ArgParser control to use with this control 
     22     */ 
     23    argParser: OpenLayers.Control.ArgParser, 
    1824 
    1925    /**  
    2026     * Property: element  
     
    8187        //make sure we have an arg parser attached 
    8288        for(var i=0, len=this.map.controls.length; i<len; i++) { 
    8389            var control = this.map.controls[i]; 
    84             if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") { 
     90            if (control.CLASS_NAME == this.argParser.CLASS_NAME) { 
    8591                 
    8692                // If a permalink is added to the map, and an ArgParser already 
    8793                // exists, we override the displayProjection to be the one 
     
    94100            } 
    95101        } 
    96102        if (i == this.map.controls.length) { 
    97             this.map.addControl(new OpenLayers.Control.ArgParser( 
     103            this.map.addControl(new this.argParser( 
    98104                { 'displayProjection': this.displayProjection }));        
    99105        } 
    100106 
     
    110116        OpenLayers.Control.prototype.draw.apply(this, arguments); 
    111117           
    112118        if (!this.element) { 
     119            this.div.className = this.displayClass; 
    113120            this.element = document.createElement("a"); 
    114121            this.element.innerHTML = OpenLayers.i18n("permalink"); 
    115122            this.element.href=""; 
     
    121128            'changebaselayer': this.updateLink, 
    122129            scope: this 
    123130        }); 
     131         
     132        // Make it so there is at least a link even though the map may not have 
     133        // moved yet. 
     134        this.updateLink(); 
     135         
    124136        return this.div; 
    125137    }, 
    126138    
     
    128140     * Method: updateLink  
    129141     */ 
    130142    updateLink: function() { 
    131         var center = this.map.getCenter(); 
     143        var href = this.base; 
     144        if (href.indexOf('?') != -1) { 
     145            href = href.substring( 0, href.indexOf('?') ); 
     146        } 
     147 
     148        href += '?' + OpenLayers.Util.getParameterString(this.createParams()); 
     149        this.element.href = href; 
     150    },  
     151     
     152    /** 
     153     * APIMethod: createParams 
     154     * Creates the parameters that need to be encoded into the permalink url. 
     155     *  
     156     * Parameters: 
     157     * center - {<OpenLayers.LonLat>} center to encode in the permalink. 
     158     *     Defaults to the current map center. 
     159     * zoom - {Integer} zoom level to encode in the permalink. Defaults to the 
     160     *     current map zoom level. 
     161     * layers - {Array(<OpenLayers.Layer>)} layers to encode in the permalink. 
     162     *     Defaults to the current map layers. 
     163     *  
     164     * Returns: 
     165     * {Object} Hash of parameters that will be url-encoded into the 
     166     * permalink. 
     167     */ 
     168    createParams: function(center, zoom, layers) { 
     169        center = center || this.map.getCenter(); 
     170        zoom = zoom || this.map.getZoom(); 
     171        layers = layers || this.map.layers;   
     172           
     173        var params = OpenLayers.Util.getParameters(this.base); 
    132174         
    133         // Map not initialized yet. Break out of this function. 
     175        // If there's still no center, map is not initialized yet.  
     176        // Break out of this function, and simply return the params from the 
     177        // base link. 
    134178        if (!center) {  
    135             return;  
     179            return params;  
    136180        } 
    137181 
    138         var params = OpenLayers.Util.getParameters(this.base); 
    139          
    140182        params.zoom = this.map.getZoom();  
    141183        var lat = center.lat; 
    142184        var lon = center.lon; 
     
    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"