OpenLayers OpenLayers

Ticket #1489: 1489-r7874-A1.patch

File 1489-r7874-A1.patch, 5.9 kB (added by ahocevar, 4 months ago)

argParser renamed to argParserClass

  • 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(); 
     144        var href = this.base; 
     145        if (href.indexOf('?') != -1) { 
     146            href = href.substring( 0, href.indexOf('?') ); 
     147        } 
     148 
     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           
     174        var params = OpenLayers.Util.getParameters(this.base); 
    132175         
    133         // Map not initialized yet. Break out of this function. 
     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. 
    134179        if (!center) {  
    135             return;  
     180            return params;  
    136181        } 
    137182 
    138         var params = OpenLayers.Util.getParameters(this.base); 
    139          
    140183        params.zoom = this.map.getZoom();  
    141184        var lat = center.lat; 
    142185        var lon = center.lon; 
     
    163206            } 
    164207        } 
    165208 
    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; 
     209        return params; 
    173210    },  
    174211 
    175212    CLASS_NAME: "OpenLayers.Control.Permalink"