OpenLayers OpenLayers

Changeset 3282

Show
Ignore:
Timestamp:
06/07/07 14:01:52 (1 year ago)
Author:
euzuro
Message:

fix for #742 -- Adds getControl() and removeControl() methods to the map

Files:

Legend:

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

    r3264 r3282  
    599599    }, 
    600600     
     601    /** 
     602     * @param {String} id ID of the control to return 
     603     *  
     604     * @returns The control from the map's list of controls which has a  
     605     *           matching 'id'. If none found, returns null. 
     606     * @type OpenLayers.Control 
     607     */     
     608    getControl: function (id) { 
     609        var returnControl = null; 
     610        for(var i=0; i < this.controls.length; i++) { 
     611            var control = this.controls[i]; 
     612            if (control.id == id) { 
     613                returnControl = control; 
     614                break; 
     615            } 
     616        } 
     617        return returnControl; 
     618    }, 
     619     
     620    /** Remove a control from the map. Removes the control both from the map  
     621     *   object's internal array of controls, as well as from the map's  
     622     *   viewPort (assuming the control was not added outsideViewport) 
     623     *  
     624     * @param {String} id ID of the control to remove 
     625     */     
     626    removeControl: function (id) { 
     627        var control = this.getControl(id); 
     628        if (control) { 
     629            if (!control.outsideViewport) { 
     630                this.viewPortDiv.removeChild(control.div) 
     631            } 
     632            OpenLayers.Util.removeItem(this.controls, control); 
     633        } 
     634    }, 
     635 
    601636  /********************************************************/ 
    602637  /*                                                      */ 
  • trunk/openlayers/tests/test_Map.html

    r3278 r3282  
    391391        t.eq(nodeCount, head.childNodes.length, "with no theme, a node is not added to document head" ); 
    392392    } 
     393     
     394    function test_19_Map_getControl(t) { 
     395        t.plan(2); 
     396         
     397        var map1 = new OpenLayers.Map('map'); 
     398         
     399        var control = new OpenLayers.Control(); 
     400        map1.addControl(control); 
     401 
     402        var gotControl = map1.getControl(control.id); 
     403        t.ok(gotControl == control, "got right control"); 
     404         
     405        gotControl = map1.getControl("bogus id"); 
     406        t.ok(gotControl == null, "getControl() for bad id returns null"); 
     407    } 
     408 
     409    function test_19_Map_removeControl(t) { 
     410        t.plan(6); 
     411         
     412        var oldNumControls, newNumControls; 
     413         
     414        var map1 = new OpenLayers.Map('map'); 
     415        oldNumControls = map1.controls.length; 
     416         
     417        var control = new OpenLayers.Control(); 
     418        map1.addControl(control); 
     419 
     420    //add control         
     421        newNumControls = map1.controls.length; 
     422        t.ok( newNumControls = oldNumControls + 1, "adding a control increases control count") 
     423 
     424        var foundDiv = false; 
     425        for(var i=0; i < map1.viewPortDiv.childNodes.length; i++) { 
     426            var childNode = map1.viewPortDiv.childNodes[i]; 
     427            if (childNode == control.div) { 
     428                foundDiv = true; 
     429            } 
     430        } 
     431        t.ok(foundDiv, "new control's div correctly added to viewPort"); 
     432 
     433    //remove control         
     434        map1.removeControl(control.id) 
     435        newNumControls = map1.controls.length; 
     436        t.ok( newNumControls == oldNumControls, "removing the control decreases control count") 
     437 
     438        var gotControl = map1.getControl(control.id); 
     439        t.ok( gotControl == null, "control no longer in map's controls array"); 
     440 
     441        var foundDiv = false; 
     442        for(var i=0; i < map1.viewPortDiv.childNodes.length; i++) { 
     443            var childNode = map1.viewPortDiv.childNodes[i]; 
     444            if (childNode == control.div) { 
     445                foundDiv = true; 
     446            } 
     447        } 
     448        t.ok(!foundDiv, "control no longer child of viewPort"); 
     449 
     450    //remove bogus 
     451        map1.removeControl("bogus id"); 
     452        newNumControls = map1.controls.length; 
     453        t.ok( newNumControls == oldNumControls, "removing bad controlid doesnt crash or decrease control count") 
     454    } 
    393455 
    394456    function test_99_Map_destroy (t) {