OpenLayers OpenLayers

Changeset 1634

Show
Ignore:
Timestamp:
10/05/06 23:28:53 (2 years ago)
Author:
euzuro
Message:

break permalink into two controls: permalink and argparser. make the arg parser a default control that is added to the map. furthermore, make it such that on addition to the map, the permalink will hunt through the maps controls to make sure there is an argparser control loaded, and if not it will load one itself. now we add a new parameter to the permalink suite: 'layer'. the application will now save the layer visibility information. finally, update tests so nothing breaks.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers.js

    r1623 r1634  
    9494        "OpenLayers/Control/PanZoom.js", 
    9595        "OpenLayers/Control/PanZoomBar.js", 
     96        "OpenLayers/Control/ArgParser.js", 
    9697        "OpenLayers/Control/Permalink.js", 
    9798        "OpenLayers/Control/Scale.js", 
  • trunk/openlayers/lib/OpenLayers/Control/ArgParser.js

    r1621 r1634  
    88 * @requires OpenLayers/Control.js 
    99 */ 
    10 OpenLayers.Control.Permalink = OpenLayers.Class.create(); 
    11 OpenLayers.Control.Permalink.prototype =  
     10OpenLayers.Control.ArgParser = OpenLayers.Class.create(); 
     11OpenLayers.Control.ArgParser.prototype =  
    1212  OpenLayers.Util.extend( new OpenLayers.Control(), { 
    1313 
    14     /** @type DOMElement */ 
    15     element: null, 
    16      
    17     /** @type String */ 
    18     base: '', 
    19      
    2014    /** @type OpenLayers.LonLat */ 
    2115    center: null, 
     
    3529    initialize: function(element, base) { 
    3630        OpenLayers.Control.prototype.initialize.apply(this, arguments); 
    37         this.element = element;         
    38         if (base) { 
    39             this.base = base; 
     31    }, 
     32 
     33    /** Set the map property for the control.  
     34     *  
     35     * @param {OpenLayers.Map} map 
     36     */ 
     37    setMap: function(map) { 
     38        OpenLayers.Control.prototype.setMap.apply(this, arguments); 
     39 
     40        //make sure we dont already have an arg parser attached 
     41        for(var i=0; i< this.map.controls.length; i++) { 
     42            var control = this.map.controls[i]; 
     43            if ( (control != this) && 
     44                 (control.CLASS_NAME == "OpenLayers.Control.ArgParser") ) { 
     45                break; 
     46            } 
     47        } 
     48        if (i == this.map.controls.length) { 
     49 
     50            var args = OpenLayers.Util.getArgs(); 
     51            if (args.lat && args.lon) { 
     52                this.center = new OpenLayers.LonLat(parseFloat(args.lon), 
     53                                                    parseFloat(args.lat)); 
     54                if (args.zoom) { 
     55                    this.zoom = parseInt(args.zoom); 
     56                } 
     57     
     58                // when we add a new baselayer to see when we can set the center 
     59                this.map.events.register('changebaselayer', this,  
     60                                         this.setCenter); 
     61                this.setCenter(); 
     62            } 
     63     
     64            if (args.layers) { 
     65                this.layers = args.layers; 
     66     
     67                // when we add a new layer, set its visibility  
     68                this.map.events.register('addlayer', this,  
     69                                         this.configureLayers); 
     70                this.configureLayers(); 
     71            } 
    4072        } 
    4173    }, 
    42  
    43     /** 
    44      * @type DOMElement 
    45      */     
    46     draw: function() { 
    47         OpenLayers.Control.prototype.draw.apply(this, arguments); 
    48         var args = OpenLayers.Util.getArgs(); 
    49  
    50         if (args.lat && args.lon) { 
    51             this.center = new OpenLayers.LonLat(parseFloat(args.lon), 
    52                                                 parseFloat(args.lat)); 
    53             if (args.zoom) { 
    54                 this.zoom = parseInt(args.zoom); 
    55             } 
    56  
    57             // when we add a new baselayer to see when we can set the center 
    58             this.map.events.register('changebaselayer', this, this.setCenter); 
    59             this.setCenter(); 
    60         } 
    61  
    62         if (args.layers) { 
    63             this.layers = args.layers; 
    64  
    65             // when we add a new layer, set its visibility  
    66             this.map.events.register('addlayer', this, this.configureLayers); 
    67             this.configureLayers(); 
    68         } 
    69          
    70         if (!this.element) { 
    71             this.element = document.createElement("a"); 
    72             this.div.style.right = "3px"; 
    73             this.div.style.bottom = "3px"; 
    74             this.div.style.left = ""; 
    75             this.div.style.top = ""; 
    76             this.div.style.display = "block"; 
    77             this.div.style.position = "absolute"; 
    78             this.element.style.fontSize="smaller"; 
    79             this.element.innerHTML = "Permalink"; 
    80             this.element.href=""; 
    81             this.div.appendChild(this.element); 
    82         } 
    83         this.map.events.register('moveend', this, this.updateLink); 
    84         return this.div; 
    85     }, 
    8674    
    87     /** 
    88      *  
    89      */ 
    90     updateLink: function() { 
    91         var center = this.map.getCenter(); 
    92         var zoom = "zoom=" + this.map.getZoom();  
    93         var lat = "lat=" + Math.round(center.lat*100000)/100000; 
    94         var lon = "lon=" + Math.round(center.lon*100000)/100000; 
    95  
    96         var layers = "layers="; 
    97         for(var i=0; i< this.map.layers.length; i++) { 
    98             var layer = this.map.layers[i]; 
    99  
    100             if (layer.isBaseLayer) { 
    101                 layers += (layer == this.map.baseLayer) ? "B" : "0"; 
    102             } else { 
    103                 layers += (layer.getVisibility()) ? "T" : "F";            
    104             } 
    105         } 
    106         var href = this.base + "?" + lat + "&" + lon + "&" + zoom +  
    107                                    "&" + layers;  
    108         this.element.href = href; 
    109     },  
    110  
    11175    /** As soon as a baseLayer has been loaded, we center and zoom 
    11276     *   ...and remove the handler. 
     
    146110     
    147111    /** @final @type String */ 
    148     CLASS_NAME: "OpenLayers.Control.Permalink
     112    CLASS_NAME: "OpenLayers.Control.ArgParser
    149113}); 
  • trunk/openlayers/lib/OpenLayers/Control/Permalink.js

    r1621 r1634  
    1717    /** @type String */ 
    1818    base: '', 
    19      
    20     /** @type OpenLayers.LonLat */ 
    21     center: null, 
    22      
    23     /** @type int */ 
    24     zoom: null, 
    25  
    26     /** @type Array */ 
    27     layers: null, 
    2819 
    2920    /** 
     
    4132    }, 
    4233 
     34    /** Set the map property for the control.  
     35     *  
     36     * @param {OpenLayers.Map} map 
     37     */ 
     38    setMap: function(map) { 
     39        OpenLayers.Control.prototype.setMap.apply(this, arguments); 
     40 
     41        //make sure we have an arg parser attached 
     42        for(var i=0; i< this.map.controls.length; i++) { 
     43            var control = this.map.controls[i]; 
     44            if (control.CLASS_NAME == "OpenLayers.Control.ArgParser") { 
     45                break; 
     46            } 
     47        } 
     48        if (i == this.map.controls.length) { 
     49            this.map.addControl(new OpenLayers.Control.ArgParser());        
     50        } 
     51 
     52    }, 
     53 
    4354    /** 
    4455     * @type DOMElement 
     
    4657    draw: function() { 
    4758        OpenLayers.Control.prototype.draw.apply(this, arguments); 
    48         var args = OpenLayers.Util.getArgs(); 
    4959 
    50         if (args.lat && args.lon) { 
    51             this.center = new OpenLayers.LonLat(parseFloat(args.lon), 
    52                                                 parseFloat(args.lat)); 
    53             if (args.zoom) { 
    54                 this.zoom = parseInt(args.zoom); 
    55             } 
    56  
    57             // when we add a new baselayer to see when we can set the center 
    58             this.map.events.register('changebaselayer', this, this.setCenter); 
    59             this.setCenter(); 
    60         } 
    61  
    62         if (args.layers) { 
    63             this.layers = args.layers; 
    64  
    65             // when we add a new layer, set its visibility  
    66             this.map.events.register('addlayer', this, this.configureLayers); 
    67             this.configureLayers(); 
    68         } 
    69          
    7060        if (!this.element) { 
    7161            this.element = document.createElement("a"); 
     
    10999    },  
    110100 
    111     /** As soon as a baseLayer has been loaded, we center and zoom 
    112      *   ...and remove the handler. 
    113      */ 
    114     setCenter: function() { 
    115          
    116         if (this.map.baseLayer) { 
    117             //dont need to listen for this one anymore 
    118             this.map.events.unregister('changebaselayer', this,  
    119                                        this.setCenter); 
    120                                         
    121             this.map.setCenter(this.center, this.zoom); 
    122         } 
    123     }, 
    124  
    125     /** As soon as all the layers are loaded, cycle through them and  
    126      *   hide or show them.  
    127      */ 
    128     configureLayers: function() { 
    129  
    130         if (this.layers.length == this.map.layers.length) {  
    131             this.map.events.unregister('addlayer', this, this.configureLayers); 
    132  
    133             for(var i=0; i < this.layers.length; i++) { 
    134                  
    135                 var layer = this.map.layers[i]; 
    136                 var c = this.layers.charAt(i); 
    137                  
    138                 if (c == "B") { 
    139                     this.map.setBaseLayer(layer); 
    140                 } else if ( (c == "T") || (c == "F") ) { 
    141                     layer.setVisibility(c == "T"); 
    142                 } 
    143             } 
    144         } 
    145     },      
    146      
    147101    /** @final @type String */ 
    148102    CLASS_NAME: "OpenLayers.Control.Permalink" 
  • trunk/openlayers/lib/OpenLayers/Map.js

    r1625 r1634  
    165165            if (OpenLayers.Control != null) { // running full or lite? 
    166166                this.controls = [ new OpenLayers.Control.MouseDefaults(), 
    167                                   new OpenLayers.Control.PanZoom()]; 
     167                                  new OpenLayers.Control.PanZoom(), 
     168                                  new OpenLayers.Control.ArgParser() 
     169                                ]; 
    168170            } else { 
    169171                this.controls = []; 
  • trunk/openlayers/tests/test_Control_MouseToolbar.html

    r1450 r1634  
    1818        map.addControl(control); 
    1919        t.ok( control.map === map, "Control.map is set to the map object" ); 
    20         t.ok( map.controls[2] === control, "map.controls contains control" ); 
    21         t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Control div zIndexed properly" ); 
    22         t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Viewport div contains control div" ); 
     20        t.ok( map.controls[3] === control, "map.controls contains control" ); 
     21        t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Control div zIndexed properly" ); 
     22        t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Viewport div contains control div" ); 
    2323        t.eq( control.div.style.top, "6px", "Control div top located correctly by default"); 
    2424 
  • trunk/openlayers/tests/test_Control_PanZoom.html

    r1424 r1634  
    1818        map.addControl(control); 
    1919        t.ok( control.map === map, "Control.map is set to the map object" ); 
    20         t.ok( map.controls[2] === control, "map.controls contains control" ); 
    21         t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Control div zIndexed properly" ); 
    22         t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 3, "Viewport div contains control div" ); 
     20        t.ok( map.controls[3] === control, "map.controls contains control" ); 
     21        t.eq( parseInt(control.div.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Control div zIndexed properly" ); 
     22        t.eq( parseInt(map.viewPortDiv.lastChild.style.zIndex), map.Z_INDEX_BASE['Control'] + 4, "Viewport div contains control div" ); 
    2323        t.eq( control.div.style.top, "4px", "Control div top located correctly by default"); 
    2424 
  • trunk/openlayers/tests/test_Control_Permalink.html

    r1621 r1634  
    4747        map = new OpenLayers.Map($('map')); 
    4848        map.addControl(control); 
    49         t.eq(map.controls[2].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." ); 
     49        t.eq(map.controls[3].div.firstChild.nodeName, "A", "Permalink control creates div with 'a' inside." ); 
    5050  } 
    5151  // -->