OpenLayers OpenLayers

Changeset 5976

Show
Ignore:
Timestamp:
02/03/08 12:35:39 (1 year ago)
Author:
elemoine
Message:

Add properties stopClick, stopDown, and stopUp to the feature handler. If
stopClick is true, clicks handled by the feature handler don't propagate to
other click listeners; otherwise handled clicks do propagate. The same kind of
rule applies to stopDown and stopUp. These properties default to true. Thanks
to Attila Csipa for expressing the need for this feature and cooking up the
first patch. r=tschaub. (closes #1266)

Files:

Legend:

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

    r5975 r5976  
    7373     */ 
    7474    geometryTypes: null, 
    75      
     75 
     76    /** 
     77     * Property: stopClick 
     78     * {Boolean} If stopClick is set to true, handled clicks do not 
     79     *      propagate to other click listeners. Otherwise, handled clicks 
     80     *      do propagate. Unhandled clicks always propagate, whatever the 
     81     *      value of stopClick. Defaults to true. 
     82     */ 
     83    stopClick: true, 
     84 
     85    /** 
     86     * Property: stopDown 
     87     * {Boolean} If stopDown is set to true, handled mousedowns do not 
     88     *      propagate to other mousedown listeners. Otherwise, handled 
     89     *      mousedowns do propagate. Unhandled mousedowns always propagate, 
     90     *      whatever the value of stopDown. Defaults to true. 
     91     */ 
     92    stopDown: true, 
     93 
     94    /** 
     95     * Property: stopUp 
     96     * {Boolean} If stopUp is set to true, handled mouseups do not 
     97     *      propagate to other mouseup listeners. Otherwise, handled mouseups 
     98     *      do propagate. Unhandled mouseups always propagate, whatever the 
     99     *      value of stopUp. Defaults to true. 
     100     */ 
     101    stopUp: true, 
     102 
    76103    /** 
    77104     * Property: layerIndex 
     
    107134    mousedown: function(evt) { 
    108135        this.down = evt.xy; 
    109         return !this.handle(evt)
     136        return this.handle(evt) ? !this.stopDown : true
    110137    }, 
    111138     
     
    120147    mouseup: function(evt) { 
    121148        this.up = evt.xy; 
    122         return !this.handle(evt)
     149        return this.handle(evt) ? !this.stopUp : true
    123150    }, 
    124151 
     
    135162     */ 
    136163    click: function(evt) { 
    137         return !this.handle(evt)
     164        return this.handle(evt) ? !this.stopClick : true
    138165    }, 
    139166         
     
    192219     * 
    193220     * Returns: 
    194      * {Boolean} Stop event propagation
     221     * {Boolean} The event occurred over a relevant feature
    195222     */ 
    196223    handle: function(evt) { 
    197224        var type = evt.type; 
    198         var stopEvtPropag = false; 
     225        var handled = false; 
    199226        var previouslyIn = !!(this.feature); // previously in a feature 
    200227        var click = (type == "click" || type == "dblclick"); 
     
    213240                } 
    214241                this.lastFeature = this.feature; 
    215                 stopEvtPropag = true; 
     242                handled = true; 
    216243            } else { 
    217244                // not in to a feature 
     
    226253            } 
    227254        } 
    228         return stopEvtPropag
     255        return handled
    229256    }, 
    230257     
  • trunk/openlayers/tests/Handler/test_Feature.html

    r5506 r5976  
    122122        handler.handle("click", {});  
    123123    } 
     124 
    124125    function test_Handler_Feature_callbacks(t) { 
    125126        t.plan(9); 
     
    239240    } 
    240241 
     242    function test_Handler_Feature_stopHandled(t) { 
     243        t.plan(3); 
     244        var map = new OpenLayers.Map('map'); 
     245        var control = new OpenLayers.Control(); 
     246        map.addControl(control); 
     247        var layer = new OpenLayers.Layer(); 
     248        map.addLayer(layer); 
     249        var handler = new OpenLayers.Handler.Feature(control, layer); 
     250        handler.activate(); 
     251        handler.handle = function(evt) { return /* handled */ true; }; 
     252        var  evtPx = {xy: new OpenLayers.Pixel(Math.random(), Math.random())}; 
     253        map.events.register("click", map, function(e) { 
     254            t.ok(!handler.stopClick, "clicks propagate with stopClick set to false" ); 
     255        }); 
     256        map.events.register("mousedown", map, function(e) { 
     257            t.ok(!handler.stopDown, "mousedown propagate with stopDown set to false" ); 
     258        }); 
     259        map.events.register("mouseup", map, function(e) { 
     260            t.ok(!handler.stopUp, "mouseup propagate with stopUp set to false" ); 
     261        }); 
     262 
     263        // 0 test 
     264        map.events.triggerEvent('click', evtPx); 
     265        // 0 test 
     266        map.events.triggerEvent('mousedown', evtPx); 
     267        // 0 test 
     268        map.events.triggerEvent('mousedown', evtPx); 
     269 
     270        // 1 test 
     271        handler.stopClick = false; 
     272        map.events.triggerEvent('click', evtPx); 
     273        // 1 test 
     274        handler.stopDown = false; 
     275        map.events.triggerEvent('mousedown', evtPx); 
     276        // 1 test 
     277        handler.stopUp = false; 
     278        map.events.triggerEvent('mouseup', evtPx); 
     279         
     280        // 3 tests total 
     281    } 
    241282 
    242283  </script>