OpenLayers OpenLayers

Ticket #1009: Control.destroy.removeControl.00.patch

File Control.destroy.removeControl.00.patch, 2.9 kB (added by fredj, 1 year ago)

call removeControl() in control.destroy(), add tests in test_Control.html, test pass in FF and IE

  • tests/test_Control.html

    old new  
    22<head> 
    33  <script src="../lib/OpenLayers.js"></script> 
    44  <script type="text/javascript"> 
    5     var map;  
    6     function test_01_Control_constructor (t) { 
    7         t.plan( 1 ); 
     5    function test_Control_constructor(t) { 
     6        t.plan(2); 
    87     
    9         control = new OpenLayers.Control(); 
    10         t.ok( control instanceof OpenLayers.Control, "new OpenLayers.Control returns object" ); 
     8        var control = new OpenLayers.Control(); 
     9 
     10        t.ok(control instanceof OpenLayers.Control, "new OpenLayers.Control returns object"); 
     11        t.eq(control.displayClass, "olControl", "displayClass set correctly"); 
    1112    } 
    1213 
     14    function test_Control_addControl(t) { 
     15        t.plan(2); 
     16     
     17        var map = new OpenLayers.Map('map'); 
     18        var control = new OpenLayers.Control(); 
     19        map.addControl(control); 
     20 
     21        t.ok(control.map === map, "Control.map is set to the map object" ); 
     22        t.ok(map.controls[map.controls.length - 1] === control, "map.controls contains control"); 
     23    } 
     24 
     25    function test_Control_destroy(t) { 
     26        t.plan(3); 
     27     
     28        var map = new OpenLayers.Map('map'); 
     29        var control = new OpenLayers.Control(); 
     30        map.addControl(control); 
     31 
     32        control.destroy(); 
     33        t.ok(map.controls[map.controls.length - 1] != control, "map.controls doesn't contains control"); 
     34 
     35        t.ok(control.map == null, "Control.map is null"); 
     36        t.ok(control.handler == null, "Control.handler is null"); 
     37    } 
     38 
    1339  </script> 
    1440</head> 
    1541<body> 
  • lib/OpenLayers/Map.js

    old new  
    797797    removeControl: function (control) { 
    798798        //make sure control is non-null and actually part of our map 
    799799        if ( (control) && (control == this.getControl(control.id)) ) { 
    800             if (!control.outsideViewport) { 
    801                 this.viewPortDiv.removeChild(control.div) 
     800            if (!control.outsideViewport && control.div) { 
     801                this.viewPortDiv.removeChild(control.div); 
    802802            } 
    803803            OpenLayers.Util.removeItem(this.controls, control); 
    804804        } 
  • lib/OpenLayers/Control.js

    old new  
    130130        // eliminate circular references 
    131131        if (this.handler) { 
    132132            this.handler.destroy(); 
    133         }     
    134         this.map = null; 
     133            this.handler = null; 
     134        } 
     135        if (this.map) { 
     136            this.map.removeControl(this); 
     137            this.map = null; 
     138        } 
    135139    }, 
    136140 
    137141    /**