OpenLayers OpenLayers

Ticket #1628: 1628-r7874.B1.patch

File 1628-r7874.B1.patch, 11.0 kB (added by ahocevar, 4 months ago)
  • tests/Handler/Feature.html

    old new  
    3030    } 
    3131 
    3232    function test_Handler_Feature_activate(t) { 
    33         t.plan(4); 
     33        t.plan(3); 
    3434        var map = new OpenLayers.Map('map'); 
    3535        var control = new OpenLayers.Control(); 
    3636        map.addControl(control); 
     
    4747        activated = handler.activate(); 
    4848        t.ok(activated, 
    4949             "activate returns true if the handler was not already active"); 
    50         t.eq(handler.layerIndex, zIndex, 
    51              "layerIndex property properly set"); 
    5250        t.eq(parseInt(layer.div.style.zIndex), 
    53              map.Z_INDEX_BASE['Popup'] - 1
     51             map.Z_INDEX_BASE['Feature']
    5452             "layer z-index properly adjusted"); 
    5553         
    5654    } 
     
    227225        map.addControl(control); 
    228226        var layer = new OpenLayers.Layer(); 
    229227        map.addLayer(layer); 
     228        var layerIndex = parseInt(layer.div.style.zIndex); 
     229         
    230230        var handler = new OpenLayers.Handler.Feature(control, layer); 
    231231        handler.active = false; 
    232232        var deactivated = handler.deactivate(); 
     
    234234             "deactivate returns false if the handler was not already active"); 
    235235         
    236236        handler.active = true; 
    237         handler.layerIndex = Math.floor(Math.random() * 10); 
    238237 
    239238        deactivated = handler.deactivate(); 
    240239        t.ok(deactivated, 
    241240             "deactivate returns true if the handler was active already"); 
    242241        t.eq(parseInt(layer.div.style.zIndex), 
    243              handler.layerIndex, 
     242             layerIndex, 
    244243             "deactivate sets the layer z-index back"); 
    245244    } 
    246245 
  • tests/manual/vector-layer-zindex.html

    old new  
     1<html xmlns="http://www.w3.org/1999/xhtml"> 
     2  <head> 
     3    <title>Vector Layer ZIndex Test</title> 
     4    <style type="text/css"> 
     5        body { 
     6            font-size: 0.8em; 
     7        } 
     8        p { 
     9            padding-top: 1em; 
     10        } 
     11        #map { 
     12            margin: 1em; 
     13            width: 512px; 
     14            height: 512px; 
     15        } 
     16    </style> 
     17 
     18    <script src="../../lib/OpenLayers.js"></script> 
     19    <script type="text/javascript"> 
     20        var map, layerA, layerB, layerV, selectControl1, selectControl2; 
     21 
     22        function init() { 
     23            map = new OpenLayers.Map('map'); 
     24            var wmsLayer = new OpenLayers.Layer.WMS( 
     25                "OpenLayers WMS",  
     26                "http://labs.metacarta.com/wms/vmap0", 
     27                {layers: 'basic'} 
     28            ); 
     29 
     30            layerV = new OpenLayers.Layer.Vector('v'); 
     31            var feature = new OpenLayers.Feature.Vector( 
     32                new OpenLayers.Geometry.Polygon([ 
     33                    new OpenLayers.Geometry.LinearRing([ 
     34                        new OpenLayers.Geometry.Point(-110, 60), 
     35                        new OpenLayers.Geometry.Point(-110, 30), 
     36                        new OpenLayers.Geometry.Point(-80, 30), 
     37                        new OpenLayers.Geometry.Point(-110, 60) 
     38                    ]) 
     39                ]) 
     40            ); 
     41            layerV.addFeatures([feature]); 
     42            selectControl1 = new OpenLayers.Control.SelectFeature(layerV); 
     43            selectControl2 = new OpenLayers.Control.SelectFeature(layerV, { 
     44                hover: true, 
     45                selectStyle: OpenLayers.Util.applyDefaults({fillColor: "red"}, OpenLayers.Feature.Vector.style["select"]), 
     46                onSelect: function(feature) { 
     47                    selectControl2.unselect(feature); 
     48                    layerV.drawFeature(feature, selectControl2.selectStyle); 
     49                } 
     50            }); 
     51 
     52            layerA = new OpenLayers.Layer('a', {'isBaseLayer': false}); 
     53            layerB = new OpenLayers.Layer.WMS( 
     54                'b', 'http://www2.dmsolutions.ca/cgi-bin/mswms_gmap', { 
     55                    'layers': [ 
     56                        'bathymetry', 'land_fn', 'park', 'drain_fn', 'drainage', 
     57                        'prov_bound', 'fedlimit', 'rail', 'road', 'popplace' 
     58                    ], 
     59                    'transparent': 'true', 
     60                    'format': 'image/png' 
     61                }, { 
     62                    'reproject': false 
     63            }); 
     64 
     65            map.addLayers([wmsLayer, layerV, layerA, layerB]); 
     66            map.addControl(selectControl2); 
     67            map.addControl(selectControl1); 
     68            map.addControl(new OpenLayers.Control.MousePosition()); 
     69            selectControl2.activate(); 
     70            selectControl1.activate(); 
     71 
     72            map.setCenter(new OpenLayers.LonLat(-90, 20), 2); 
     73        } 
     74 
     75        function removeLayerA() { 
     76            if (OpenLayers.Util.indexOf(map.layers, layerA) > -1) { 
     77                map.removeLayer(layerA); 
     78            } 
     79        } 
     80         
     81        function toggleSelectControl1() { 
     82            if (selectControl1.active) { 
     83                selectControl1.deactivate(); 
     84                alert("SelectFeature control for clicks deactivated."); 
     85            } else { 
     86                selectControl1.activate(); 
     87                alert("SelectFeature control for clicks activated."); 
     88            } 
     89        } 
     90 
     91        function toggleSelectControl2() { 
     92            if (selectControl2.active) { 
     93                selectControl2.deactivate(); 
     94                alert("SelectFeature control for hover deactivated."); 
     95            } else { 
     96                selectControl2.activate(); 
     97                alert("SelectFeature control for hover activated."); 
     98            } 
     99        } 
     100    </script> 
     101  </head> 
     102  <body onload="init()"> 
     103    <h1 id="title">Vector Layer ZIndex Test</h1> 
     104    <div id="map"></div> 
     105    <p> 
     106 
     107    The map includes one base layer (vmap0) and three overlays, namely a vector 
     108    layer, a fake layer with no images, and a dmsolutions layer. The overlays are 
     109    added to the map in this order: the vector layer, the fake layer, and the 
     110    dmsolutions layer. The map also includes a select feature control, which 
     111    when activated bumped the vector layer z-index to some high value. This 
     112    makes feature selection work, even though other overlays were added after 
     113    the vector layer. 
     114        
     115    </p> 
     116    <p> 
     117 
     118    If the fake layer is removed from the map (first link below), the vector layer's 
     119    z-index must not be reset, so the vector layer must not go under the 
     120    dmsolutions layer and feature selection must continue to function as 
     121    expected. 
     122        
     123    </p> 
     124    <p> 
     125 
     126    If one of the SelectFeature controls is deactivated or activated (second 
     127    and third link below), the vector layer should change it's position in the 
     128    layer stack: on top if at least one of the controls is activated, covered 
     129    by other layers if both are deactivated. 
     130 
     131    </p> 
     132 
     133    <p> 
     134      <a href="javascript:removeLayerA()">Remove the fake layer</a> 
     135      <br/><a href="javascript:toggleSelectControl1()">Toggle the click SelectFeature control's active status</a> 
     136      <br/><a href="javascript:toggleSelectControl2()">Toggle the hover SelectFeature control's active status</a> 
     137    </p> 
     138  </body> 
     139</html> 
  • lib/OpenLayers/Map.js

    old new  
    2323     * Constant: Z_INDEX_BASE 
    2424     * {Object} Base z-indexes for different classes of thing  
    2525     */ 
    26     Z_INDEX_BASE: { BaseLayer: 100, Overlay: 325, Popup: 750, Control: 1000 }, 
     26    Z_INDEX_BASE: { 
     27        BaseLayer: 100, 
     28        Overlay: 325, 
     29        Feature: 725, 
     30        Popup: 750, 
     31        Control: 1000 
     32    }, 
    2733 
    2834    /** 
    2935     * Constant: EVENT_TYPES 
  • lib/OpenLayers/Handler/Feature.js

    old new  
    9999     *      value of stopUp. Defaults to false. 
    100100     */ 
    101101    stopUp: false, 
    102  
    103     /** 
    104      * Property: layerIndex 
    105      * {Int} 
    106      */ 
    107     layerIndex: null, 
    108102     
    109103    /** 
    110104     * Constructor: OpenLayers.Handler.Feature 
     
    300294    activate: function() { 
    301295        var activated = false; 
    302296        if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) { 
    303             this.layerIndex = this.layer.div.style.zIndex; 
    304             this.layer.div.style.zIndex = this.map.Z_INDEX_BASE['Popup'] - 1; 
     297            this.moveLayerToTop(); 
     298            this.map.events.on({ 
     299                "removelayer": this.moveLayerToTop, 
     300                "changelayer": this.moveLayerBack, 
     301                scope: this 
     302            }); 
    305303            activated = true; 
    306304        } 
    307305        return activated; 
    308306    }, 
    309307     
    310308    /** 
    311      * Method: activate  
    312      * Turn of the handler.  Returns false if the handler was already active. 
     309     * Method: deactivate  
     310     * Turn off the handler.  Returns false if the handler was already active. 
    313311     * 
    314312     * Returns:  
    315313     * {Boolean} 
     
    317315    deactivate: function() { 
    318316        var deactivated = false; 
    319317        if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { 
    320             if (this.layer && this.layer.div) { 
    321                 this.layer.div.style.zIndex = this.layerIndex; 
    322             } 
     318            this.moveLayerBack(); 
    323319            this.feature = null; 
    324320            this.lastFeature = null; 
    325321            this.down = null; 
    326322            this.up = null; 
     323            this.map.events.un({ 
     324                "removelayer": this.moveLayerToTop, 
     325                "changelayer": this.moveLayerBack, 
     326                scope: this 
     327            }); 
    327328            deactivated = true; 
    328329        } 
    329330        return deactivated; 
    330331    }, 
     332     
     333    /** 
     334     * Method: moveLayerToTop 
     335     * Moves the layer for this handler to the top, so mouse events can reach 
     336     * it. 
     337     */ 
     338    moveLayerToTop: function() { 
     339        var index = Math.max(this.map.Z_INDEX_BASE['Feature'] - 1, 
     340            this.layer.div.style.zIndex) + 1; 
     341        this.layer.setZIndex(index); 
     342    }, 
     343     
     344    /** 
     345     * Method: moveLayerBack 
     346     * Moves the layer back to the position determined by the map's layers 
     347     * array. 
     348     * @param {Object} evt 
     349     */ 
     350    moveLayerBack: function(evt) { 
     351        if (!evt || evt.property == "order") { 
     352            var index = this.layer.div.style.zIndex - 1; 
     353            if (index >= this.map.Z_INDEX_BASE['Feature']) { 
     354                this.layer.setZIndex(index); 
     355            } else { 
     356                this.map.setLayerZIndex(this.layer, 
     357                    OpenLayers.Util.indexOf(this.map.layers, this.layer)); 
     358            } 
     359        } 
     360    }, 
    331361 
    332362    CLASS_NAME: "OpenLayers.Handler.Feature" 
    333363});