OpenLayers OpenLayers

Ticket #1020: point_click.patch

File point_click.patch, 11.3 kB (added by tschaub, 1 year ago)

add click listener to stop clicks while editing

  • tests/Handler/test_Point.html

    old new  
    1 <html> 
    2 <head> 
    3   <script src="../../lib/OpenLayers.js"></script> 
    4   <script type="text/javascript"> 
    5     function test_Handler_Point_constructor(t) { 
    6         t.plan(3); 
    7         var control = new OpenLayers.Control(); 
    8         control.id = Math.random(); 
    9         var callbacks = {foo: "bar"}; 
    10         var options = {bar: "foo"}; 
    11          
    12         var oldInit = OpenLayers.Handler.prototype.initialize; 
    13          
    14         OpenLayers.Handler.prototype.initialize = function(con, call, opt) { 
    15             t.eq(con.id, control.id, 
    16                  "constructor calls parent with the correct control"); 
    17             t.eq(call, callbacks, 
    18                  "constructor calls parent with the correct callbacks"); 
    19             t.eq(opt, options, 
    20                  "constructor calls parent with the correct options"); 
    21         } 
    22         var handler = new OpenLayers.Handler.Point(control, callbacks, options); 
    23  
    24         OpenLayers.Handler.prototype.initialize = oldInit; 
    25     } 
    26  
    27     function test_Handler_Point_activation(t) { 
    28         t.plan(3); 
    29         var map = new OpenLayers.Map('map'); 
    30         var control = new OpenLayers.Control(); 
    31         map.addControl(control); 
    32         var handler = new OpenLayers.Handler.Point(control); 
    33         handler.active = true; 
    34         var activated = handler.activate(); 
    35         t.ok(!activated, 
    36              "activate returns false if the handler was already active"); 
    37         handler.active = false; 
    38         activated = handler.activate(); 
    39         t.ok(activated, 
    40              "activate returns true if the handler was not already active"); 
    41         activated = handler.deactivate(); 
    42         t.ok(activated, 
    43              "deactivate returns true if the handler was active already"); 
    44     } 
    45  
     1<html> 
     2<head> 
     3  <script src="../../lib/OpenLayers.js"></script> 
     4  <script type="text/javascript"> 
     5    function test_Handler_Point_constructor(t) { 
     6        t.plan(3); 
     7        var control = new OpenLayers.Control(); 
     8        control.id = Math.random(); 
     9        var callbacks = {foo: "bar"}; 
     10        var options = {bar: "foo"}; 
     11         
     12        var oldInit = OpenLayers.Handler.prototype.initialize; 
     13         
     14        OpenLayers.Handler.prototype.initialize = function(con, call, opt) { 
     15            t.eq(con.id, control.id, 
     16                 "constructor calls parent with the correct control"); 
     17            t.eq(call, callbacks, 
     18                 "constructor calls parent with the correct callbacks"); 
     19            t.eq(opt, options, 
     20                 "constructor calls parent with the correct options"); 
     21        } 
     22        var handler = new OpenLayers.Handler.Point(control, callbacks, options); 
     23 
     24        OpenLayers.Handler.prototype.initialize = oldInit; 
     25    } 
     26 
     27    function test_Handler_Point_activation(t) { 
     28        t.plan(3); 
     29        var map = new OpenLayers.Map('map'); 
     30        var control = new OpenLayers.Control(); 
     31        map.addControl(control); 
     32        var handler = new OpenLayers.Handler.Point(control); 
     33        handler.active = true; 
     34        var activated = handler.activate(); 
     35        t.ok(!activated, 
     36             "activate returns false if the handler was already active"); 
     37        handler.active = false; 
     38        activated = handler.activate(); 
     39        t.ok(activated, 
     40             "activate returns true if the handler was not already active"); 
     41        activated = handler.deactivate(); 
     42        t.ok(activated, 
     43             "deactivate returns true if the handler was active already"); 
     44    } 
     45 
     46    function test_Handler_Point_events(t) { 
     47        t.plan(29); 
     48         
     49        var map = new OpenLayers.Map('map'); 
     50        var control = { 
     51            map: map 
     52        }; 
     53        var handler = new OpenLayers.Handler.Point(control); 
     54 
     55        // list below events that should be handled (events) and those 
     56        // that should not be handled (nonevents) by the handler 
     57        var events = ["click", "dblclick", "mousedown", "mouseup", "mousemove"]; 
     58        var nonevents = ["mouseout", "resize", "focus", "blur"]; 
     59        map.events.registerPriority = function(type, obj, func) { 
     60            var r = func(); 
     61            if(typeof r == "string") { 
     62                // this is one of the mock handler methods 
     63                t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 
     64                     "registered method is not one of the events " + 
     65                     "that should not be handled"); 
     66                t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
     67                     "activate calls registerPriority with browser event: " + type); 
     68                t.eq(typeof func, "function", 
     69                     "activate calls registerPriority with a function"); 
     70                t.eq(func(), type, 
     71                     "activate calls registerPriority with the correct method"); 
     72                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Point", 
     73                     "activate calls registerPriority with the handler"); 
     74            } 
     75        } 
     76         
     77        // set browser event like properties on the handler 
     78        for(var i=0; i<events.length; ++i) { 
     79            setMethod(events[i]); 
     80        } 
     81        function setMethod(key) { 
     82            handler[key] = function() {return key}; 
     83        } 
     84 
     85        var activated = handler.activate(); 
     86        handler.destroy(); 
     87 
     88        // test that click and dblclick are stopped 
     89        var handler = new OpenLayers.Handler.Point(control); 
     90        var oldStop = OpenLayers.Event.stop; 
     91        OpenLayers.Event.stop = function(evt) { 
     92            t.ok(evt.type == "click" || evt.type == "dblclick", 
     93                 evt.type + " stopped"); 
     94        } 
     95        t.eq(handler.click({type: "click"}), false, "click returns false"); 
     96        t.eq(handler.dblclick({type: "dblclick"}), false, "dblclick returns false"); 
     97        OpenLayers.Event.stop = oldStop; 
     98 
     99    } 
     100 
     101 
    46102    function test_Handler_Point_deactivation(t) { 
    47103        t.plan(1); 
    48104        var map = new OpenLayers.Map('map'); 
     
    56112        t.eq(handler.layer, null, 
    57113             "deactivate doesn't throw an error if layer was" + 
    58114             " previously destroyed"); 
    59     } 
    60  
    61     function test_Handler_Point_bounds(t) { 
    62         t.plan(4); 
    63         var map = new OpenLayers.Map('map'); 
    64         map.addLayer(new OpenLayers.Layer.WMS("", "", {})); 
    65         map.zoomToMaxExtent(); 
    66         var control = new OpenLayers.Control(); 
    67         map.addControl(control); 
    68         var handler = new OpenLayers.Handler.Point(control); 
    69         var activated = handler.activate(); 
    70         var px = new OpenLayers.Pixel(150, 75); 
    71         var evt = {xy: px, which: 1}; 
    72         handler.mousedown(evt); 
    73         var lonlat = map.getLonLatFromPixel(px); 
    74         t.eq(handler.point.geometry.x, lonlat.lon, "X is correct");  
    75         t.eq(handler.point.geometry.y, lonlat.lat, "Y is correct");  
    76         t.ok(handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(lonlat.lon,lonlat.lat,lonlat.lon,lonlat.lat)), "Correct bounds");  
    77         var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; 
    78         handler.mousemove(evt); 
    79         t.ok(!handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(0,0,0,0)), "Bounds changed after moving mouse");  
    80     }      
    81          
    82     function test_Handler_Point_destroy(t) { 
    83         t.plan(4); 
    84         var map = new OpenLayers.Map('map'); 
    85         map.addLayer(new OpenLayers.Layer.WMS("", "", {})); 
    86         map.zoomToMaxExtent(); 
    87         var control = new OpenLayers.Control(); 
    88         map.addControl(control); 
    89         var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'}); 
    90  
    91         handler.activate(); 
    92         var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; 
    93         handler.mousedown(evt); 
    94  
    95         t.ok(handler.layer, 
    96              "handler has a layer prior to destroy"); 
    97         t.ok(handler.point, 
    98              "handler has a point prior to destroy"); 
    99         handler.destroy(); 
    100         t.eq(handler.layer, null, 
    101              "handler.layer is null after destroy"); 
    102         t.eq(handler.point, null, 
    103              "handler.point is null after destroy"); 
    104     } 
    105      
    106  
    107  
    108   </script> 
    109 </head> 
    110 <body> 
    111     <div id="map" style="width: 300px; height: 150px;"/> 
    112 </body> 
    113 </html> 
     115    } 
     116 
     117    function test_Handler_Point_bounds(t) { 
     118        t.plan(4); 
     119        var map = new OpenLayers.Map('map'); 
     120        map.addLayer(new OpenLayers.Layer.WMS("", "", {})); 
     121        map.zoomToMaxExtent(); 
     122        var control = new OpenLayers.Control(); 
     123        map.addControl(control); 
     124        var handler = new OpenLayers.Handler.Point(control); 
     125        var activated = handler.activate(); 
     126        var px = new OpenLayers.Pixel(150, 75); 
     127        var evt = {xy: px, which: 1}; 
     128        handler.mousedown(evt); 
     129        var lonlat = map.getLonLatFromPixel(px); 
     130        t.eq(handler.point.geometry.x, lonlat.lon, "X is correct");  
     131        t.eq(handler.point.geometry.y, lonlat.lat, "Y is correct");  
     132        t.ok(handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(lonlat.lon,lonlat.lat,lonlat.lon,lonlat.lat)), "Correct bounds");  
     133        var evt = {xy: new OpenLayers.Pixel(175, 100), which: 1}; 
     134        handler.mousemove(evt); 
     135        t.ok(!handler.point.geometry.getBounds().equals(new OpenLayers.Bounds(0,0,0,0)), "Bounds changed after moving mouse");  
     136    }      
     137         
     138    function test_Handler_Point_destroy(t) { 
     139        t.plan(4); 
     140        var map = new OpenLayers.Map('map'); 
     141        map.addLayer(new OpenLayers.Layer.WMS("", "", {})); 
     142        map.zoomToMaxExtent(); 
     143        var control = new OpenLayers.Control(); 
     144        map.addControl(control); 
     145        var handler = new OpenLayers.Handler.Point(control, {foo: 'bar'}); 
     146 
     147        handler.activate(); 
     148        var evt = {xy: new OpenLayers.Pixel(150, 75), which: 1}; 
     149        handler.mousedown(evt); 
     150 
     151        t.ok(handler.layer, 
     152             "handler has a layer prior to destroy"); 
     153        t.ok(handler.point, 
     154             "handler has a point prior to destroy"); 
     155        handler.destroy(); 
     156        t.eq(handler.layer, null, 
     157             "handler.layer is null after destroy"); 
     158        t.eq(handler.point, null, 
     159             "handler.point is null after destroy"); 
     160    } 
     161     
     162 
     163 
     164  </script> 
     165</head> 
     166<body> 
     167    <div id="map" style="width: 300px; height: 150px;"/> 
     168</body> 
     169</html> 
  • lib/OpenLayers/Handler/Point.js

    old new  
    173173    }, 
    174174 
    175175    /** 
     176     * Method: click 
     177     * Handle clicks.  Clicks are stopped from propagating to other listeners 
     178     *     on map.events or other dom elements. 
     179     *  
     180     * Parameters: 
     181     * evt - {Event} The browser event 
     182     * 
     183     * Returns:  
     184     * {Boolean} Allow event propagation 
     185     */ 
     186    click: function(evt) { 
     187        OpenLayers.Event.stop(evt); 
     188        return false; 
     189    }, 
     190 
     191    /** 
    176192     * Method: dblclick 
    177      * Handle double clicks. 
     193     * Handle clicks.  Double-clicks are stopped from propagating to other 
     194     *     listeners on map.events or other dom elements. 
    178195     *  
    179196     * Parameters: 
    180197     * evt - {Event} The browser event