OpenLayers OpenLayers

Ticket #987: layer.3.patch

File layer.3.patch, 6.0 kB (added by euzuro, 3 months ago)

update to already great patch -- just added some comments for extra clarity, double-return in function, long lines, etc.

  • tests/Layer.html

    old new  
    145145    } 
    146146 
    147147    function test_Layer_initResolutions(t) { 
    148         t.plan(12); 
     148        t.plan(15); 
    149149        var map = new OpenLayers.Map("map"); 
    150150        var options, layer; 
    151151         
    152         // three tests for minResolution, maxResolution, and numZoomLevels 
     152        // tests for minResolution, maxResolution, and numZoomLevels 
    153153        options = { 
    154154            minResolution: 1.5, 
    155155            maxResolution: 10.5, 
     
    163163        t.eq(layer.maxResolution.toPrecision(6), (10.5).toPrecision(6), 
    164164             "(with numZoomLevels) layer maxResolution preserved"); 
    165165        t.eq(layer.numZoomLevels, 5, "(with numZoomLevels) layer numZoomLevels preserved"); 
    166          
     166        t.eq(layer.alwaysInRange, false, "Always in range is set to false due to passed options.")         
     167 
    167168        // three tests for minResolution, and maxResolution 
    168169        options = { 
    169170            minResolution: 1.5, 
     
    206207        t.eq(layer.maxScale.toPrecision(6), (155).toPrecision(6), 
    207208             "(without numZoomLevels) layer maxScale preserved"); 
    208209        t.eq(layer.numZoomLevels, 4, "(without numZoomLevels) layer numZoomLevels calculated"); 
     210         
     211        layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326', 'map': map}); 
     212        layer.initResolutions(); 
     213        t.eq(layer.alwaysInRange, true, "always in range true if only get projection.");      
     214         
     215        OpenLayers.Layer.prototype.alwaysInRange = false; 
     216        layer = new OpenLayers.Layer("test", {'projection': 'EPSG:4326', 'map': map}); 
     217        layer.initResolutions(); 
     218        t.eq(layer.alwaysInRange, false, "always in range true if overridden on prototype.");      
     219        OpenLayers.Layer.prototype.alwaysInRange = null; 
    209220 
    210221        map.destroy(); 
    211222         
  • lib/OpenLayers/Layer.js

    old new  
    3838    opacity: null, 
    3939 
    4040    /** 
     41     * APIProperty: alwaysInRange 
     42     * {Boolean} If a layer's display should not be scale-based, this should  
     43     *     be set to true. This will cause the layer, as an overlay, to always  
     44     *     be 'active', by always returning true from the calculateInRange()  
     45     *     function.  
     46     *  
     47     *     If not explicitly specified for a layer, its value will be  
     48     *     determined on startup in initResolutions() based on whether or not  
     49     *     any scale-specific properties have been set as options on the  
     50     *     layer. If no scale-specific options have been set on the layer, we  
     51     *     assume that it should always be in range. 
     52     *  
     53     *     See #987 for more info. 
     54     */ 
     55    alwaysInRange: null,    
     56 
     57    /** 
    4158     * Constant: EVENT_TYPES 
    4259     * {Array(String)} Supported application event types.  Register a listener 
    4360     *     for a particular event with the following syntax: 
     
    615632     *  
    616633     * Returns: 
    617634     * {Boolean} The layer is displayable at the current map's current 
    618      *     resolution. 
     635     *     resolution. Note that if 'alwaysInRange' is true for the layer,  
     636     *     this function will always return true. 
    619637     */ 
    620638    calculateInRange: function() { 
    621639        var inRange = false; 
    622         if (this.map) { 
    623             var resolution = this.map.getResolution(); 
    624             inRange = ( (resolution >= this.minResolution) && 
    625                         (resolution <= this.maxResolution) ); 
     640 
     641        if (this.alwaysInRange) { 
     642            inRange = true; 
     643        } else { 
     644            if (this.map) { 
     645                var resolution = this.map.getResolution(); 
     646                inRange = ( (resolution >= this.minResolution) && 
     647                            (resolution <= this.maxResolution) ); 
     648            } 
    626649        } 
    627650        return inRange; 
    628651    }, 
     
    677700          'numZoomLevels', 'maxZoomLevel' 
    678701        ); 
    679702 
     703        //these are the properties which do *not* imply that user wishes  
     704        // this layer to be scale-dependant 
     705        var notScaleProps = ['projection', 'units'];     
     706 
     707        //should the layer be scale-dependant? default is false -- this will  
     708        // only be set true if we find that the user has specified a property 
     709        // from the 'props' array that is not in 'notScaleProps' 
     710        var useInRange = false; 
     711 
    680712        // First we create a new object where we will store all of the  
    681713        //  resolution-related properties that we find in either the layer's 
    682714        //  'options' array or from the map. 
     
    684716        var confProps = {};         
    685717        for(var i=0, len=props.length; i<len; i++) { 
    686718            var property = props[i]; 
     719             
     720            // If the layer had one of these properties set *and* it is  
     721            // a scale property (is not a non-scale property), then we assume 
     722            // the user did intend to use scale-dependant display (useInRange). 
     723            if (this.options[property] &&  
     724                OpenLayers.Util.indexOf(notScaleProps, property) == -1) { 
     725                useInRange = true; 
     726            } 
     727                    
    687728            confProps[property] = this.options[property] || this.map[property]; 
    688729        } 
    689730 
     731        //only automatically set 'alwaysInRange' if the user hasn't already  
     732        // set it (to true or false, since the default is null). If user did 
     733        // not intend to use scale-dependant display then we set they layer 
     734        // as alwaysInRange. This means calculateInRange() will always return  
     735        // true and the layer will never be turned off due to scale changes. 
     736        // 
     737        if (this.alwaysInRange == null) { 
     738            this.alwaysInRange = !useInRange; 
     739        } 
     740 
    690741        // Do not use the scales array set at the map level if  
    691742        // either minScale or maxScale or both are set at the 
    692743        // layer level