OpenLayers OpenLayers

Changeset 7879

Show
Ignore:
Timestamp:
08/27/08 10:02:21 (3 months ago)
Author:
ahocevar
Message:

Made the Feature handler more robust to things that are related to changing layer order on the map, by registering an event handler that will bring the handler's layer back to the top of the layer stack in the DOM. Contains a manual test. r=elemoine (closes #1628)

Files:

Legend:

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

    r7873 r7879  
    100100     */ 
    101101    stopUp: false, 
    102  
    103     /** 
    104      * Property: layerIndex 
    105      * {Int} 
    106      */ 
    107     layerIndex: null, 
    108102     
    109103    /** 
     
    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.handleMapEvents, 
     300                "changelayer": this.handleMapEvents, 
     301                scope: this 
     302            }); 
    305303            activated = true; 
    306304        } 
     
    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:  
     
    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.handleMapEvents, 
     325                "changelayer": this.handleMapEvents, 
     326                scope: this 
     327            }); 
    327328            deactivated = true; 
    328329        } 
    329330        return deactivated; 
     331    }, 
     332     
     333    /** 
     334     * Method handleMapEvents 
     335     *  
     336     * Parameters: 
     337     * evt - {Object} 
     338     */ 
     339    handleMapEvents: function(evt) { 
     340        if (!evt.property || evt.property == "order") { 
     341            this.moveLayerToTop(); 
     342        } 
     343    }, 
     344     
     345    /** 
     346     * Method: moveLayerToTop 
     347     * Moves the layer for this handler to the top, so mouse events can reach 
     348     * it. 
     349     */ 
     350    moveLayerToTop: function() { 
     351        var index = Math.max(this.map.Z_INDEX_BASE['Feature'] - 1, 
     352            this.layer.getZIndex()) + 1; 
     353        this.layer.setZIndex(index); 
     354         
     355    }, 
     356     
     357    /** 
     358     * Method: moveLayerBack 
     359     * Moves the layer back to the position determined by the map's layers 
     360     * array. 
     361     */ 
     362    moveLayerBack: function() { 
     363        var index = this.layer.getZIndex() - 1; 
     364        if (index >= this.map.Z_INDEX_BASE['Feature']) { 
     365            this.layer.setZIndex(index); 
     366        } else { 
     367            this.map.setLayerZIndex(this.layer, 
     368                this.map.getLayerIndex(this.layer)); 
     369        } 
    330370    }, 
    331371 
  • trunk/openlayers/lib/OpenLayers/Layer.js

    r7874 r7879  
    11001100 
    11011101    /** 
     1102     * Method: getZIndex 
     1103     *  
     1104     * Returns:  
     1105     * {Integer} the z-index of this layer 
     1106     */     
     1107    getZIndex: function () { 
     1108        return this.div.style.zIndex; 
     1109    }, 
     1110 
     1111    /** 
    11021112     * Method: setZIndex 
    11031113     *  
  • trunk/openlayers/lib/OpenLayers/Map.js

    r7874 r7879  
    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    /** 
  • trunk/openlayers/tests/Handler/Feature.html

    r7682 r7879  
    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(); 
     
    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         
     
    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; 
     
    235235         
    236236        handler.active = true; 
    237         handler.layerIndex = Math.floor(Math.random() * 10); 
    238237 
    239238        deactivated = handler.deactivate(); 
     
    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    }