OpenLayers OpenLayers

Changeset 6435

Show
Ignore:
Timestamp:
03/04/08 19:07:46 (10 months ago)
Author:
tschaub
Message:

Adding eventListeners property to layer, control, and map. Setting this property at construction registers the given listeners based on event type key. r=elemoine (closes #1404)

Files:

Legend:

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

    r6230 r6435  
    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 
     
    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    }, 
     
    231243            this.position = px.clone(); 
    232244        } 
    233         this.moveTo(this.position);         
     245        this.moveTo(this.position); 
    234246        return this.div; 
    235247    }, 
  • trunk/openlayers/lib/OpenLayers/Events.js

    r6200 r6435  
    652652        evt.object = this.object; 
    653653        evt.element = this.element; 
     654        if(!evt.type) { 
     655            evt.type = type; 
     656        } 
    654657     
    655658        // execute all callbacks registered for specified type 
  • trunk/openlayers/lib/OpenLayers/Layer.js

    r6332 r6435  
    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 
     
    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 
  • trunk/openlayers/lib/OpenLayers/Map.js

    r6423 r6435  
    338338 
    339339    /** 
     340     * APIProperty: eventListeners 
     341     * {Object} If set as an option at construction, the eventListeners 
     342     *     object will be registered with <OpenLayers.Events.on>.  Object 
     343     *     structure must be a listeners object as shown in the example for 
     344     *     the events.on method. 
     345     */ 
     346    eventListeners: null, 
     347 
     348    /** 
    340349     * Property: panMethod 
    341350     * {Function} The Easing function to be used for tweening.  Default is 
     
    409418                                            this.fallThrough); 
    410419        this.updateSize(); 
     420        if(this.eventListeners instanceof Object) { 
     421            this.events.on(this.eventListeners); 
     422        } 
    411423  
    412424        // update the map size and location before the map moves 
  • trunk/openlayers/tests/test_Control.html

    r5910 r6435  
    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); 
  • trunk/openlayers/tests/test_Layer.html

    r6332 r6435  
    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); 
  • trunk/openlayers/tests/test_Map.html

    r6111 r6435  
    8484        t.eq( map.maxResolution, 3.14159, "map.maxResolution set correctly via options hashtable" ); 
    8585        t.eq( map.theme, 'foo', "map theme set correctly." ); 
     86    } 
     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(); 
     103 
     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(); 
    86112    } 
    87113