OpenLayers OpenLayers

Ticket #1404: eventListeners.patch

File eventListeners.patch, 13.6 kB (added by tschaub, 10 months ago)

allow for setting event listeners at construction

  • tests/test_Map.html

    old new  
    8484        t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hashtable" ); 
    8585        t.eq( map.theme, 'foo', "map theme set correctly." ); 
    8686    } 
     87     
     88    function test_eventListeners(t) { 
     89        t.plan(1); 
     90         
     91        var method = OpenLayers.Events.prototype.on; 
     92        // test that events.on is called at map construction 
     93        var options = { 
     94            eventListeners: {foo: "bar"}, 
     95            controls: [] 
     96        }; 
     97        OpenLayers.Events.prototype.on = function(obj) { 
     98            t.eq(obj, options.eventListeners, "events.on called with eventListeners"); 
     99        } 
     100        var map = new OpenLayers.Map('map', options); 
     101        OpenLayers.Events.prototype.on = method; 
     102        map.destroy(); 
    87103 
     104        // if events.on is called again, this will fail due to an extra test 
     105        // test map without eventListeners 
     106        OpenLayers.Events.prototype.on = function(obj) { 
     107            t.fail("events.on called without eventListeners"); 
     108        } 
     109        var map2 = new OpenLayers.Map("map", {controls: []}); 
     110        OpenLayers.Events.prototype.on = method; 
     111        map2.destroy(); 
     112    } 
     113 
    88114    function test_05_Map_center(t) { 
    89115        t.plan(4); 
    90116        map = new OpenLayers.Map('map'); 
  • tests/test_Control.html

    old new  
    2929        t.eq( control.title, titleText, "control.title set correctly" );  
    3030    } 
    3131     
     32    function test_eventListeners(t) { 
     33        t.plan(1); 
     34         
     35        var method = OpenLayers.Events.prototype.on; 
     36        // test that events.on is called at control construction 
     37        var options = { 
     38            eventListeners: {foo: "bar"} 
     39        }; 
     40        OpenLayers.Events.prototype.on = function(obj) { 
     41            t.eq(obj, options.eventListeners, "events.on called with eventListeners"); 
     42        } 
     43        var control = new OpenLayers.Control(options); 
     44        OpenLayers.Events.prototype.on = method; 
     45        control.destroy(); 
     46 
     47        // if events.on is called again, this will fail due to an extra test 
     48        // test control without eventListeners 
     49        OpenLayers.Events.prototype.on = function(obj) { 
     50            t.fail("events.on called without eventListeners"); 
     51        } 
     52        var control2 = new OpenLayers.Control(); 
     53        OpenLayers.Events.prototype.on = method; 
     54        control2.destroy(); 
     55    } 
     56 
    3257    function test_Control_destroy(t) { 
    3358        t.plan(3); 
    3459     
  • tests/test_Layer.html

    old new  
    119119        t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly"); 
    120120    } 
    121121     
     122    function test_eventListeners(t) { 
     123        t.plan(1); 
     124         
     125        var method = OpenLayers.Events.prototype.on; 
     126        // test that events.on is called at layer construction 
     127        var options = { 
     128            eventListeners: {foo: "bar"} 
     129        }; 
     130        OpenLayers.Events.prototype.on = function(obj) { 
     131            t.eq(obj, options.eventListeners, "events.on called with eventListeners"); 
     132        } 
     133        var layer = new OpenLayers.Layer("test", options); 
     134        OpenLayers.Events.prototype.on = method; 
     135        layer.destroy(); 
     136         
     137        // if events.on is called again, this will fail due to an extra test 
     138        // test layer without eventListeners 
     139        OpenLayers.Events.prototype.on = function(obj) { 
     140            t.fail("events.on called without eventListeners"); 
     141        } 
     142        var layer2 = new OpenLayers.Layer("test"); 
     143        OpenLayers.Events.prototype.on = method; 
     144        layer2.destroy(); 
     145    } 
     146 
    122147    function test_Layer_initResolutions(t) { 
    123148        t.plan(12); 
    124149        var map = new OpenLayers.Map("map"); 
  • lib/OpenLayers/Map.js

    old new  
    326326    panTween: null, 
    327327 
    328328    /** 
     329     * APIProperty: eventListeners 
     330     * {Object} If set as an option at construction, the eventListeners 
     331     *     object will be registered with <OpenLayers.Events.on>.  Object 
     332     *     structure must be a listeners object as shown in the example for 
     333     *     the events.on method. 
     334     */ 
     335    eventListeners: null, 
     336 
     337    /** 
    329338     * Property: panMethod 
    330339     * {Function} The Easing function to be used for tweening.  Default is 
    331340     * OpenLayers.Easing.Expo.easeOut. Setting this to 'null' turns off 
     
    397406                                            this.EVENT_TYPES,  
    398407                                            this.fallThrough); 
    399408        this.updateSize(); 
     409        if(this.eventListeners instanceof Object) { 
     410            this.events.on(this.eventListeners); 
     411        } 
    400412  
    401413        // update the map size and location before the map moves 
    402414        this.events.register("movestart", this, this.updateSize); 
  • lib/OpenLayers/Control.js

    old new  
    104104     */ 
    105105    handler: null, 
    106106 
     107    /** 
     108     * APIProperty: eventListeners 
     109     * {Object} If set as an option at construction, the eventListeners 
     110     *     object will be registered with <OpenLayers.Events.on>.  Object 
     111     *     structure must be a listeners object as shown in the example for 
     112     *     the events.on method. 
     113     */ 
     114    eventListeners: null, 
     115 
    107116    /**  
    108117     * Property: events 
    109118     * {<OpenLayers.Events>} Events instance for triggering control specific 
     
    155164        OpenLayers.Util.extend(this, options); 
    156165         
    157166        this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES); 
     167        if(this.eventListeners instanceof Object) { 
     168            this.events.on(this.eventListeners); 
     169        } 
    158170        this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); 
    159171    }, 
    160172 
     
    230242        if (px != null) { 
    231243            this.position = px.clone(); 
    232244        } 
    233         this.moveTo(this.position);         
     245        this.moveTo(this.position); 
    234246        return this.div; 
    235247    }, 
    236248 
  • lib/OpenLayers/Events.js

    old new  
    429429        if (this.element != null) { 
    430430            this.attachToElement(element); 
    431431        } 
     432         
    432433    }, 
    433434 
    434435    /** 
     
    651652        } 
    652653        evt.object = this.object; 
    653654        evt.element = this.element; 
     655        if(!evt.type) { 
     656            evt.type = type; 
     657        } 
    654658     
    655659        // execute all callbacks registered for specified type 
    656660        // get a clone of the listeners array to 
  • lib/OpenLayers/Layer.js

    old new  
    139139    options: null, 
    140140 
    141141    /** 
     142     * APIProperty: eventListeners 
     143     * {Object} If set as an option at construction, the eventListeners 
     144     *     object will be registered with <OpenLayers.Events.on>.  Object 
     145     *     structure must be a listeners object as shown in the example for 
     146     *     the events.on method. 
     147     */ 
     148    eventListeners: null, 
     149 
     150    /** 
    142151     * APIProperty: gutter 
    143152     * {Integer} Determines the width (in pixels) of the gutter around image 
    144153     *     tiles to ignore.  By setting this property to a non-zero value, 
     
    272281 
    273282            this.events = new OpenLayers.Events(this, this.div,  
    274283                                                this.EVENT_TYPES); 
     284            if(this.eventListeners instanceof Object) { 
     285                this.events.on(this.eventListeners); 
     286            } 
     287 
    275288        } 
    276289 
    277290        if (this.wrapDateLine) { 
  • examples/events.html

    old new  
     1<html xmlns="http://www.w3.org/1999/xhtml"> 
     2    <head> 
     3        <title>OpenLayers Event Handling</title> 
     4        <style type="text/css"> 
     5            #map { 
     6                width: 512px; 
     7                height: 256px; 
     8                border: 1px solid gray; 
     9            } 
     10            #panel { 
     11                margin: 5px; 
     12                height: 30px;  
     13                width: 200px; 
     14            } 
     15            #panel div {  
     16                float: left; 
     17                margin-left: 5px; 
     18                width: 25px; 
     19                height: 25px; 
     20                border: 1px solid gray; 
     21            } 
     22            #output { 
     23                position: absolute; 
     24                left: 550px; 
     25                top: 40px; 
     26                width: 350px; 
     27                height: 400px; 
     28            } 
     29            div.blueItemInactive { 
     30                background-color: #aac; 
     31            } 
     32            div.blueItemActive { 
     33                background-color: #33c; 
     34            } 
     35            div.orangeItemInactive { 
     36                background-color: #ca6; 
     37            } 
     38            div.orangeItemActive { 
     39                background-color: #ea0; 
     40            } 
     41            div.greenItemInactive { 
     42                background-color: #aca; 
     43            } 
     44            div.greenItemActive { 
     45                background-color: #3c3; 
     46            } 
     47                 
     48        </style> 
     49        <script src="../lib/OpenLayers.js"></script> 
     50        <script type="text/javascript"> 
     51            var map, panel; 
     52 
     53            function init(){ 
     54                 
     55                // define custom map event listeners 
     56                function mapEvent(event) { 
     57                    log(event.type); 
     58                } 
     59                function mapBaseLayerChanged(event) { 
     60                    log(event.type + " " + event.layer.name); 
     61                } 
     62                function mapLayerChanged(event) { 
     63                    log(event.type + " " + event.layer.name + " " + event.property); 
     64                } 
     65                map = new OpenLayers.Map('map', { 
     66                    eventListeners: { 
     67                        "moveend": mapEvent, 
     68                        "zoomend": mapEvent, 
     69                        "changelayer": mapLayerChanged, 
     70                        "changebaselayer": mapBaseLayerChanged 
     71                    } 
     72                }); 
     73 
     74                panel = new OpenLayers.Control.Panel( 
     75                    {div: document.getElementById("panel")} 
     76                ); 
     77                 
     78                // define custom event listeners 
     79                function toolActivate(event) { 
     80                    log("activate " + event.object.displayClass); 
     81                } 
     82                function toolDeactivate(event) { 
     83                    log("deactivate " + event.object.displayClass); 
     84                } 
     85                 
     86                // Multiple objects can share listeners with the same scope 
     87                var toolListeners = { 
     88                    "activate": toolActivate, 
     89                    "deactivate": toolDeactivate 
     90                }; 
     91                var blue = new OpenLayers.Control({ 
     92                    type: OpenLayers.Control.TYPE_TOGGLE, 
     93                    eventListeners: toolListeners, 
     94                    displayClass: "blue" 
     95                }); 
     96                var orange = new OpenLayers.Control({ 
     97                    type: OpenLayers.Control.TYPE_TOGGLE, 
     98                    eventListeners: toolListeners, 
     99                    displayClass: "orange" 
     100                }); 
     101                var green = new OpenLayers.Control({ 
     102                    type: OpenLayers.Control.TYPE_TOGGLE, 
     103                    eventListeners: toolListeners, 
     104                    displayClass: "green" 
     105                }); 
     106                 
     107                // add buttons to a panel 
     108                panel.addControls([blue, orange, green]); 
     109                map.addControl(panel); 
     110                 
     111                var vmap = new OpenLayers.Layer.WMS( 
     112                    "OpenLayers WMS", 
     113                    "http://labs.metacarta.com/wms/vmap0", 
     114                    {layers: 'basic'} 
     115                ); 
     116                var landsat = new OpenLayers.Layer.WMS( 
     117                    "NASA Global Mosaic", 
     118                    "http://t1.hypercube.telascience.org/cgi-bin/landsat7",  
     119                    {layers: "landsat7"} 
     120                ); 
     121                var nexrad = new OpenLayers.Layer.WMS( 
     122                    "Nexrad", 
     123                    "http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi", 
     124                    {layers:"nexrad-n0r-wmst", transparent: "TRUE", format: 'image/png'}, 
     125                    {isBaseLayer: false} 
     126                ); 
     127 
     128 
     129                map.addLayers([vmap, landsat, nexrad]); 
     130                map.addControl(new OpenLayers.Control.LayerSwitcher()); 
     131                map.zoomToMaxExtent(); 
     132 
     133            } 
     134            function log(msg) { 
     135                document.getElementById("output").innerHTML += msg + "\n"; 
     136            } 
     137        </script> 
     138    </head> 
     139    <body onload="init()"> 
     140        <h1 id="title">Event Handling</h1> 
     141 
     142        <div id="tags"> 
     143        </div> 
     144 
     145        <p id="shortdesc"> 
     146            Demonstrating various styles of event handling in OpenLayers. 
     147        </p> 
     148 
     149        <div id="map"></div> 
     150        <div id="panel"></div> 
     151        <textarea id="output"></textarea> 
     152        <div id="docs"></div> 
     153    </body> 
     154</html>