OpenLayers OpenLayers

Ticket #1649: patch-1649-r7658-A0.diff

File patch-1649-r7658-A0.diff, 5.1 kB (added by elemoine, 5 months ago)
  • tests/Strategy.html

    old new  
    44  <script type="text/javascript"> 
    55 
    66    function test_initialize(t) { 
    7         t.plan(2); 
     7        t.plan(4); 
    88        var options = {}; 
    99        var strategy = new OpenLayers.Strategy(options); 
    1010 
    1111        t.ok(strategy instanceof OpenLayers.Strategy, 
    1212             "new OpenLayers.Strategy returns object" ); 
    1313        t.eq(strategy.options, options, "constructor sets this.options"); 
     14        t.eq(strategy.active, false, "constructor sets this.active to false"); 
     15        t.eq(strategy.autoActivate, true, "constructor does not modify this.autoActivate"); 
    1416    } 
    1517     
    1618    function test_destroy(t) { 
     
    2325        t.eq(strategy.layer, null, "destroy nullify protocol.layer"); 
    2426    } 
    2527 
     28    function test_activate(t) { 
     29        t.plan(4); 
     30        var strategy = new OpenLayers.Strategy({ 
     31            layer: 'foo' 
     32        }); 
     33 
     34        var ret; 
     35        ret = strategy.activate(); 
     36 
     37        t.eq(strategy.active, true, "activate sets this.active to true on first call"); 
     38        t.eq(ret, true, "activate returns true on first call"); 
     39 
     40        ret = strategy.activate(); 
     41 
     42        t.eq(strategy.active, true, "activate does not modify this.active on second call"); 
     43        t.eq(ret, false, "activate returns false on second call"); 
     44    } 
     45 
     46    function test_deactivate(t) { 
     47        t.plan(4); 
     48        var strategy = new OpenLayers.Strategy({ 
     49            layer: 'foo' 
     50        }); 
     51        strategy.activate(); 
     52 
     53        var ret; 
     54        ret = strategy.deactivate(); 
     55 
     56        t.eq(strategy.active, false, "deactivate sets this.active to false on first call"); 
     57        t.eq(ret, true, "deactivate returns true on first call"); 
     58 
     59        ret = strategy.deactivate(); 
     60 
     61        t.eq(strategy.active, false, "deactivate does not modify this.active on second call"); 
     62        t.eq(ret, false, "deactivate returns false on second call"); 
     63    } 
     64 
    2665  </script> 
    2766</head> 
    2867<body> 
  • lib/OpenLayers/Layer/Vector.js

    old new  
    296296            this.renderer.setSize(this.map.getSize()); 
    297297        } 
    298298        if(this.strategies) { 
    299             for(var i=0, len=this.strategies.length; i<len; i++) { 
    300                 this.strategies[i].activate(); 
     299            var strategy, i, len; 
     300            for(i=0, len=this.strategies.length; i<len; i++) { 
     301                strategy = this.strategies[i]; 
     302                if(strategy.autoActivate) { 
     303                    strategy.activate(); 
     304                } 
    301305            } 
    302306        } 
    303307    }, 
     308 
     309    /** 
     310     * Method: removeMap 
     311     * The layer has been removed from the map. 
     312     * 
     313     * Parameters: 
     314     * map - {<OpenLayers.Map>} 
     315     */ 
     316    removeMap: function(map) { 
     317        if(this.strategies) { 
     318            var strategy, i, len; 
     319            for(i=0, len=this.strategies.length; i<len; i++) { 
     320                strategy = this.strategies[i]; 
     321                if(strategy.autoActivate) { 
     322                    strategy.deactivate(); 
     323                } 
     324            } 
     325        } 
     326    }, 
    304327     
    305328    /** 
    306329     * Method: onMapResize 
  • lib/OpenLayers/Strategy.js

    old new  
    2121     */ 
    2222    options: null, 
    2323 
     24    /**  
     25     * Property: active  
     26     * {Boolean} The control is active. 
     27     */ 
     28    active: null, 
     29 
    2430    /** 
     31     * Property: autoActivate 
     32     * {Boolean} True if the layer including the strategy is responsible 
     33     *      for activating and deactivating the strategy, false otherwise. 
     34     *      Defaults to true. 
     35     */ 
     36    autoActivate: true, 
     37 
     38    /** 
    2539     * Constructor: OpenLayers.Strategy 
    2640     * Abstract class for vector strategies.  Create instances of a subclass. 
    2741     * 
     
    3246    initialize: function(options) { 
    3347        OpenLayers.Util.extend(this, options); 
    3448        this.options = options; 
     49        this.active = false; 
    3550    }, 
    3651     
    3752    /** 
     
    5671    /** 
    5772     * Method: activate 
    5873     * Activate the strategy.  Register any listeners, do appropriate setup. 
     74     * 
     75     * Returns: 
     76     * {Boolean} True if the strategy was effectively deactivated and false 
     77     *     if it was already activated. 
    5978     */ 
    6079    activate: function() { 
     80        if (!this.active) { 
     81            this.active = true; 
     82            return true; 
     83        } 
     84        return false; 
    6185    }, 
    6286     
    6387    /** 
    6488     * Method: deactivate 
    6589     * Deactivate the strategy.  Unregister any listeners, do appropriate 
    6690     *     tear-down. 
     91     * 
     92     * Returns: 
     93     * {Boolean} True if the strategy was effectively activated and false 
     94     *     if it was already deactivated. 
    6795     */ 
    6896    deactivate: function() { 
     97        if (this.active) { 
     98            this.active = false; 
     99            return true; 
     100        } 
     101        return false; 
    69102    }, 
    70103    
    71104    CLASS_NAME: "OpenLayers.Strategy"